~ubuntu-branches/ubuntu/saucy/libhamcrest1.2-java/saucy

« back to all changes in this revision

Viewing changes to hamcrest-unit-test/src/main/java/org/hamcrest/generator/ReflectiveFactoryReaderTest.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-12-02 17:55:55 UTC
  • Revision ID: package-import@ubuntu.com-20111202175555-xuj86jbpi8mehr1o
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hamcrest.generator;
 
2
 
 
3
import junit.framework.TestCase;
 
4
import org.hamcrest.BaseMatcher;
 
5
import org.hamcrest.Factory;
 
6
import org.hamcrest.Matcher;
 
7
 
 
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;
 
13
import java.util.Map;
 
14
import java.util.Set;
 
15
 
 
16
@SuppressWarnings("unused")
 
17
public class ReflectiveFactoryReaderTest extends TestCase {
 
18
 
 
19
    public static class SimpleSetOfMatchers {
 
20
 
 
21
        @Factory
 
22
        public static Matcher<String> firstMethod() {
 
23
            return null;
 
24
        }
 
25
 
 
26
        @Factory
 
27
        public static Matcher<String> secondMethod() {
 
28
            return null;
 
29
        }
 
30
 
 
31
    }
 
32
 
 
33
    public void testIteratesOverFactoryMethods() {
 
34
        Iterable<FactoryMethod> reader = new ReflectiveFactoryReader(SimpleSetOfMatchers.class);
 
35
        Iterator<FactoryMethod> methods = reader.iterator();
 
36
 
 
37
        assertTrue("Expected first method", methods.hasNext());
 
38
        FactoryMethod firstMethod = methods.next();
 
39
        assertEquals("firstMethod", firstMethod.getName());
 
40
        assertEquals(SimpleSetOfMatchers.class.getName(), firstMethod.getMatcherClass());
 
41
 
 
42
        assertTrue("Expected second method", methods.hasNext());
 
43
        FactoryMethod secondMethod = methods.next();
 
44
        assertEquals("secondMethod", secondMethod.getName());
 
45
        assertEquals(SimpleSetOfMatchers.class.getName(), secondMethod.getMatcherClass());
 
46
 
 
47
        assertFalse("Expected no more methods", methods.hasNext());
 
48
    }
 
49
 
 
50
    public static class MatchersWithDodgySignatures {
 
51
 
 
52
        @Factory
 
53
        public Matcher<String> notStatic() {
 
54
            return null;
 
55
        }
 
56
 
 
57
        @Factory
 
58
        static Matcher<String> notPublic() {
 
59
            return null;
 
60
        }
 
61
 
 
62
        public static Matcher<String> noAnnotation() {
 
63
            return null;
 
64
        }
 
65
 
 
66
        @Factory
 
67
        public static Matcher<String> goodMethod() {
 
68
            return null;
 
69
        }
 
70
 
 
71
        @Factory
 
72
        public static Matcher<String> anotherGoodMethod() {
 
73
            return null;
 
74
        }
 
75
 
 
76
        @Factory
 
77
        public static String wrongReturnType() {
 
78
            return null;
 
79
        }
 
80
 
 
81
    }
 
82
 
 
83
    public void testOnlyReadsPublicStaticAnnotatedMethodsThatReturnMatcher() {
 
84
        Iterable<FactoryMethod> reader = new ReflectiveFactoryReader(MatchersWithDodgySignatures.class);
 
85
        Iterator<FactoryMethod> methods = reader.iterator();
 
86
 
 
87
        assertTrue("Expected first method", methods.hasNext());
 
88
        assertEquals("goodMethod", methods.next().getName());
 
89
 
 
90
        assertTrue("Expected second method", methods.hasNext());
 
91
        assertEquals("anotherGoodMethod", methods.next().getName());
 
92
 
 
93
        assertFalse("Expected no more methods", methods.hasNext());
 
94
    }
 
95
 
 
96
    public static class GenerifiedMatchers {
 
97
 
 
98
        @Factory
 
99
        public static Matcher<Comparator<String>> generifiedType() {
 
100
            return null;
 
101
        }
 
102
 
 
103
        @SuppressWarnings("unchecked")
 
104
        @Factory
 
105
        public static Matcher noGenerifiedType() {
 
106
            return null;
 
107
        }
 
108
 
 
109
        @Factory
 
110
        public static Matcher<Map<? extends Set<Long>, Factory>> crazyType() {
 
111
            return null;
 
112
        }
 
113
 
 
114
    }
 
115
 
 
116
    public void testReadsFullyQualifiedGenericType() {
 
117
        FactoryMethod method = readMethod(GenerifiedMatchers.class, "generifiedType");
 
118
        assertEquals("java.util.Comparator<java.lang.String>", method.getGenerifiedType());
 
119
    }
 
120
 
 
121
    public void testReadsNullGenerifiedTypeIfNotPresent() {
 
122
        FactoryMethod method = readMethod(GenerifiedMatchers.class, "noGenerifiedType");
 
123
        assertNull(method.getGenerifiedType());
 
124
    }
 
125
 
 
126
    public void testReadsGenericsInGenericType() {
 
127
        FactoryMethod method = readMethod(GenerifiedMatchers.class, "crazyType");
 
128
        assertEquals(
 
129
                "java.util.Map<? extends java.util.Set<java.lang.Long>, org.hamcrest.Factory>",
 
130
                method.getGenerifiedType());
 
131
    }
 
132
 
 
133
    public static class ParamterizedMatchers {
 
134
 
 
135
        @Factory
 
136
        public static Matcher<String> withParam(String someString, int[] numbers, Collection<Object> things) {
 
137
            return null;
 
138
        }
 
139
 
 
140
        @Factory
 
141
        public static Matcher<String> withArray(String[] array) {
 
142
            return null;
 
143
        }
 
144
 
 
145
        @Factory
 
146
        public static Matcher<String> withVarArgs(String... things) {
 
147
            return null;
 
148
        }
 
149
 
 
150
        @Factory
 
151
        public static Matcher<String> withGenerifiedParam(Collection<? extends Comparable<String>> things, Set<String[]>[] x) {
 
152
            return null;
 
153
        }
 
154
 
 
155
    }
 
156
 
 
157
    public void testReadsParameterTypes() {
 
158
        FactoryMethod method = readMethod(ParamterizedMatchers.class, "withParam");
 
159
        List<FactoryMethod.Parameter> params = method.getParameters();
 
160
        assertEquals(3, params.size());
 
161
 
 
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());
 
165
    }
 
