Skip to content

Commit 8dc3d8c

Browse files
author
joeretro
committed
More HamCrest -> Hamcrest
1 parent 428e3f5 commit 8dc3d8c

File tree

8 files changed

+312
-7
lines changed

8 files changed

+312
-7
lines changed

BUILDING.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
**********************
2-
********************* Building HamCrest *********************
2+
********************* Building Hamcrest *********************
33
**********************
44

55
--[ Build requirements ]---000-------------------------------

LICENSE.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(BSD Style License)
22

3-
Copyright (c) 2000-2006, HamCrest.org
3+
Copyright (c) 2000-2006, Hamcrest.org
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without
@@ -11,7 +11,7 @@ conditions and the following disclaimer. Redistributions in binary form must rep
1111
the above copyright notice, this list of conditions and the following disclaimer in
1212
the documentation and/or other materials provided with the distribution.
1313

14-
Neither the name of HamCrest nor the names of its contributors may be used to endorse
14+
Neither the name of Hamcrest nor the names of its contributors may be used to endorse
1515
or promote products derived from this software without specific prior written
1616
permission.
1717

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
************
3-
********************** HamCrest **********************
3+
********************** Hamcrest **********************
44
************
55

66
"TODO: catchy phrase"

src/examples/org/hamcrest/examples/junit3/ExampleWithAssertThat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import static org.hamcrest.text.StringContains.containsString;
88

