Skip to content

Commit d1ce178

Browse files
author
smgfreeman
committed
Generator for CoreMatchers now returns type of Matcher for sugar methods, so that and() and or() can support method chaining.
1 parent 9aa0c3b commit d1ce178

8 files changed

Lines changed: 51 additions & 38 deletions

File tree

‎hamcrest-generator/src/main/java/org/hamcrest/generator/FactoryMethod.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ public class FactoryMethod {
1818

1919
private final String matcherClass;
2020
private final String factoryMethod;
21+
private final String returnType;
2122

2223
private String generifiedType;
2324
private List<Parameter> parameters = new ArrayList<Parameter>();
2425
private List<String> exceptions = new ArrayList<String>();
2526
private List<String> genericTypeParameters = new ArrayList<String>();
2627
private String javaDoc;
2728

28-
public FactoryMethod(String matcherClass, String factoryMethod) {
29+
public FactoryMethod(String matcherClass, String factoryMethod, String returnType) {
2930
this.matcherClass = matcherClass;
3031
this.factoryMethod = factoryMethod;
32+
this.returnType = returnType;
3133
}
3234

3335
/**
@@ -37,6 +39,14 @@ public String getMatcherClass() {
3739
return matcherClass;
3840
}
3941

42+
/**
43+
* @return The fully qualified name of the type returned by the method. This should be a
44+
* subclass of org.hamcrest.Matcher.
45+
*/
46+
public String getReturnType() {
47+
return returnType;
48+
}
49+
4050
/**
4151
* Original name of factory method.
4252
*/

‎hamcrest-generator/src/main/java/org/hamcrest/generator/HamcrestFactoryWriter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void writeMethod(String generatedMethodName, FactoryMethod factoryMethodT
5454
indent();
5555
output.append("public static ");
5656
writeGenericTypeParameters(factoryMethodToDelegateTo);
57-
output.append("org.hamcrest.Matcher");
57+
output.append(factoryMethodToDelegateTo.getReturnType());
5858
if (factoryMethodToDelegateTo.getGenerifiedType() != null) {
5959
output.append('<').append(factoryMethodToDelegateTo.getGenerifiedType()).append('>');
6060
}

‎hamcrest-generator/src/main/java/org/hamcrest/generator/ReflectiveFactoryReader.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ && isPublic(javaMethod.getModifiers())
101101
private FactoryMethod buildFactoryMethod(Method javaMethod) {
102102
FactoryMethod result = new FactoryMethod(
103103
javaMethod.getDeclaringClass().getName(),
104-
javaMethod.getName());
104+
javaMethod.getName(),
105+
javaMethod.getReturnType().getName());
105106

106107
for (TypeVariable<Method> typeVariable : javaMethod.getTypeParameters()) {
107108
boolean hasBound = false;

‎hamcrest-unit-test/src/main/java/org/hamcrest/generator/EasyMock2FactoryWriterTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected void setUp() throws Exception {
1616
}
1717

1818
public void testWritesMethodDelegationMethodWrappedInAdapter() throws IOException {
19-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject");
19+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject", "unusedReturnType");
2020

2121
factoryWriter.writeMethod(method.getName(), method);
2222
assertEquals("" +
@@ -30,7 +30,7 @@ public void testWritesMethodDelegationMethodWrappedInAdapter() throws IOExceptio
3030
}
3131

3232
public void testWritesReturnType() throws IOException {
33-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString");
33+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString", "unusedReturnType");
3434
method.setGenerifiedType("String");
3535

3636
factoryWriter.writeMethod(method.getName(), method);
@@ -45,7 +45,7 @@ public void testWritesReturnType() throws IOException {
4545
}
4646

4747
public void testWritesAdvancedGenerifiedMatcherType() throws IOException {
48-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing");
48+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing", "unusedReturnType");
4949
method.setGenerifiedType("java.util.Map<com.Foo<Cheese>,?>");
5050

5151
factoryWriter.writeMethod(method.getName(), method);
@@ -60,7 +60,7 @@ public void testWritesAdvancedGenerifiedMatcherType() throws IOException {
6060
}
6161

6262
public void testWritesParameters() throws IOException {
63-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between");
63+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between", "unusedReturnType");
6464
method.addParameter("int[]", "lower");
6565
method.addParameter("com.blah.Cheesable<Long>...", "upper");
6666

@@ -76,7 +76,7 @@ public void testWritesParameters() throws IOException {
7676
}
7777

7878
public void testWritesExceptions() throws IOException {
79-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky");
79+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky", "unusedReturnType");
8080
method.addException("java.io.IOException");
8181
method.addException("com.foo.CheeselessException");
8282

‎hamcrest-unit-test/src/main/java/org/hamcrest/generator/HamcrestFactoryWriterTest.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
import junit.framework.TestCase;
77

88
public class HamcrestFactoryWriterTest extends TestCase {
9-
109
private FactoryWriter factoryWriter;
1110
private StringWriter output = new StringWriter();
1211

12+
@Override
1313
protected void setUp() throws Exception {
1414
super.setUp();
1515
factoryWriter = new HamcrestFactoryWriter(null, null, output);
1616
}
1717

1818
public void testWritesMethodDelegationMethod() throws IOException {
19-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject");
19+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject", "matcher.ReturnType");
2020

2121
factoryWriter.writeMethod(method.getName(), method);
2222
assertEquals("" +
23-
" public static org.hamcrest.Matcher anyObject() {\n" +
23+
" public static matcher.ReturnType anyObject() {\n" +
2424
" return com.example.MyMatcher.anyObject();\n" +
2525
" }\n" +
2626
"\n",
@@ -30,8 +30,8 @@ public void testWritesMethodDelegationMethod() throws IOException {
3030
public void testWritesCompleteJavaSource() throws IOException {
3131
factoryWriter = new HamcrestFactoryWriter("com.some.package", "SomeClass", output);
3232
factoryWriter.writeHeader();
33-
factoryWriter.writeMethod("method1", new FactoryMethod("com.example.MyMatcher", "method1"));
34-
factoryWriter.writeMethod("method2", new FactoryMethod("com.example.MyMatcher", "method2"));
33+
factoryWriter.writeMethod("method1", new FactoryMethod("com.example.MyMatcher", "method1", "matcher.ReturnType"));
34+
factoryWriter.writeMethod("method2", new FactoryMethod("com.example.MyMatcher", "method2", "matcher.ReturnType"));
3535
factoryWriter.writeFooter();
3636

3737
assertEquals("" +
@@ -40,11 +40,11 @@ public void testWritesCompleteJavaSource() throws IOException {
4040
"\n" +
4141
"public class SomeClass {\n" +
4242
"\n" +
43-
" public static org.hamcrest.Matcher method1() {\n" +
43+
" public static matcher.ReturnType method1() {\n" +
4444
" return com.example.MyMatcher.method1();\n" +
4545
" }\n" +
4646
"\n" +
47-
" public static org.hamcrest.Matcher method2() {\n" +
47+
" public static matcher.ReturnType method2() {\n" +
4848
" return com.example.MyMatcher.method2();\n" +
4949
" }\n" +
5050
"\n" +
@@ -53,77 +53,77 @@ public void testWritesCompleteJavaSource() throws IOException {
5353
}
5454

5555
public void testWritesGenerifiedMatcherType() throws IOException {
56-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString");
56+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString", "matcher.ReturnType");
5757
method.setGenerifiedType("String");
5858

5959
factoryWriter.writeMethod(method.getName(), method);
6060
assertEquals("" +
61-
" public static org.hamcrest.Matcher<String> anyString() {\n" +
61+
" public static matcher.ReturnType<String> anyString() {\n" +
6262
" return com.example.MyMatcher.anyString();\n" +
6363
" }\n" +
6464
"\n",
6565
output.toString());
6666
}
6767

6868
public void testWritesAdvancedGenerifiedMatcherType() throws IOException {
69-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing");
69+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing", "matcher.ReturnType");
7070
method.setGenerifiedType("java.util.Map<com.Foo<Cheese>,?>");
7171

7272
factoryWriter.writeMethod(method.getName(), method);
7373
assertEquals("" +
74-
" public static org.hamcrest.Matcher<java.util.Map<com.Foo<Cheese>,?>> weirdThing() {\n" +
74+
" public static matcher.ReturnType<java.util.Map<com.Foo<Cheese>,?>> weirdThing() {\n" +
7575
" return com.example.MyMatcher.weirdThing();\n" +
7676
" }\n" +
7777
"\n",
7878
output.toString());
7979
}
8080

8181
public void testWritesParameters() throws IOException {
82-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between");
82+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between", "matcher.ReturnType");
8383
method.addParameter("int[]", "lower");
8484
method.addParameter("com.blah.Cheesable<Long>...", "upper");
8585

8686
factoryWriter.writeMethod(method.getName(), method);
8787
assertEquals("" +
88-
" public static org.hamcrest.Matcher between(int[] lower, com.blah.Cheesable<Long>... upper) {\n" +
88+
" public static matcher.ReturnType between(int[] lower, com.blah.Cheesable<Long>... upper) {\n" +
8989
" return com.example.MyMatcher.between(lower, upper);\n" +
9090
" }\n" +
9191
"\n",
9292
output.toString());
9393
}
9494

9595
public void testWritesExceptions() throws IOException {
96-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky");
96+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky", "matcher.ReturnType");
9797
method.addException("java.io.IOException");
9898
method.addException("com.foo.CheeselessException");
9999

100100
factoryWriter.writeMethod(method.getName(), method);
101101
assertEquals("" +
102-
" public static org.hamcrest.Matcher tricky() throws java.io.IOException, com.foo.CheeselessException {\n" +
102+
" public static matcher.ReturnType tricky() throws java.io.IOException, com.foo.CheeselessException {\n" +
103103
" return com.example.MyMatcher.tricky();\n" +
104104
" }\n" +
105105
"\n",
106106
output.toString());
107107
}
108108

109109
public void testWritesGenericTypeParameters() throws IOException {
110-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky");
110+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky", "matcher.ReturnType");
111111
method.addGenericTypeParameter("T");
112112
method.addGenericTypeParameter("V extends String & Cheese");
113113
method.addParameter("T", "t");
114114
method.addParameter("List<V>", "v");
115115

116116
factoryWriter.writeMethod(method.getName(), method);
117117
assertEquals("" +
118-
" public static <T, V extends String & Cheese> org.hamcrest.Matcher tricky(T t, List<V> v) {\n" +
118+
" public static <T, V extends String & Cheese> matcher.ReturnType tricky(T t, List<V> v) {\n" +
119119
" return com.example.MyMatcher.tricky(t, v);\n" +
120120
" }\n" +
121121
"\n",
122122
output.toString());
123123
}
124124

125125
public void testWritesJavaDoc() throws IOException {
126-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "needsDoc");
126+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "needsDoc", "matcher.ReturnType");
127127
method.setJavaDoc("This is a complicated method.\nIt needs docs.\n\n@see MoreStuff");
128128

129129
factoryWriter.writeMethod(method.getName(), method);
@@ -134,19 +134,19 @@ public void testWritesJavaDoc() throws IOException {
134134
" * \n" +
135135
" * @see MoreStuff\n" +
136136
" */\n" +
137-
" public static org.hamcrest.Matcher needsDoc() {\n" +
137+
" public static matcher.ReturnType needsDoc() {\n" +
138138
" return com.example.MyMatcher.needsDoc();\n" +
139139
" }\n" +
140140
"\n",
141141
output.toString());
142142
}
143143

144144
public void testWritesMethodWithNameOverriden() throws IOException {
145-
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "eq");
145+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "eq", "matcher.ReturnType");
146146

147147
factoryWriter.writeMethod("anotherName", method);
148148
assertEquals("" +
149-
" public static org.hamcrest.Matcher anotherName() {\n" +
149+
" public static matcher.ReturnType anotherName() {\n" +
150150
" return com.example.MyMatcher.eq();\n" +
151151
" }\n" +
152152
"\n",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class QDoxFactoryReaderTest extends TestCase {
1111

1212
public void testExtractsOriginalParameterNamesFromSource() {
13-
FactoryMethod method = new FactoryMethod("org.SomeClass", "someMethod");
13+
FactoryMethod method = new FactoryMethod("org.SomeClass", "someMethod", "unusedReturnType");
1414
method.addParameter("java.lang.String", "badParamName");
1515

1616
String input = "" +
@@ -25,7 +25,7 @@ public void testExtractsOriginalParameterNamesFromSource() {
2525
}
2626

2727
public void testExtractsOriginalJavaDocFromSource() {
28-
FactoryMethod method = new FactoryMethod("org.SomeClass", "someMethod");
28+
FactoryMethod method = new FactoryMethod("org.SomeClass", "someMethod", "unusedReturnType");
2929

3030
String input = "" +
3131
"package org;\n" +

‎hamcrest-unit-test/src/main/java/org/hamcrest/generator/QuickReferenceWriterTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class QuickReferenceWriterTest extends TestCase {
1313
private PrintStream expected;
1414
private QuickReferenceWriter writer;
1515

16+
@Override
1617
protected void setUp() throws Exception {
1718
super.setUp();
1819
actualBuffer = new ByteArrayOutputStream();
@@ -23,13 +24,13 @@ protected void setUp() throws Exception {
2324
}
2425

2526
public void testWritesSimplifiedSummaryOfMatchers() throws IOException {
26-
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod");
27+
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod", "unusedReturnType");
2728
namedMethod.addParameter("Cheese", "a");
2829
namedMethod.addParameter("int", "b");
2930
namedMethod.setGenerifiedType("String");
3031
writer.writeMethod("namedMethod", namedMethod);
3132

32-
FactoryMethod anotherMethod = new FactoryMethod("SomeClass", "anotherMethod");
33+
FactoryMethod anotherMethod = new FactoryMethod("SomeClass", "anotherMethod", "unusedReturnType");
3334
anotherMethod.setGenerifiedType("int");
3435
writer.writeMethod("anotherMethod", anotherMethod);
3536

@@ -39,7 +40,7 @@ public void testWritesSimplifiedSummaryOfMatchers() throws IOException {
3940
}
4041

4142
public void testRemovesPackageNames() throws IOException {
42-
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod");
43+
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod", "unusedReturnType");
4344
namedMethod.addParameter("com.blah.Foo", "a");
4445
namedMethod.addParameter("com.foo.Cheese<x.y.Zoo>", "b");
4546
namedMethod.setGenerifiedType("java.lang.Cheese");

‎hamcrest-unit-test/src/main/java/org/hamcrest/generator/config/XmlConfiguratorTest.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.hamcrest.Factory;
1313
import org.hamcrest.Matcher;
14+
import org.hamcrest.core.CombinableMatcher;
1415
import org.hamcrest.generator.FactoryMethod;
1516
import org.hamcrest.generator.FactoryWriter;
1617
import org.hamcrest.generator.SugarConfiguration;
@@ -36,11 +37,11 @@ public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
3637
"</matchers>"));
3738

3839
assertThat(sugarConfiguration.factoryMethods(),
39-
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher1")));
40+
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher1", "org.hamcrest.Matcher")));
4041
assertThat(sugarConfiguration.factoryMethods(),
41-
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher2")));
42+
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher2", "org.hamcrest.Matcher")));
4243
assertThat(sugarConfiguration.factoryMethods(),
43-
hasItem(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3")));
44+
hasItem(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3", "org.hamcrest.CombinableMatcher")));
4445
}
4546

4647
private InputSource createXml(String xml) {
@@ -65,7 +66,7 @@ public static Matcher matcher2() {
6566
@SuppressWarnings("unchecked")
6667
public static class AnotherMatcher {
6768
@Factory
68-
public static Matcher matcher3() {
69+
public static CombinableMatcher matcher3() {
6970
return null;
7071
}
7172
}

0 commit comments

Comments
 (0)