Skip to content

Commit df8ea39

Browse files
committed
Refactor the DirectoryIndexerTest to facilitate testing
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent b519f11 commit df8ea39

1 file changed

Lines changed: 48 additions & 4 deletions

File tree

src/test/java/org/scijava/annotations/DirectoryIndexerTest.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@
3939
import static org.junit.Assume.assumeTrue;
4040

4141
import java.io.File;
42+
import java.io.IOException;
43+
import java.lang.annotation.Annotation;
4244
import java.net.URL;
45+
import java.net.URLConnection;
46+
import java.util.ArrayList;
47+
import java.util.Collections;
48+
import java.util.Enumeration;
49+
import java.util.List;
4350
import java.util.Map;
4451
import java.util.TreeMap;
4552

@@ -79,11 +86,14 @@ public void testIndexer() throws Exception {
7986

8087
// read the index
8188
final Map<String, IndexItem<Complex>> map =
82-
new TreeMap<String, IndexItem<Complex>>();
83-
for (final IndexItem<Complex> item : Index.load(Complex.class)) {
84-
map.put(item.className(), item);
85-
}
89+
readIndex(Complex.class, DirectoryIndexerTest.class.getClassLoader());
90+
91+
testDefaultAnnotations(map);
92+
}
8693

94+
public static void
95+
testDefaultAnnotations(Map<String, IndexItem<Complex>> map)
96+
{
8797
assertEquals(4, map.size());
8898
Complex a = map.get(AnnotatedA.class.getName()).annotation();
8999
assertEquals("Hello, World!", a.simple().string1());
@@ -118,4 +128,38 @@ public void testIndexer() throws Exception {
118128
public static String getResourcePath(final Class<?> clazz) {
119129
return clazz.getName().replace('.', '/') + ".class";
120130
}
131+
132+
public static <A extends Annotation> Map<String, IndexItem<A>> readIndex(
133+
final Class<A> annotationClass, final URL... directories)
134+
{
135+
final ClassLoader loader = new ClassLoader() {
136+
137+
@Override
138+
public final Enumeration<URL> getResources(final String path)
139+
throws IOException
140+
{
141+
final List<URL> urls = new ArrayList<URL>();
142+
for (final URL directory : directories) {
143+
final URL url = new URL(directory, path);
144+
final URLConnection connection = url.openConnection();
145+
if (connection.getLastModified() > 0) {
146+
urls.add(url);
147+
}
148+
}
149+
return Collections.enumeration(urls);
150+
}
151+
};
152+
return readIndex(annotationClass, loader);
153+
}
154+
155+
public static <A extends Annotation> Map<String, IndexItem<A>> readIndex(
156+
final Class<A> annotationClass, final ClassLoader loader)
157+
{
158+
// read the index
159+
final Map<String, IndexItem<A>> map = new TreeMap<String, IndexItem<A>>();
160+
for (final IndexItem<A> item : Index.load(annotationClass, loader)) {
161+
map.put(item.className(), item);
162+
}
163+
return map;
164+
}
121165
}

0 commit comments

Comments
 (0)