1
package org.hamcrest.generator.config;
3
import static org.hamcrest.MatcherAssert.assertThat;
4
import static org.hamcrest.Matchers.hasItem;
6
import java.io.StringReader;
7
import java.util.ArrayList;
10
import junit.framework.TestCase;
12
import org.hamcrest.Factory;
13
import org.hamcrest.Matcher;
14
import org.hamcrest.core.CombinableMatcher;
15
import org.hamcrest.generator.FactoryMethod;
16
import org.hamcrest.generator.FactoryWriter;
17
import org.hamcrest.generator.SugarConfiguration;
18
import org.xml.sax.InputSource;
20
public class XmlConfiguratorTest extends TestCase {
22
private MockSugarConfiguration sugarConfiguration;
23
private XmlConfigurator config;
26
protected void setUp() throws Exception {
28
sugarConfiguration = new MockSugarConfiguration();
29
config = new XmlConfigurator(sugarConfiguration, getClass().getClassLoader());
32
public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
33
config.load(createXml("" +
35
" <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$SomeMatcher'/>" +
36
" <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$AnotherMatcher'/>" +
39
assertThat(sugarConfiguration.factoryMethods(),
40
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher1", "org.hamcrest.Matcher")));
41
assertThat(sugarConfiguration.factoryMethods(),
42
hasItem(new FactoryMethod(SomeMatcher.class.getName(), "matcher2", "org.hamcrest.Matcher")));
43
assertThat(sugarConfiguration.factoryMethods(),
44
hasItem(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3", "org.hamcrest.CombinableMatcher")));
47
private InputSource createXml(String xml) {
48
return new InputSource(new StringReader(xml));
53
@SuppressWarnings("unchecked")
54
public static class SomeMatcher {
56
public static Matcher matcher1() {
61
public static Matcher matcher2() {
66
@SuppressWarnings("unchecked")
67
public static class AnotherMatcher {
69
public static CombinableMatcher matcher3() {
75
* Simple 'record and check' style mock. Not using a mocking library to avoid
76
* cyclical dependency between mocking library and hamcrest.
78
private static class MockSugarConfiguration implements SugarConfiguration {
80
private final List<FactoryMethod> seenFactoryMethods = new ArrayList<FactoryMethod>();
81
private final List<FactoryWriter> seenFactoryWriters = new ArrayList<FactoryWriter>();
83
public void addWriter(FactoryWriter factoryWriter) {
84
seenFactoryWriters.add(factoryWriter);
87
public void addFactoryMethod(FactoryMethod method) {
88
seenFactoryMethods.add(method);
91
public void addFactoryMethods(Iterable<FactoryMethod> methods) {
92
for (FactoryMethod method : methods) {
93
addFactoryMethod(method);
97
public List<FactoryMethod> factoryMethods() {
98
return seenFactoryMethods;