Skip to content

Commit

Permalink
refs #99 - reverted to using relative paths and added test to ensure
Browse files Browse the repository at this point in the history
that format for fetch file is correct

This reverts commit e7e76da.
  • Loading branch information
johnscancella committed Jan 23, 2018
1 parent ef91511 commit b4b3ef4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchItem> itemsToFetch, final Path outputDir, final Charset charsetName) throws IOException{
public static void writeFetchFile(final List<FetchItem> 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(' ');

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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);
}
}

0 comments on commit b4b3ef4

Please sign in to comment.