Skip to content

Commit 8d089ab

Browse files
committed
FileUtilsTest: expand testListContents()
It now tests with and without recursion, and with and without directory inclusion.
1 parent 1160213 commit 8d089ab

1 file changed

Lines changed: 54 additions & 12 deletions

File tree

‎src/test/java/org/scijava/util/FileUtilsTest.java‎

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package org.scijava.util;
3333

34+
import static org.junit.Assert.assertArrayEquals;
3435
import static org.junit.Assert.assertEquals;
3536
import static org.junit.Assert.assertNotNull;
3637
import static org.junit.Assert.assertTrue;
@@ -179,14 +180,14 @@ public void testLimitPath() {
179180

180181
@Test
181182
public void testListContents() throws IOException, URISyntaxException {
183+
// verify that listContents on a non-existent file returns the empty set
182184
File nonExisting;
183185
int i = 0;
184186
for (;;) {
185187
nonExisting = new File("" + i);
186188
if (!nonExisting.exists()) break;
187189
i++;
188190
}
189-
190191
try {
191192
Collection<URL> urls = FileUtils.listContents(nonExisting.toURI().toURL());
192193
assertNotNull(urls);
@@ -195,31 +196,72 @@ public void testListContents() throws IOException, URISyntaxException {
195196
e.printStackTrace();
196197
}
197198

198-
// list items inside a .jar file
199-
final File jarFile = File.createTempFile("listFileContentsTest", ".jar");
199+
// write some items to a temporary .jar file
200+
final String subDir = "sub 𝄞directory/";
201+
final String subSubDir = "more 𝄢stuff/";
202+
final File jarFile = File.createTempFile("listContentsTest", ".jar");
200203
final FileOutputStream out = new FileOutputStream(jarFile);
201204
final JarOutputStream jarOut = new JarOutputStream(out);
202205
try {
203-
jarOut.putNextEntry(new JarEntry("sub 𝄞directory/hello.txt"));
206+
jarOut.putNextEntry(new JarEntry(subDir));
207+
jarOut.closeEntry();
208+
jarOut.putNextEntry(new JarEntry(subDir + subSubDir));
209+
jarOut.closeEntry();
210+
// NB: This entry is not in the subdirectory, and should not be listed.
211+
jarOut.putNextEntry(new JarEntry("foo.txt"));
212+
jarOut.write("bar".getBytes());
213+
jarOut.closeEntry();
214+
// NB: The next two entries are directly beneath the subdirectory.
215+
jarOut.putNextEntry(new JarEntry(subDir + "hello.txt"));
204216
jarOut.write("world".getBytes());
205217
jarOut.closeEntry();
206-
jarOut.putNextEntry(new JarEntry("sub 𝄞directory/rock.txt"));
218+
jarOut.putNextEntry(new JarEntry(subDir + "rock.txt"));
207219
jarOut.write("roll".getBytes());
208220
jarOut.closeEntry();
221+
// NB: The last two entries are beneath a second subdirectory,
222+
// and should only be listed when the recurse flag is set to true.
223+
jarOut.putNextEntry(new JarEntry(subDir + subSubDir + "fox.txt"));
224+
jarOut.write("the quick brown fox".getBytes());
225+
jarOut.closeEntry();
226+
jarOut.putNextEntry(new JarEntry(subDir + subSubDir + "dog.txt"));
227+
jarOut.write("jumps over the lazy dog".getBytes());
228+
jarOut.closeEntry();
209229
jarOut.close();
210230
} finally {
211231
out.close();
212232
}
213233

214-
final String path = new URI(null, null, "!/sub 𝄞directory/", null).toString();
215-
final String url = "jar:" + jarFile.toURI().toURL() + path;
216-
final Collection<URL> set = FileUtils.listContents(new URL(url));
217-
final URL[] list = set.toArray(new URL[set.size()]);
234+
final String uriPath = new URI(null, null, "!/" + subDir, null).toString();
235+
final String urlPath = "jar:" + jarFile.toURI().toURL() + uriPath;
236+
final URL url = new URL(urlPath);
237+
final URL subSubURL = new URL(urlPath + "more%20𝄢stuff/");
238+
final URL helloURL = new URL(urlPath + "hello.txt");
239+
final URL rockURL = new URL(urlPath + "rock.txt");
240+
final URL foxURL = new URL(urlPath + "more%20𝄢stuff/fox.txt");
241+
final URL dogURL = new URL(urlPath + "more%20𝄢stuff/dog.txt");
242+
243+
// check listContents: recursive without directories
244+
final Collection<URL> setTT = FileUtils.listContents(url, true, true);
245+
final URL[] listTT = setTT.toArray(new URL[setTT.size()]);
246+
assertArrayEquals(new URL[] { helloURL, rockURL, foxURL, dogURL }, listTT);
247+
248+
// check listContents: recursive including directories
249+
final Collection<URL> setTF = FileUtils.listContents(url, true, false);
250+
final URL[] listTF = setTF.toArray(new URL[setTF.size()]);
251+
assertArrayEquals(
252+
new URL[] { subSubURL, helloURL, rockURL, foxURL, dogURL }, listTF);
253+
254+
// check listContents: non-recursive without directories
255+
final Collection<URL> setFT = FileUtils.listContents(url, false, true);
256+
final URL[] listFT = setFT.toArray(new URL[setFT.size()]);
257+
assertArrayEquals(new URL[] { helloURL, rockURL }, listFT);
218258

219-
assertEquals(2, list.length);
220-
assertEquals(new URL(url + "hello.txt"), list[0]);
221-
assertEquals(new URL(url + "rock.txt"), list[1]);
259+
// check listContents: non-recursive including directories
260+
final Collection<URL> setFF = FileUtils.listContents(url, false, false);
261+
final URL[] listFF = setFF.toArray(new URL[setFF.size()]);
262+
assertArrayEquals(new URL[] { subSubURL, helloURL, rockURL }, listFF);
222263

264+
// clean up
223265
assertTrue(jarFile.delete());
224266
}
225267

0 commit comments

Comments
 (0)