Search in sources :

Example 6 with Version

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);
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 7 with Version

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);
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 8 with Version

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);
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Version(gov.loc.repository.bagit.domain.Version) Charset(java.nio.charset.Charset) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)

Example 9 with Version

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));
}
Also used : Version(gov.loc.repository.bagit.domain.Version) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with 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));
        }
    }
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) Manifest(gov.loc.repository.bagit.domain.Manifest) File(java.io.File) Test(org.junit.Test)

Aggregations

Version (gov.loc.repository.bagit.domain.Version)24 Path (java.nio.file.Path)19 Test (org.junit.Test)17 Bag (gov.loc.repository.bagit.domain.Bag)11 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)7 File (java.io.File)6 Manifest (gov.loc.repository.bagit.domain.Manifest)4 Charset (java.nio.charset.Charset)4 HashSet (java.util.HashSet)3 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 Metadata (gov.loc.repository.bagit.domain.Metadata)1 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)1 MissingBagitFileException (gov.loc.repository.bagit.exceptions.MissingBagitFileException)1