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.coverage.grid;
018:
019: // J2SE dependencies
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.util.logging.Logger;
026:
027: // JUnit dependencies
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: // OpenGIS dependencies
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: import org.geotools.referencing.crs.DefaultGeographicCRS;
036: import org.geotools.util.logging.Logging;
037: import org.geotools.resources.Utilities;
038:
039: /**
040: * Tests the {@link GridCoverage2D} implementation.
041: *
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/GridCoverageTest.java $
043: * @version $Id: GridCoverageTest.java 27862 2007-11-12 19:51:19Z desruisseaux $
044: * @author Martin Desruisseaux
045: */
046: public class GridCoverageTest extends TestCase {
047: /**
048: * Run the suite from the command line.
049: */
050: public static void main(final String[] args) {
051: junit.textui.TestRunner.run(suite());
052: }
053:
054: /**
055: * Returns the test suite.
056: */
057: public static Test suite() {
058: return new TestSuite(GridCoverageTest.class);
059: }
060:
061: /**
062: * Constructs a test case with the given name.
063: */
064: public GridCoverageTest(final String name) {
065: super (name);
066: }
067:
068: /**
069: * Tests the construction and access to a grid coverage.
070: *
071: * @throws IOException if an I/O operation was needed and failed.
072: */
073: public void testGridCoverage() throws IOException {
074: final GridCoverage2D coverage = getRandomCoverage();
075: assertNotNull(coverage);
076: // Not much more test to do here, since most tests has been done
077: // inside 'getRandomCoverage'. This method will be overridden by
078: // 'InterpolatorTest', which will perform more tests.
079: for (int i = GridCoverageExamples.getNumExamples(); --i >= 0;) {
080: assertNotNull(GridCoverageExamples.getExample(i));
081: }
082: }
083:
084: /**
085: * Tests the serialization of a grid coverage.
086: *
087: * @throws IOException if an I/O operation was needed and failed.
088: */
089: public void testSerialization() throws IOException,
090: ClassNotFoundException {
091: final GridCoverage2D coverage = getRandomCoverage();
092: assertNotNull(coverage);
093: coverage.tileEncoding = null;
094: /*
095: * The previous line is not something that we should do.
096: * But we want to test the default GridCoverage2D encoding.
097: */
098: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
099: final ObjectOutputStream out = new ObjectOutputStream(buffer);
100: try {
101: out.writeObject(coverage.geophysics(false));
102: // out.writeObject(coverage.geophysics(true ));
103: } catch (IllegalArgumentException e) {
104: /*
105: * TODO: this exception occurs when ImageLayout contains a SampleModel or a ColorModel
106: * unknow to javax.media.jai.remote.SerializerFactory getState(...) method. This
107: * happen if an operation we applied on the coverage in some subclass (especially
108: * OperationsTest). Ignore the exception for now, but we need to revisit this
109: * issue later.
110: */
111: if (getClass().equals(GridCoverageTest.class)) {
112: e.printStackTrace();
113: }
114: out.close();
115: return;
116: }
117: out.close();
118: /*
119: * Deserialization requires J2SE 1.5 or above.
120: */
121: if (System.getProperty("java.version").compareTo("1.5") >= 0) {
122: final ObjectInputStream in = new ObjectInputStream(
123: new ByteArrayInputStream(buffer.toByteArray()));
124: GridCoverage2D read;
125: read = (GridCoverage2D) in.readObject();
126: assertSame(read, read.geophysics(false));
127: // read = (GridCoverage2D) in.readObject(); assertSame(read, read.geophysics(true ));
128: // assertNotSame(read, read.geophysics(true));
129: in.close();
130: } else {
131: Logging
132: .getLogger("org.geotools.coverage.grid")
133: .fine(
134: "Deserialization test skipped for pre-1.5 Java version.");
135: }
136: }
137:
138: /**
139: * Applies an operation on the specified coverage, if wanted. The
140: * default implementation returns {@code coverage} with no change.
141: */
142: protected GridCoverage2D transform(final GridCoverage2D coverage) {
143: return coverage;
144: }
145:
146: /**
147: * Returns a grid coverage filled with random values. The coordinate
148: * reference system default to {@link DefaultGeographicCRS#WGS84}.
149: *
150: * @return A random coverage.
151: */
152: protected GridCoverage2D getRandomCoverage() {
153: return getRandomCoverage(DefaultGeographicCRS.WGS84);
154: }
155:
156: /**
157: * Returns a grid coverage filled with random values.
158: *
159: * @param crs The coverage coordinate reference system.
160: * @return A random coverage.
161: */
162: protected GridCoverage2D getRandomCoverage(
163: final CoordinateReferenceSystem crs) {
164: final GridCoverage2D original = GridCoverageExamples
165: .getRandomCoverage(crs);
166: final GridCoverage2D coverage = transform(original);
167: /*
168: * Grid coverage construction finished. Now test it. Some tests will not be applicable
169: * if a subclass overridden the 'transform' method are returned a transformed coverage.
170: * We detect this case when 'coverage != original'.
171: */
172: assertSame(coverage.getRenderedImage(), coverage
173: .getRenderableImage(0, 1).createDefaultRendering());
174: if (!coverage.getCoordinateReferenceSystem().equals(crs)) {
175: assertEquals("Resampler2D", Utilities
176: .getShortClassName(coverage));
177: }
178: /*
179: * Tests the creation of a "geophysics" view. This test make sure that the
180: * 'geophysics' method do not creates more grid coverage than needed.
181: */
182: GridCoverage2D geophysics = coverage.geophysics(true);
183: assertSame(coverage, coverage.geophysics(false));
184: assertSame(coverage, geophysics.geophysics(false));
185: assertSame(geophysics, geophysics.geophysics(true));
186: assertFalse(coverage.equals(geophysics));
187: assertFalse(coverage.getSampleDimension(0)
188: .getSampleToGeophysics().isIdentity());
189: assertTrue(geophysics.getSampleDimension(0)
190: .getSampleToGeophysics().isIdentity());
191: return coverage;
192: }
193: }
|