From b4b3ef416736e874ee75bb6e3a068073c54918b3 Mon Sep 17 00:00:00 2001 From: John Scancella Date: Tue, 23 Jan 2018 09:22:11 -0500 Subject: [PATCH] refs #99 - reverted to using relative paths and added test to ensure that format for fetch file is correct This reverts commit e7e76da3ca5e9fe24a24be0bd929c3f4de41c0eb. --- .../repository/bagit/writer/BagWriter.java | 2 +- .../repository/bagit/writer/FetchWriter.java | 9 +++--- .../bagit/writer/FetchWriterTest.java | 29 ++++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/gov/loc/repository/bagit/writer/BagWriter.java b/src/main/java/gov/loc/repository/bagit/writer/BagWriter.java index b94bd6244..8c87a4ff4 100644 --- a/src/main/java/gov/loc/repository/bagit/writer/BagWriter.java +++ b/src/main/java/gov/loc/repository/bagit/writer/BagWriter.java @@ -61,7 +61,7 @@ public static void write(final Bag bag, final Path outputDir) throws IOException } if(bag.getItemsToFetch().size() > 0){ logger.debug(messages.getString("writing_fetch_file")); - FetchWriter.writeFetchFile(bag.getItemsToFetch(), bagitDir, bag.getFileEncoding()); + FetchWriter.writeFetchFile(bag.getItemsToFetch(), bagitDir, bag.getRootDir(), bag.getFileEncoding()); } if(bag.getTagManifests().size() > 0){ logger.debug(messages.getString("writing_tag_manifests")); diff --git a/src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java b/src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java index d91941e9c..30c4c780c 100644 --- a/src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java +++ b/src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java @@ -29,22 +29,23 @@ private FetchWriter(){ * * @param itemsToFetch the list of {@link FetchItem}s to write into the fetch.txt * @param outputDir the root of the bag + * @param bagitRootDir the path to the root of the bag * @param charsetName the name of the encoding for the file * * @throws IOException if there was a problem writing a file */ - public static void writeFetchFile(final List itemsToFetch, final Path outputDir, final Charset charsetName) throws IOException{ + public static void writeFetchFile(final List itemsToFetch, final Path outputDir, final Path bagitRootDir, final Charset charsetName) throws IOException{ logger.debug(messages.getString("writing_fetch_file_to_path"), outputDir); final Path fetchFilePath = outputDir.resolve("fetch.txt"); for(final FetchItem item : itemsToFetch){ - final String line = formatFetchLine(item); + final String line = formatFetchLine(item, bagitRootDir); logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath); Files.write(fetchFilePath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE); } } - private static String formatFetchLine(final FetchItem fetchItem){ + private static String formatFetchLine(final FetchItem fetchItem, final Path bagitRootDir){ final StringBuilder sb = new StringBuilder(); sb.append(fetchItem.getUrl()).append(' '); @@ -55,7 +56,7 @@ private static String formatFetchLine(final FetchItem fetchItem){ sb.append(fetchItem.getLength()).append(' '); } - sb.append(fetchItem.getPath()); + sb.append(RelativePathWriter.formatRelativePathString(bagitRootDir, fetchItem.getPath())); return sb.toString(); } diff --git a/src/test/java/gov/loc/repository/bagit/writer/FetchWriterTest.java b/src/test/java/gov/loc/repository/bagit/writer/FetchWriterTest.java index 23697a132..a97103ee9 100644 --- a/src/test/java/gov/loc/repository/bagit/writer/FetchWriterTest.java +++ b/src/test/java/gov/loc/repository/bagit/writer/FetchWriterTest.java @@ -4,8 +4,10 @@ import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -37,7 +39,32 @@ public void testWriteFetchFile() throws Exception{ assertFalse(fetch.exists()); - FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), StandardCharsets.UTF_8); + FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), rootPath, StandardCharsets.UTF_8); assertTrue(fetch.exists()); } + + @Test + public void testFetchFileIsFormattedCorrectly() throws Exception{ + File rootDir = folder.newFolder(); + Path rootPath = rootDir.toPath(); + File fetch = new File(rootDir, "fetch.txt"); + List 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 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 actualLines = Files.readAllLines(fetch.toPath()); + + assertEquals(expectedLines, actualLines); + } }