Packager { public static Bag createBag(File dir) throws Exception { return BagCreator.bagInPlace( dir.toPath(), Arrays.asList(StandardSupportedAlgorithms.MD5), true); }  static Bag createBag(File dir); static boolean validateBag(File dir); static boolean addMetadata(File bagDir,
                                      String depositMetadata,
                                      String vaultMetadata,
                                      String fileTypeMetadata,
                                      String externalMetadata); static boolean addMetaFile(File tagManifest, Path metadataDirPath, String metadataFileName, String metadata, SupportedAlgorithm alg); static String computeFileHash(File file, SupportedAlgorithm alg); static boolean extractMetadata(File bagDir, File metaDir); static void main(String[] args); static final String metadataDirName; static final String depositMetaFileName; static final String vaultMetaFileName; static final String fileTypeMetaFileName; static final String externalMetaFileName; }
@Test public void testCreateBag() { final String TEST_FILE = "item.pdf"; final List<String> expectFiles = Arrays.asList( "bagit.txt", "bag-info.txt", "manifest-md5.txt", "tagmanifest-md5.txt", "bagit.txt"); File dir = new File(testDir, "createbag"); dir.mkdir(); try{ FileUtils.copyFileToDirectory( new File(packagerResources, TEST_FILE), dir); } catch(IOException ex){ fail(ex.getMessage()); } try { Bag bag = Packager.createBag(dir); assertNotNull(bag); for (File file : dir.listFiles()) { if (file.isFile()) { assertTrue(expectFiles.contains(file.getName())); } } File testFile = new File(dir + File.separator + "data", TEST_FILE); assertTrue(testFile.exists()); FileInputStream fis = new FileInputStream(testFile); String md5 = DigestUtils.md5Hex(fis); fis.close(); Manifest manifest= bag.getPayLoadManifests().iterator().next(); assertEquals(md5, manifest.getFileToChecksumMap().get(testFile.toPath())); } catch(Exception ex) { ex.printStackTrace(); fail(ex.getMessage()); } }
@Test public void testInvalidFilenames() { final String TEST_FILE = "col:n.tiff"; File dir = new File(testDir, "invalidfilenames"); dir.mkdir(); try{ FileUtils.copyFileToDirectory( new File(packagerResources, TEST_FILE), dir); } catch(IOException ex){ fail(ex.getMessage()); } try { if(Packager.createBag(dir) != null){ } } catch(Exception ex) { ex.printStackTrace(); fail(ex.getMessage()); } }