~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/config/XmlConfiguratorTest.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.config;
 
2
 
 
3
import static org.hamcrest.MatcherAssert.assertThat;
 
4
import static org.hamcrest.Matchers.hasItem;
 
5
 
 
6
import java.io.StringReader;
 
7
import java.util.ArrayList;
 
8
import java.util.List;
 
9
 
 
10
import junit.framework.TestCase;
 
11
 
 
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;
 
19
 
 
20
public class XmlConfiguratorTest extends TestCase {
 
21
 
 
22
    private MockSugarConfiguration sugarConfiguration;
 
23
    private XmlConfigurator config;
 
24
 
 
25
    @Override
 
26
    protected void setUp() throws Exception {
 
27
        super.setUp();
 
28
        sugarConfiguration = new MockSugarConfiguration();
 
29
        config = new XmlConfigurator(sugarConfiguration, getClass().getClassLoader());
 
30
    }
 
31
 
 
32
    public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception {
 
33
        config.load(createXml("" +
 
34
                "<matchers>" +
 
35
                "  <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$SomeMatcher'/>" +
 
36
                "  <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$AnotherMatcher'/>" +
 
37
                "</matchers>"));
 
38
 
 
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")));
 
45
    }
 
46
 
 
47
    private InputSource createXml(String xml) {
 
48
        return new InputSource(new StringReader(xml));
 
49
    }
 
50
 
 
51
    // Sample Matchers
 
52
 
 
53
    @SuppressWarnings("unchecked")
 
54
    public static class SomeMatcher {
 
55
        @Factory
 
56
        public static Matcher matcher1() {
 
57
            return null;
 
58
        }
 
59
 
 
60
        @Factory
 
61
        public static Matcher matcher2() {
 
62
            return null;
 
63
        }
 
64
    }
 
65
 
 
66
    @SuppressWarnings("unchecked")
 
67
    public static class AnotherMatcher {
 
68
        @Factory
 
69
        public static CombinableMatcher matcher3() {
 
70
            return null;
 
71
        }
 
72
    }
 
73
 
 
74
    /**
 
75
     * Simple 'record and check' style mock. Not using a mocking library to avoid
 
76
     * cyclical dependency between mocking library and hamcrest.
 
77
     */
 
78
    private static class MockSugarConfiguration implements SugarConfiguration {
 
79
 
 
80
        private final List<FactoryMethod> seenFactoryMethods = new ArrayList<FactoryMethod>();
 
81
        private final List<FactoryWriter> seenFactoryWriters = new ArrayList<FactoryWriter>();
 
82
 
 
83
        public void addWriter(FactoryWriter factoryWriter) {
 
84
            seenFactoryWriters.add(factoryWriter);
 
85
        }
 
86
 
 
87
        public void addFactoryMethod(FactoryMethod method) {
 
88
            seenFactoryMethods.add(method);
 
89
        }
 
90
 
 
91
        public void addFactoryMethods(Iterable<FactoryMethod> methods) {
 
92
            for (FactoryMethod method : methods) {
 
93
                addFactoryMethod(method);
 
94
            }
 
95
        }
 
96
 
 
97
        public List<FactoryMethod> factoryMethods() {
 
98
            return seenFactoryMethods;
 
99
        }
 
100
    }
 
101
 
 
102
}