|
| 1 | +package org.hamcrest.generator.config; |
| 2 | + |
| 3 | +import junit.framework.TestCase; |
| 4 | +import org.hamcrest.Factory; |
| 5 | +import org.hamcrest.Matcher; |
| 6 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 7 | +import static org.hamcrest.collection.IsCollectionContaining.collectionContainingAllOf; |
| 8 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 9 | +import org.hamcrest.generator.FactoryMethod; |
| 10 | +import org.hamcrest.generator.FactoryWriter; |
| 11 | +import org.hamcrest.generator.SugarConfiguration; |
| 12 | +import org.xml.sax.InputSource; |
| 13 | + |
| 14 | +import java.io.StringReader; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class XmlConfiguratorTest extends TestCase { |
| 19 | + |
| 20 | + private MockSugarConfiguration sugarConfiguration; |
| 21 | + private StubClassLoader classLoader; |
| 22 | + private XmlConfigurator config; |
| 23 | + |
| 24 | + protected void setUp() throws Exception { |
| 25 | + super.setUp(); |
| 26 | + sugarConfiguration = new MockSugarConfiguration(); |
| 27 | + classLoader = new StubClassLoader(); |
| 28 | + config = new XmlConfigurator(sugarConfiguration, classLoader); |
| 29 | + } |
| 30 | + |
| 31 | + public void testAddsMatcherFactoryMethodsToConfiguration() throws Exception { |
| 32 | + config.load(createXml("" + |
| 33 | + "<matchers>" + |
| 34 | + " <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$SomeMatcher'/>" + |
| 35 | + " <factory class='org.hamcrest.generator.config.XmlConfiguratorTest$AnotherMatcher'/>" + |
| 36 | + "</matchers>")); |
| 37 | + |
| 38 | + assertThat(sugarConfiguration.factoryMethods(), |
| 39 | + collectionContainingAllOf( |
| 40 | + equalTo(new FactoryMethod(SomeMatcher.class.getName(), "matcher1")), |
| 41 | + equalTo(new FactoryMethod(SomeMatcher.class.getName(), "matcher2")), |
| 42 | + equalTo(new FactoryMethod(AnotherMatcher.class.getName(), "matcher3")) |
| 43 | + )); |
| 44 | + } |
| 45 | + |
| 46 | + private InputSource createXml(String xml) { |
| 47 | + return new InputSource(new StringReader(xml)); |
| 48 | + } |
| 49 | + |
| 50 | + // Sample Matchers |
| 51 | + |
| 52 | + public static class SomeMatcher { |
| 53 | + @Factory |
| 54 | + public static Matcher matcher1() { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + @Factory |
| 59 | + public static Matcher matcher2() { |
| 60 | + return null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static class AnotherMatcher { |
| 65 | + @Factory |
| 66 | + public static Matcher matcher3() { |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Simple 'record and check' style mock. Not using a mocking library to avoid |
| 73 | + * cyclical dependency between mocking library and hamcrest. |
| 74 | + */ |
| 75 | + private static class MockSugarConfiguration implements SugarConfiguration { |
| 76 | + |
| 77 | + private final List<FactoryMethod> seenFactoryMethods = new ArrayList<FactoryMethod>(); |
| 78 | + private final List<FactoryWriter> seenFactoryWriters = new ArrayList<FactoryWriter>(); |
| 79 | + |
| 80 | + public void addWriter(FactoryWriter factoryWriter) { |
| 81 | + seenFactoryWriters.add(factoryWriter); |
| 82 | + } |
| 83 | + |
| 84 | + public void addFactoryMethod(FactoryMethod method) { |
| 85 | + seenFactoryMethods.add(method); |
| 86 | + } |
| 87 | + |
| 88 | + public void addFactoryMethods(Iterable<FactoryMethod> methods) { |
| 89 | + for (FactoryMethod method : methods) { |
| 90 | + addFactoryMethod(method); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public List<FactoryMethod> factoryMethods() { |
| 95 | + return seenFactoryMethods; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static class StubClassLoader extends ClassLoader { |
| 100 | + public Class<?> loadClass(String name) throws ClassNotFoundException { |
| 101 | + return super.loadClass(name); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments