use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.
the class PathUtilsTest method testGetDataDirUsingBag.
@Test
public void testGetDataDirUsingBag() throws IOException {
Bag bag = new Bag(new Version(2, 0));
bag.setRootDir(Paths.get("foo"));
Path expectedPath = bag.getRootDir();
Path actualPath = PathUtils.getDataDir(bag);
assertEquals(expectedPath, actualPath);
bag = new Bag(new Version(0, 97));
bag.setRootDir(Paths.get("foo"));
expectedPath = bag.getRootDir().resolve("data");
actualPath = PathUtils.getDataDir(bag);
assertEquals(expectedPath, actualPath);
}
use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.
the class PathUtilsTest method testGetBagitDirUsingBag.
@Test
public void testGetBagitDirUsingBag() {
Bag bag = new Bag(new Version(2, 0));
bag.setRootDir(Paths.get("foo"));
Path expectedPath = bag.getRootDir().resolve(".bagit");
Path actualPath = PathUtils.getBagitDir(bag);
assertEquals(expectedPath, actualPath);
bag = new Bag(new Version(0, 97));
bag.setRootDir(Paths.get("foo"));
expectedPath = bag.getRootDir();
actualPath = PathUtils.getBagitDir(bag);
assertEquals(expectedPath, actualPath);
}
use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.
the class BagitTextFileReader method readBagitTextFile.
/**
* Read the bagit.txt file and return the version and encoding.
*
* @param bagitFile the bagit.txt file
* @return the bag {@link Version} and {@link Charset} encoding of the tag files
*
* @throws IOException if there is a problem reading a file. The file MUST be in UTF-8 encoding.
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to "key: value"
* @throws InvalidBagitFileFormatException if the bagit.txt file does not conform to the bagit spec
*/
public static SimpleImmutableEntry<Version, Charset> readBagitTextFile(final Path bagitFile) throws IOException, UnparsableVersionException, InvalidBagMetadataException, InvalidBagitFileFormatException {
logger.debug(messages.getString("reading_version_and_encoding"), bagitFile);
throwErrorIfByteOrderMarkIsPresent(bagitFile);
final List<SimpleImmutableEntry<String, String>> pairs = KeyValueReader.readKeyValuesFromFile(bagitFile, ":", StandardCharsets.UTF_8);
String version = null;
Charset encoding = null;
for (final SimpleImmutableEntry<String, String> pair : pairs) {
if ("BagIt-Version".equals(pair.getKey())) {
version = pair.getValue();
logger.debug(messages.getString("bagit_version"), version);
}
if ("Tag-File-Character-Encoding".equals(pair.getKey())) {
encoding = Charset.forName(pair.getValue());
logger.debug(messages.getString("tag_file_encoding"), encoding);
}
}
if (version == null || encoding == null) {
throw new InvalidBagitFileFormatException(messages.getString("invalid_bagit_text_file_error"));
}
final Version parsedVersion = parseVersion(version);
if (parsedVersion.isSameOrNewer(VERSION_1_0)) {
final List<String> lines = Files.readAllLines(bagitFile, StandardCharsets.UTF_8);
throwErrorIfLinesDoNotMatchStrict(lines);
}
return new SimpleImmutableEntry<>(parsedVersion, encoding);
}
use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.
the class VersionCheckerTest method testLinterIgnoreOldVersion.
@Test
public void testLinterIgnoreOldVersion() {
Set<BagitWarning> warnings = new HashSet<>();
VersionChecker.checkVersion(new Version(0, 95), warnings, Arrays.asList(BagitWarning.OLD_BAGIT_VERSION));
assertFalse(warnings.contains(BagitWarning.OLD_BAGIT_VERSION));
}
use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.
the class BagCreatorTest method testBagInPlaceIncludingHidden.
@Test
public void testBagInPlaceIncludingHidden() throws IOException, NoSuchAlgorithmException {
TestStructure structure = createTestStructure();
Bag bag = BagCreator.bagInPlace(Paths.get(folder.getRoot().toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), true);
assertEquals(new Version(0, 97), bag.getVersion());
File expectedManifest = new File(folder.getRoot(), "manifest-md5.txt");
assertTrue(expectedManifest.exists());
File expectedTagManifest = new File(folder.getRoot(), "tagmanifest-md5.txt");
assertTrue(expectedTagManifest.exists());
File bagitFile = new File(folder.getRoot(), "bagit.txt");
assertTrue(bagitFile.exists());
for (Manifest manifest : bag.getPayLoadManifests()) {
for (Path expectedPayloadFile : manifest.getFileToChecksumMap().keySet()) {
assertTrue(expectedPayloadFile + " doesn't exist but it should!", structure.regularPayloadFiles.contains(expectedPayloadFile) || structure.hiddenPayloadFiles.contains(expectedPayloadFile));
}
}
}
Aggregations