Skip to content

Commit b23b6d5

Browse files
committed
refs #93 - fixed baginplace error when including hidden files
1 parent 020cdc1 commit b23b6d5

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/main/java/gov/loc/repository/bagit/creator/BagCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static Bag bagInPlace(final Path root, final Collection<SupportedAlgorith
5353
Files.createDirectory(dataDir);
5454
try(final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(root)){
5555
for(final Path path : directoryStream){
56-
if(!path.equals(dataDir) && !Files.isHidden(path) || includeHidden){
56+
if(!path.equals(dataDir) && (!Files.isHidden(path) || includeHidden)){
5757
Files.move(path, dataDir.resolve(path.getFileName()));
5858
}
5959
}

src/test/java/gov/loc/repository/bagit/creator/BagCreatorTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
88
import java.security.NoSuchAlgorithmException;
9+
import java.util.ArrayList;
910
import java.util.Arrays;
1011
import java.util.List;
1112

@@ -26,7 +27,7 @@ public class BagCreatorTest extends Assert {
2627

2728
@Test
2829
public void testBagInPlace() throws IOException, NoSuchAlgorithmException{
29-
List<Path> expectedPayloadFiles = createTestStructure();
30+
TestStructure structure = createTestStructure();
3031

3132
Bag bag = BagCreator.bagInPlace(Paths.get(folder.getRoot().toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), false);
3233

@@ -43,12 +44,40 @@ public void testBagInPlace() throws IOException, NoSuchAlgorithmException{
4344

4445
for(Manifest manifest : bag.getPayLoadManifests()){
4546
for(Path expectedPayloadFile : manifest.getFileToChecksumMap().keySet()){
46-
assertTrue(expectedPayloadFiles.contains(expectedPayloadFile));
47+
assertTrue(structure.regularPayloadFiles.contains(expectedPayloadFile));
4748
}
4849
}
4950
}
5051

51-
private List<Path> createTestStructure() throws IOException{
52+
@Test
53+
public void testBagInPlaceIncludingHidden() throws IOException, NoSuchAlgorithmException{
54+
TestStructure structure = createTestStructure();
55+
56+
Bag bag = BagCreator.bagInPlace(Paths.get(folder.getRoot().toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), true);
57+
58+
assertEquals(new Version(0, 97), bag.getVersion());
59+
60+
File expectedManifest = new File(folder.getRoot(), "manifest-md5.txt");
61+
assertTrue(expectedManifest.exists());
62+
63+
File expectedTagManifest = new File(folder.getRoot(), "tagmanifest-md5.txt");
64+
assertTrue(expectedTagManifest.exists());
65+
66+
File bagitFile = new File(folder.getRoot(), "bagit.txt");
67+
assertTrue(bagitFile.exists());
68+
69+
for(Manifest manifest : bag.getPayLoadManifests()){
70+
for(Path expectedPayloadFile : manifest.getFileToChecksumMap().keySet()){
71+
assertTrue(expectedPayloadFile + " doesn't exist but it should!",
72+
structure.regularPayloadFiles.contains(expectedPayloadFile) ||
73+
structure.hiddenPayloadFiles.contains(expectedPayloadFile));
74+
}
75+
}
76+
}
77+
78+
private TestStructure createTestStructure() throws IOException{
79+
TestStructure structure = new TestStructure();
80+
5281
Path rootDir = Paths.get(folder.getRoot().toURI());
5382
Path dataDir = rootDir.resolve("data");
5483

@@ -74,7 +103,15 @@ private List<Path> createTestStructure() throws IOException{
74103
File file3 = new File(hiddenDirectory, "file3.txt");
75104
file3.createNewFile();
76105

77-
return Arrays.asList(dataDir.resolve(file1Path.getFileName()), dataDir.resolve(file2Path.getFileName()));
106+
structure.regularPayloadFiles.add(dataDir.resolve(file1Path.getFileName()));
107+
structure.regularPayloadFiles.add(dataDir.resolve(file2Path.getFileName()));
108+
109+
structure.hiddenPayloadFiles.add(dataDir.resolve(hiddenFile.getName()));
110+
Path hiddenDirPath = dataDir.resolve(hiddenDirectory.getName());
111+
Path hiddenFile2Path = hiddenDirPath.resolve(hiddenFile2.getName());
112+
structure.hiddenPayloadFiles.add(hiddenFile2Path);
113+
structure.hiddenPayloadFiles.add(hiddenDirPath.resolve(file3.getName()));
114+
return structure;
78115
}
79116

80117
@Test
@@ -99,4 +136,9 @@ public void testCreateDotBagit() throws IOException, NoSuchAlgorithmException{
99136
assertTrue(Files.exists(expectedTagManifestFile));
100137
assertTrue(Files.size(expectedTagManifestFile) > 0);
101138
}
139+
140+
private class TestStructure{
141+
List<Path> regularPayloadFiles = new ArrayList<>();
142+
List<Path> hiddenPayloadFiles = new ArrayList<>();
143+
}
102144
}

0 commit comments

Comments
 (0)