View Javadoc
1   package org.opentrafficsim.base.parameters;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNotEquals;
6   import static org.junit.Assert.assertNotNull;
7   import static org.junit.Assert.assertTrue;
8   import static org.junit.Assert.fail;
9   
10  import java.lang.reflect.InvocationTargetException;
11  
12  import org.djunits.unit.AccelerationUnit;
13  import org.djunits.unit.LengthUnit;
14  import org.djunits.unit.LinearDensityUnit;
15  import org.djunits.unit.SpeedUnit;
16  import org.djunits.value.vdouble.scalar.Acceleration;
17  import org.djunits.value.vdouble.scalar.Duration;
18  import org.djunits.value.vdouble.scalar.Frequency;
19  import org.djunits.value.vdouble.scalar.Length;
20  import org.djunits.value.vdouble.scalar.LinearDensity;
21  import org.djunits.value.vdouble.scalar.Speed;
22  import org.djutils.exceptions.Throw;
23  import org.junit.Test;
24  import org.opentrafficsim.base.parameters.constraint.Constraint;
25  import org.opentrafficsim.base.parameters.constraint.ConstraintInterface;
26  
27  /**
28   * <p>
29   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
33   * initial version 7 apr. 2016 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
37   */
38  
39  public class ParametersTest implements ConstraintInterface
40  {
41  
42      /**
43       * Defaults tests.
44       */
45      @Test
46      public final void defaultsTest()
47      {
48          Parameters params = new ParameterSet().setDefaultParameters(ParameterTypes.class);
49          try
50          {
51              assertTrue("Default value is not correctly set.",
52                      params.getParameter(ParameterTypes.A).equals(ParameterTypes.A.getDefaultValue()));
53          }
54          catch (ParameterException exception)
55          {
56              fail("Default value is not set at all.");
57          }
58      }
59  
60      /**
61       * Constructor tests.
62       */
63      @Test
64      public final void constructorTest()
65      {
66          // Check Parameters constructor
67          ParameterSet params = new ParameterSet();
68          assertNotNull("Default constructor should not return null.", params);
69          if (!params.getParameters().isEmpty())
70          {
71              fail("Constructed Parameters has a non-empty parameter map.");
72          }
73  
74          // Check ParameterType construction (id, description, class, defaultValue)
75          Length defaultValue = new Length(1.0, LengthUnit.SI);
76          ParameterTypeLength a = new ParameterTypeLength("a", "along", defaultValue);
77          assertEquals("Parameter type id not properly set.", "a", a.getId());
78          assertEquals("Parameter type description not properly set.", "along", a.getDescription());
79          assertTrue("has a default value", a.hasDefaultValue());
80          try
81          {
82              assertEquals("Parameter type default value not properly set.", defaultValue, a.getDefaultValue());
83          }
84          catch (ParameterException exception)
85          {
86              fail("Parameter type default value given in constructor was not set.");
87          }
88  
89          // Check ParameterType construction (id, description, class)
90          ParameterTypeLength b = new ParameterTypeLength("b", "blong");
91          assertEquals("Parameter type id not properly set.", "b", b.getId());
92          assertEquals("Parameter type description not properly set.", "blong", b.getDescription());
93          assertFalse("does not have a default value", b.hasDefaultValue());
94          try
95          {
96              b.getDefaultValue();
97              fail("Parameter type returned a default value, while none was provided.");
98          }
99          catch (ParameterException pe)
100         {
101             // ignore expected exception
102         }
103         assertTrue("toString returns something with ParameterType in it", b.toString().contains("ParameterType"));
104     }
105 
106     /**
107      * Parameter value default range tests.
108      */
109     @Test
110     public final void defaultRangeTest()
111     {
112         // Check default values that should work
113         checkDefaultValue(1.0, POSITIVE, false);
114         checkDefaultValue(-1.0, NEGATIVE, false);
115         checkDefaultValue(1.0, POSITIVEZERO, false);
116         checkDefaultValue(0.0, POSITIVEZERO, false);
117         checkDefaultValue(-1.0, NEGATIVEZERO, false);
118         checkDefaultValue(-0.0, NEGATIVEZERO, false);
119         checkDefaultValue(-1.0, NONZERO, false);
120         checkDefaultValue(1.0, NONZERO, false);
121         checkDefaultValue(0.0, UNITINTERVAL, false);
122         checkDefaultValue(0.5, UNITINTERVAL, false);
123         checkDefaultValue(1.0, UNITINTERVAL, false);
124         checkDefaultValue(1.0, ATLEASTONE, false);
125         // Check default values that should not work
126         checkDefaultValue(-1.0, POSITIVE, true);
127         checkDefaultValue(0.0, POSITIVE, true);
128         checkDefaultValue(1.0, NEGATIVE, true);
129         checkDefaultValue(0.0, NEGATIVE, true);
130         checkDefaultValue(-1.0, POSITIVEZERO, true);
131         checkDefaultValue(1.0, NEGATIVEZERO, true);
132         checkDefaultValue(0.0, NONZERO, true);
133         checkDefaultValue(-0.01, UNITINTERVAL, true);
134         checkDefaultValue(1.01, UNITINTERVAL, true);
135         checkDefaultValue(0.99, ATLEASTONE, true);
136 
137         // Check set values that should work
138         checkSetValue(1.0, POSITIVE, false);
139         checkSetValue(-1.0, NEGATIVE, false);
140         checkSetValue(1.0, POSITIVEZERO, false);
141         checkSetValue(0.0, POSITIVEZERO, false);
142         checkSetValue(-1.0, NEGATIVEZERO, false);
143         checkSetValue(-0.0, NEGATIVEZERO, false);
144         checkSetValue(-1.0, NONZERO, false);
145         checkSetValue(1.0, NONZERO, false);
146         checkSetValue(0.0, UNITINTERVAL, false);
147         checkSetValue(0.5, UNITINTERVAL, false);
148         checkSetValue(1.0, UNITINTERVAL, false);
149         checkSetValue(1.0, ATLEASTONE, false);
150         // Check set values that should not work
151         checkSetValue(-1.0, POSITIVE, true);
152         checkSetValue(0.0, POSITIVE, true);
153         checkSetValue(1.0, NEGATIVE, true);
154         checkSetValue(0.0, NEGATIVE, true);
155         checkSetValue(-1.0, POSITIVEZERO, true);
156         checkSetValue(1.0, NEGATIVEZERO, true);
157         checkSetValue(0.0, NONZERO, true);
158         checkSetValue(-0.01, UNITINTERVAL, true);
159         checkSetValue(1.01, UNITINTERVAL, true);
160         checkSetValue(0.99, ATLEASTONE, true);
161     }
162 
163     /**
164      * Checks a default value.
165      * @param value Value to check.
166      * @param constraint Constraint to perform.
167      * @param shouldFail Whether the check should fail.
168      */
169     private void checkDefaultValue(final double value, final Constraint<Number> constraint, final boolean shouldFail)
170     {
171         try
172         {
173             new ParameterTypeAcceleration("a", "along", new Acceleration(value, AccelerationUnit.SI), constraint);
174             if (shouldFail)
175             {
176                 fail("Default value " + value + " fails default " + constraint + " constraint.");
177             }
178         }
179         catch (RuntimeException re)
180         {
181             if (!shouldFail)
182             {
183                 re.printStackTrace();
184                 fail("Default value " + value + " does not fail default " + constraint + " constraint.");
185             }
186         }
187     }
188 
189     /**
190      * Checks a set value.
191      * @param value Value to check.
192      * @param constraint Constraint to perform.
193      * @param shouldFail Whether the check should fail.
194      */
195     private void checkSetValue(final double value, final Constraint<Number> constraint, final boolean shouldFail)
196     {
197         try
198         {
199             Parameters params = new ParameterSet();
200             ParameterTypeAcceleration a = new ParameterTypeAcceleration("a", "along", constraint);
201             params.setParameter(a, new Acceleration(value, AccelerationUnit.SI));
202             if (shouldFail)
203             {
204                 fail("Set value " + value + " fails default " + constraint + " constraint.");
205             }
206         }
207         catch (ParameterException pe)
208         {
209             if (!shouldFail)
210             {
211                 fail("Set value " + value + " does not fail default " + constraint + " constraint.");
212             }
213         }
214     }
215 
216     /**
217      * Parameter value custom constraint tests.
218      */
219     @Test
220     public final void customConstraintTest()
221     {
222 
223         // Check values that should work
224         Parameters params = new ParameterSet();
225         try
226         {
227             // requirement: v1 < v2
228             params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
229             params.setParameter(v2, new Speed(4.0, SpeedUnit.KM_PER_HOUR));
230             params.setParameter(v1, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
231             params.setParameter(v2, new Speed(5.0, SpeedUnit.KM_PER_HOUR));
232         }
233         catch (ParameterException pe)
234         {
235             fail("Custom check of set parameter value with value of other parameter fails for correct values.");
236         }
237 
238         // Check values that should not work, set v1 first
239         params = new ParameterSet();
240         try
241         {
242             // requirement: v1 < v2
243             params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
244             params.setParameter(v2, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
245             fail("Custom check of set parameter value with value of other parameter does not fail for wrong values.");
246         }
247         catch (ParameterException pe)
248         {
249             // Should fail
250         }
251 
252         // Check values that should not work, set v2 first
253         params = new ParameterSet();
254         try
255         {
256             // requirement: v1 < v2
257             params.setParameter(v2, new Speed(2.0, SpeedUnit.KM_PER_HOUR));
258             params.setParameter(v1, new Speed(3.0, SpeedUnit.KM_PER_HOUR));
259             fail("Custom check of set parameter value with value of other parameter does not fail for wrong values.");
260         }
261         catch (ParameterException pe)
262         {
263             // Should fail
264         }
265 
266     }
267 
268     /** Helper parameter type for custom constraint checks. */
269     private static ParameterTypeSpeed v1 = new ParameterTypeSpeed("v1", "v1long")
270     {
271         /** */
272         private static final long serialVersionUID = 20160400L;
273 
274         @SuppressWarnings("synthetic-access")
275         @Override
276         public void check(final Speed v, final Parameters paramsa) throws ParameterException
277         {
278             Speed u2 = paramsa.getParameterOrNull(v2);
279             Throw.when(u2 != null && v.si > u2.si, ParameterException.class, "Value of v1 is larger than value of v2.");
280         }
281     };
282 
283     /** Helper parameter type for custom constraint checks. */
284     private static ParameterTypeSpeed v2 = new ParameterTypeSpeed("v2", "v2long")
285     {
286         /** */
287         private static final long serialVersionUID = 20160400L;
288 
289         @SuppressWarnings("synthetic-access")
290         @Override
291         public void check(final Speed v, final Parameters paramsa) throws ParameterException
292         {
293             Speed u1 = paramsa.getParameterOrNull(v1);
294             Throw.when(u1 != null && v.si < u1.si, ParameterException.class, "Value of v2 is smaller than value of v1.");
295         }
296     };
297 
298     /**
299      * Tests the set/reset mechanism.
300      * @throws ParameterException Should not be thrown, is for untested methods (in this test) that throw the exception.
301      */
302     @SuppressWarnings("cast")
303     @Test
304     public final void setResetTest() throws ParameterException
305     {
306         ParameterTypeInteger a = new ParameterTypeInteger("a", "along", 0);
307 
308         // exception reset without set: no value -> reset
309         Parameters params = new ParameterSet();
310         try
311         {
312             params.resetParameter(a);
313             fail("Reset of parameter that was never set does not fail.");
314         }
315         catch (ParameterException pe)
316         {
317             // Should fail
318         }
319 
320         // exception for get after reset to no value: no value -> set -> reset -> get
321         params = new ParameterSet();
322         params.setParameterResettable(a, 1);
323         params.resetParameter(a);
324         try
325         {
326             params.getParameter(a);
327             fail("Get of parameter that was not given before set and reset, does not fail.");
328         }
329         catch (ParameterException pe)
330         {
331             // Should fail
332         }
333 
334         // exception for multiple resets: no value -> set -> reset -> reset
335         params = new ParameterSet();
336         params.setParameterResettable(a, 1);
337         params.resetParameter(a);
338         try
339         {
340             params.resetParameter(a);
341             fail("Second reset without intermediate set does not fail when first reset was to no value.");
342         }
343         catch (ParameterException pe)
344         {
345             // Should fail
346         }
347 
348         // exception for multiple resets: set -> set -> reset -> reset
349         params = new ParameterSet();
350         params.setParameterResettable(a, 1);
351         params.setParameterResettable(a, 2);
352         params.resetParameter(a);
353         try
354         {
355             params.resetParameter(a);
356             fail("Second reset without intermediate set does not fail when first reset was to a value.");
357         }
358         catch (ParameterException pe)
359         {
360             // Should fail
361         }
362 
363         // no exception: set -> reset -> set -> reset
364         params = new ParameterSet();
365         params.setParameterResettable(a, 1);
366         params.resetParameter(a);
367         params.setParameterResettable(a, 2);
368         try
369         {
370             params.resetParameter(a);
371         }
372         catch (ParameterException pe)
373         {
374             fail("Reset fails after set, with reset before that set.");
375             // Should not fail
376         }
377 
378         // same value: set(1) -> set(2) -> reset -> get(1?)
379         params = new ParameterSet();
380         params.setParameterResettable(a, 1);
381         params.setParameterResettable(a, 2);
382         params.resetParameter(a);
383         assertEquals("Value after reset should be the same as before last set.", 1.0, (double) params.getParameter(a), 0.0);
384 
385         // no reset after (none resettable) set
386         params = new ParameterSet();
387         params.setParameter(a, 1);
388         try
389         {
390             params.resetParameter(a);
391             fail("Reset should fail after regular set.");
392         }
393         catch (ParameterException pe)
394         {
395             // should fail
396         }
397 
398         // no reset after (none resettable) set, even with resettable set before
399         params = new ParameterSet();
400         params.setParameterResettable(a, 1);
401         params.setParameter(a, 2);
402         try
403         {
404             params.resetParameter(a);
405             fail("Reset should fail after regular set, dispite resettable set before.");
406         }
407         catch (ParameterException pe)
408         {
409             // should fail
410         }
411 
412         // same value: regular set(1) -> set(2) -> reset -> get(1?)
413         params = new ParameterSet();
414         params.setParameter(a, 1);
415         params.setParameterResettable(a, 2);
416         params.resetParameter(a);
417         assertEquals("Value after reset should be the same as before last set.", 1.0, (double) params.getParameter(a), 0.0);
418 
419         // If null value is ever going to be allowed, use these tests to check proper set/reset.
420         // // check null is not the same as 'no value': no value -> set(null) -> reset -> get
421         // // (null setting does not work on primitive data types, parameter type 'a' cannot be used)
422         // ParameterTypeFrequency b = new ParameterTypeFrequency("b", "blong");
423         // bc = new BehavioralCharacteristics();
424         // params.setParameter(b, null);
425         // bc.resetParameter(b);
426         // try
427         // {
428         // // as there was no value before the null set, this should fail
429         // params.getParameter(b);
430         // fail("Reset after setting of null is not properly handled.");
431         // }
432         // catch (ParameterException pe)
433         // {
434         // // should fail
435         // }
436         //
437         // // check null is not the same as no value: no value -> set(null) -> set(value) -> reset -> get(null?)
438         // params.setParameter(b, null);
439         // params.setParameter(b, new Frequency(12, FrequencyUnit.SI));
440         // bc.resetParameter(b);
441         // // assertEquals() with null cannot be used (defaults into deprecated array method)
442         // if (params.getParameter(b) != null)
443         // {
444         // fail("Value after reset is not equal to null, which it was before the last set.");
445         // }
446 
447     }
448 
449     /**
450      * Tests equalizations.
451      * @throws ParameterException Should not be thrown, is for untested methods (in this test) that throw the exception.
452      */
453     @Test
454     public final void equalizeTest() throws ParameterException
455     {
456         // equal double values from different parameters should be equal
457         ParameterTypeDouble a1 = new ParameterTypeDouble("a", "along", 0.0);
458         ParameterTypeDouble a2 = new ParameterTypeDouble("a", "along", 0.0);
459         Parameters params1 = new ParameterSet();
460         Parameters params2 = new ParameterSet();
461         params1.setParameter(a1, 4.0);
462         params2.setParameter(a2, 4.0);
463         assertEquals("Equal double values from different parameter types should be equal.", params1.getParameter(a1),
464                 params2.getParameter(a2), 0.0);
465 
466         // equal DoubleScalar.Rel values should be equal from different characteristic sets
467         ParameterTypeLinearDensity b1 = new ParameterTypeLinearDensity("b", "blong");
468         ParameterTypeLinearDensity b2 = new ParameterTypeLinearDensity("b", "blong");
469         params1.setParameter(b1, new LinearDensity(4.0, LinearDensityUnit.SI));
470         params2.setParameter(b2, new LinearDensity(4.0, LinearDensityUnit.SI));
471         assertEquals(
472                 "Equal DoubleScalar.Rel values from different parameter types and different characteristics should be equal.",
473                 params1.getParameter(b1), params2.getParameter(b2));
474 
475         // equal DoubleScalar.Rel values should be equal from the same characteristic set
476         params1.setParameter(b2, new LinearDensity(4.0, LinearDensityUnit.SI));
477         assertEquals(
478                 "Equal DoubleScalar.Rel values from different parameter types and the same characteristics should be equal.",
479                 params1.getParameter(b1), params1.getParameter(b2));
480 
481         // values of parameter types with different value classes are not equal
482         params1.setParameter(a1, 4.0);
483         params1.setParameter(b1, new LinearDensity(4.0, LinearDensityUnit.SI));
484         assertNotEquals("Values of different parameter type value classes should not be equal.", params1.getParameter(a1),
485                 params1.getParameter(b1));
486 
487     }
488 
489     /**
490      * Test that exceptions are thrown when trying to use null values.
491      */
492     @Test
493     public final void testNullNotAllowed()
494     {
495         // null default value
496         try
497         {
498             new ParameterTypeSpeed("v", "vlong", null, POSITIVE);
499             fail("Setting a default value of 'null' on ParameterType did not fail.");
500         }
501         catch (RuntimeException re)
502         {
503             // should fail
504         }
505         try
506         {
507             new ParameterTypeSpeed("v", "vlong", null, POSITIVE);
508             fail("Setting a default value of 'null' on ParameterTypeSpeed did not fail.");
509         }
510         catch (RuntimeException re)
511         {
512             // should fail
513         }
514 
515         // set null value
516         ParameterTypeSpeed v = new ParameterTypeSpeed("v", "vlong");
517         Parameters params = new ParameterSet();
518         try
519         {
520             params.setParameter(v, null);
521             fail("Setting a value of 'null' did not fail.");
522         }
523         catch (ParameterException pe)
524         {
525             // should fail
526         }
527 
528     }
529 
530     /**
531      * Checks whether default values are properly set, or not in case not given.
532      * @throws SecurityException Reflection.
533      * @throws NoSuchMethodException Reflection.
534      * @throws InvocationTargetException Reflection.
535      * @throws IllegalArgumentException Reflection.
536      * @throws IllegalAccessException Reflection.
537      * @throws InstantiationException Reflection.
538      */
539     @Test
540     public final void checkDefaultValues() throws InstantiationException, IllegalAccessException, IllegalArgumentException,
541             InvocationTargetException, NoSuchMethodException, SecurityException
542     {
543         // @formatter:off
544         // TODO: checkDefaultValuesPerClass(ParameterTypeNumeric.class,       Speed.createSI(3));
545         checkDefaultValuesPerClass(ParameterTypeSpeed.class,         Speed.createSI(3));
546         checkDefaultValuesPerClass(ParameterTypeAcceleration.class,  Acceleration.createSI(3));
547         checkDefaultValuesPerClass(ParameterTypeLength.class,        Length.createSI(3));
548         checkDefaultValuesPerClass(ParameterTypeFrequency.class,     Frequency.createSI(3));
549         checkDefaultValuesPerClass(ParameterTypeDuration.class,      Duration.createSI(3));
550         checkDefaultValuesPerClass(ParameterTypeLinearDensity.class, LinearDensity.createSI(3));
551         checkDefaultValuesPerClass(ParameterTypeBoolean.class,       new Boolean(false));
552         checkDefaultValuesPerClass(ParameterTypeDouble.class,        new Double(3));
553         checkDefaultValuesPerClass(ParameterTypeInteger.class,       new Integer(3));
554         // @formatter:on
555     }
556 
557     /**
558      * @param clazz AbstractParameterType subclass to test.
559      * @param defaultValue Default value to test with.
560      * @param <R> Subclass of AbstractParameterType.
561      * @throws SecurityException Reflection.
562      * @throws NoSuchMethodException Reflection.
563      * @throws InvocationTargetException Reflection.
564      * @throws IllegalArgumentException Reflection.
565      * @throws IllegalAccessException Reflection.
566      * @throws InstantiationException Reflection.
567      */
568     private <R extends ParameterType<?>> void checkDefaultValuesPerClass(final Class<R> clazz, final Object defaultValue)
569             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
570             NoSuchMethodException, SecurityException
571     {
572         // none set
573         ParameterType<?> ld;
574         if (clazz.equals(ParameterTypeNumeric.class))
575         {
576             ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class).newInstance("v", "vcong",
577                     getClass(defaultValue));
578         }
579         else
580         {
581             ld = clazz.getDeclaredConstructor(String.class, String.class).newInstance("v", "vcong");
582         }
583         try
584         {
585             ld.getDefaultValue();
586             fail("Could obtain a default value that was not set.");
587         }
588         catch (ParameterException pe)
589         {
590             // should fail
591         }
592         String toStringResult = ld.toString();
593         // System.out.println("tostring yields \"" + toStringResult + "\"");
594         // System.out.println("clazz is " + clazz.getSimpleName());
595         assertTrue("toString returns something with the class name in it ", toStringResult.contains(clazz.getSimpleName()));
596 
597         // none set, including default check
598         if (!clazz.equals(ParameterTypeBoolean.class)) // boolean has no checks
599         {
600             if (clazz.equals(ParameterTypeNumeric.class))
601             {
602                 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Constraint.class).newInstance("v",
603                         "vcong", getClass(defaultValue), POSITIVE);
604             }
605             else
606             {
607                 ld = clazz.getDeclaredConstructor(String.class, String.class, Constraint.class).newInstance("v", "vcong",
608                         POSITIVE);
609             }
610             try
611             {
612                 ld.getDefaultValue();
613                 fail("Could obtain a default value that was not set.");
614             }
615             catch (ParameterException pe)
616             {
617                 // should fail
618             }
619         }
620 
621         // value set
622         if (clazz.equals(ParameterTypeNumeric.class))
623         {
624             ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Number.class).newInstance("v", "vcong",
625                     getClass(defaultValue), defaultValue);
626         }
627         else
628         {
629             ld = clazz.getDeclaredConstructor(String.class, String.class, getClass(defaultValue)).newInstance("v", "vcong",
630                     defaultValue);
631         }
632         try
633         {
634             ld.getDefaultValue();
635         }
636         catch (ParameterException pe)
637         {
638             fail("Could not obtain a default value that was set.");
639         }
640         // value set, including default check
641         if (!clazz.equals(ParameterTypeBoolean.class)) // boolean has no checks
642         {
643             if (clazz.equals(ParameterTypeNumeric.class))
644             {
645                 ld = clazz.getDeclaredConstructor(String.class, String.class, Class.class, Number.class, Constraint.class)
646                         .newInstance("v", "vcong", getClass(defaultValue), defaultValue, POSITIVE);
647             }
648             else
649             {
650                 ld = clazz.getDeclaredConstructor(String.class, String.class, defaultValue.getClass(), Constraint.class)
651                         .newInstance("v", "vcong", defaultValue, POSITIVE);
652             }
653             try
654             {
655                 ld.getDefaultValue();
656             }
657             catch (ParameterException pe)
658             {
659                 fail("Could not obtain a default value that was set.");
660             }
661         }
662     }
663 
664     /**
665      * Returns the class of given default value for reflection.
666      * @param defaultValue Default value.
667      * @return Class of given default value for reflection.
668      */
669     private Class<? extends Object> getClass(final Object defaultValue)
670     {
671         if (defaultValue instanceof Boolean)
672         {
673             return Boolean.TYPE;
674         }
675         else if (defaultValue instanceof Double)
676         {
677             return Double.TYPE;
678         }
679         else if (defaultValue instanceof Integer)
680         {
681             return Integer.TYPE;
682         }
683         return defaultValue.getClass();
684     }
685 
686     /**
687      * Tests the merging of parameter sets using setAll.
688      * @throws ParameterException parameter exception
689      */
690     @Test
691     public final void mergeTest() throws ParameterException
692     {
693         ParameterSet paramsA = new ParameterSet();
694         paramsA.setDefaultParameter(ParameterTypes.A);
695         ParameterSet paramsB = new ParameterSet();
696         paramsB.setDefaultParameter(ParameterTypes.B);
697         paramsB.setAllIn(paramsA);
698         assertTrue("When merging set B with set A, set A should contain the parameters of set B.",
699                 paramsA.contains(ParameterTypes.B));
700         assertTrue("When merging set B with set A, parameter values should be equal.",
701                 paramsA.getParameter(ParameterTypes.B).eq(paramsB.getParameter(ParameterTypes.B)));
702         assertFalse("When merging set B with set A, set B should not contain the parameters of set A.",
703                 paramsB.contains(ParameterTypes.A));
704     }
705 
706 }