001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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;
009: * version 2.1 of the License.
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: */
017: package org.geotools.gce.image;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.text.ParseException;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.logging.Logger;
028:
029: import junit.textui.TestRunner;
030:
031: import org.geotools.coverage.grid.GridCoverage2D;
032: import org.geotools.test.TestData;
033: import org.opengis.coverage.grid.Format;
034: import org.opengis.parameter.GeneralParameterValue;
035: import org.opengis.parameter.ParameterValueGroup;
036: import org.opengis.referencing.FactoryException;
037: import org.opengis.referencing.operation.TransformException;
038:
039: /**
040: * Test class for WorldImageWriter. This test tries to read, writer and re-read
041: * successive images checking for errors.
042: *
043: * @author Simone Giannecchini
044: * @author rgould
045: * @source $URL:
046: * http://svn.geotools.org/geotools/trunk/gt/modules/plugin/image/src/test/java/org/geotools/gce/image/WorldImageWriterTest.java $
047: */
048: public class WorldImageWriterTest extends WorldImageBaseTestCase {
049: private final static String[] supportedFormat = new String[] {
050: "tiff", "gif", "png", "bmp", "jpeg" };
051:
052: private Logger logger = org.geotools.util.logging.Logging
053: .getLogger(WorldImageWriterTest.class.toString());
054:
055: /** The format for the image e will write. */
056: private String format;
057:
058: public WorldImageWriterTest(String name) {
059: super (name);
060: }
061:
062: protected void setUp() throws Exception {
063: super .setUp();
064: File testData = TestData.file(this , ".");
065: new File(testData, "write").mkdir();
066: }
067:
068: /**
069: * This method simply read all the respecting a predefined pattern inside
070: * the testData directory and then it tries to read, write and re-read them
071: * back. All the possible errors are caught.
072: *
073: * @throws MalformedURLException
074: * @throws IOException
075: * @throws IllegalArgumentException
076: * @throws FactoryException
077: * @throws TransformException
078: * @throws ParseException
079: * @throws ParseException
080: */
081: public void testWrite() throws MalformedURLException, IOException,
082: IllegalArgumentException, FactoryException,
083: TransformException, ParseException {
084:
085: // checking test data directory for all kind of inputs
086: final File test_data_dir = TestData.file(this , null);
087: final String[] fileList = test_data_dir
088: .list(new MyFileFilter());
089: final int length = fileList.length;
090: final int numSupportedFormat = supportedFormat.length;
091: for (int j = 0; j < numSupportedFormat; j++) {
092: format = supportedFormat[j];
093: final StringBuffer buff = new StringBuffer("Format is ")
094: .append(format).append("\n");
095: for (int i = 0; i < length; i++) {
096: buff.append(" file is ").append(fileList[i]).append(
097: "\n");
098: // url
099: final URL url = TestData.getResource(this , fileList[i]);
100: assertTrue(url != null);
101: this .write(url);
102:
103: // getting file
104: final File file = TestData.file(this , fileList[i]);
105: assertTrue(file != null);
106: // starting write test
107: this .write(file);
108:
109: }
110: logger.info(buff.toString());
111: }
112:
113: }
114:
115: /**
116: * This method is responsible for loading the provided source object as a
117: * cverage then for writing it on the temp directoy and finally for
118: * rereading the coverage back into memory in order to display it.
119: *
120: * @param source
121: * Object The object on disk representing the coverage to test.
122: *
123: * @throws IOException
124: * @throws IllegalArgumentException
125: * @throws FactoryException
126: * @throws TransformException
127: * @throws ParseException
128: */
129: private void write(Object source) throws IOException,
130: IllegalArgumentException, FactoryException,
131: TransformException, ParseException {
132: // instantiating a reader
133: WorldImageReader wiReader = new WorldImageReader(source);
134:
135: // reading the original coverage
136: GridCoverage2D coverage = (GridCoverage2D) wiReader.read(null);
137:
138: assertNotNull(coverage);
139: assertNotNull(coverage.getRenderedImage());
140: assertNotNull(coverage.getEnvelope());
141:
142: // remember to provide a valid name, it wil be mde unique by the helper
143: // function temp
144: final StringBuffer buff = new StringBuffer("./write/temp")
145: .append(".").append(format);
146: final File tempFile = TestData.temp(this , buff.toString());
147:
148: // getting a writer
149: final WorldImageWriter wiWriter = new WorldImageWriter(tempFile);
150:
151: // writing parameters for png
152: final Format writerFormat = wiWriter.getFormat();
153: writerFormat.getWriteParameters().parameter("Format").setValue(
154: format);
155:
156: // setting write parameters
157: ParameterValueGroup params = wiWriter.getFormat()
158: .getWriteParameters();
159: params.parameter(WorldImageFormat.FORMAT.getName().toString())
160: .setValue(format);
161: GeneralParameterValue[] gpv = { params
162: .parameter(WorldImageFormat.FORMAT.getName().toString()) };
163: // writing
164: wiWriter.write(coverage, gpv);
165: wiWriter.dispose();
166:
167: // reading again
168: assertTrue(tempFile.exists());
169: wiReader = new WorldImageReader(tempFile);
170: coverage = (GridCoverage2D) wiReader.read(null);
171:
172: // displaying the coverage
173: if (TestData.isInteractiveTest())
174: coverage.show();
175: else
176: coverage.getRenderedImage().getData();
177: wiReader.dispose();
178: coverage.dispose();
179: }
180:
181: /**
182: * TestRunner for testing inside a java application. It gives us the ability
183: * to keep windows open to inspect what happened.
184: *
185: * @param args
186: */
187: public static void main(String[] args) {
188: TestRunner.run(WorldImageWriterTest.class);
189: }
190: }
|