1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.util.lang;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.allOf;
20 import static org.hamcrest.Matchers.equalTo;
21 import static org.hamcrest.Matchers.hasItem;
22 import static org.hamcrest.Matchers.is;
23
24 import java.net.URL;
25 import java.net.URLClassLoader;
26 import java.util.List;
27
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import de.smartics.testdoc.annotations.Uut;
32 import de.smartics.testdoc.categories.type.Coverage;
33
34
35
36
37 @Uut(type = ClassPathDirectoryListing.class)
38 @SuppressWarnings("unchecked")
39 public class ClassPathDirectoryListingTest
40 {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Test
58 public void readsFilesFromTheFileSystem()
59 {
60 final ClassLoader loader = getClass().getClassLoader();
61 final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
62 final String resourcePath = "de/smartics/util/lang/testdir";
63
64 final List<String> listing = uut.list(resourcePath);
65 assertThat(listing.size(), is(equalTo(3)));
66 assertThat(listing,
67 allOf(hasItem("subdir"), hasItem("file-1.txt"), hasItem("file-2.txt")));
68 }
69
70 @Test
71 public void readsFilesFromAJarArchive()
72 {
73 final URL jarUrl = getClass().getResource("test-archive.jar");
74 final ClassLoader loader = new URLClassLoader(new URL[] { jarUrl }, null);
75
76 final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
77 final String resourcePath = "testdir";
78
79 final List<String> listing = uut.list(resourcePath);
80 assertThat(listing.size(), is(equalTo(3)));
81 assertThat(listing,
82 allOf(hasItem("subdir"), hasItem("file-1.txt"), hasItem("file-2.txt")));
83 }
84
85 @Test
86 public void readsFromJarRoot()
87 {
88 final URL jarUrl = getClass().getResource("test-archive.jar");
89 final ClassLoader loader = new URLClassLoader(new URL[] { jarUrl });
90
91 final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
92 final String resourcePath = "";
93
94 final List<String> listing = uut.list(resourcePath);
95 assertThat(listing.size(), is(equalTo(1)));
96 assertThat(listing, allOf(hasItem("de")));
97 }
98
99 @Test(expected = NullPointerException.class)
100 @Category(Coverage.class)
101 public void doesNotAllowNullAsResourcePath()
102 {
103 final ClassLoader loader = getClass().getClassLoader();
104 final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
105 final String resourcePath = null;
106
107 uut.list(resourcePath);
108 }
109
110 @Test(expected = IllegalArgumentException.class)
111 @Category(Coverage.class)
112 public void throwsIllegalArgumentExceptionIfResourceNotFoundOnClassPath()
113 {
114 final ClassLoader loader = getClass().getClassLoader();
115 final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
116 final String resourcePath = "unknown";
117
118 uut.list(resourcePath);
119 }
120 }