166
 
 
167
    public void testReadsArrayAndVarArgParameterTypes() {
 
168
        FactoryMethod arrayMethod = readMethod(ParamterizedMatchers.class, "withArray");
 
169
        assertEquals("java.lang.String[]", arrayMethod.getParameters().get(0).getType());
 
170
 
 
171
        FactoryMethod varArgsMethod = readMethod(ParamterizedMatchers.class, "withVarArgs");
 
172
        assertEquals("java.lang.String...", varArgsMethod.getParameters().get(0).getType());
 
173
    }
 
174
 
 
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());
 
181
    }
 
182
 
 
183
    public void testCannotReadParameterNamesSoMakesThemUpInstead() {
 
184
        FactoryMethod method = readMethod(ParamterizedMatchers.class, "withParam");
 
185
        List<FactoryMethod.Parameter> params = method.getParameters();
 
186
 
 
187
        assertEquals("param1", params.get(0).getName());
 
188
        assertEquals("param2", params.get(1).getName());
 
189
        assertEquals("param3", params.get(2).getName());
 
190
    }
 
191
 
 
192
    public static class ExceptionalMatchers {
 
193
 
 
194
        @Factory
 
195
        public static Matcher<String> withExceptions() throws Error, IOException, RuntimeException {
 
196
            return null;
 
197
        }
 
198
 
 
199
    }
 
200
 
 
201
    public void testReadsExceptions() {
 
202
        FactoryMethod method = readMethod(ExceptionalMatchers.class, "withExceptions");
 
203
        List<String> exceptions = method.getExceptions();
 
204
        assertEquals(3, exceptions.size());
 
205
 
 
206
        assertEquals("java.lang.Error", exceptions.get(0));
 
207
        assertEquals("java.io.IOException", exceptions.get(1));
 
208
        assertEquals("java.lang.RuntimeException", exceptions.get(2));
 
209
    }
 
210
 
 
211
    public static class WithJavaDoc {
 
212
 
 
213
        /**
 
214
         * Look at me!
 
215
         *
 
216
         * @return something
 
217
         */
 
218
        @Factory
 
219
        public static Matcher<String> documented() {
 
220
            return null;
 
221
        }
 
222
 
 
223
    }
 
224
 
 
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());
 
229
    }
 
230
 
 
231
    public static class G {
 
232
 
 
233
        @Factory
 
234
        public static <T, V extends List<String> & Comparable<String>> Matcher<Map<T, V[]>> x(Set<T> t, V v) {
 
235
            return null;
 
236
        }
 
237
 
 
238
    }
 
239
 
 
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());
 
248
    }
 
249
 
 
250
    public static class SubclassOfMatcher {
 
251
        @Factory
 
252
        public static BaseMatcher<?> subclassMethod() {
 
253
            return null;
 
254
        }
 
255
    }
 
256
 
 
257
    public void testCatchesSubclasses() {
 
258
        assertNotNull(readMethod(SubclassOfMatcher.class, "subclassMethod"));
 
259
    }
 
260
 
 
261
    private FactoryMethod readMethod(Class<?> cls, String methodName) {
 
262
        for (FactoryMethod method : new ReflectiveFactoryReader(cls)) {
 
263
            if (method.getName().equals(methodName)) {
 
264
                return method;
 
265
            }
 
266
        }
 
267
        return null;
 
268
    }
 
269
 
 
270
}