001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, 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: // J2SE dependencies and extensions
020: import java.awt.geom.AffineTransform;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.StringWriter;
027: import java.util.Arrays;
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.HashSet;
031: import java.util.Map;
032: import java.util.Random;
033: import java.util.Set;
034: import javax.units.NonSI;
035: import javax.units.SI;
036: import javax.units.Unit;
037:
038: // JUnit dependencies
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: // OpenGIS dependencies
044: import org.opengis.parameter.InvalidParameterCardinalityException;
045: import org.opengis.parameter.InvalidParameterNameException;
046: import org.opengis.parameter.InvalidParameterTypeException;
047: import org.opengis.parameter.InvalidParameterValueException;
048: import org.opengis.parameter.ParameterNotFoundException;
049: import org.opengis.parameter.ParameterDescriptorGroup;
050: import org.opengis.parameter.ParameterDescriptor;
051: import org.opengis.referencing.cs.AxisDirection;
052: import org.opengis.referencing.datum.VerticalDatumType;
053: import org.opengis.referencing.operation.MathTransform;
054:
055: // Geotools dependencies
056: import org.geotools.referencing.operation.transform.ProjectiveTransform;
057: import org.geotools.referencing.operation.matrix.GeneralMatrix;
058: import org.geotools.referencing.wkt.Formatter;
059:
060: /**
061: * Tests the <code>org.geotools.parameter</code> package.
062: *
063: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/parameter/ParametersTest.java $
064: * @version $Id: ParametersTest.java 28264 2007-12-05 21:53:08Z desruisseaux $
065: * @author Martin Desruisseaux
066: */
067: public final class ParametersTest extends TestCase {
068: /**
069: * Run the suite from the command line.
070: */
071: public static void main(String[] args) {
072: org.geotools.util.logging.Logging.GEOTOOLS
073: .forceMonolineConsoleOutput();
074: junit.textui.TestRunner.run(suite());
075: }
076:
077: /**
078: * Returns the test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(ParametersTest.class);
082: }
083:
084: /**
085: * Constructs a test case.
086: */
087: public ParametersTest(String testName) {
088: super (testName);
089: }
090:
091: /**
092: * Tests integer and floating point values in a wide range of values. Some on those
093: * values are cached (e.g. 0, 90, 360) because frequently used. It should be transparent
094: * to the user. Test also unit conversions (degrees to radians in this case).
095: */
096: public void testSequence() {
097: for (int i = -1000; i <= 1000; i++) {
098: assertEquals("new (Integer, ...)", i, new Parameter(
099: "Integer", i).intValue());
100: assertEquals("new (Double, ...)", i, new Parameter(
101: "Double", i, null).doubleValue(), 0.0);
102: assertEquals("new (Double, ...)", i, new Parameter(
103: "Double", i, Unit.ONE).doubleValue(), 0.0);
104: assertEquals("new (Double, ...)", Math.toRadians(i),
105: new Parameter("Double", i, NonSI.DEGREE_ANGLE)
106: .doubleValue(SI.RADIAN), 1E-6);
107: }
108: }
109:
110: /**
111: * Creates a parameter bounded by some range of integer numbers, and tests values
112: * inside and outside this range. Tests also the uses of values of the wrong type.
113: */
114: public void testRangeIntegers() {
115: Parameter param;
116: param = new Parameter(new DefaultParameterDescriptor("Range",
117: 15, -30, +40));
118: assertEquals("intValue", 15, param.intValue());
119: assertEquals("doubleValue", 15, param.doubleValue(), 0.0);
120: param.setValue(12);
121: assertEquals("intValue", 12, param.intValue());
122: assertEquals("doubleValue", 12, param.doubleValue(), 0.0);
123: try {
124: param.setValue(50);
125: fail("setValue(> max)");
126: } catch (InvalidParameterValueException exception) {
127: // This is the expected exception.
128: assertEquals("Range", exception.getParameterName());
129: }
130: try {
131: param.setValue(-40);
132: fail("setValue(< min)");
133: } catch (InvalidParameterValueException exception) {
134: // This is the expected exception.
135: assertEquals("Range", exception.getParameterName());
136: }
137: try {
138: param.setValue(10.0);
139: fail("setValue(double)");
140: } catch (InvalidParameterValueException exception) {
141: // This is the expected exception.
142: assertEquals("Range", exception.getParameterName());
143: }
144: assertEquals("Clone not equals: ", param, param.clone());
145: }
146:
147: /**
148: * Creates a parameter bounded by some range of floating point numbers, and tests values
149: * inside and outside this range. Tests also the uses of values of the wrong types.
150: */
151: public void testRangeDoubles() {
152: Parameter param;
153: param = new Parameter(new DefaultParameterDescriptor("Range",
154: 15.0, -30.0, +40.0, null));
155: assertEquals("intValue", 15, param.intValue());
156: assertEquals("doubleValue", 15, param.doubleValue(), 0.0);
157: param.setValue(12.0);
158: assertEquals("intValue", 12, param.intValue());
159: assertEquals("doubleValue", 12, param.doubleValue(), 0.0);
160: try {
161: param.setValue(50.0);
162: fail("setValue(> max)");
163: } catch (InvalidParameterValueException exception) {
164: // This is the expected exception.
165: assertEquals("Range", exception.getParameterName());
166: }
167: try {
168: param.setValue(-40.0);
169: fail("setValue(< min)");
170: } catch (InvalidParameterValueException exception) {
171: // This is the expected exception.
172: assertEquals("Range", exception.getParameterName());
173: }
174: try {
175: param.setValue("12");
176: fail("setValue(String)");
177: } catch (InvalidParameterValueException exception) {
178: // This is the expected exception.
179: assertEquals("Range", exception.getParameterName());
180: }
181: assertEquals("equals(clone)", param, param.clone());
182: }
183:
184: /**
185: * Tests parameter for a code list. Try to inserts invalid values. Try also to insert a
186: * new code list. This operation should fails if the new code list is created after the
187: * parameter.
188: */
189: public void testCodeList() {
190: Parameter param = new Parameter("Test",
191: AxisDirection.DISPLAY_UP);
192: ParameterDescriptor op = (ParameterDescriptor) param
193: .getDescriptor();
194: assertEquals("Set<AxisDirection>", new HashSet(Arrays
195: .asList(AxisDirection.values())), op.getValidValues());
196: assertNull("defaultValue", op.getDefaultValue());
197: param.setValue(AxisDirection.DOWN);
198: try {
199: param.setValue(VerticalDatumType.ELLIPSOIDAL);
200: fail("setValue(VerticalDatumType)");
201: } catch (InvalidParameterValueException exception) {
202: // This is the expected exception.
203: assertEquals("Test", exception.getParameterName());
204: }
205: AxisDirection dummy = AxisDirection.valueOf("Dummy");
206: try {
207: param.setValue(dummy);
208: fail("setValue(AxisDirection)");
209: } catch (InvalidParameterValueException exception) {
210: // This is the expected exception.
211: assertEquals("Test", exception.getParameterName());
212: }
213: param = new Parameter("Test", AxisDirection.DISPLAY_UP);
214: param.setValue(dummy); // Should not fails.
215: assertEquals("equals(clone)", param, param.clone());
216: }
217:
218: /**
219: * Test {@link DefaultParameterDescriptor} construction.
220: */
221: public void testParameterDescriptor() {
222: ParameterDescriptor descriptor;
223: Parameter parameter;
224:
225: descriptor = new DefaultParameterDescriptor("Test", 12, 4, 20,
226: SI.METER);
227: parameter = (Parameter) descriptor.createValue();
228: assertEquals("name", "Test", descriptor.getName().getCode());
229: assertEquals("unit", SI.METER, descriptor.getUnit());
230: assertEquals("class", Double.class, descriptor.getValueClass());
231: assertEquals("defaultValue", new Double(12), descriptor
232: .getDefaultValue());
233: assertEquals("minimum", new Double(4), descriptor
234: .getMinimumValue());
235: assertEquals("maximum", new Double(20), descriptor
236: .getMaximumValue());
237: assertEquals("value", 12, parameter.intValue());
238: assertEquals("unit", SI.METER, parameter.getUnit());
239: for (int i = 4; i <= 20; i++) {
240: parameter.setValue(i);
241: assertEquals("value", new Double(i), parameter.getValue());
242: assertEquals("unit", SI.METER, parameter.getUnit());
243: assertEquals("value", i, parameter.doubleValue(SI.METER), 0);
244: }
245: try {
246: parameter.setValue(3.0);
247: fail("setValue(< min)");
248: } catch (InvalidParameterValueException exception) {
249: // This is the expected exception.
250: assertEquals("Test", exception.getParameterName());
251: }
252: try {
253: parameter.setValue("12");
254: fail("setValue(Sring)");
255: } catch (InvalidParameterValueException exception) {
256: // This is the expected exception.
257: assertEquals("Test", exception.getParameterName());
258: }
259: for (int i = 400; i <= 2000; i += 100) {
260: parameter.setValue(i, SI.CENTI(SI.METER));
261: assertEquals("value", new Double(i), parameter.getValue());
262: assertEquals("unit", SI.CENTI(SI.METER), parameter
263: .getUnit());
264: assertEquals("value", i / 100, parameter
265: .doubleValue(SI.METER), 0);
266: }
267: try {
268: descriptor = new DefaultParameterDescriptor("Test", 3, 4,
269: 20);
270: fail("setValue(< min)");
271: } catch (InvalidParameterValueException exception) {
272: // This is the expected exception.
273: assertEquals("Test", exception.getParameterName());
274: }
275: try {
276: descriptor = new DefaultParameterDescriptor("Test", 12, 20,
277: 4);
278: fail("ParameterDescriptor(min > max)");
279: } catch (IllegalArgumentException exception) {
280: // This is the expected exception.
281: }
282: }
283:
284: /**
285: * Test {@link Parameter} construction.
286: */
287: public void testParameterValue() throws IOException,
288: ClassNotFoundException {
289: Parameter parameter;
290: ParameterDescriptor descriptor;
291: Set validValues;
292:
293: parameter = new Parameter("Test", 14);
294: descriptor = (ParameterDescriptor) parameter.getDescriptor();
295: assertNull("unit", parameter.getUnit());
296: assertEquals("intValue", 14, parameter.intValue());
297: assertEquals("doubleValue", 14, parameter.doubleValue(), 0);
298: assertEquals("type", Integer.class, descriptor.getValueClass());
299: assertEquals("name", "Test", descriptor.getName().getCode());
300: assertEquals("defaultValue", new Integer(0), descriptor
301: .getDefaultValue());
302: assertNull("minimum", descriptor.getMinimumValue());
303: assertNull("maximum", descriptor.getMaximumValue());
304: assertNull("unit", descriptor.getUnit());
305: assertNull("validValues", descriptor.getValidValues());
306: try {
307: parameter.doubleValue(SI.METER);
308: fail("doubleValue(METER)");
309: } catch (IllegalStateException exception) {
310: // This is the expected exception.
311: }
312: try {
313: parameter.stringValue();
314: fail("stringValue()");
315: } catch (InvalidParameterTypeException exception) {
316: // This is the expected exception.
317: assertEquals("Test", exception.getParameterName());
318: }
319: serialize(parameter);
320:
321: parameter = new Parameter("Test", 3, SI.METER);
322: descriptor = (ParameterDescriptor) parameter.getDescriptor();
323: assertEquals("intValue", 3, parameter.intValue());
324: assertEquals("doubleValue", 3, parameter.doubleValue(), 0);
325: assertEquals("doubleValue", 300, parameter.doubleValue(SI
326: .CENTI(SI.METER)), 0);
327: assertEquals("name", "Test", descriptor.getName().getCode());
328: assertEquals("unit", SI.METER, descriptor.getUnit());
329: assertNull("defaultValue", descriptor.getDefaultValue());
330: assertNull("minimum", descriptor.getMinimumValue());
331: assertNull("maximum", descriptor.getMaximumValue());
332: assertNull("validValues", descriptor.getValidValues());
333: try {
334: parameter.stringValue();
335: fail("stringValue()");
336: } catch (InvalidParameterTypeException exception) {
337: // This is the expected exception.
338: assertEquals("Test", exception.getParameterName());
339: }
340: serialize(parameter);
341:
342: parameter = new Parameter("Test", AxisDirection.NORTH);
343: descriptor = (ParameterDescriptor) parameter.getDescriptor();
344: validValues = descriptor.getValidValues();
345: assertEquals("value", AxisDirection.NORTH, parameter.getValue());
346: assertEquals("name", "Test", descriptor.getName().getCode());
347: assertNull("unit", descriptor.getUnit());
348: assertNull("defaultValue", descriptor.getDefaultValue());
349: assertNull("minimum", descriptor.getMinimumValue());
350: assertNull("maximum", descriptor.getMaximumValue());
351: assertTrue("validValues", validValues
352: .contains(AxisDirection.NORTH));
353: assertTrue("validValues", validValues
354: .contains(AxisDirection.SOUTH));
355: assertTrue("validValues", validValues
356: .contains(AxisDirection.DISPLAY_LEFT));
357: assertTrue("validValues", validValues
358: .contains(AxisDirection.PAST));
359: assertEquals("validValues", new HashSet(Arrays
360: .asList(AxisDirection.values())), validValues);
361: try {
362: parameter.doubleValue();
363: fail("doubleValue should not be allowed on AxisDirection");
364: } catch (InvalidParameterTypeException exception) {
365: // This is the expected exception.
366: assertEquals("Test", exception.getParameterName());
367: }
368: serialize(parameter);
369: }
370:
371: /**
372: * Test parameter values group.
373: */
374: public void testGroup() throws IOException {
375: final ParameterWriter writer = new ParameterWriter(
376: new StringWriter());
377: final Integer ONE = new Integer(1);
378: final ParameterDescriptor p1, p2, p3, p4;
379: p1 = new DefaultParameterDescriptor(Collections.singletonMap(
380: "name", "1"), Integer.class, null, ONE, null, null,
381: null, true);
382: p2 = new DefaultParameterDescriptor(Collections.singletonMap(
383: "name", "2"), Integer.class, null, ONE, null, null,
384: null, true);
385: p3 = new DefaultParameterDescriptor(Collections.singletonMap(
386: "name", "3"), Integer.class, null, ONE, null, null,
387: null, false);
388: p4 = new DefaultParameterDescriptor(Collections.singletonMap(
389: "name", "4"), Integer.class, null, ONE, null, null,
390: null, false) {
391: /**
392: * We are cheating here: <code>maximumOccurs</code> should always be 1 for
393: * <code>ParameterValue</code>. However, the Geotools implementation should
394: * be robust enough to accept other values. We will test that.
395: */
396: public int getMaximumOccurs() {
397: return 2;
398: }
399: };
400:
401: final Parameter v1, v2, v3, v4, v1b, v2b, v3b, v4b;
402: v1 = new Parameter(p1);
403: v1.setValue(10);
404: v2 = new Parameter(p2);
405: v2.setValue(20);
406: v3 = new Parameter(p3);
407: v3.setValue(30);
408: v4 = new Parameter(p4);
409: v4.setValue(40);
410: v1b = new Parameter(p1);
411: v1b.setValue(-10);
412: v2b = new Parameter(p2);
413: v2b.setValue(-20);
414: v3b = new Parameter(p3);
415: v3b.setValue(-30);
416: v4b = new Parameter(p4);
417: v4b.setValue(-40);
418:
419: ParameterDescriptorGroup descriptor;
420: ParameterGroup group;
421: Collection content;
422: Map properties;
423: Parameter automatic;
424:
425: /* --------------------------------------------- *
426: * Case (v1, v2, v3) where:
427: * - v1 is mandatory
428: * - v2 is mandatory
429: * - v3 is optional
430: * --------------------------------------------- */
431: properties = Collections.singletonMap("name", "group");
432: group = new ParameterGroup(properties, new Parameter[] { v1,
433: v2, v3 });
434: descriptor = (ParameterDescriptorGroup) group.getDescriptor();
435: content = descriptor.descriptors();
436: writer.format(group); // Ensure there is no exception there.
437: assertEquals("name", "group", descriptor.getName().getCode());
438: assertEquals("descriptors", 3, content.size());
439: assertTrue("contains(p1)", content.contains(p1));
440: assertTrue("contains(p2)", content.contains(p2));
441: assertTrue("contains(p3)", content.contains(p3));
442: assertFalse("contains(p4)", content.contains(p4));
443: assertSame("descriptor(\"1\")", p1, descriptor.descriptor("1"));
444: assertSame("descriptor(\"2\")", p2, descriptor.descriptor("2"));
445: assertSame("descriptor(\"3\")", p3, descriptor.descriptor("3"));
446:
447: // Checks default values
448: content = group.values();
449: assertEquals("values.size()", 3, content.size());
450: assertTrue("contains(v1)", content.contains(v1));
451: assertTrue("contains(v2)", content.contains(v2));
452: assertTrue("contains(v3)", content.contains(v3));
453: assertFalse("contains(v4)", content.contains(v4));
454: assertFalse("contains(v1b)", content.contains(v1b));
455: assertFalse("contains(v2b)", content.contains(v2b));
456: assertFalse("contains(v3b)", content.contains(v3b));
457: assertSame("parameter(\"1\")", v1, group.parameter("1"));
458: assertSame("parameter(\"2\")", v2, group.parameter("2"));
459: assertSame("parameter(\"3\")", v3, group.parameter("3"));
460: assertEquals("parameter(\"1\")", 10, group.parameter("1")
461: .intValue());
462: assertEquals("parameter(\"2\")", 20, group.parameter("2")
463: .intValue());
464: assertEquals("parameter(\"3\")", 30, group.parameter("3")
465: .intValue());
466:
467: // Tests the replacement of some values
468: assertFalse("remove(v1b)", content.remove(v1b));
469: try {
470: assertTrue(content.remove(v1));
471: fail("v1 is a mandatory parameter; it should not be removeable.");
472: } catch (InvalidParameterCardinalityException e) {
473: // This is the expected exception.
474: assertEquals("1", e.getParameterName());
475: assertNotNull(e.getMessage());
476: }
477: try {
478: assertTrue(content.add(v4));
479: fail("v4 is not a parameter for this group.");
480: } catch (InvalidParameterNameException e) {
481: // This is the expected exception.
482: assertEquals("4", e.getParameterName());
483: assertNotNull(e.getMessage());
484: }
485: assertTrue("add(v1b)", content.add(v1b));
486: assertTrue("add(v2b)", content.add(v2b));
487: assertTrue("add(v3b)", content.add(v3b));
488: assertFalse("add(v1b)", content.add(v1b)); // Already present
489: assertFalse("add(v2b)", content.add(v2b)); // Already present
490: assertFalse("add(v3b)", content.add(v3b)); // Already present
491: assertEquals("parameter(\"1b\")", -10, group.parameter("1")
492: .intValue());
493: assertEquals("parameter(\"2b\")", -20, group.parameter("2")
494: .intValue());
495: assertEquals("parameter(\"3b\")", -30, group.parameter("3")
496: .intValue());
497: assertEquals("values.size()", 3, content.size());
498:
499: // Tests equality
500: assertEquals("new", group, group = new ParameterGroup(
501: descriptor, new Parameter[] { v1b, v2b, v3b }));
502:
503: /* --------------------------------------------- *
504: * Case (v1, v2) where:
505: * - v1 is mandatory
506: * - v2 is mandatory
507: * - v3 is optional and initially omitted
508: * --------------------------------------------- */
509: group = new ParameterGroup(descriptor,
510: new Parameter[] { v1, v2 });
511: descriptor = (ParameterDescriptorGroup) group.getDescriptor();
512: content = group.values();
513: automatic = (Parameter) v3.getDescriptor().createValue(); // Remove cast with J2SE 1.5
514: writer.format(group); // Ensure there is no exception there.
515: assertEquals("values.size()", 2, content.size());
516: assertTrue("contains(v1)", content.contains(v1));
517: assertTrue("contains(v2)", content.contains(v2));
518: assertFalse("contains(v3)", content.contains(v3));
519: assertFalse("contains(v4)", content.contains(v4));
520: assertFalse("contains(v1b)", content.contains(v1b));
521: assertFalse("contains(v2b)", content.contains(v2b));
522: assertFalse("contains(v3b)", content.contains(v3b));
523: assertSame("parameter(\"1\")", v1, group.parameter("1"));
524: assertSame("parameter(\"2\")", v2, group.parameter("2"));
525: assertFalse("contains(automatic)", content.contains(automatic));
526: assertNotEquals("parameter(\"3\")", v3, group.parameter("3")); // Should have automatically created.
527: assertTrue("contains(automatic)", content.contains(automatic));
528: try {
529: assertNotNull(group.parameter("4"));
530: fail("v4 parameter should not be allowed in this group.");
531: } catch (ParameterNotFoundException e) {
532: // This is the expected exception.
533: assertEquals("4", e.getParameterName());
534: assertNotNull(e.getMessage());
535: }
536:
537: // Tests the replacement of some values
538: assertFalse("remove(v1b)", content.remove(v1b));
539: assertEquals("values.size()", 3, content.size());
540: assertFalse("remove(v3)", content.remove(v3));
541: assertEquals("values.size()", 3, content.size());
542: assertTrue("remove(auto)", content.remove(automatic));
543: assertEquals("values.size()", 2, content.size());
544: try {
545: assertTrue(content.remove(v1));
546: fail("v1 is a mandatory parameter; it should not be removeable.");
547: } catch (InvalidParameterCardinalityException e) {
548: // This is the expected exception.
549: assertEquals("1", e.getParameterName());
550: assertNotNull(e.getMessage());
551: }
552:
553: assertEquals("values.size()", 2, content.size());
554: assertTrue("add(v1b)", content.add(v1b));
555: assertTrue("add(v2b)", content.add(v2b));
556: assertTrue("add(v3b)", content.add(v3b));
557: assertFalse("add(v1b)", content.add(v1b)); // Already present
558: assertFalse("add(v2b)", content.add(v2b)); // Already present
559: assertFalse("add(v3b)", content.add(v3b)); // Already present
560: assertEquals("parameter(\"1b\")", -10, group.parameter("1")
561: .intValue());
562: assertEquals("parameter(\"2b\")", -20, group.parameter("2")
563: .intValue());
564: assertEquals("parameter(\"3b\")", -30, group.parameter("3")
565: .intValue());
566: assertEquals("values.size()", 3, content.size());
567:
568: /* --------------------------------------------- *
569: * Case (v1, v4, v3, v4b) where:
570: * - v1 is mandatory
571: * - v3 is optional
572: * - v4 is optional and can be included twice.
573: * --------------------------------------------- */
574: try {
575: group = new ParameterGroup(properties, new Parameter[] {
576: v1, v3, v4, v3b });
577: fail("Adding two 'v3' value should not be allowed");
578: } catch (InvalidParameterCardinalityException e) {
579: // This is the expected exception.
580: assertEquals("3", e.getParameterName());
581: assertNotNull(e.getMessage());
582: }
583: group = new ParameterGroup(properties, new Parameter[] { v1,
584: v4, v3, v4b });
585: descriptor = (ParameterDescriptorGroup) group.getDescriptor();
586: content = group.values();
587: automatic = (Parameter) v3.getDescriptor().createValue(); // Remove cast with J2SE 1.5
588: writer.format(group); // Ensure there is no exception there.
589: assertEquals("values.size()", 4, content.size());
590: assertTrue("contains(v1)", content.contains(v1));
591: assertFalse("contains(v2)", content.contains(v2));
592: assertTrue("contains(v3)", content.contains(v3));
593: assertTrue("contains(v4)", content.contains(v4));
594: assertFalse("contains(v1b)", content.contains(v1b));
595: assertFalse("contains(v2b)", content.contains(v2b));
596: assertFalse("contains(v3b)", content.contains(v3b));
597: assertTrue("contains(v4b)", content.contains(v4b));
598: assertSame("parameter(\"1\")", v1, group.parameter("1"));
599: assertSame("parameter(\"3\")", v3, group.parameter("3"));
600: assertSame("parameter(\"4\")", v4, group.parameter("4"));
601: assertTrue("remove(v3)", content.remove(v3));
602: assertFalse("contains(automatic)", content.contains(automatic));
603: assertNotEquals("parameter(\"3\")", v3, group.parameter("3")); // Should have automatically created.
604: assertTrue("contains(automatic)", content.contains(automatic));
605:
606: try {
607: new ParameterGroup(descriptor, new Parameter[] { v4, v3 });
608: fail("Parameter 1 was mandatory.");
609: } catch (InvalidParameterCardinalityException exception) {
610: // This is the expected exception.
611: assertEquals("1", exception.getParameterName());
612: }
613: try {
614: new ParameterGroup(descriptor, new Parameter[] { v1, v4,
615: v3, v3b });
616: fail("Parameter 3 was not allowed to be inserted twice.");
617: } catch (InvalidParameterCardinalityException exception) {
618: // This is the expected exception.
619: assertEquals("3", exception.getParameterName());
620: }
621: try {
622: new ParameterGroup(descriptor, new Parameter[] { v1, v3,
623: v1b });
624: fail("Parameter 1 was not allowed to be inserted twice.");
625: } catch (InvalidParameterCardinalityException exception) {
626: // This is the expected exception.
627: assertEquals("1", exception.getParameterName());
628: }
629:
630: /* --------------------------------------------- *
631: * Case (v1, v2) where:
632: * - v1 is mandatory
633: * - v2 is mandatory
634: * --------------------------------------------- */
635: group = new ParameterGroup(properties,
636: new Parameter[] { v1, v2 });
637: descriptor = (ParameterDescriptorGroup) group.getDescriptor();
638: content = descriptor.descriptors();
639: writer.format(group); // Ensure there is no exception there.
640: assertEquals("name", "group", descriptor.getName().getCode());
641: assertEquals("descriptors.size()", 2, content.size());
642: assertTrue("contains(p1)", content.contains(p1));
643: assertTrue("contains(p2)", content.contains(p2));
644: assertFalse("contains(p3)", content.contains(p3));
645: assertSame("descriptor(\"1\")", p1, descriptor.descriptor("1"));
646: assertSame("descriptor(\"2\")", p2, descriptor.descriptor("2"));
647: try {
648: assertSame("p3", p3, descriptor.descriptor("3"));
649: fail("p3 should not exists.");
650: } catch (ParameterNotFoundException e) {
651: // This is the expected exception
652: assertEquals("3", e.getParameterName());
653: }
654:
655: content = group.values();
656: assertEquals("values.size()", 2, content.size());
657: assertTrue("contains(v1)", content.contains(v1));
658: assertTrue("contains(v2)", content.contains(v2));
659: assertFalse("contains(v3)", content.contains(v3));
660: assertFalse("contains(v1b)", content.contains(v1b));
661: assertFalse("contains(v2b)", content.contains(v2b));
662: assertFalse("contains(v3b)", content.contains(v3b));
663: assertSame("parameter(\"1\")", v1, group.parameter("1"));
664: assertSame("parameter(\"2\")", v2, group.parameter("2"));
665: try {
666: assertSame("parameter(\"3\")", v3, group.parameter("3"));
667: fail("v3 should not exists");
668: } catch (ParameterNotFoundException e) {
669: // This is the expected exception
670: assertEquals("3", e.getParameterName());
671: }
672:
673: /* --------------------------------------------- *
674: * Case (v1, v3) where:
675: * - v1 is mandatory
676: * - v3 is optional
677: * --------------------------------------------- */
678: group = new ParameterGroup(properties,
679: new Parameter[] { v1, v3 });
680: descriptor = (ParameterDescriptorGroup) group.getDescriptor();
681: content = descriptor.descriptors();
682: writer.format(group); // Ensure there is no exception there.
683: assertEquals("name", "group", descriptor.getName().getCode());
684: assertEquals("descriptors.size()", 2, content.size());
685: assertTrue("contains(p1)", content.contains(p1));
686: assertFalse("contains(p2)", content.contains(p2));
687: assertTrue("contains(p3)", content.contains(p3));
688: assertSame("descriptor(\"1\")", p1, descriptor.descriptor("1"));
689: assertSame("descriptor(\"3\")", p3, descriptor.descriptor("3"));
690: try {
691: assertSame("descriptor(\"2\")", p2, descriptor
692: .descriptor("2"));
693: fail("p2 should not exists");
694: } catch (ParameterNotFoundException e) {
695: // This is the expected exception
696: assertEquals("2", e.getParameterName());
697: }
698:
699: content = group.values();
700: assertEquals("values.size()", 2, content.size());
701: assertTrue("contains(v1)", content.contains(v1));
702: assertFalse("contains(v2)", content.contains(v2));
703: assertTrue("contains(v3)", content.contains(v3));
704: assertFalse("contains(v1b)", content.contains(v1b));
705: assertFalse("contains(v2b)", content.contains(v2b));
706: assertFalse("contains(v3b)", content.contains(v3b));
707: assertSame("parameter(\"1\")", v1, group.parameter("1"));
708: assertSame("parameter(\"3\")", v3, group.parameter("3"));
709: try {
710: assertSame("parameter(\"2\")", v2, group.parameter("2"));
711: fail("v2 should not exists");
712: } catch (ParameterNotFoundException e) {
713: // This is the expected exception
714: assertEquals("2", e.getParameterName());
715: }
716:
717: /* --------------------------------------------- *
718: * Construction tests
719: * --------------------------------------------- */
720: group = new ParameterGroup(properties, new Parameter[] { v1,
721: v2, v3, v4, v4b });
722: writer.format(group); // Ensure there is no exception there.
723: assertEquals("values.size()", 5, group.values().size());
724: try {
725: new ParameterGroup(properties, new Parameter[] { v1, v2,
726: v3, v3b });
727: fail("Parameter 3 was not allowed to be inserted twice.");
728: } catch (InvalidParameterCardinalityException e) {
729: // This is the expected exception.
730: assertEquals("3", e.getParameterName());
731: }
732: try {
733: new ParameterGroup(properties, new Parameter[] { v1, v3,
734: v1b });
735: fail("Parameter 1 was not allowed to be inserted twice.");
736: } catch (InvalidParameterCardinalityException e) {
737: // This is the expected exception.
738: assertEquals("1", e.getParameterName());
739: }
740: }
741:
742: /**
743: * Test WKT formatting of transforms backed by matrix.
744: */
745: public void testMatrix() {
746: final Formatter formatter = new Formatter();
747: final GeneralMatrix matrix = new GeneralMatrix(4);
748: matrix.setElement(0, 2, 4);
749: matrix.setElement(1, 0, -2);
750: matrix.setElement(2, 3, 7);
751: MathTransform transform = ProjectiveTransform.create(matrix);
752: assertFalse(transform instanceof AffineTransform);
753: formatter.append(transform);
754: assertEquals("PARAM_MT[\"Affine\", "
755: + "PARAMETER[\"num_row\", 4], "
756: + "PARAMETER[\"num_col\", 4], "
757: + "PARAMETER[\"elt_0_2\", 4.0], "
758: + "PARAMETER[\"elt_1_0\", -2.0], "
759: + "PARAMETER[\"elt_2_3\", 7.0]]", formatter.toString());
760: matrix.setSize(3, 3);
761: transform = ProjectiveTransform.create(matrix);
762: assertTrue(transform instanceof AffineTransform);
763: formatter.clear();
764: formatter.append(transform);
765: assertEquals("PARAM_MT[\"Affine\", "
766: + "PARAMETER[\"num_row\", 3], "
767: + "PARAMETER[\"num_col\", 3], "
768: + "PARAMETER[\"elt_0_2\", 4.0], "
769: + "PARAMETER[\"elt_1_0\", -2.0]]", formatter.toString());
770: }
771:
772: /**
773: * Tests the storage of matrix parameters.
774: */
775: public void textMatrixEdit() {
776: final int size = 8;
777: final Random random = new Random(47821365);
778: final GeneralMatrix matrix = new GeneralMatrix(size);
779: for (int j = 0; j < size; j++) {
780: for (int i = 0; i < size; i++) {
781: matrix
782: .setElement(j, i,
783: 200 * random.nextDouble() - 100);
784: }
785: }
786: final MatrixParameterDescriptors descriptor = new MatrixParameterDescriptors(
787: Collections.singletonMap("name", "Test"));
788: for (int height = 2; height <= size; height++) {
789: for (int width = 2; width <= size; width++) {
790: MatrixParameters parameters = (MatrixParameters) descriptor
791: .createValue();
792: GeneralMatrix copy = (GeneralMatrix) matrix.clone();
793: copy.setSize(height, width);
794: parameters.setMatrix(copy);
795: assertEquals("height", height, ((Parameter) parameters
796: .parameter("num_row")).intValue());
797: assertEquals("width", width, ((Parameter) parameters
798: .parameter("num_col")).intValue());
799: assertEquals("equals", copy, parameters.getMatrix());
800: assertEquals("equals", parameters, parameters.clone());
801: }
802: }
803: }
804:
805: /**
806: * Test the serialization of the given object.
807: */
808: private static void serialize(final Object object)
809: throws IOException, ClassNotFoundException {
810: final ByteArrayOutputStream out = new ByteArrayOutputStream();
811: final ObjectOutputStream outs = new ObjectOutputStream(out);
812: outs.writeObject(object);
813: outs.close();
814:
815: final ObjectInputStream in = new ObjectInputStream(
816: new ByteArrayInputStream(out.toByteArray()));
817: final Object test = in.readObject();
818: in.close();
819:
820: assertNotSame("Serialization", object, test);
821: assertEquals("Serialization", object, test);
822: assertEquals("Serialization", object.hashCode(), test
823: .hashCode());
824: }
825:
826: /**
827: * Ensure that the specified objects are not equals.
828: */
829: private static void assertNotEquals(final String message,
830: final Object o1, final Object o2) {
831: assertNotNull(message, o1);
832: assertNotNull(message, o2);
833: assertNotSame(message, o1, o2);
834: assertFalse(message, o1.equals(o2));
835: }
836: }
|