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: package org.geotools.referencing.factory.epsg;
017:
018: // Java dependencies
019: import java.net.URL;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.geotools.referencing.CRS;
028: import org.geotools.referencing.ReferencingFactoryFinder;
029: import org.opengis.referencing.FactoryException;
030: import org.opengis.referencing.NoSuchAuthorityCodeException;
031: import org.opengis.referencing.crs.CRSAuthorityFactory;
032: import org.opengis.referencing.crs.CoordinateReferenceSystem;
033: import org.opengis.referencing.crs.ProjectedCRS;
034:
035: /**
036: * Checks the exception thrown by the fallback system do report actual errors when the code is
037: * available but for some reason broken, and not "code not found" ones.
038: *
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/factory/epsg/FallbackAuthorityFactoryTest.java $
040: * @version $Id: FallbackAuthorityFactoryTest.java 29124 2008-02-07 08:22:48Z aaime $
041: * @author Andrea Aime (TOPP)
042: */
043: public class FallbackAuthorityFactoryTest extends TestCase {
044: /**
045: * Set to {@code true} for printing debugging information.
046: */
047: private static final boolean VERBOSE = false;
048:
049: /**
050: * The extra factory.
051: */
052: private FactoryEPSGExtra extra;
053:
054: /**
055: * Returns the test suite.
056: */
057: public static Test suite() {
058: return new TestSuite(FallbackAuthorityFactoryTest.class);
059: }
060:
061: /**
062: * Run the test from the command line.
063: *
064: * @param args the command line arguments.
065: */
066: public static void main(final String[] args) {
067: org.geotools.util.logging.Logging.GEOTOOLS
068: .forceMonolineConsoleOutput();
069: junit.textui.TestRunner.run(suite());
070: }
071:
072: /**
073: * Creates a test case with the specified name.
074: */
075: public FallbackAuthorityFactoryTest(final String name) {
076: super (name);
077: }
078:
079: /**
080: * Adds the extra factory to the set of authority factories.
081: */
082: public void setUp() {
083: assertNull(extra);
084: extra = new FactoryEPSGExtra();
085: ReferencingFactoryFinder.addAuthorityFactory(extra);
086: ReferencingFactoryFinder.scanForPlugins();
087: }
088:
089: /**
090: * Removes the extra factory from the set of authority factories.
091: */
092: public void tearDown() {
093: if (org.geotools.test.TestData.isBaseJavaPlatform()) {
094: // Disabled in J2SE 1.4 build because of a bug.
095: // TODO: Remove when we will be target J2SE 1.4 or 1.5.
096: return;
097: }
098: assertNotNull(extra);
099: ReferencingFactoryFinder.removeAuthorityFactory(extra);
100: extra = null;
101: }
102:
103: /**
104: * Makes sure that the testing {@link FactoryEPSGExtra} has precedence over
105: * {@link FactoryUsingWKT}.
106: */
107: public void testFactoryOrdering() {
108: if (org.geotools.test.TestData.isBaseJavaPlatform()) {
109: // Disabled in J2SE 1.4 build because of a bug.
110: // TODO: Remove when we will be target J2SE 1.4 or 1.5.
111: return;
112: }
113: Set factories = ReferencingFactoryFinder
114: .getCRSAuthorityFactories(null);
115: boolean foundWkt = false;
116: boolean foundExtra = false;
117: for (Iterator it = factories.iterator(); it.hasNext();) {
118: CRSAuthorityFactory factory = (CRSAuthorityFactory) it
119: .next();
120: Class type = factory.getClass();
121: if (VERBOSE) {
122: System.out.println(type);
123: }
124: if (type == FactoryEPSGExtra.class) {
125: foundExtra = true;
126: } else if (type == FactoryUsingWKT.class) {
127: foundWkt = true;
128: assertTrue(
129: "We should have encountered WKT factory after the extra one",
130: foundExtra);
131: }
132: }
133: assertTrue(foundWkt);
134: assertTrue(foundExtra);
135: }
136:
137: /**
138: * Tests the {@code 42101} code. The purpose of this test is mostly
139: * to make sure that {@link FactoryUsingWKT} is in the chain.
140: */
141: public void test42101() throws FactoryException {
142: assertTrue(CRS.decode("EPSG:42101") instanceof ProjectedCRS);
143: }
144:
145: /**
146: * Tests the {@code 00001} fake code.
147: */
148: public void test00001() throws FactoryException {
149: if (org.geotools.test.TestData.isBaseJavaPlatform()) {
150: // Disabled in J2SE 1.4 build because of a bug.
151: // TODO: Remove when we will be target J2SE 1.4 or 1.5.
152: return;
153: }
154: try {
155: CRS.decode("EPSG:00001");
156: fail("This code should not be there");
157: } catch (NoSuchAuthorityCodeException e) {
158: fail("The code 00001 is there, exception should report it's broken");
159: } catch (FactoryException e) {
160: // cool, that's what we expected
161: }
162: }
163:
164: /**
165: * GEOT-1702, make sure looking up for an existing code does not result in a
166: * StackOverflowException
167: * @throws FactoryException
168: */
169: public void testLookupSuccessfull() throws FactoryException {
170: CoordinateReferenceSystem crs = CRS.decode("EPSG:42101");
171: String code = CRS.lookupIdentifier(crs, true);
172: assertEquals("EPSG:42101", code);
173: }
174:
175: /**
176: * GEOT-1702, make sure looking up for a non existing code does not result in a
177: * StackOverflowException
178: * @throws FactoryException
179: */
180: public void testLookupFailing() throws FactoryException {
181: String wkt = "PROJCS[\"Google Mercator\", \r\n"
182: + " GEOGCS[\"WGS 84\", \r\n"
183: + " DATUM[\"World Geodetic System 1984\", \r\n"
184: + " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], \r\n"
185: + " AUTHORITY[\"EPSG\",\"6326\"]], \r\n"
186: + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], \r\n"
187: + " UNIT[\"degree\", 0.017453292519943295], \r\n"
188: + " AXIS[\"Geodetic latitude\", NORTH], \r\n"
189: + " AXIS[\"Geodetic longitude\", EAST], \r\n"
190: + " AUTHORITY[\"EPSG\",\"4326\"]], \r\n"
191: + " PROJECTION[\"Mercator_1SP\"], \r\n"
192: + " PARAMETER[\"latitude_of_origin\", 0.0], \r\n"
193: + " PARAMETER[\"central_meridian\", 0.0], \r\n"
194: + " PARAMETER[\"scale_factor\", 1.0], \r\n"
195: + " PARAMETER[\"false_easting\", 0.0], \r\n"
196: + " PARAMETER[\"false_northing\", 0.0], \r\n"
197: + " UNIT[\"m\", 1.0], \r\n"
198: + " AXIS[\"Easting\", EAST], \r\n"
199: + " AXIS[\"Northing\", NORTH], \r\n"
200: + " AUTHORITY[\"EPSG\",\"900913\"]]";
201: CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
202: assertNull(CRS.lookupIdentifier(crs, true));
203: }
204:
205: /**
206: * Extra class used to make sure we have {@link FactoryUsingWKT} among the fallbacks
207: * (used to check the fallback mechanism).
208: *
209: * @author Andrea Aime (TOPP)
210: */
211: private static class FactoryEPSGExtra extends FactoryUsingWKT {
212: /**
213: * Creates a factory to be registered before {@link FactoryUsingWKT} in the fallback chain.
214: */
215: public FactoryEPSGExtra() {
216: // make sure we are before FactoryUsingWKT in the fallback chain
217: super (null, DEFAULT_PRIORITY + 5);
218: }
219:
220: /**
221: * Returns the URL to the test file that contains a broken CRS for code EPSG:1.
222: */
223: protected URL getDefinitionsURL() {
224: return FactoryUsingWKT.class
225: .getResource("epsg2.properties");
226: }
227: }
228: }
|