Search in sources :

Example 11 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagReaderTest method testReadVersion2_0Bag.

@Test
public void testReadVersion2_0Bag() throws Exception {
    Path rootBag = Paths.get(getClass().getClassLoader().getResource("bags/v2_0/bag").toURI());
    Path[] payloadFiles = new Path[] { rootBag.resolve("dir1/test3.txt"), rootBag.resolve("dir2/dir3/test5.txt"), rootBag.resolve("dir2/test4.txt"), rootBag.resolve("test1.txt"), rootBag.resolve("test2.txt") };
    Bag returnedBag = sut.read(rootBag);
    assertNotNull(returnedBag);
    assertEquals(new Version(2, 0), returnedBag.getVersion());
    Manifest payloadManifest = (Manifest) returnedBag.getPayLoadManifests().toArray()[0];
    for (Path payloadFile : payloadFiles) {
        assertTrue("payload manifest should contain " + payloadFile, payloadManifest.getFileToChecksumMap().containsKey(payloadFile));
    }
}
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) Test(org.junit.Test)

Example 12 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagReaderTest method testReadVersion0_95.

@Test
public void testReadVersion0_95() throws Exception {
    Path rootDir = Paths.get(getClass().getClassLoader().getResource("bags/v0_95/bag").toURI());
    Bag bag = sut.read(rootDir);
    assertEquals(new Version(0, 95), bag.getVersion());
    for (SimpleImmutableEntry<String, String> keyValue : bag.getMetadata().getAll()) {
        if ("Package-Size".equals(keyValue.getKey())) {
            assertEquals("260 GB", keyValue.getValue());
        }
    }
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) Test(org.junit.Test)

Example 13 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class MetadataWriterTest method testWriteBagitInfoFile.

@Test
public void testWriteBagitInfoFile() throws IOException {
    File rootDir = folder.newFolder();
    File bagInfo = new File(rootDir, "bag-info.txt");
    File packageInfo = new File(rootDir, "package-info.txt");
    Metadata metadata = new Metadata();
    metadata.add("key1", "value1");
    metadata.add("key2", "value2");
    metadata.add("key3", "value3");
    assertFalse(bagInfo.exists());
    assertFalse(packageInfo.exists());
    MetadataWriter.writeBagMetadata(metadata, new Version(0, 96), Paths.get(rootDir.toURI()), StandardCharsets.UTF_8);
    assertTrue(bagInfo.exists());
    MetadataWriter.writeBagMetadata(metadata, new Version(0, 95), Paths.get(rootDir.toURI()), StandardCharsets.UTF_8);
    assertTrue(packageInfo.exists());
}
Also used : Version(gov.loc.repository.bagit.domain.Version) Metadata(gov.loc.repository.bagit.domain.Metadata) File(java.io.File) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 14 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class MandatoryVerifier method checkBagitFileExists.

/**
 * make sure the bagit.txt file exists
 *
 * @param rootDir the root directory of the bag
 * @param version the version of the bag
 * @throws MissingBagitFileException if the bag does not contain the bagit.txt file as required by the bagit specification
 */
public static void checkBagitFileExists(final Path rootDir, final Version version) throws MissingBagitFileException {
    logger.info("Checking if bagit.txt file exists");
    Path bagitFile = rootDir.resolve("bagit.txt");
    // @Incubating
    if (version.isSameOrNewer(new Version(2, 0))) {
        // is it a .bagit version?
        bagitFile = rootDir.resolve(DOT_BAGIT_DIR_NAME + File.separator + "bagit.txt");
    }
    if (!Files.exists(bagitFile)) {
        final String formattedMessage = messages.getString("file_should_exist_error");
        throw new MissingBagitFileException(MessageFormatter.format(formattedMessage, bagitFile).getMessage());
    }
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException)

Example 15 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class PathUtilsTest method testGetDataDirUsingVersion.

@Test
public void testGetDataDirUsingVersion() throws IOException {
    Path input = Paths.get("foo");
    Path expectedPath = input;
    Path actualPath = PathUtils.getDataDir(new Version(2, 0), input);
    assertEquals(expectedPath, actualPath);
    expectedPath = input.resolve("data");
    actualPath = PathUtils.getDataDir(new Version(0, 97), input);
    assertEquals(expectedPath, actualPath);
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) 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