001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, 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.coverage.io;
018:
019: // J2SE dependencies
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023:
024: // JUnit dependencies
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: // OpenGIS dependencies
030: import org.opengis.coverage.grid.GridRange;
031: import org.opengis.coverage.grid.GridCoverage;
032: import org.opengis.referencing.crs.GeographicCRS;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: // Geotools dependencies
036: import org.geotools.referencing.CRS;
037: import org.geotools.resources.Arguments;
038:
039: /**
040: * Tests the {@link MetadataBuilder} implementation.
041: *
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/test/java/org/geotools/coverage/io/MetadataBuilderTest.java $
043: * @version $Id: MetadataBuilderTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
044: * @author Martin Desruisseaux
045: */
046: public class MetadataBuilderTest extends TestCase {
047: /**
048: * Set to a non-null value for printing some diagnostic message to the standard output.
049: */
050: private static PrintWriter out;
051:
052: /**
053: * Run the suit from the command line. Run the test with the
054: * "-print" option in order to print test to standard output.
055: */
056: public static void main(final String[] args) {
057: final Arguments arguments = new Arguments(args);
058: if (arguments.getFlag("-print")) {
059: out = arguments.out;
060: } else {
061: out = new PrintWriter(new StringWriter());
062: }
063: arguments.getRemainingArguments(0);
064: org.geotools.util.logging.Logging.GEOTOOLS
065: .forceMonolineConsoleOutput();
066: junit.textui.TestRunner.run(suite());
067: }
068:
069: /**
070: * Returns the test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(MetadataBuilderTest.class);
074: }
075:
076: /**
077: * Constructs a test case with the given name.
078: */
079: public MetadataBuilderTest(final String name) {
080: super (name);
081: }
082:
083: /**
084: * Tests the addition of alias.
085: */
086: public void testAlias() throws IOException {
087: final MetadataBuilder parser = new MetadataBuilder();
088: /*
089: * Tests "add" operations.
090: */
091: parser.add("Alias 1", "Value 1");
092: parser.add("Alias 2", "Value 2");
093: try {
094: parser.add(" alias 1", "Value X");
095: fail(); // We should not get there.
096: } catch (AmbiguousMetadataException exception) {
097: // This is the expected exception.
098: if (out != null) {
099: out.println(exception);
100: }
101: }
102: parser.add("Alias 1", "Value 1"); // Already defined
103: parser.add("Alias 3", "Value 3");
104: /*
105: * Tests "addAlias" operations.
106: */
107: parser.addAlias(MetadataBuilder.X_RESOLUTION, "Alias 1");
108: parser.addAlias(MetadataBuilder.Y_RESOLUTION, "Alias 2");
109: parser.addAlias(MetadataBuilder.Y_RESOLUTION, "Alias 2bis");
110: parser.addAlias(MetadataBuilder.X_RESOLUTION, "Alias 1bis");
111: parser.addAlias(MetadataBuilder.X_RESOLUTION, "Alias 1"); // Already defined
112: try {
113: parser.addAlias(MetadataBuilder.X_RESOLUTION, "Alias 2");
114: fail(); // We should not get there.
115: } catch (AmbiguousMetadataException exception) {
116: // This is the expected exception.
117: if (out != null) {
118: out.println(exception);
119: }
120: }
121: parser.add("Alias 2bis", "Value 2");
122: try {
123: parser.add("Alias 1bis", "Value 2");
124: fail(); // We should not get there.
125: } catch (AmbiguousMetadataException exception) {
126: // This is the expected exception.
127: if (out != null) {
128: out.println(exception);
129: }
130: }
131: /*
132: * Tests "get" operations.
133: */
134: assertEquals("Value 1", parser
135: .get(MetadataBuilder.X_RESOLUTION));
136: assertEquals("Value 2", parser
137: .get(MetadataBuilder.Y_RESOLUTION));
138: try {
139: parser.get(MetadataBuilder.DATUM);
140: fail(); // We should not get there.
141: } catch (MissingMetadataException exception) {
142: // This is the expected exception.
143: if (out != null) {
144: out.println(exception);
145: }
146: }
147: /*
148: * Tests "getAsDouble" and "getAsInt" operations.
149: */
150: parser.add("ULX", "40");
151: parser.add("ULY", "12.5");
152: parser.addAlias(MetadataBuilder.X_MINIMUM, "ULX");
153: parser.addAlias(MetadataBuilder.Y_MAXIMUM, "ULY");
154: assertEquals(40, parser.getAsDouble(MetadataBuilder.X_MINIMUM),
155: 0);
156: assertEquals(12.5, parser
157: .getAsDouble(MetadataBuilder.Y_MAXIMUM), 0);
158: assertEquals(40, parser.getAsInt(MetadataBuilder.X_MINIMUM));
159: try {
160: parser.getAsInt(MetadataBuilder.Y_MAXIMUM);
161: fail(); // We should not get there.
162: } catch (MetadataException exception) {
163: // This is the expected exception.
164: if (out != null) {
165: out.println(exception);
166: }
167: }
168: if (out != null) {
169: out.println();
170: parser.listMetadata(out);
171: out.flush();
172: }
173: }
174:
175: /**
176: * Tests the formatting.
177: */
178: public void testFormat() throws IOException {
179: final MetadataBuilder parser = new MetadataBuilder();
180: /*
181: * Do not add a COORDINATE_REFERENCE_SYSTEM property, because we want
182: * to test the MetadataBuilder capability to create it from scratch.
183: */
184: parser.addAlias(MetadataBuilder.PROJECTION, "Projection");
185: parser.addAlias(MetadataBuilder.DATUM, "Datum");
186: parser.addAlias(MetadataBuilder.UNITS, "Units");
187: parser.addAlias(MetadataBuilder.X_MINIMUM, "Upper left X");
188: parser.addAlias(MetadataBuilder.Y_MAXIMUM, "Upper left Y");
189: parser.addAlias(MetadataBuilder.X_RESOLUTION, "Resolution X");
190: parser.addAlias(MetadataBuilder.Y_RESOLUTION, "Resolution Y");
191: parser.addAlias(MetadataBuilder.WIDTH, "Width");
192: parser.addAlias(MetadataBuilder.HEIGHT, "Height");
193:
194: final GridCoverage coverage = GridCoverageExamples
195: .getExample(0);
196: parser.add(coverage);
197: if (out != null) {
198: out.println(parser);
199: out.flush();
200: }
201: assertEquals(35.0, parser
202: .getAsDouble(MetadataBuilder.X_MINIMUM), 1E-8);
203: assertEquals(5.0,
204: parser.getAsDouble(MetadataBuilder.Y_MAXIMUM), 1E-8);
205: assertEquals(0.1, parser
206: .getAsDouble(MetadataBuilder.X_RESOLUTION), 1E-8);
207: assertEquals(0.1, parser
208: .getAsDouble(MetadataBuilder.Y_RESOLUTION), 1E-8);
209: assertEquals(450, parser.getAsInt(MetadataBuilder.WIDTH));
210: assertEquals(460, parser.getAsInt(MetadataBuilder.HEIGHT));
211:
212: final GridRange range = parser.getGridRange();
213: assertEquals("Width", 450, range.getLength(0));
214: assertEquals("Height", 460, range.getLength(1));
215:
216: final CoordinateReferenceSystem expectedCRS = coverage
217: .getCoordinateReferenceSystem();
218: final CoordinateReferenceSystem createdCRS = parser
219: .getCoordinateReferenceSystem();
220: assertTrue("The test data changed!",
221: expectedCRS instanceof GeographicCRS);
222: assertTrue("Created wrong CRS type.",
223: createdCRS instanceof GeographicCRS);
224: assertNotSame("Not testing creation.", expectedCRS, createdCRS);
225: assertTrue("Created incompatible CRS.", CRS
226: .equalsIgnoreMetadata(expectedCRS, createdCRS));
227:
228: parser.addAlias(MetadataBuilder.COORDINATE_REFERENCE_SYSTEM,
229: "CRS");
230: parser.add(coverage);
231: assertSame("Should not create CRS anymore.", expectedCRS,
232: parser.getCoordinateReferenceSystem());
233: }
234: }
|