001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.referencing.factory.wms;
017:
018: // J2SE dependencies
019: import java.util.Collection;
020: import java.util.logging.Level;
021:
022: // JUnit dependencies
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: // OpenGIS dependencies
028: import org.opengis.referencing.FactoryException;
029: import org.opengis.referencing.crs.GeographicCRS;
030: import org.opengis.referencing.crs.CRSAuthorityFactory;
031: import org.opengis.referencing.crs.CoordinateReferenceSystem;
032:
033: // Geotools dependencies
034: import org.geotools.referencing.CRS;
035: import org.geotools.resources.Arguments;
036: import org.geotools.referencing.ReferencingFactoryFinder;
037: import org.geotools.referencing.crs.DefaultGeographicCRS;
038: import org.geotools.referencing.factory.IdentifiedObjectFinder;
039: import org.geotools.referencing.factory.AbstractAuthorityFactory;
040: import org.geotools.referencing.factory.BufferedAuthorityFactory;
041:
042: /**
043: * Tests {@link WebCRSFactory}.
044: *
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/factory/wms/CRSTest.java $
046: * @version $Id: CRSTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
047: * @author Martin Desruisseaux
048: */
049: public final class CRSTest extends TestCase {
050: /**
051: * The factory to test.
052: */
053: private AbstractAuthorityFactory factory;
054:
055: /**
056: * Run the suite from the command line.
057: */
058: public static void main(final String[] args) {
059: final Arguments arguments = new Arguments(args);
060: final boolean log = arguments.getFlag("-log");
061: arguments.getRemainingArguments(0);
062: org.geotools.util.logging.Logging.GEOTOOLS
063: .forceMonolineConsoleOutput(log ? Level.CONFIG : null);
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: /**
068: * Returns the test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(CRSTest.class);
072: }
073:
074: /**
075: * Creates a suite of the given name.
076: */
077: public CRSTest(final String name) {
078: super (name);
079: }
080:
081: /**
082: * Initializes the factory to test.
083: */
084: protected void setUp() throws Exception {
085: super .setUp();
086: factory = new WebCRSFactory();
087: }
088:
089: /**
090: * Tests the registration in {@link ReferencingFactoryFinder}.
091: */
092: public void testFactoryFinder() {
093: final Collection authorities = ReferencingFactoryFinder
094: .getAuthorityNames();
095: assertTrue(authorities.contains("CRS"));
096: CRSAuthorityFactory found = ReferencingFactoryFinder
097: .getCRSAuthorityFactory("CRS", null);
098: assertTrue(found instanceof WebCRSFactory);
099: }
100:
101: /**
102: * Checks the authority names.
103: */
104: public void testAuthority() {
105: final Collection identifiers = factory.getAuthority()
106: .getIdentifiers();
107: assertTrue(identifiers.contains("CRS"));
108: assertFalse(identifiers.contains("EPSG"));
109: assertFalse(identifiers.contains("AUTO"));
110: assertFalse(identifiers.contains("AUTO2"));
111: }
112:
113: /**
114: * Tests the CRS:84 code.
115: */
116: public void testCRS84() throws FactoryException {
117: GeographicCRS crs = factory.createGeographicCRS("CRS:84");
118: assertSame(crs, factory.createGeographicCRS("84"));
119: assertSame(crs, factory.createGeographicCRS("CRS84"));
120: assertSame(crs, factory.createGeographicCRS("CRS:CRS84"));
121: assertSame(crs, factory.createGeographicCRS("crs : crs84"));
122: assertNotSame(crs, factory.createGeographicCRS("CRS:83"));
123: assertFalse(DefaultGeographicCRS.WGS84.equals(crs));
124: assertTrue(CRS.equalsIgnoreMetadata(DefaultGeographicCRS.WGS84,
125: crs));
126: }
127:
128: /**
129: * Tests the CRS:83 code.
130: */
131: public void testCRS83() throws FactoryException {
132: GeographicCRS crs = factory.createGeographicCRS("CRS:83");
133: assertSame(crs, factory.createGeographicCRS("83"));
134: assertSame(crs, factory.createGeographicCRS("CRS83"));
135: assertSame(crs, factory.createGeographicCRS("CRS:CRS83"));
136: assertNotSame(crs, factory.createGeographicCRS("CRS:84"));
137: assertFalse(CRS.equalsIgnoreMetadata(
138: DefaultGeographicCRS.WGS84, crs));
139: }
140:
141: /**
142: * Tests the {@link IdentifiedObjectFinder#find} method.
143: */
144: public void testFind() throws FactoryException {
145: final GeographicCRS CRS84 = factory
146: .createGeographicCRS("CRS:84");
147: final IdentifiedObjectFinder finder = factory
148: .getIdentifiedObjectFinder(CoordinateReferenceSystem.class);
149: assertTrue("Newly created finder should default to full scan.",
150: finder.isFullScanAllowed());
151:
152: finder.setFullScanAllowed(false);
153: assertSame(
154: "Should find without the need for scan, since we can use the CRS:84 identifier.",
155: CRS84, finder.find(CRS84));
156:
157: finder.setFullScanAllowed(true);
158: assertSame(
159: "Allowing scanning should not make any difference for this CRS84 instance.",
160: CRS84, finder.find(CRS84));
161:
162: assertNotSame("Required condition for next test.", CRS84,
163: DefaultGeographicCRS.WGS84);
164: assertFalse("Required condition for next test.", CRS84
165: .equals(DefaultGeographicCRS.WGS84));
166: assertTrue("Required condition for next test.",
167: CRS.equalsIgnoreMetadata(CRS84,
168: DefaultGeographicCRS.WGS84));
169:
170: finder.setFullScanAllowed(false);
171: assertNull(
172: "Should not find WGS84 without a full scan, since it doesn't contains the CRS:84 identifier.",
173: finder.find(DefaultGeographicCRS.WGS84));
174:
175: finder.setFullScanAllowed(true);
176: assertSame(
177: "A full scan should allow us to find WGS84, since it is equals ignoring metadata to CRS:84.",
178: CRS84, finder.find(DefaultGeographicCRS.WGS84));
179:
180: finder.setFullScanAllowed(false);
181: assertNull("The scan result should not be cached.", finder
182: .find(DefaultGeographicCRS.WGS84));
183:
184: // --------------------------------------------------
185: // Same test than above, using a CRS created from WKT
186: // --------------------------------------------------
187:
188: String wkt = "GEOGCS[\"WGS 84\",\n"
189: + " DATUM[\"WGS84\",\n"
190: + " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563]],\n"
191: + " PRIMEM[\"Greenwich\", 0.0],\n"
192: + " UNIT[\"degree\", 0.017453292519943295]]";
193: CoordinateReferenceSystem search = CRS.parseWKT(wkt);
194: assertFalse("Required condition for next test.", CRS84
195: .equals(search));
196: assertTrue("Required condition for next test.", CRS
197: .equalsIgnoreMetadata(CRS84, search));
198:
199: finder.setFullScanAllowed(false);
200: assertNull(
201: "Should not find WGS84 without a full scan, since it doesn't contains the CRS:84 identifier.",
202: finder.find(search));
203:
204: finder.setFullScanAllowed(true);
205: assertSame(
206: "A full scan should allow us to find WGS84, since it is equals ignoring metadata to CRS:84.",
207: CRS84, finder.find(search));
208:
209: assertEquals("CRS:84", finder.findIdentifier(search));
210: }
211:
212: /**
213: * Tests the {@link IdentifiedObjectFinder#find} method through a buffered authority factory.
214: * The objects found are expected to be cached.
215: */
216: public void testBufferedFind() throws FactoryException {
217: final AbstractAuthorityFactory factory = new Buffered(
218: this .factory);
219: final GeographicCRS CRS84 = factory
220: .createGeographicCRS("CRS:84");
221: final IdentifiedObjectFinder finder = factory
222: .getIdentifiedObjectFinder(CoordinateReferenceSystem.class);
223:
224: finder.setFullScanAllowed(false);
225: assertSame(
226: "Should find without the need for scan, since we can use the CRS:84 identifier.",
227: CRS84, finder.find(CRS84));
228:
229: finder.setFullScanAllowed(false);
230: assertNull(
231: "Should not find WGS84 without a full scan, since it doesn't contains the CRS:84 identifier.",
232: finder.find(DefaultGeographicCRS.WGS84));
233:
234: finder.setFullScanAllowed(true);
235: assertSame(
236: "A full scan should allow us to find WGS84, since it is equals ignoring metadata to CRS:84.",
237: CRS84, finder.find(DefaultGeographicCRS.WGS84));
238:
239: finder.setFullScanAllowed(false);
240: assertSame(
241: "At the contrary of testFind(), the scan result should be cached.",
242: CRS84, finder.find(DefaultGeographicCRS.WGS84));
243:
244: assertEquals("CRS:84", finder
245: .findIdentifier(DefaultGeographicCRS.WGS84));
246: }
247:
248: /**
249: * For {@link #testBufferedFind}.
250: */
251: private static final class Buffered extends
252: BufferedAuthorityFactory implements CRSAuthorityFactory {
253: Buffered(final AbstractAuthorityFactory factory) {
254: super(factory);
255: }
256: }
257: }
|