99
/**
10-
* Demonstrates how HamCrest matchers can be used with assertThat()
10+
* Demonstrates how Hamcrest matchers can be used with assertThat()
1111
* using JUnit 3.8.x.
1212
*
1313
* @author Joe Walnes

src/examples/org/hamcrest/examples/testng/ExampleWithAssertThat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.testng.annotations.Test;
88

99
/**
10-
* Demonstrates how HamCrest matchers can be used with assertThat()
10+
* Demonstrates how Hamcrest matchers can be used with assertThat()
1111
* using TestNG.
1212
*
1313
* @author Joe Walnes

src/examples/org/hamcrest/examples/testng/ExampleWithEasyMock2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.testng.annotations.Configuration;
77

88
/**
9-
* Demonstrates how HamCrest matchers can be used from EasyMock with
9+
* Demonstrates how Hamcrest matchers can be used from EasyMock with
1010
* TestNG.
1111
*
1212
* @author Joe Walnes
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.hamcrest.generator;
2+
3+
import java.io.PrintWriter;
4+
import java.io.Writer;
5+
import java.io.IOException;
6+
7+
/**
8+
* {@link FactoryWriter} that outputs Java code which simply delegates all
9+
* factory methods to factory methods elsewhere. This is useful for grouping
10+
* lots of matcher classes into one class, so you know where to look to find
11+
* matchers.
12+
*
13+
* @author Joe Walnes
14+
* @see FactoryWriter
15+
*/
16+
public class EasyMock2FactoryWriter implements FactoryWriter {
17+
18+
private final PrintWriter output;
19+
private final String javaPackageName;
20+
private final String javaClassName;
21+
22+
private String indentationString = " ";
23+
private String newLine = "\n";
24+
25+
private int indentation = 1;
26+
27+
public EasyMock2FactoryWriter(Writer output, String javaPackageName, String javaClassName) {
28+
this.javaPackageName = javaPackageName;
29+
this.javaClassName = javaClassName;
30+
this.output = new PrintWriter(output);
31+
}
32+
33+
public void writeHeader() throws IOException {
34+
output.append("package ").append(javaPackageName).append(';').append(newLine).append(newLine);
35+
output.append("public class ").append(javaClassName).append(" {").append(newLine).append(newLine);
36+
}
37+
38+
public void writeFooter() throws IOException {
39+
output.append('}').append(newLine);
40+
}
41+
42+
public void close() throws IOException {
43+
output.close();
44+
}
45+
46+
public void flush() throws IOException {
47+
output.flush();
48+
}
49+
50+
public void writeMethod(String generatedMethodName, FactoryMethod factoryMethodToDelegateTo)
51+
throws IOException {
52+
writeJavaDoc(factoryMethodToDelegateTo);
53+
indent();
54+
output.append("public static ");
55+
//writeGenericTypeParameters(factoryMethodToDelegateTo);
56+
String returnType = factoryMethodToDelegateTo.getGenerifiedType();
57+
if (returnType == null) {
58+
returnType = "java.lang.Object";
59+
}
60+
output.append(returnType);
61+
output.append(' ').append(generatedMethodName);
62+
writeParameters(factoryMethodToDelegateTo);
63+
writeExceptions(factoryMethodToDelegateTo);
64+
output.append(" {").append(newLine);
65+
indentation++;
66+
writeMethodBody(factoryMethodToDelegateTo);
67+
indentation--;
68+
indent();
69+
output.append('}').append(newLine).append(newLine);
70+
}
71+
72+
private void writeGenericTypeParameters(FactoryMethod factoryMethod) {
73+
if (!factoryMethod.getGenericTypeParameters().isEmpty()) {
74+
output.append('<');
75+
boolean seenFirst = false;
76+
for (String type : factoryMethod.getGenericTypeParameters()) {
77+
if (seenFirst) {
78+
output.append(", ");
79+
} else {
80+
seenFirst = true;
81+
}
82+
output.append(type);
83+
}
84+
output.append("> ");
85+
}
86+
}
87+
88+
private void writeMethodBody(FactoryMethod factoryMethod) {
89+
indent();
90+
output.append("org.hamcrest.integration.EasyMockAdapter.adapt(").append(newLine);
91+
indentation++;
92+
indent();
93+
output.append(factoryMethod.getMatcherClass());
94+
output.append('.').append(factoryMethod.getName());
95+
output.append('(');
96+
boolean seenFirst = false;
97+
for (FactoryMethod.Parameter parameter : factoryMethod.getParameters()) {
98+
if (seenFirst) {
99+
output.append(", ");
100+
} else {
101+
seenFirst = true;
102+
}
103+
output.append(parameter.getName());
104+
}
105+
output.append("));").append(newLine);
106+
indentation--;
107+
indent();
108+
output.append("return null;").append(newLine);
109+
}
110+
111+
private void writeExceptions(FactoryMethod factoryMethod) {
112+
boolean seenFirst = false;
113+
for (String exception : factoryMethod.getExceptions()) {
114+
if (seenFirst) {
115+
output.append(", ");
116+
} else {
117+
output.append(" throws ");
118+
seenFirst = true;
119+
}
120+
output.append(exception);
121+
}
122+
}
123+
124+
private void writeParameters(FactoryMethod factoryMethod) {
125+
output.append('(');
126+
boolean seenFirst = false;
127+
for (FactoryMethod.Parameter parameter : factoryMethod.getParameters()) {
128+
if (seenFirst) {
129+
output.append(", ");
130+
} else {
131+
seenFirst = true;
132+
}
133+
output.append(parameter.getType()).append(' ').append(parameter.getName());
134+
}
135+
output.append(')');
136+
}
137+
138+
private void writeJavaDoc(FactoryMethod factoryMethod) {
139+
if (factoryMethod.getJavaDoc() != null) {
140+
String[] lines = factoryMethod.getJavaDoc().split("\n");
141+
if (lines.length > 0) {
142+
indent();
143+
output.append("/**").append(newLine);
144+
for (String line : lines) {
145+
indent();
146+
output.append(" * ").append(line).append(newLine);
147+
}
148+
indent();
149+
output.append(" */").append(newLine);
150+
}
151+
}
152+
}
153+
154+
private void indent() {
155+
for (int i = 0; i < indentation; i++) {
156+
output.append(indentationString);
157+
}
158+
}
159+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.hamcrest.generator;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.io.StringWriter;
6+
import java.io.IOException;
7+
8+
public class EasyMock2FactoryWriterTest extends TestCase {
9+
10+
private FactoryWriter factoryWriter;
11+
private StringWriter output = new StringWriter();
12+
13+
protected void setUp() throws Exception {
14+
super.setUp();
15+
factoryWriter = new EasyMock2FactoryWriter(output, "com.blah", "EasyMatchers");
16+
}
17+
18+
public void testWritesMethodDelegationMethodWrappedInAdapter() throws IOException {
19+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject");
20+
21+
factoryWriter.writeMethod(method.getName(), method);
22+
assertEquals("" +
23+
" public static java.lang.Object anyObject() {\n" +
24+
" org.hamcrest.integration.EasyMockAdapter.adapt(\n" +
25+
" com.example.MyMatcher.anyObject());\n" +
26+
" return null;\n" +
27+
" }\n" +
28+
"\n",
29+
output.toString());
30+
}
31+
32+
public void testWritesReturnType() throws IOException {
33+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString");
34+
method.setGenerifiedType("String");
35+
36+
factoryWriter.writeMethod(method.getName(), method);
37+
assertEquals("" +
38+
" public static String anyString() {\n" +
39+
" org.hamcrest.integration.EasyMockAdapter.adapt(\n" +
40+
" com.example.MyMatcher.anyString());\n" +
41+
" return null;\n" +
42+
" }\n" +
43+
"\n",
44+
output.toString());
45+
}
46+
47+
public void testWritesAdvancedGenerifiedMatcherType() throws IOException {
48+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing");
49+
method.setGenerifiedType("java.util.Map<com.Foo<Cheese>,?>");
50+
51+
factoryWriter.writeMethod(method.getName(), method);
52+
assertEquals("" +
53+
" public static java.util.Map<com.Foo<Cheese>,?> weirdThing() {\n" +
54+
" org.hamcrest.integration.EasyMockAdapter.adapt(\n" +
55+
" com.example.MyMatcher.weirdThing());\n" +
56+
" return null;\n" +
57+
" }\n" +
58+
"\n",
59+
output.toString());
60+
}
61+
62+
public void testWritesParameters() throws IOException {
63+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between");
64+
method.addParameter("int[]", "lower");
65+
method.addParameter("com.blah.Cheesable<Long>...", "upper");
66+
67+
factoryWriter.writeMethod(method.getName(), method);
68+
assertEquals("" +
69+
" public static java.lang.Object between(int[] lower, com.blah.Cheesable<Long>... upper) {\n" +
70+
" org.hamcrest.integration.EasyMockAdapter.adapt(\n" +
71+
" com.example.MyMatcher.between(lower, upper));\n" +
72+
" return null;\n" +
73+
" }\n" +
74+
"\n",
75+
output.toString());
76+
}
77+
78+
public void testWritesExceptions() throws IOException {
79+
FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky");
80+
method.addException("java.io.IOException");
81+
method.addException("com.foo.CheeselessException");
82+
83+
factoryWriter.writeMethod(method.getName(), method);
84+
assertEquals("" +
85+
" public static java.lang.Object tricky() throws java.io.IOException, com.foo.CheeselessException {\n" +
86+
" org.hamcrest.integration.EasyMockAdapter.adapt(\n" +
87+
" com.example.MyMatcher.tricky());\n" +
88+
" return null;\n" +
89+
" }\n" +
90+
"\n",
91+
output.toString());
92+
}
93+
//
94+
// public void testWritesGenericTypeParameters() throws IOException {
95+
// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky");
96+
// method.addGenericTypeParameter("T");
97+
// method.addGenericTypeParameter("V extends String & Cheese");
98+
// method.addParameter("T", "t");
99+
// method.addParameter("List<V>", "v");
100+
//
101+
// factoryWriter.writeMethod(method.getName(), method);
102+
// assertEquals("" +
103+
// " public static <T, V extends String & Cheese> org.hamcrest.Matcher tricky(T t, List<V> v) {\n" +
104+
// " return com.example.MyMatcher.tricky(t, v);\n" +
105+
// " }\n" +
106+
// "\n",
107+
// output.toString());
108+
// }
109+
//
110+
// public void testWritesJavaDoc() throws IOException {
111+
// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "needsDoc");
112+
// method.setJavaDoc("This is a complicated method.\nIt needs docs.\n\n@see MoreStuff");
113+
//
114+
// factoryWriter.writeMethod(method.getName(), method);
115+
// assertEquals("" +
116+
// " /**\n" +
117+
// " * This is a complicated method.\n" +
118+
// " * It needs docs.\n" +
119+
// " * \n" +
120+
// " * @see MoreStuff\n" +
121+
// " */\n" +
122+
// " public static org.hamcrest.Matcher needsDoc() {\n" +
123+
// " return com.example.MyMatcher.needsDoc();\n" +
124+
// " }\n" +
125+
// "\n",
126+
// output.toString());
127+
// }
128+
//
129+
// public void testWritesMethodWithNameOverriden() throws IOException {
130+
// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "eq");
131+
//
132+
// factoryWriter.writeMethod("anotherName", method);
133+
// assertEquals("" +
134+
// " public static org.hamcrest.Matcher anotherName() {\n" +
135+
// " return com.example.MyMatcher.eq();\n" +
136+
// " }\n" +
137+
// "\n",
138+
// output.toString());
139+
// }
140+
141+
// primitives
142+
// arrays?
143+
// and/or/not/etc
144+
145+
146+
}

0 commit comments

Comments
 (0)