001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
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.image.io.text;
018:
019: import java.io.StringWriter;
020: import java.util.Locale;
021: import java.io.IOException;
022: import java.util.StringTokenizer;
023: import javax.imageio.IIOImage;
024: import java.awt.image.BufferedImage;
025: import java.awt.image.ColorModel;
026: import java.awt.image.DataBuffer;
027: import java.awt.image.WritableRaster;
028: import java.text.FieldPosition;
029: import java.text.NumberFormat;
030:
031: import org.geotools.image.io.PaletteFactory;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: /**
038: * Tests {@link TextMatrixImageWriter}.
039: *
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/test/java/org/geotools/image/io/text/TextMatrixImageWriterTest.java $
041: * @version $Id: TextMatrixImageWriterTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
042: * @author Martin Desruisseaux
043: */
044: public class TextMatrixImageWriterTest extends TestCase {
045: /**
046: * The image to test.
047: */
048: private static IIOImage image;
049:
050: /**
051: * The writer to test.
052: */
053: private static TextMatrixImageWriter writer;
054:
055: /**
056: * Run the suite from the command line.
057: */
058: public static void main(final String[] args) {
059: org.geotools.util.logging.Logging.GEOTOOLS
060: .forceMonolineConsoleOutput();
061: junit.textui.TestRunner.run(suite());
062: }
063:
064: /**
065: * Returns the test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(TextMatrixImageWriterTest.class);
069: }
070:
071: /**
072: * Constructs a test case with the given name.
073: */
074: public TextMatrixImageWriterTest(final String name) {
075: super (name);
076: }
077:
078: /**
079: * Creates the image to test.
080: */
081: protected void setUp() throws IOException {
082: if (image != null) {
083: return;
084: }
085: final int width = 8;
086: final int height = 10;
087: final ColorModel cm = PaletteFactory.getDefault()
088: .getContinuousPalette("grayscale", 0f, 1f,
089: DataBuffer.TYPE_FLOAT, 1, 0).getColorModel();
090: final WritableRaster raster = cm
091: .createCompatibleWritableRaster(width, height);
092: for (int y = 0; y < height; y++) {
093: for (int x = 0; x < width; x++) {
094: double value = (10 * y + x) / 100.0;
095: if (y >= 5) {
096: value += 88;
097: }
098: raster.setSample(x, y, 0, value);
099: }
100: }
101: image = new IIOImage(
102: new BufferedImage(cm, raster, false, null), null, null);
103: TextMatrixImageWriter.Spi spi = new TextMatrixImageWriter.Spi();
104: spi.locale = Locale.CANADA;
105: writer = new TextMatrixImageWriter(spi);
106: }
107:
108: /**
109: * Tests the number format.
110: */
111: public void testCreateNumberFormat() {
112: assertEquals(Locale.CANADA, writer.getDataLocale(null));
113:
114: final NumberFormat format = writer.createNumberFormat(image,
115: null);
116: assertEquals(2, format.getMinimumFractionDigits());
117: assertEquals(2, format.getMaximumFractionDigits());
118: assertEquals(1, format.getMinimumIntegerDigits());
119: assertEquals("0.12", format.format(0.1216));
120: assertEquals("-0.30", format.format(-0.2978));
121:
122: final FieldPosition pos = writer
123: .getExpectedFractionPosition(format);
124: assertEquals("Field type", NumberFormat.FRACTION_FIELD, pos
125: .getField());
126: assertEquals("Fraction width", 2, pos.getEndIndex()
127: - pos.getBeginIndex());
128: assertEquals("Total width (including sign)", 6, pos
129: .getEndIndex());
130: }
131:
132: /**
133: * Tests the write operation.
134: */
135: public void testWrite() throws IOException {
136: final StringWriter out = new StringWriter();
137: writer.setOutput(out);
138: writer.write(image);
139: out.close();
140: final StringTokenizer tokens = new StringTokenizer(out
141: .toString());
142: while (tokens.hasMoreTokens()) {
143: final String t = tokens.nextToken();
144: final int dot = t.indexOf('.');
145: assertTrue(dot >= 0);
146: assertEquals(3, t.length() - dot);
147: final float value = Float.parseFloat(t);
148: assertTrue(value >= 0 && value < 100);
149: }
150: }
151: }
|