|
1 | 1 | package org.hamcrest.generator; |
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
4 | 6 | import static org.junit.Assert.assertTrue; |
5 | 7 |
|
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileReader; |
6 | 10 | import java.io.StringReader; |
7 | 11 | import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.Comparator; |
8 | 14 | import java.util.Iterator; |
9 | 15 | import java.util.List; |
10 | 16 |
|
@@ -84,15 +90,168 @@ public final class QDoxFactoryReaderTest { |
84 | 90 | factoryMethod.getJavaDoc()); |
85 | 91 | } |
86 | 92 |
|
| 93 | + @Test public void |
| 94 | + iteratesOverFactoryMethods() throws FileNotFoundException { |
| 95 | + final QDoxFactoryReader reader = readerForTestClass("SimpleSetOfMatchers"); |
| 96 | + final List<FactoryMethod> methods = methodsReadBy(reader); |
| 97 | + |
| 98 | + assertEquals(2, methods.size()); |
| 99 | + |
| 100 | + final String expectedClass = "test.SimpleSetOfMatchers"; |
| 101 | + |
| 102 | + assertEquals("firstMethod", methods.get(0).getName()); |
| 103 | + assertEquals(expectedClass, methods.get(0).getMatcherClass()); |
| 104 | + |
| 105 | + assertEquals("secondMethod", methods.get(1).getName()); |
| 106 | + assertEquals(expectedClass, methods.get(1).getMatcherClass()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test public void |
| 110 | + onlyReadsPublicStaticAnnotatedMethodsThatReturnNonVoid() throws FileNotFoundException { |
| 111 | + final QDoxFactoryReader reader = readerForTestClass("MatchersWithDodgySignatures"); |
| 112 | + final List<FactoryMethod> methods = methodsReadBy(reader); |
| 113 | + |
| 114 | + assertEquals(2, methods.size()); |
| 115 | + assertEquals("anotherGoodMethod", methods.get(0).getName()); |
| 116 | + assertEquals("goodMethod", methods.get(1).getName()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test public void |
| 120 | + readsFullyQualifiedGenericType() throws FileNotFoundException { |
| 121 | + FactoryMethod method = readMethod("GenerifiedMatchers", "generifiedType"); |
| 122 | + assertEquals("java.util.Comparator<java.lang.String>", method.getGenerifiedType()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test public void |
| 126 | + readsNullGenerifiedTypeIfNotPresent() throws FileNotFoundException { |
| 127 | + FactoryMethod method = readMethod("GenerifiedMatchers", "noGenerifiedType"); |
| 128 | + assertNull(method.getGenerifiedType()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test public void |
| 132 | + readsGenericsInGenericType() throws FileNotFoundException { |
| 133 | + FactoryMethod method = readMethod("GenerifiedMatchers", "crazyType"); |
| 134 | + assertEquals( |
| 135 | + "java.util.Map<? extends java.util.Set<java.lang.Long>, org.hamcrest.Factory>", |
| 136 | + method.getGenerifiedType()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test public void |
| 140 | + readsParameterTypes() throws FileNotFoundException { |
| 141 | + FactoryMethod method = readMethod("ParameterizedMatchers", "withParam"); |
| 142 | + List<FactoryMethod.Parameter> params = method.getParameters(); |
| 143 | + assertEquals(3, params.size()); |
| 144 | + |
| 145 | + assertEquals("java.lang.String", params.get(0).getType()); |
| 146 | + assertEquals("int[]", params.get(1).getType()); |
| 147 | + assertEquals("java.util.Collection<java.lang.Object>", params.get(2).getType()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test public void |
| 151 | + readsArrayAndVarArgParameterTypes() throws FileNotFoundException { |
| 152 | + FactoryMethod arrayMethod = readMethod("ParameterizedMatchers", "withArray"); |
| 153 | + assertEquals("java.lang.String[]", arrayMethod.getParameters().get(0).getType()); |
| 154 | + |
| 155 | + FactoryMethod varArgsMethod = readMethod("ParameterizedMatchers", "withVarArgs"); |
| 156 | + assertEquals("java.lang.String...", varArgsMethod.getParameters().get(0).getType()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test public void |
| 160 | + readsGenerifiedParameterTypes() throws FileNotFoundException { |
| 161 | + FactoryMethod method = readMethod("ParameterizedMatchers", "withGenerifiedParam"); |
| 162 | + |
| 163 | + assertEquals("java.util.Collection<? extends java.lang.Comparable<java.lang.String>>", |
| 164 | + method.getParameters().get(0).getType()); |
| 165 | + |
| 166 | + String expected = System.getProperty("java.version").startsWith("1.7.") |
| 167 | + ? "java.util.Set<[Ljava.lang.String;>[]" |
| 168 | + : "java.util.Set<java.lang.String[]>[]"; |
| 169 | + |
| 170 | + assertEquals(expected, method.getParameters().get(1).getType()); |
| 171 | + } |
| 172 | + |
| 173 | + @Test public void |
| 174 | + cannotReadParameterNamesSoMakesThemUpInstead() throws FileNotFoundException { |
| 175 | + FactoryMethod method = readMethod("ParameterizedMatchers", "withParam"); |
| 176 | + List<FactoryMethod.Parameter> params = method.getParameters(); |
| 177 | + |
| 178 | + assertEquals("param1", params.get(0).getName()); |
| 179 | + assertEquals("param2", params.get(1).getName()); |
| 180 | + assertEquals("param3", params.get(2).getName()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test public void |
| 184 | + readsExceptions() throws FileNotFoundException { |
| 185 | + FactoryMethod method = readMethod("ExceptionalMatchers", "withExceptions"); |
| 186 | + List<String> exceptions = method.getExceptions(); |
| 187 | + assertEquals(3, exceptions.size()); |
| 188 | + |
| 189 | + assertEquals("java.lang.Error", exceptions.get(0)); |
| 190 | + assertEquals("java.io.IOException", exceptions.get(1)); |
| 191 | + assertEquals("java.lang.RuntimeException", exceptions.get(2)); |
| 192 | + } |
| 193 | + |
| 194 | + @Test public void |
| 195 | + cannotReadJavaDoc() throws FileNotFoundException { |
| 196 | + // JavaDoc information is not available through reflection alone. |
| 197 | + FactoryMethod method = readMethod("WithJavaDoc", "documented"); |
| 198 | + assertEquals(null, method.getJavaDoc()); |
| 199 | + } |
| 200 | + |
| 201 | + @Test public void |
| 202 | + readsGenericTypeParameters() throws FileNotFoundException { |
| 203 | + FactoryMethod method = readMethod("G", "x"); |
| 204 | + assertEquals("T", method.getGenericTypeParameters().get(0)); |
| 205 | + assertEquals("V extends java.util.List<java.lang.String> & java.lang.Comparable<java.lang.String>", |
| 206 | + method.getGenericTypeParameters().get(1)); |
| 207 | + assertEquals("java.util.Map<T, V[]>", method.getGenerifiedType()); |
| 208 | + assertEquals("java.util.Set<T>", method.getParameters().get(0).getType()); |
| 209 | + assertEquals("V", method.getParameters().get(1).getType()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test public void |
| 213 | + catchesSubclasses() throws FileNotFoundException { |
| 214 | + assertNotNull(readMethod("SubclassOfMatcher", "subclassMethod")); |
| 215 | + } |
| 216 | + |
| 217 | + private static List<FactoryMethod> methodsReadBy(final Iterable<FactoryMethod> reader) { |
| 218 | + final List<FactoryMethod> extractedMethods = new ArrayList<FactoryMethod>(); |
| 219 | + for (FactoryMethod factoryMethod : reader) { |
| 220 | + extractedMethods.add(factoryMethod); |
| 221 | + } |
| 222 | + Collections.sort(extractedMethods, new Comparator<FactoryMethod>() { |
| 223 | + @Override public int compare(FactoryMethod o1, FactoryMethod o2) { |
| 224 | + return o1.getName().compareTo(o2.getName()); |
| 225 | + } |
| 226 | + }); |
| 227 | + return extractedMethods; |
| 228 | + } |
| 229 | + |
| 230 | + private static QDoxFactoryReader readerForTestClass(String name) |
| 231 | + throws FileNotFoundException { |
| 232 | + QDox qdox = new QDox(); |
| 233 | + qdox.addSource(new FileReader("src/test/java-source/test/" + name + ".java")); |
| 234 | + |
| 235 | + return new QDoxFactoryReader(qdox, "test." + name); |
| 236 | + } |
| 237 | + |
| 238 | + private static FactoryMethod readMethod(String name, String methodName) throws FileNotFoundException { |
| 239 | + for (FactoryMethod method : readerForTestClass(name)) { |
| 240 | + if (method.getName().equals(methodName)) { |
| 241 | + return method; |
| 242 | + } |
| 243 | + } |
| 244 | + return null; |
| 245 | + } |
| 246 | + |
87 | 247 | private static FactoryMethod wrapUsingQDoxedSource(FactoryMethod originalMethod, String className, String input) { |
88 | 248 | List<FactoryMethod> originalMethods = new ArrayList<FactoryMethod>(); |
89 | 249 | originalMethods.add(originalMethod); |
90 | 250 |
|
91 | 251 | QDox qdox = new QDox(); |
92 | 252 | qdox.addSource(new StringReader(input)); |
93 | 253 |
|
94 | | - QDoxFactoryReader qDoxFactoryReader = new QDoxFactoryReader( |
95 | | - originalMethods, qdox, className); |
| 254 | + QDoxFactoryReader qDoxFactoryReader = new QDoxFactoryReader(qdox, className); |
96 | 255 | return getFirstFactoryMethod(qDoxFactoryReader); |
97 | 256 | } |
98 | 257 |
|
|
0 commit comments