|
39 | 39 | import static org.junit.Assume.assumeTrue; |
40 | 40 |
|
41 | 41 | import java.io.File; |
| 42 | +import java.io.IOException; |
| 43 | +import java.lang.annotation.Annotation; |
42 | 44 | 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; |
43 | 50 | import java.util.Map; |
44 | 51 | import java.util.TreeMap; |
45 | 52 |
|
@@ -79,11 +86,14 @@ public void testIndexer() throws Exception { |
79 | 86 |
|
80 | 87 | // read the index |
81 | 88 | 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 | + } |
86 | 93 |
|
| 94 | + public static void |
| 95 | + testDefaultAnnotations(Map<String, IndexItem<Complex>> map) |
| 96 | + { |
87 | 97 | assertEquals(4, map.size()); |
88 | 98 | Complex a = map.get(AnnotatedA.class.getName()).annotation(); |
89 | 99 | assertEquals("Hello, World!", a.simple().string1()); |
@@ -118,4 +128,38 @@ public void testIndexer() throws Exception { |
118 | 128 | public static String getResourcePath(final Class<?> clazz) { |
119 | 129 | return clazz.getName().replace('.', '/') + ".class"; |
120 | 130 | } |
| 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 | + } |
121 | 165 | } |
0 commit comments