Skip to content

Commit b4b3ef4

Browse files
committed
refs #99 - reverted to using relative paths and added test to ensure
that format for fetch file is correct This reverts commit e7e76da.
1 parent ef91511 commit b4b3ef4

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/main/java/gov/loc/repository/bagit/writer/BagWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void write(final Bag bag, final Path outputDir) throws IOException
6161
}
6262
if(bag.getItemsToFetch().size() > 0){
6363
logger.debug(messages.getString("writing_fetch_file"));
64-
FetchWriter.writeFetchFile(bag.getItemsToFetch(), bagitDir, bag.getFileEncoding());
64+
FetchWriter.writeFetchFile(bag.getItemsToFetch(), bagitDir, bag.getRootDir(), bag.getFileEncoding());
6565
}
6666
if(bag.getTagManifests().size() > 0){
6767
logger.debug(messages.getString("writing_tag_manifests"));

src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ private FetchWriter(){
2929
*
3030
* @param itemsToFetch the list of {@link FetchItem}s to write into the fetch.txt
3131
* @param outputDir the root of the bag
32+
* @param bagitRootDir the path to the root of the bag
3233
* @param charsetName the name of the encoding for the file
3334
*
3435
* @throws IOException if there was a problem writing a file
3536
*/
36-
public static void writeFetchFile(final List<FetchItem> itemsToFetch, final Path outputDir, final Charset charsetName) throws IOException{
37+
public static void writeFetchFile(final List<FetchItem> itemsToFetch, final Path outputDir, final Path bagitRootDir, final Charset charsetName) throws IOException{
3738
logger.debug(messages.getString("writing_fetch_file_to_path"), outputDir);
3839
final Path fetchFilePath = outputDir.resolve("fetch.txt");
3940

4041
for(final FetchItem item : itemsToFetch){
41-
final String line = formatFetchLine(item);
42+
final String line = formatFetchLine(item, bagitRootDir);
4243
logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
4344
Files.write(fetchFilePath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
4445
}
4546
}
4647

47-
private static String formatFetchLine(final FetchItem fetchItem){
48+
private static String formatFetchLine(final FetchItem fetchItem, final Path bagitRootDir){
4849
final StringBuilder sb = new StringBuilder();
4950
sb.append(fetchItem.getUrl()).append(' ');
5051

@@ -55,7 +56,7 @@ private static String formatFetchLine(final FetchItem fetchItem){
5556
sb.append(fetchItem.getLength()).append(' ');
5657
}
5758

58-
sb.append(fetchItem.getPath());
59+
sb.append(RelativePathWriter.formatRelativePathString(bagitRootDir, fetchItem.getPath()));
5960

6061
return sb.toString();
6162
}

src/test/java/gov/loc/repository/bagit/writer/FetchWriterTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import java.lang.reflect.InvocationTargetException;
55
import java.net.URL;
66
import java.nio.charset.StandardCharsets;
7+
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.nio.file.Paths;
10+
import java.util.ArrayList;
911
import java.util.Arrays;
1012
import java.util.List;
1113

@@ -37,7 +39,32 @@ public void testWriteFetchFile() throws Exception{
3739

3840

3941
assertFalse(fetch.exists());
40-
FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), StandardCharsets.UTF_8);
42+
FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), rootPath, StandardCharsets.UTF_8);
4143
assertTrue(fetch.exists());
4244
}
45+
46+
@Test
47+
public void testFetchFileIsFormattedCorrectly() throws Exception{
48+
File rootDir = folder.newFolder();
49+
Path rootPath = rootDir.toPath();
50+
File fetch = new File(rootDir, "fetch.txt");
51+
List<FetchItem> itemsToFetch = new ArrayList<>();
52+
53+
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")));
54+
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")));
55+
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")));
56+
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")));
57+
itemsToFetch.add(new FetchItem(new URL("http://localhost:8989/bags/v0_96/holey-bag/data/test2.txt"), null, rootPath.resolve("data/test2.txt")));
58+
59+
FetchWriter.writeFetchFile(itemsToFetch, Paths.get(rootDir.toURI()), rootPath, StandardCharsets.UTF_8);
60+
61+
List<String> expectedLines = Arrays.asList("http://localhost:8989/bags/v0_96/holey-bag/data/dir1/test3.txt - data/dir1/test3.txt",
62+
"http://localhost:8989/bags/v0_96/holey-bag/data/dir2/dir3/test5.txt - data/dir2/dir3/test5.txt",
63+
"http://localhost:8989/bags/v0_96/holey-bag/data/dir2/test4.txt - data/dir2/test4.txt",
64+
"http://localhost:8989/bags/v0_96/holey-bag/data/test%201.txt - data/test 1.txt",
65+
"http://localhost:8989/bags/v0_96/holey-bag/data/test2.txt - data/test2.txt");
66+
List<String> actualLines = Files.readAllLines(fetch.toPath());
67+
68+
assertEquals(expectedLines, actualLines);
69+
}
4370
}

0 commit comments

Comments
 (0)