Search in sources :

Example 1 with FetchItem

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

the class BagReaderTest method testReadUTF_16_Encoding.

@Test
public void testReadUTF_16_Encoding() throws Exception {
    Metadata expectedMetaData = new Metadata();
    expectedMetaData.add("Bag-Software-Agent", "bagit.py <http://github.com/libraryofcongress/bagit-python>");
    expectedMetaData.add("Bagging-Date", "2016-02-26");
    expectedMetaData.add("Contact-Email", "cadams@loc.gov");
    expectedMetaData.add("Contact-Name", "Chris Adams");
    expectedMetaData.add("Payload-Oxum", "58.2");
    Path bagPath = Paths.get(new File("src/test/resources/UTF-16-encoded-tag-files").toURI());
    List<FetchItem> expectedFetchItems = new ArrayList<>();
    expectedFetchItems.add(new FetchItem(new URL("http://localhost/foo/data/dir1/test3.txt"), -1l, bagPath.resolve("data/dir1/test3.txt")));
    Bag bag = sut.read(bagPath);
    assertNotNull(bag);
    assertEquals(StandardCharsets.UTF_16, bag.getFileEncoding());
    assertEquals(expectedMetaData, bag.getMetadata());
    assertEquals(expectedFetchItems, bag.getItemsToFetch());
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) Metadata(gov.loc.repository.bagit.domain.Metadata) ArrayList(java.util.ArrayList) Bag(gov.loc.repository.bagit.domain.Bag) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 2 with FetchItem

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

the class FetchWriterTest method testFetchFileIsFormattedCorrectly.

@Test
public void testFetchFileIsFormattedCorrectly() throws Exception {
    File rootDir = folder.newFolder();
    Path rootPath = rootDir.toPath();
    File fetch = new File(rootDir, "fetch.txt");
    List<FetchItem> itemsToFetch = new ArrayList<>();
    itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/dir1/test3.txt"), null, rootPath.resolve("data/dir1/test3.txt")));
    itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/dir2/dir3/test5.txt"), null, rootPath.resolve("data/dir2/dir3/test5.txt")));
    itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/dir2/test4.txt"), null, rootPath.resolve("data/dir2/test4.txt")));
    itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/test%201.txt"), null, rootPath.resolve("data/test 1.txt")));
    itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/test2.txt"), null, rootPath.resolve("data/test2.txt")));
    FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), rootPath, StandardCharsets.UTF_8);
    List<String> expectedLines = Arrays.asList("http://localhost:8989/bags/v0_96/holey-bag/data/dir1/test3.txt - data/dir1/test3.txt", "http://localhost:8989/bags/v0_96/holey-bag/data/dir2/dir3/test5.txt - data/dir2/dir3/test5.txt", "http://localhost:8989/bags/v0_96/holey-bag/data/dir2/test4.txt - data/dir2/test4.txt", "http://localhost:8989/bags/v0_96/holey-bag/data/test%201.txt - data/test 1.txt", "http://localhost:8989/bags/v0_96/holey-bag/data/test2.txt - data/test2.txt");
    List<String> actualLines = Files.readAllLines(fetch.toPath());
    assertEquals(expectedLines, actualLines);
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 3 with FetchItem

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

the class FetchHttpFileExample method fetchFileUsingJavaStandardLibrary.

/**
 * <b> THIS IS JUST AN EXAMPLE. DO NOT USE IN PRODUCTION!</b>
 *
 * @throws IOException if there is a problem getting the file
 */
@Test
public void fetchFileUsingJavaStandardLibrary() throws IOException {
    // in actual usage you would iterate over the list of FetchItem in the Bag
    FetchItem item = new FetchItem(new URL("https://en.wikipedia.org/wiki/Main_Page"), 0l, folder.newFile("Main_page.html").toPath());
    try {
        Files.copy(item.url.openStream(), item.path, StandardCopyOption.REPLACE_EXISTING);
        assertTrue(Files.exists(item.path));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FetchItem(gov.loc.repository.bagit.domain.FetchItem) URL(java.net.URL) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with FetchItem

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

the class FetchReaderTest method testReadFetchWithNoSizeSpecified.

@Test
public void testReadFetchWithNoSizeSpecified() throws Exception {
    Path fetchFile = Paths.get(getClass().getClassLoader().getResource("fetchFiles/fetchWithNoSizeSpecified.txt").toURI());
    List<FetchItem> returnedItems = FetchReader.readFetch(fetchFile, StandardCharsets.UTF_8, fetchFile.getParent());
    for (FetchItem item : returnedItems) {
        assertNotNull(item.url);
        assertTrue(urls.contains(item.url));
        assertEquals(Long.valueOf(-1), item.length);
        assertNotNull(item.path);
    }
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 5 with FetchItem

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

the class FetchReaderTest method testReadFetchWithSizeSpecified.

@Test
public void testReadFetchWithSizeSpecified() throws Exception {
    Path fetchFile = Paths.get(getClass().getClassLoader().getResource("fetchFiles/fetchWithSizeSpecified.txt").toURI());
    List<FetchItem> returnedItems = FetchReader.readFetch(fetchFile, StandardCharsets.UTF_8, Paths.get("/foo"));
    for (FetchItem item : returnedItems) {
        assertNotNull(item.url);
        assertTrue(urls.contains(item.url));
        assertTrue(item.length > 0);
        assertNotNull(item.path);
    }
}
Also used : Path(java.nio.file.Path) FetchItem(gov.loc.repository.bagit.domain.FetchItem) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Aggregations

FetchItem (gov.loc.repository.bagit.domain.FetchItem)9 Path (java.nio.file.Path)8 Test (org.junit.Test)7 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)5 URL (java.net.URL)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 Bag (gov.loc.repository.bagit.domain.Bag)1 Manifest (gov.loc.repository.bagit.domain.Manifest)1 Metadata (gov.loc.repository.bagit.domain.Metadata)1 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1