001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, 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;
009: * version 2.1 of the License.
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: * This package contains documentation from OpenGIS specifications.
017: * OpenGIS consortium's work is fully acknowledged here.
018: */
019: package org.geotools.referencing.factory;
020:
021: // JUnit dependencies
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: // OpenGIS dependencies
027: import org.opengis.referencing.crs.*;
028: import org.opengis.referencing.datum.*;
029: import org.opengis.referencing.IdentifiedObject;
030: import org.opengis.referencing.FactoryException;
031:
032: // Geotools dependencies
033: import org.geotools.referencing.crs.*;
034: import org.geotools.referencing.datum.*;
035: import org.geotools.referencing.ReferencingFactoryFinder;
036:
037: /**
038: * Tests the {@link AuthorityFactoryProxy} implementation.
039: *
040: * @author Martin Desruisseaux
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/factory/AuthorityFactoryProxyTest.java $
042: * @version $Id: AuthorityFactoryProxyTest.java 25396 2007-05-01 17:05:00Z desruisseaux $
043: */
044: public final class AuthorityFactoryProxyTest extends TestCase {
045: /**
046: * Run the suite from the command line.
047: */
048: public static void main(final String[] args) {
049: junit.textui.TestRunner.run(suite());
050: }
051:
052: /**
053: * Returns the test suite.
054: */
055: public static Test suite() {
056: return new TestSuite(AuthorityFactoryProxyTest.class);
057: }
058:
059: /**
060: * Creates a suite of the given name.
061: */
062: public AuthorityFactoryProxyTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * Tests {@link AuthorityFactoryProxy#getType(Class)}.
068: */
069: public void testType() {
070: assertEquals(ProjectedCRS.class, AuthorityFactoryProxy
071: .getType(ProjectedCRS.class));
072: assertEquals(ProjectedCRS.class, AuthorityFactoryProxy
073: .getType(DefaultProjectedCRS.class));
074: assertEquals(GeographicCRS.class, AuthorityFactoryProxy
075: .getType(GeographicCRS.class));
076: assertEquals(GeographicCRS.class, AuthorityFactoryProxy
077: .getType(DefaultGeographicCRS.class));
078: assertEquals(DerivedCRS.class, AuthorityFactoryProxy
079: .getType(DefaultDerivedCRS.class));
080: assertEquals(CoordinateReferenceSystem.class,
081: AuthorityFactoryProxy.getType(AbstractDerivedCRS.class));
082: assertEquals(GeodeticDatum.class, AuthorityFactoryProxy
083: .getType(DefaultGeodeticDatum.class));
084: }
085:
086: /**
087: * Tests {@link AuthorityFactoryProxy#create}. We uses the CRS factory for testing purpose.
088: */
089: public void testCreate() throws FactoryException {
090: final CRSAuthorityFactory factory = ReferencingFactoryFinder
091: .getCRSAuthorityFactory("CRS", null);
092: final CoordinateReferenceSystem expected = factory
093: .createCoordinateReferenceSystem("83");
094: AuthorityFactoryProxy proxy;
095: /*
096: * Try the proxy using the 'createGeographicCRS', 'createCoordinateReferenceSystem'
097: * and 'createObject' methods. The later uses a generic implementation, while the
098: * first two should use specialized implementations.
099: */
100: proxy = AuthorityFactoryProxy.getInstance(factory,
101: GeographicCRS.class);
102: assertTrue(proxy.getClass().getName().endsWith("Geographic"));
103: assertSame(expected, proxy.create("83"));
104: assertSame(expected, proxy.create("CRS:83"));
105:
106: proxy = AuthorityFactoryProxy.getInstance(factory,
107: CoordinateReferenceSystem.class);
108: assertTrue(proxy.getClass().getName().endsWith("CRS"));
109: assertSame(expected, proxy.create("83"));
110: assertSame(expected, proxy.create("CRS:83"));
111:
112: proxy = AuthorityFactoryProxy.getInstance(factory,
113: IdentifiedObject.class);
114: assertTrue(proxy.getClass().getName().endsWith("Default"));
115: assertSame(expected, proxy.create("83"));
116: assertSame(expected, proxy.create("CRS:83"));
117: /*
118: * Try using the 'createProjectedCRS' method, which should not
119: * be supported for the CRS factory (at least not for code "83").
120: */
121: proxy = AuthorityFactoryProxy.getInstance(factory,
122: ProjectedCRS.class);
123: assertTrue(proxy.getClass().getName().endsWith("Projected"));
124: try {
125: assertSame(expected, proxy.create("83"));
126: fail();
127: } catch (FactoryException e) {
128: // This is the expected exception.
129: assertTrue(e.getCause() instanceof ClassCastException);
130: }
131: /*
132: * Try using the 'createTemporalCRS' method, which should not
133: * be supported for the CRS factory (at least not for code "83").
134: * In addition, this code test the generic proxy instead of the
135: * specialized 'GeographicCRS' and 'ProjectedCRS' variants.
136: */
137: proxy = AuthorityFactoryProxy.getInstance(factory,
138: TemporalCRS.class);
139: assertTrue(proxy.getClass().getName().endsWith("Default"));
140: try {
141: assertSame(expected, proxy.create("83"));
142: fail();
143: } catch (FactoryException e) {
144: // This is the expected exception.
145: assertTrue(e.getCause() instanceof ClassCastException);
146: }
147: }
148:
149: /**
150: * Tests {@link IdentifiedObjectFinder#createFromCodes}.
151: * We uses the CRS factory for testing purpose.
152: */
153: public void testCreateFromCodes() throws FactoryException {
154: final CRSAuthorityFactory factory = ReferencingFactoryFinder
155: .getCRSAuthorityFactory("CRS", null);
156: final IdentifiedObjectFinder proxy = new IdentifiedObjectFinder(
157: factory, GeographicCRS.class);
158: CoordinateReferenceSystem expected = factory
159: .createCoordinateReferenceSystem("84");
160: assertNotSame(expected, DefaultGeographicCRS.WGS84);
161: assertSame(expected, proxy.createFromCodes(expected));
162: assertSame(expected, proxy.createFromIdentifiers(expected));
163: assertNull(proxy.createFromNames(expected));
164: assertSame(expected, proxy
165: .createFromCodes(DefaultGeographicCRS.WGS84));
166: assertNull(proxy
167: .createFromIdentifiers(DefaultGeographicCRS.WGS84));
168: assertNull(proxy.createFromNames(DefaultGeographicCRS.WGS84));
169:
170: expected = factory.createCoordinateReferenceSystem("83");
171: assertSame(expected, proxy.createFromCodes(expected));
172: assertSame(expected, proxy.createFromIdentifiers(expected));
173: assertNull(proxy.createFromNames(expected));
174: }
175: }
|