Skip to content

Commit 9bc78cb

Browse files
committed
refactor generator unit tests so that they dont depend on hamcrest-core classes (only api interfaces
1 parent 08f500d commit 9bc78cb

2 files changed

Lines changed: 30 additions & 36 deletions

File tree

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

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

33
import junit.framework.TestCase;
4-
import org.hamcrest.BaseMatcher;
4+
5+
import org.hamcrest.Description;
56
import org.hamcrest.Factory;
67
import org.hamcrest.Matcher;
78

@@ -247,11 +248,15 @@ public void testReadsGenericTypeParameters() {
247248
assertEquals("V", method.getParameters().get(1).getType());
248249
}
249250

251+
public static final class SubMatcher<T> implements Matcher<T> {
252+
@Override public void describeTo(Description description) { }
253+
@Override public boolean matches(Object item) { return false; }
254+
@Override public void describeMismatch(Object item, Description mismatchDescription) { }
255+
@Override @Deprecated public void _dont_implement_Matcher___instead_extend_BaseMatcher_() { }
256+
}
257+
250258
public static class SubclassOfMatcher {
251-
@Factory
252-
public static BaseMatcher<?> subclassMethod() {
253-
return null;
254-
}
259+
@Factory public static SubMatcher<?> subclassMethod() { return null; }
255260
}
256261

257262
public void testCatchesSubclasses() {

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

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package org.hamcrest.generator.config;
22

3+
import java.io.StringReader;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
37
import junit.framework.TestCase;
8+
9+
import org.hamcrest.Description;
410
import org.hamcrest.Factory;
511
import org.hamcrest.Matcher;
6-
import org.hamcrest.core.CombinableMatcher;
712
import org.hamcrest.generator.FactoryMethod;
813
import org.hamcrest.generator.FactoryWriter;
914
import org.hamcrest.generator.SugarConfiguration;
1015
import org.xml.sax.InputSource;
1116

12-
import java.io.StringReader;
13-
import java.util.ArrayList;
14-
import java.util.List;
15-
16-
import static org.hamcrest.MatcherAssert.assertThat;
17-
import static org.hamcrest.core.IsCollectionContaining.hasItem;
18-
1917
public class XmlConfiguratorTest extends TestCase {
2018

2119
private MockSugarConfiguration sugarConfiguration;
@@ -35,12 +33,10 @@ public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
3533
" <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$AnotherMatcher'/>" +
3634
"</matchers>"));
3735

38-
assertThat(sugarConfiguration.factoryMethods(),
39-
hasItem(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher1", "org.hamcrest.Matcher")));
40-
assertThat(sugarConfiguration.factoryMethods(),
41-
hasItem(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher2", "org.hamcrest.Matcher")));
42-
assertThat(sugarConfiguration.factoryMethods(),
43-
hasItem(new FactoryMethod(AnotherMatcher.class.getName().replace('$', '.'), "matcher3", "org.hamcrest.CombinableMatcher")));
36+
final List<FactoryMethod> result = sugarConfiguration.factoryMethods();
37+
assertTrue(result.contains(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher1", "org.hamcrest.Matcher")));
38+
assertTrue(result.contains(new FactoryMethod(SomeMatcher.class.getName().replace('$', '.'), "matcher2", "org.hamcrest.Matcher")));
39+
assertTrue(result.contains(new FactoryMethod(AnotherMatcher.class.getName().replace('$', '.'), "matcher3", "org.hamcrest.MyMatcher")));
4440
}
4541

4642
private static InputSource createXml(String xml) {
@@ -51,30 +47,24 @@ private static InputSource createXml(String xml) {
5147

5248
@SuppressWarnings("rawtypes")
5349
public static class SomeMatcher {
54-
@Factory
55-
public static Matcher matcher1() {
56-
return null;
57-
}
58-
59-
@Factory
60-
public static Matcher matcher2() {
61-
return null;
62-
}
50+
@Factory public static Matcher matcher1() { return null; }
51+
@Factory public static Matcher matcher2() { return null; }
6352
}
6453

6554
@SuppressWarnings("rawtypes")
66-
public static class AnotherMatcher {
67-
@Factory
68-
public static CombinableMatcher matcher3() {
69-
return null;
70-
}
55+
public static class AnotherMatcher<T> implements Matcher<T> {
56+
@Override public void describeTo(Description description) { }
57+
@Override public boolean matches(Object item) { return false; }
58+
@Override public void describeMismatch(Object item, Description mismatchDescription) { }
59+
@Override @Deprecated public void _dont_implement_Matcher___instead_extend_BaseMatcher_() { }
60+
@Factory public static AnotherMatcher matcher3() { return null; }
7161
}
7262

7363
/**
7464
* Simple 'record and check' style mock. Not using a mocking library to avoid
75-
* cyclical dependency between mocking library and hamcrest.
65+
* cyclic dependency between mocking library and hamcrest.
7666
*/
77-
private static class MockSugarConfiguration implements SugarConfiguration {
67+
private static final class MockSugarConfiguration implements SugarConfiguration {
7868

7969
private final List<FactoryMethod> seenFactoryMethods = new ArrayList<FactoryMethod>();
8070
private final List<FactoryWriter> seenFactoryWriters = new ArrayList<FactoryWriter>();
@@ -100,5 +90,4 @@ public List<FactoryMethod> factoryMethods() {
10090
return seenFactoryMethods;
10191
}
10292
}
103-
10493
}

0 commit comments

Comments
 (0)