001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, 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.parameter;
018:
019: import java.util.List;
020: import java.util.Arrays;
021: import java.util.ArrayList;
022: import java.util.logging.Logger;
023: import javax.media.jai.JAI;
024: import javax.media.jai.ParameterList;
025: import javax.media.jai.OperationDescriptor;
026: import javax.media.jai.OperationRegistry;
027: import javax.media.jai.RegistryElementDescriptor;
028: import javax.media.jai.registry.RenderedRegistryMode;
029:
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: import org.opengis.util.GenericName;
035: import org.opengis.parameter.ParameterValue;
036: import org.opengis.parameter.ParameterValueGroup;
037: import org.opengis.parameter.ParameterDescriptor;
038: import org.opengis.parameter.GeneralParameterValue;
039: import org.opengis.parameter.GeneralParameterDescriptor;
040:
041: import org.geotools.TestData;
042: import org.geotools.metadata.iso.citation.Citations;
043:
044: /**
045: * Tests the wrapper for JAI's parameters.
046: *
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/parameter/ImagingParametersTest.java $
048: * @version $Id: ImagingParametersTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
049: * @author Martin Desruisseaux
050: * @author Simone Giannecchini
051: */
052: public final class ImagingParametersTest extends TestCase {
053: /**
054: * Run the suite from the command line.
055: */
056: public static void main(final String[] args) {
057: org.geotools.util.logging.Logging.GEOTOOLS
058: .forceMonolineConsoleOutput();
059: junit.textui.TestRunner.run(suite());
060: }
061:
062: /**
063: * Returns the test suite.
064: */
065: public static Test suite() {
066: return new TestSuite(ImagingParametersTest.class);
067: }
068:
069: /**
070: * Constructs a test case.
071: */
072: public ImagingParametersTest(String testName) {
073: super (testName);
074: }
075:
076: /**
077: * Tests {@link ImagingParameters}.
078: */
079: public void testDescriptors() {
080: final String author = Citations.JAI.getTitle().toString();
081: final String vendor = "com.sun.media.jai";
082: final String mode = RenderedRegistryMode.MODE_NAME;
083: final RegistryElementDescriptor descriptor;
084: final ImagingParameterDescriptors parameters;
085: descriptor = JAI.getDefaultInstance().getOperationRegistry()
086: .getDescriptor(mode, "AddConst");
087: parameters = new ImagingParameterDescriptors(descriptor);
088: final GenericName alias = (GenericName) parameters.getAlias()
089: .iterator().next();
090: /*
091: * Tests the operation-wide properties.
092: */
093: assertEquals("Name", "AddConst", parameters.getName().getCode());
094: assertEquals("Authority", author, parameters.getName()
095: .getAuthority().getTitle().toString());
096: assertEquals("Vendor", vendor, alias.getScope().toString());
097: assertNotNull("Version", parameters.getName().getVersion());
098: assertLocalized("Vendor", alias.getScope()
099: .toInternationalString());
100: assertLocalized("Remarks", parameters.getRemarks());
101: assertTrue("Remarks", parameters.getRemarks().toString().trim()
102: .length() > 0);
103: /*
104: * Tests the properties for a specific parameter in the parameter group.
105: */
106: final ParameterDescriptor param = (ParameterDescriptor) parameters
107: .descriptor("constants");
108: assertEquals("Name", "constants", param.getName().getCode());
109: assertEquals("Type", double[].class, param.getValueClass());
110: assertEquals("Default", 1,
111: ((double[]) param.getDefaultValue()).length);
112: assertNull("Minimum", param.getMinimumValue());
113: assertNull("Maximum", param.getMaximumValue());
114: assertNull("Valid values", param.getValidValues());
115: assertLocalized("Remarks", param.getRemarks());
116: assertFalse(parameters.getRemarks().toString().trim()
117: .equalsIgnoreCase(param.getRemarks().toString().trim()));
118: /*
119: * Tests parameter values.
120: */
121: final ImagingParameters values = (ImagingParameters) parameters
122: .createValue();
123: for (int i = 0; i < 20; i++) {
124: final ParameterValue before = values.parameter("constants");
125: if ((i % 5) == 0) {
126: values.parameters.setParameter("constants",
127: new double[] { i });
128: } else {
129: values.parameter("constants").setValue(
130: new double[] { i });
131: }
132: assertTrue(Arrays.equals(values.parameter("constants")
133: .doubleValueList(), (double[]) values.parameters
134: .getObjectParameter("constants")));
135: assertSame(before, values.parameter("constants"));
136: }
137: assertNotNull(values.toString());
138: /*
139: * Tests clone. Requires J2SE 1.5 or above.
140: */
141: if (!TestData.isBaseJavaPlatform()) {
142: final ImagingParameters copy = (ImagingParameters) values
143: .clone();
144: assertNotSame("clone", values, copy);
145: assertNotSame("clone", values.parameters, copy.parameters);
146: if (false) {
147: // NOTE: As of J2SE 1.5 and JAI 1.1, ParameterBlockJAI
148: // doesn't implements the 'equals' method.
149: assertEquals("clone", values.parameters,
150: copy.parameters);
151: assertEquals("clone", values, copy);
152: }
153: }
154: }
155:
156: /**
157: * Ensures that the specified character sequence created from JAI parameters preserve the
158: * localization infos.
159: */
160: private static void assertLocalized(final String name,
161: final CharSequence title) {
162: assertTrue(name, title instanceof ImagingParameterDescription);
163: }
164:
165: /**
166: * Tests the wrapper with a parameter overriden.
167: */
168: public void testExtensions() {
169: /*
170: * The parameter descriptor for the subsampling.
171: */
172: final ParameterDescriptor SPATIAL_SUBSAMPLING_X = new DefaultParameterDescriptor(
173: Citations.OGC, "xPeriod", Double.class, // Value class (mandatory)
174: null, // Array of valid values
175: null, // Default value
176: new Double(0), // Minimal value
177: null, // Maximal value
178: null, // Unit of measure
179: false); // Parameter is optional
180:
181: // Gets the descriptors for extrema JAI operation
182: final OperationRegistry registry = JAI.getDefaultInstance()
183: .getOperationRegistry();
184: final OperationDescriptor operation = (OperationDescriptor) registry
185: .getDescriptor(RenderedRegistryMode.MODE_NAME,
186: "Extrema");
187:
188: // Gets the ImagingParameterDescriptors to replace xPeriod
189: final List replacingDescriptors = new ArrayList(1);
190: replacingDescriptors.add(SPATIAL_SUBSAMPLING_X);
191: final ImagingParameterDescriptors ripd = new ImagingParameterDescriptors(
192: operation, replacingDescriptors);
193:
194: // Sets the parameter we want to override
195: final ParameterValueGroup rip = (ParameterValueGroup) ripd
196: .createValue();
197: assertSame(ripd, rip.getDescriptor());
198: final ParameterValue p = rip.parameter("xPeriod");
199: assertSame(SPATIAL_SUBSAMPLING_X, p.getDescriptor());
200:
201: // Note that we are supposed to use spatial coordinates for this value we are seeting here.
202: p.setValue(new Double(2.3));
203: assertTrue(p.toString().startsWith("xPeriod = 2.3"));
204:
205: // Tests direct access to the parameter list.
206: final ParameterList pl = ((ImagingParameters) rip).parameters;
207: assertSame(pl, pl.setParameter("xPeriod", 2));
208: assertSame(pl, pl.setParameter("yPeriod", 2));
209: assertEquals(2, pl.getIntParameter("xPeriod"));
210: assertEquals(2, pl.getIntParameter("yPeriod"));
211: assertEquals(
212: "Setting 'xPeriod' on ParameterList should have no effect on ParameterValue.",
213: 2.3, p.doubleValue(), 1E-6);
214: assertEquals(
215: "'yPeriod' should still backed by the ParameterList.",
216: 2, rip.parameter("yPeriod").intValue());
217: }
218: }
|