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());
}
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);
}
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();
}
}
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);
}
}
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);
}
}
Aggregations