Packager { public static boolean validateBag(File dir) throws Exception { boolean isValid = false; Bag bag = new BagReader().read(dir.toPath()); BagVerifier bv = new BagVerifier(); try { bv.isValid(bag, false); isValid = true; } catch(IOException | CorruptChecksumException | InvalidBagitFileFormatException | VerificationException | InterruptedException | MaliciousPathException | MissingPayloadManifestException | MissingPayloadDirectoryException | FileNotInPayloadDirectoryException | MissingBagitFileException | UnsupportedAlgorithmException ex){ log.warn("Bag " + bag.getRootDir() + " is invalid: " + ex.getMessage()); } finally { bv.close(); } return isValid; }  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 testValidateBag() { final String TEST_FILE1 = "item.pdf"; final String TEST_FILE2 = "banjo.jpg"; final String TEST_FILE_WITH_SPACE = "peacock butterfly.jpeg"; final String EMPTY_DIR_NAME = "emptydir"; final String CHILD_DIR_NAME = "childdir"; final String CHILD_DIR_WITH_SPACE = "second childdir"; final String TEST_FILE_WITH_US = "ko_ala.jpg"; final String TEST_FILE_WITH_HY = "peng-uins.jpg"; File parentDir = new File(testDir, "validatebag"); parentDir.mkdir(); File emptyDir = new File(parentDir, EMPTY_DIR_NAME); emptyDir.mkdir(); File childDir = new File(parentDir, CHILD_DIR_NAME); File test1file = null; File test2file = null; File test3file = null; File secondChildDir = new File(parentDir, CHILD_DIR_WITH_SPACE); secondChildDir.mkdir(); File test4file = null; File test5file = null; try{ test1file = new File(packagerResources, TEST_FILE1); FileUtils.copyFileToDirectory( new File(packagerResources, TEST_FILE1), parentDir); test2file = new File(packagerResources, TEST_FILE2); FileUtils.copyFileToDirectory(test2file, childDir); test3file = new File(packagerResources, TEST_FILE_WITH_SPACE); FileUtils.copyFileToDirectory(test3file, parentDir); test4file = new File(packagerResources, TEST_FILE_WITH_US); FileUtils.copyFileToDirectory(test4file, secondChildDir); test5file = new File(packagerResources, TEST_FILE_WITH_HY); FileUtils.copyFileToDirectory(test5file, secondChildDir); Path source = Paths.get(childDir.getAbsolutePath() + File.separator + TEST_FILE2); Path link = Paths.get(parentDir.getAbsolutePath() + File.separator + TEST_FILE2); Path relativeSrc = link.getParent().relativize(source); Files.createSymbolicLink(link, relativeSrc); } catch(IOException ex){ ex.printStackTrace(); fail(ex.getMessage()); } try{ Packager.createBag(parentDir); assertTrue(Packager.validateBag(parentDir)); List<String> lines = FileUtils.readLines( new File(parentDir.getAbsolutePath() + File.separator + "manifest-md5.txt")); assertTrue(lines.contains(this.getChecksum(test1file) + " data" + File.separator + TEST_FILE1)); assertTrue(lines.contains(this.getChecksum(test2file) + " data" + File.separator + CHILD_DIR_NAME + File.separator + TEST_FILE2)); assertTrue(lines.contains(this.getChecksum(test3file) + " data" + File.separator + TEST_FILE_WITH_SPACE)); File dataDir = new File(parentDir + File.separator + "data"); File simLink = new File(dataDir, TEST_FILE2); assertTrue(simLink.exists() && FileUtils.isSymlink(simLink)); assertTrue(new File(dataDir, EMPTY_DIR_NAME).isDirectory()); assertTrue(new File(dataDir, TEST_FILE1).exists()); assertTrue(new File(dataDir + File.separator + CHILD_DIR_NAME, TEST_FILE2).exists()); assertTrue(new File(dataDir, TEST_FILE_WITH_SPACE).exists()); File childDirWithSpace = new File(parentDir + File.separator + "data" + File.separator + CHILD_DIR_WITH_SPACE); assertTrue(new File(childDirWithSpace, TEST_FILE_WITH_US).exists()); assertTrue(new File(childDirWithSpace, TEST_FILE_WITH_HY).exists()); } catch(Exception ex){ ex.printStackTrace(); fail(ex.getMessage()); } }