001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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.measure;
018:
019: // J2SE dependencies
020: import java.text.Format;
021: import java.text.ParseException;
022: import java.util.Date;
023: import java.util.Locale;
024: import java.util.TimeZone;
025:
026: // JUnit dependencies
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030: import junit.textui.TestRunner;
031:
032: // OpenGIS dependencies
033: import org.opengis.geometry.MismatchedDimensionException;
034:
035: // Geotools dependencies
036: import org.geotools.geometry.GeneralDirectPosition;
037: import org.geotools.referencing.crs.DefaultCompoundCRS;
038: import org.geotools.referencing.crs.AbstractCRS;
039: import org.geotools.referencing.crs.DefaultGeographicCRS;
040: import org.geotools.referencing.crs.DefaultTemporalCRS;
041: import org.geotools.referencing.crs.DefaultVerticalCRS;
042: import org.geotools.referencing.cs.DefaultTimeCS;
043: import org.geotools.referencing.datum.DefaultTemporalDatum;
044: import org.geotools.resources.Arguments;
045:
046: /**
047: * Tests formatting done by the {@link CoordinateFormat} class.
048: *
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/measure/FormatTest.java $
050: * @version $Id: FormatTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
051: * @author Martin Desruisseaux
052: */
053: public final class FormatTest extends TestCase {
054: /**
055: * Run the suite from the command line.
056: */
057: public static void main(String[] args) {
058: org.geotools.util.logging.Logging.GEOTOOLS
059: .forceMonolineConsoleOutput();
060: final Arguments arguments = new Arguments(args);
061: args = arguments.getRemainingArguments(0);
062: Locale.setDefault(arguments.locale);
063: TestRunner.run(suite());
064: }
065:
066: /**
067: * Returns the test suite.
068: */
069: public static Test suite() {
070: return new TestSuite(FormatTest.class);
071: }
072:
073: /**
074: * Constructs a test case with the given name.
075: */
076: public FormatTest(final String name) {
077: super (name);
078: }
079:
080: /**
081: * Test {@link AngleFormat}.
082: */
083: public void testAngleFormat() throws ParseException {
084: AngleFormat f = new AngleFormat("DD.ddd\u00B0", Locale.CANADA);
085: assertFormat("20.000\u00B0", new Angle(20.000), f);
086: assertFormat("20.749\u00B0", new Angle(20.749), f);
087: assertFormat("-12.247\u00B0", new Angle(-12.247), f);
088: assertFormat("13.214\u00B0N", new Latitude(13.214), f);
089: assertFormat("12.782\u00B0S", new Latitude(-12.782), f);
090:
091: f = new AngleFormat("DD.ddd\u00B0", Locale.FRANCE);
092: assertFormat("19,457\u00B0E", new Longitude(19.457), f);
093: assertFormat("78,124\u00B0S", new Latitude(-78.124), f);
094:
095: f = new AngleFormat("DDddd", Locale.CANADA);
096: assertFormat("19457E", new Longitude(19.457), f);
097: assertFormat("78124S", new Latitude(-78.124), f);
098:
099: f = new AngleFormat("DD\u00B0MM.m", Locale.CANADA);
100: assertFormat("12\u00B030.0", new Angle(12.50), f);
101: assertFormat("-10\u00B015.0", new Angle(-10.25), f);
102: }
103:
104: /**
105: * Formats an object and parse the result. The format
106: * output is compared with the expected output.
107: */
108: private static void assertFormat(final String expected,
109: final Object value, final Format format)
110: throws ParseException {
111: final String label = value.toString();
112: final String text = format.format(value);
113: assertEquals("Formatting of \"" + label + '"', expected, text);
114: assertEquals("Parsing of \"" + label + '"', value, format
115: .parseObject(text));
116: }
117:
118: /**
119: * Tests formatting of a 4-dimensional coordinates.
120: */
121: public void testCoordinateFormat() {
122: final Date epoch = new Date(1041375600000L); // January 1st, 2003
123: final DefaultTemporalDatum datum = new DefaultTemporalDatum(
124: "Time", epoch);
125: final AbstractCRS crs = new DefaultCompoundCRS(
126: "WGS84 3D + time", DefaultGeographicCRS.WGS84,
127: DefaultVerticalCRS.ELLIPSOIDAL_HEIGHT,
128: new DefaultTemporalCRS("Time", datum,
129: DefaultTimeCS.DAYS));
130: final CoordinateFormat format = new CoordinateFormat(
131: Locale.FRANCE);
132: format.setCoordinateReferenceSystem(crs);
133: format.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
134: final GeneralDirectPosition position = new GeneralDirectPosition(
135: new double[] { 23.78, -12.74, 127.9, 3.2 });
136: assertEquals("23°46,8'E 12°44,4'S 127,9\u00A0m 4 janv. 2003",
137: format.format(position));
138: /*
139: * Try a point with wrong dimension.
140: */
141: final GeneralDirectPosition wrong = new GeneralDirectPosition(
142: new double[] { 23.78, -12.74, 127.9, 3.2, 8.5 });
143: try {
144: assertNotNull(format.format(wrong));
145: fail("Excepted a mismatched dimension exception.");
146: } catch (MismatchedDimensionException e) {
147: // This is the expected dimension.
148: }
149: /*
150: * Try a null CRS. Should formats everything as numbers.
151: */
152: format.setCoordinateReferenceSystem(null);
153: assertEquals("23,78 -12,74 127,9 3,2", format.format(position));
154: assertEquals("23,78 -12,74 127,9 3,2 8,5", format.format(wrong));
155: /*
156: * Try again the original CRS, but different separator.
157: */
158: format.setCoordinateReferenceSystem(crs);
159: format.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
160: format.setSeparator("; ");
161: assertEquals(
162: "23°46,8'E; 12°44,4'S; 127,9\u00A0m; 4 janv. 2003",
163: format.format(position));
164: }
165: }
|