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;
018:
019: import java.awt.image.IndexColorModel;
020: import java.io.IOException;
021: import java.util.Arrays;
022: import java.util.List;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: /**
029: * Tests {@link Palette} and {@link PaletteFactory}.
030: *
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/test/java/org/geotools/image/io/PaletteTest.java $
032: * @version $Id: PaletteTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
033: * @author Martin Desruisseaux
034: */
035: public class PaletteTest extends TestCase {
036: /**
037: * Run the suit from the command line.
038: */
039: public static void main(final String[] args) {
040: org.geotools.util.logging.Logging.GEOTOOLS
041: .forceMonolineConsoleOutput();
042: junit.textui.TestRunner.run(suite());
043: }
044:
045: /**
046: * Returns the test suite.
047: */
048: public static Test suite() {
049: return new TestSuite(PaletteTest.class);
050: }
051:
052: /**
053: * Constructs a test case with the given name.
054: */
055: public PaletteTest(final String name) {
056: super (name);
057: }
058:
059: /**
060: * Tests the argument check performed by constructor.
061: */
062: public void testConstructor() {
063: final PaletteFactory factory = PaletteFactory.getDefault();
064: assertEquals(100, new IndexedPalette(factory, "grayscale", 0,
065: 100, 100, 1, 0).upper);
066: assertEquals(50, new IndexedPalette(factory, "grayscale", -100,
067: 50, 100, 1, 0).upper);
068: try {
069: new IndexedPalette(factory, "grayscale", 0, 100, -100, 1, 0);
070: fail("Should not accept negative size.");
071: } catch (IllegalArgumentException e) {
072: // This is the expected exception.
073: }
074: try {
075: new IndexedPalette(factory, "grayscale", 100, 50, 256, 1, 0);
076: fail("Should not accept invalid range.");
077: } catch (IllegalArgumentException e) {
078: // This is the expected exception.
079: }
080: assertEquals(40000, new IndexedPalette(factory, "grayscale", 1,
081: 40000, 0xFFFF, 1, 0).upper);
082: try {
083: new IndexedPalette(factory, "grayscale", -1, 40000, 0xFFFF,
084: 1, 0);
085: fail("Should not accept value out of range.");
086: } catch (IllegalArgumentException e) {
087: // This is the expected exception.
088: }
089: try {
090: new IndexedPalette(factory, "grayscale", 1, 70000, 0xFFFF,
091: 1, 0);
092: fail("Should not accept value out of range.");
093: } catch (IllegalArgumentException e) {
094: // This is the expected exception.
095: }
096: try {
097: new IndexedPalette(factory, "grayscale", -40000, 0, 0xFFFF,
098: 1, 0);
099: fail("Should not accept value out of range.");
100: } catch (IllegalArgumentException e) {
101: // This is the expected exception.
102: }
103: }
104:
105: /**
106: * Tests {@link PaletteFactory#getAvailableNames}.
107: */
108: public void testAvailableNames() {
109: final List names = Arrays.asList(PaletteFactory.getDefault()
110: .getAvailableNames());
111: assertFalse(names.isEmpty());
112: assertTrue(names.contains("rainbow"));
113: assertTrue(names.contains("grayscale"));
114: assertTrue(names.contains("bell"));
115: assertFalse(names.contains("Donald Duck"));
116: }
117:
118: /**
119: * Tests the cache.
120: */
121: public void testCache() {
122: final PaletteFactory factory = PaletteFactory.getDefault();
123: final Palette first = factory.getPalettePadValueFirst(
124: "rainbow", 100);
125: final Palette second = factory.getPalettePadValueFirst("bell",
126: 100);
127: final Palette third = factory.getPalettePadValueFirst(
128: "rainbow", 100);
129: assertEquals(first, third);
130: assertSame(first, third);
131: assertNotSame(first, second);
132: }
133:
134: /**
135: * Tests the color model.
136: */
137: public void testColorModel() throws IOException {
138: final PaletteFactory factory = PaletteFactory.getDefault();
139: final Palette palette = factory.getPalettePadValueFirst(
140: "rainbow", 100);
141: final IndexColorModel icm = (IndexColorModel) palette
142: .getColorModel();
143: assertEquals(100, icm.getMapSize());
144: assertEquals(0, icm.getTransparentPixel());
145: }
146: }
|