001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.factory;
017:
018: // Geotools dependencies
019: import java.util.Arrays;
020:
021: // JUnit dependencies
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Tests {@link FactoryIteratorProvider} addition in {@link Factories}.
028: *
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/factory/FactoryIteratorProviderTest.java $
030: * @version $Id: FactoryIteratorProviderTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
031: * @author Martin Desruisseaux
032: */
033: public class FactoryIteratorProviderTest extends TestCase {
034: /**
035: * Run the suite from the command line.
036: */
037: public static void main(String[] args) {
038: org.geotools.util.logging.Logging.GEOTOOLS
039: .forceMonolineConsoleOutput();
040: junit.textui.TestRunner.run(suite());
041: }
042:
043: /**
044: * Returns the test suite.
045: */
046: public static Test suite() {
047: return new TestSuite(FactoryIteratorProviderTest.class);
048: }
049:
050: /**
051: * Constructs a test case.
052: */
053: public FactoryIteratorProviderTest(final String testName) {
054: super (testName);
055: }
056:
057: /**
058: * Creates a new, initially empty, factory registry.
059: */
060: private static FactoryRegistry createRegistry() {
061: return new FactoryRegistry(Arrays
062: .asList(new Class[] { DummyFactory.class }));
063: }
064:
065: /**
066: * Returns a factory from the specified registry.
067: *
068: * @throws FactoryNotFoundException if no factory was found.
069: * @throws FactoryRegistryException if a factory can't be returned for some other reason.
070: */
071: private static DummyFactory getFactory(
072: final FactoryRegistry registry,
073: final Class/*<? extends DummyFactory>*/type)
074: throws FactoryRegistryException {
075: Hints hints = null;
076: if (type != null) {
077: hints = new Hints(DummyFactory.DUMMY_FACTORY, type);
078: }
079: return (DummyFactory) registry.getServiceProvider(
080: DummyFactory.class, null, hints,
081: DummyFactory.DUMMY_FACTORY);
082: }
083:
084: /**
085: * Tests the registration of {@link DummyFactory} instances from
086: * {@link DummyFactoryIteratorProvider}.
087: */
088: public void testRegistration() {
089: FactoryRegistry registry = createRegistry();
090: /*
091: * Tests the initially empty factory.
092: */
093: try {
094: assertNotNull(getFactory(registry, null));
095: fail("No factory should have been found.");
096: } catch (FactoryNotFoundException e) {
097: // This is the expected exception.
098: }
099: /*
100: * Tests after the addition of a new provider.
101: */
102: final FactoryIteratorProvider provider1 = new DummyFactoryIteratorProvider(
103: true);
104: try {
105: Factories.addFactoryIteratorProvider(provider1);
106: assertNotNull(getFactory(registry, null));
107: DummyFactory factory;
108:
109: factory = getFactory(registry, DummyFactory.Example1.class);
110: assertEquals(DummyFactory.Example1.class, factory
111: .getClass());
112: factory = getFactory(registry, DummyFactory.Example2.class);
113: assertEquals(DummyFactory.Example2.class, factory
114: .getClass());
115: try {
116: assertNotNull(getFactory(registry,
117: DummyFactory.Example3.class));
118: fail("Example #3 should not be registered.");
119: } catch (FactoryNotFoundException e) {
120: // This is the expected exception.
121: }
122: try {
123: assertNotNull(getFactory(registry,
124: DummyFactory.Example4.class));
125: fail("Example #4 should not be registered.");
126: } catch (FactoryNotFoundException e) {
127: // This is the expected exception.
128: }
129: /*
130: * Add yet an other provider, and test again.
131: */
132: final FactoryIteratorProvider provider2 = new DummyFactoryIteratorProvider(
133: false);
134: try {
135: Factories.addFactoryIteratorProvider(provider2);
136: factory = getFactory(registry,
137: DummyFactory.Example1.class);
138: assertEquals(DummyFactory.Example1.class, factory
139: .getClass());
140: factory = getFactory(registry,
141: DummyFactory.Example2.class);
142: assertEquals(DummyFactory.Example2.class, factory
143: .getClass());
144: factory = getFactory(registry,
145: DummyFactory.Example3.class);
146: assertEquals(DummyFactory.Example3.class, factory
147: .getClass());
148: factory = getFactory(registry,
149: DummyFactory.Example4.class);
150: assertEquals(DummyFactory.Example4.class, factory
151: .getClass());
152: } finally {
153: Factories.removeFactoryIteratorProvider(provider2);
154: }
155: } finally {
156: Factories.removeFactoryIteratorProvider(provider1);
157: }
158: /*
159: * Tests with a new registry, which should be empty.
160: */
161: registry = createRegistry();
162: try {
163: assertNotNull(getFactory(registry, null));
164: fail("The factory should be empty again.");
165: } catch (FactoryNotFoundException e) {
166: // This is the expected exception.
167: }
168: }
169: }
|