1
package org.hamcrest.generator;
3
import junit.framework.TestCase;
4
import org.hamcrest.BaseMatcher;
5
import org.hamcrest.Factory;
6
import org.hamcrest.Matcher;
8
import java.io.IOException;
9
import java.util.Collection;
10
import java.util.Comparator;
11
import java.util.Iterator;
12
import java.util.List;
16
@SuppressWarnings("unused")
17
public class ReflectiveFactoryReaderTest extends TestCase {
19
public static class SimpleSetOfMatchers {
22
public static Matcher<String> firstMethod() {
27
public static Matcher<String> secondMethod() {
33
public void testIteratesOverFactoryMethods() {
34
Iterable<FactoryMethod> reader = new ReflectiveFactoryReader(SimpleSetOfMatchers.class);
35
Iterator<FactoryMethod> methods = reader.iterator();
37
assertTrue("Expected first method", methods.hasNext());
38
FactoryMethod firstMethod = methods.next();
39
assertEquals("firstMethod", firstMethod.getName());
40
assertEquals(SimpleSetOfMatchers.class.getName(), firstMethod.getMatcherClass());
42
assertTrue("Expected second method", methods.hasNext());
43
FactoryMethod secondMethod = methods.next();
44
assertEquals("secondMethod", secondMethod.getName());
45
assertEquals(SimpleSetOfMatchers.class.getName(), secondMethod.getMatcherClass());
47
assertFalse("Expected no more methods", methods.hasNext());
50
public static class MatchersWithDodgySignatures {
53
public Matcher<String> notStatic() {
58
static Matcher<String> notPublic() {
62
public static Matcher<String> noAnnotation() {
67
public static Matcher<String> goodMethod() {
72
public static Matcher<String> anotherGoodMethod() {
77
public static String wrongReturnType() {
83
public void testOnlyReadsPublicStaticAnnotatedMethodsThatReturnMatcher() {
84
Iterable<FactoryMethod> reader = new ReflectiveFactoryReader(MatchersWithDodgySignatures.class);
85
Iterator<FactoryMethod> methods = reader.iterator();
87
assertTrue("Expected first method", methods.hasNext());
88
assertEquals("goodMethod", methods.next().getName());
90
assertTrue("Expected second method", methods.hasNext());
91
assertEquals("anotherGoodMethod", methods.next().getName());
93
assertFalse("Expected no more methods", methods.hasNext());
96
public static class GenerifiedMatchers {
99
public static Matcher<Comparator<String>> generifiedType() {
103
@SuppressWarnings("unchecked")
105
public static Matcher noGenerifiedType() {
110
public static Matcher<Map<? extends Set<Long>, Factory>> crazyType() {
116
public void testReadsFullyQualifiedGenericType() {
117
FactoryMethod method = readMethod(GenerifiedMatchers.class, "generifiedType");
118
assertEquals("java.util.Comparator<java.lang.String>", method.getGenerifiedType());
121
public void testReadsNullGenerifiedTypeIfNotPresent() {
122
FactoryMethod method = readMethod(GenerifiedMatchers.class, "noGenerifiedType");
123
assertNull(method.getGenerifiedType());
126
public void testReadsGenericsInGenericType() {
127
FactoryMethod method = readMethod(GenerifiedMatchers.class, "crazyType");
129
"java.util.Map<? extends java.util.Set<java.lang.Long>, org.hamcrest.Factory>",
130
method.getGenerifiedType());
133
public static class ParamterizedMatchers {
136
public static Matcher<String> withParam(String someString, int[] numbers, Collection<Object> things) {
141
public static Matcher<String> withArray(String[] array) {
146
public static Matcher<String> withVarArgs(String... things) {
151
public static Matcher<String> withGenerifiedParam(Collection<? extends Comparable<String>> things, Set<String[]>[] x) {
157
public void testReadsParameterTypes() {
158
FactoryMethod method = readMethod(ParamterizedMatchers.class, "withParam");
159
List<FactoryMethod.Parameter> params = method.getParameters();
160
assertEquals(3, params.size());
162
assertEquals("java.lang.String", params.get(0).getType());
163
assertEquals("int[]", params.get(1).getType());
164
assertEquals("java.util.Collection<java.lang.Object>", params.get(2).getType());
167
public void testReadsArrayAndVarArgParameterTypes() {
168
FactoryMethod arrayMethod = readMethod(ParamterizedMatchers.class, "withArray");
169
assertEquals("java.lang.String[]", arrayMethod.getParameters().get(0).getType());
171
FactoryMethod varArgsMethod = readMethod(ParamterizedMatchers.class, "withVarArgs");
172
assertEquals("java.lang.String...", varArgsMethod.getParameters().get(0).getType());
175
public void testReadsGenerifiedParameterTypes() {
176
FactoryMethod method = readMethod(ParamterizedMatchers.class, "withGenerifiedParam");
177
assertEquals("java.util.Collection<? extends java.lang.Comparable<java.lang.String>>",
178
method.getParameters().get(0).getType());
179
assertEquals("java.util.Set<java.lang.String[]>[]",
180
method.getParameters().get(1).getType());
183
public void testCannotReadParameterNamesSoMakesThemUpInstead() {
184
FactoryMethod method = readMethod(ParamterizedMatchers.class, "withParam");
185
List<FactoryMethod.Parameter> params = method.getParameters();
187
assertEquals("param1", params.get(0).getName());
188
assertEquals("param2", params.get(1).getName());
189
assertEquals("param3", params.get(2).getName());
192
public static class ExceptionalMatchers {
195
public static Matcher<String> withExceptions() throws Error, IOException, RuntimeException {
201
public void testReadsExceptions() {
202
FactoryMethod method = readMethod(ExceptionalMatchers.class, "withExceptions");
203
List<String> exceptions = method.getExceptions();
204
assertEquals(3, exceptions.size());
206
assertEquals("java.lang.Error", exceptions.get(0));
207
assertEquals("java.io.IOException", exceptions.get(1));
208
assertEquals("java.lang.RuntimeException", exceptions.get(2));
211
public static class WithJavaDoc {
219
public static Matcher<String> documented() {
225
public void testCannotReadJavaDoc() {
226
// JavaDoc information is not available through reflection alone.
227
FactoryMethod method = readMethod(WithJavaDoc.class, "documented");
228
assertEquals(null, method.getJavaDoc());
231
public static class G {
234
public static <T, V extends List<String> & Comparable<String>> Matcher<Map<T, V[]>> x(Set<T> t, V v) {
240
public void testReadsGenericTypeParameters() {
241
FactoryMethod method = readMethod(G.class, "x");
242
assertEquals("T", method.getGenericTypeParameters().get(0));
243
assertEquals("V extends java.util.List<java.lang.String> & java.lang.Comparable<java.lang.String>",
244
method.getGenericTypeParameters().get(1));
245
assertEquals("java.util.Map<T, V[]>", method.getGenerifiedType());
246
assertEquals("java.util.Set<T>", method.getParameters().get(0).getType());
247
assertEquals("V", method.getParameters().get(1).getType());
250
public static class SubclassOfMatcher {
252
public static BaseMatcher<?> subclassMethod() {
257
public void testCatchesSubclasses() {
258
assertNotNull(readMethod(SubclassOfMatcher.class, "subclassMethod"));
261
private FactoryMethod readMethod(Class<?> cls, String methodName) {
262
for (FactoryMethod method : new ReflectiveFactoryReader(cls)) {
263
if (method.getName().equals(methodName)) {