Skip to content

Commit 6f1f817

Browse files
committed
Copy tests over from ReflectiveFactoryReaderTest to QDoxFactoryReaderTest.
To prepare for consolidating into a single reader, copy across all tests.
1 parent c348e30 commit 6f1f817

10 files changed

Lines changed: 343 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test;
2+
3+
import java.io.IOException;
4+
5+
import org.hamcrest.Factory;
6+
import org.hamcrest.Matcher;
7+
8+
public static class ExceptionalMatchers {
9+
10+
@Factory
11+
public static Matcher<String> withExceptions() throws Error, IOException, RuntimeException {
12+
return null;
13+
}
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
import org.hamcrest.Factory;
8+
import org.hamcrest.Matcher;
9+
10+
public static class G {
11+
12+
@Factory
13+
public static <T, V extends List<String> & Comparable<String>> Matcher<Map<T, V[]>> x(Set<T> t, V v) {
14+
return null;
15+
}
16+
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test;
2+
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
import org.hamcrest.Factory;
8+
import org.hamcrest.Matcher;
9+
10+
public static class GenerifiedMatchers {
11+
12+
@Factory
13+
public static Matcher<Comparator<String>> generifiedType() {
14+
return null;
15+
}
16+
17+
@SuppressWarnings("rawtypes")
18+
@Factory
19+
public static Matcher noGenerifiedType() {
20+
return null;
21+
}
22+
23+
@Factory
24+
public static Matcher<Map<? extends Set<Long>, Factory>> crazyType() {
25+
return null;
26+
}
27+
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package test;
2+
3+
import org.hamcrest.Factory;
4+
import org.hamcrest.Matcher;
5+
6+
public static class MatchersWithDodgySignatures {
7+
8+
@Factory
9+
public Matcher<String> notStatic() {
10+
return null;
11+
}
12+
13+
@Factory
14+
static Matcher<String> notPublic() {
15+
return null;
16+
}
17+
18+
public static Matcher<String> noAnnotation() {
19+
return null;
20+
}
21+
22+
@Factory
23+
public static Matcher<String> goodMethod() {
24+
return null;
25+
}
26+
27+
@Factory
28+
public static String anotherGoodMethod() {
29+
return null;
30+
}
31+
32+
@Factory
33+
public static void wrongReturnType() {
34+
}
35+
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test;
2+
3+
import java.util.Collection;
4+
import java.util.Set;
5+
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
8+
9+
public static class ParameterizedMatchers {
10+
11+
@Factory
12+
public static Matcher<String> withParam(String someString, int[] numbers, Collection<Object> things) {
13+
return null;
14+
}
15+
16+
@Factory
17+
public static Matcher<String> withArray(String[] array) {
18+
return null;
19+
}
20+
21+
@Factory
22+
public static Matcher<String> withVarArgs(String... things) {
23+
return null;
24+
}
25+
26+
@Factory
27+
public static Matcher<String> withGenerifiedParam(Collection<? extends Comparable<String>> things, Set<String[]>[] x) {
28+
return null;
29+
}
30+
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test;
2+
3+
import org.hamcrest.Factory;
4+
import org.hamcrest.Matcher;
5+
6+
public static class SimpleSetOfMatchers {
7+
8+
@Factory
9+
public static Matcher<String> firstMethod() {
10+
return null;
11+
}
12+
13+
@Factory
14+
public static Matcher<String> secondMethod() {
15+
return null;
16+
}
17+
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
6+
public static final class SubMatcher<T> implements Matcher<T> {
7+
@Override public void describeTo(Description description) { }
8+
@Override public boolean matches(Object item) { return false; }
9+
@Override public void describeMismatch(Object item, Description mismatchDescription) { }
10+
@Override @Deprecated public void _dont_implement_Matcher___instead_extend_BaseMatcher_() { }
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package test;
2+
3+
import org.hamcrest.Factory;
4+
import org.hamcrest.generator.SubMatcher;
5+
6+
public static class SubclassOfMatcher {
7+
@Factory public static SubMatcher<?> subclassMethod() { return null; }
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test;
2+
3+
import org.hamcrest.Factory;
4+
import org.hamcrest.Matcher;
5+
6+
public static class WithJavaDoc {
7+
8+
/**
9+
* Look at me!
10+
*
11+
* @return something
12+
*/
13+
@Factory
14+
public static Matcher<String> documented() {
15+
return null;
16+
}
17+
18+
}

‎hamcrest-generator/src/test/java/org/hamcrest/generator/QDoxFactoryReaderTest.java‎

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package org.hamcrest.generator;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
46
import static org.junit.Assert.assertTrue;
57

8+
import java.io.FileNotFoundException;
9+
import java.io.FileReader;
610
import java.io.StringReader;
711
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.Comparator;
814
import java.util.Iterator;
915
import java.util.List;
1016

@@ -84,15 +90,168 @@ public final class QDoxFactoryReaderTest {
8490
factoryMethod.getJavaDoc());
8591
}
8692

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+
87247
private static FactoryMethod wrapUsingQDoxedSource(FactoryMethod originalMethod, String className, String input) {
88248
List<FactoryMethod> originalMethods = new ArrayList<FactoryMethod>();
89249
originalMethods.add(originalMethod);
90250

91251
QDox qdox = new QDox();
92252
qdox.addSource(new StringReader(input));
93253

94-
QDoxFactoryReader qDoxFactoryReader = new QDoxFactoryReader(
95-
originalMethods, qdox, className);
254+
QDoxFactoryReader qDoxFactoryReader = new QDoxFactoryReader(qdox, className);
96255
return getFirstFactoryMethod(qDoxFactoryReader);
97256
}
98257

0 commit comments

Comments
 (0)