001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.referencing.factory;
018:
019: // J2SE dependencies
020: import java.util.Collection;
021: import java.util.logging.Level;
022:
023: // JUnit dependencies
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: // OpenGIS dependencies
029: import org.opengis.referencing.IdentifiedObject;
030: import org.opengis.referencing.FactoryException;
031: import org.opengis.referencing.NoSuchAuthorityCodeException;
032: import org.opengis.referencing.crs.CRSAuthorityFactory;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: // Geotools dependencies
036: import org.geotools.referencing.CRS;
037: import org.geotools.referencing.ReferencingFactoryFinder;
038: import org.geotools.referencing.crs.DefaultGeographicCRS;
039: import org.geotools.resources.Arguments;
040:
041: /**
042: * Tests the {@link AllAuthoritiesFactory} implementation.
043: *
044: * @author Martin Desruisseaux
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/factory/AllAuthoritiesFactoryTest.java $
046: * @version $Id: AllAuthoritiesFactoryTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
047: */
048: public final class AllAuthoritiesFactoryTest extends TestCase {
049: /**
050: * Run the suite from the command line.
051: */
052: public static void main(final String[] args) {
053: final Arguments arguments = new Arguments(args);
054: final boolean log = arguments.getFlag("-log");
055: arguments.getRemainingArguments(0);
056: org.geotools.util.logging.Logging.GEOTOOLS
057: .forceMonolineConsoleOutput(log ? Level.CONFIG : null);
058: junit.textui.TestRunner.run(suite());
059: }
060:
061: /**
062: * Returns the test suite.
063: */
064: public static Test suite() {
065: return new TestSuite(AllAuthoritiesFactoryTest.class);
066: }
067:
068: /**
069: * Creates a suite of the given name.
070: */
071: public AllAuthoritiesFactoryTest(final String name) {
072: super (name);
073: }
074:
075: /**
076: * Tests the {@link AllAuthoritiesFactory#getAuthorityCodes} method.
077: */
078: public void testAuthorityCodes() throws FactoryException {
079: final CRSAuthorityFactory all = AllAuthoritiesFactory.DEFAULT;
080: final Collection codes = all
081: .getAuthorityCodes(CoordinateReferenceSystem.class);
082: assertFalse(codes.isEmpty());
083: assertTrue(codes.contains("CRS:84"));
084: assertTrue(codes.contains("AUTO:42001")
085: || codes.contains("AUTO2:42001"));
086: }
087:
088: /**
089: * Tests the {@link AllAuthoritiesFactory#createCoordinateReferenceSystem} method.
090: */
091: public void testCreateCRS() throws FactoryException {
092: final CRSAuthorityFactory auto = ReferencingFactoryFinder
093: .getCRSAuthorityFactory("AUTO", null);
094: final CRSAuthorityFactory crs = ReferencingFactoryFinder
095: .getCRSAuthorityFactory("CRS", null);
096: final CRSAuthorityFactory all = AllAuthoritiesFactory.DEFAULT;
097: CoordinateReferenceSystem actual, expected;
098:
099: actual = all.createCoordinateReferenceSystem("CRS:84");
100: expected = crs.createCoordinateReferenceSystem("84");
101: assertSame(expected, actual);
102: assertSame(expected, all.createObject("CRS:84"));
103:
104: actual = all.createCoordinateReferenceSystem("AUTO:42001,0,0");
105: expected = auto.createCoordinateReferenceSystem("42001,0,0");
106: assertSame(expected, actual);
107: assertSame(expected, all.createObject("AUTO:42001,0,0"));
108:
109: actual = all.createCoordinateReferenceSystem("CRS:27");
110: expected = crs.createCoordinateReferenceSystem("27");
111: assertSame(expected, actual);
112: assertSame(expected, all.createObject("CRS:27"));
113:
114: try {
115: all.createCoordinateReferenceSystem("84");
116: fail("Should not work without authority.");
117: } catch (NoSuchAuthorityCodeException exception) {
118: // This is the expected exception.
119: assertEquals("84", exception.getAuthorityCode());
120: }
121:
122: try {
123: all.createCoordinateReferenceSystem("FOO:84");
124: fail("Should not work with unknown authority.");
125: } catch (NoSuchAuthorityCodeException exception) {
126: // This is the expected exception.
127: assertEquals("FOO", exception.getAuthority());
128: }
129: }
130:
131: /**
132: * Tests the {@code "http://www.opengis.net/gml/srs/"} name space. This requires special
133: * processing by {@link AllAuthoritiesFactory}, since the separator character is not the
134: * usual {@code ':'}.
135: */
136: public void testHttp() throws FactoryException {
137: final CRSAuthorityFactory crs = ReferencingFactoryFinder
138: .getCRSAuthorityFactory("CRS", null);
139: final CRSAuthorityFactory all = AllAuthoritiesFactory.DEFAULT;
140: CoordinateReferenceSystem actual, expected;
141:
142: actual = all
143: .createCoordinateReferenceSystem("http://www.opengis.net/gml/srs/CRS#84");
144: expected = crs.createCoordinateReferenceSystem("84");
145: assertSame(expected, actual);
146:
147: actual = all
148: .createCoordinateReferenceSystem("HTTP://WWW.OPENGIS.NET/GML/SRS/crs#84");
149: assertSame(expected, actual);
150:
151: actual = all
152: .createCoordinateReferenceSystem("http://www.opengis.net/gml/srs/CRS.xml#84");
153: assertSame(expected, actual);
154:
155: try {
156: all
157: .createCoordinateReferenceSystem("http://www.dummy.net/gml/srs/CRS#84");
158: fail("Expected a NoSuchAuthorityCodeException");
159: } catch (NoSuchAuthorityCodeException e) {
160: assertEquals("http://www.dummy.net", e.getAuthority());
161: }
162:
163: try {
164: all
165: .createCoordinateReferenceSystem("http://www.opengis.net/gml/dummy/CRS#84");
166: fail("Expected a NoSuchAuthorityCodeException");
167: } catch (NoSuchAuthorityCodeException e) {
168: assertEquals("http://www.opengis.net/gml/srs/", e
169: .getAuthority());
170: }
171: }
172:
173: /**
174: * Tests the {@link IdentifiedObjectFinder#find} method.
175: */
176: public void testFind() throws FactoryException {
177: final AbstractAuthorityFactory all = AllAuthoritiesFactory.DEFAULT;
178: final IdentifiedObjectFinder finder = all
179: .getIdentifiedObjectFinder(CoordinateReferenceSystem.class);
180: finder.setFullScanAllowed(false);
181: assertNull("Should not find the CRS without a scan.", finder
182: .find(DefaultGeographicCRS.WGS84));
183:
184: finder.setFullScanAllowed(true);
185: final IdentifiedObject find = finder
186: .find(DefaultGeographicCRS.WGS84);
187: assertNotNull("With scan allowed, should find the CRS.", find);
188: assertTrue(CRS.equalsIgnoreMetadata(find,
189: DefaultGeographicCRS.WGS84));
190: assertSame(all.createCoordinateReferenceSystem("CRS:84"), find);
191: assertEquals("CRS:84", finder
192: .findIdentifier(DefaultGeographicCRS.WGS84));
193: }
194: }
|