View Javadoc
1   package net.sourceforge.pmd.properties;
2   
3   import net.sourceforge.pmd.PropertyDescriptor;
4   import net.sourceforge.pmd.lang.rule.properties.DoubleMultiProperty;
5   import net.sourceforge.pmd.lang.rule.properties.DoubleProperty;
6   
7   /**
8    * Evaluates the functionality of the DoubleProperty descriptor by testing its
9    * ability to catch creation errors (illegal args), flag out-of-range test
10   * values, and serialize/deserialize groups of double values onto/from a string
11   * buffer.
12   * 
13   * @author Brian Remedios
14   */
15  public class DoublePropertyTest extends AbstractPropertyDescriptorTester {
16  
17      private static final double MIN = -10.0;
18      private static final double MAX = 100.0;
19      private static final double SHIFT = 5.0;
20  
21      public DoublePropertyTest() {
22          super("Double");
23      }
24  
25      /**
26       * Creates and returns (count) number of legal Double values
27       * 
28       * @param count int
29       * @return Object
30       */
31      protected Object createValue(int count) {
32  
33          if (count == 1)
34              return Double.valueOf(randomDouble(MIN, MAX));
35  
36          Double[] values = new Double[count];
37          for (int i = 0; i < values.length; i++)
38              values[i] = (Double) createValue(1);
39          return values;
40      }
41  
42      /**
43       * Creates and returns (count) number of out-of-range values
44       * 
45       * @param count int
46       * @return Object
47       */
48      protected Object createBadValue(int count) {
49  
50          if (count == 1)
51              return Double.valueOf(randomBool() ? randomDouble(MIN - SHIFT, MIN - 0.01) : randomDouble(MAX + 0.01, MAX
52                      + SHIFT));
53  
54          Double[] values = new Double[count];
55          for (int i = 0; i < values.length; i++)
56              values[i] = (Double) createBadValue(1);
57          return values;
58      }
59  
60      /**
61       * Creates and returns a property with a (maxCount) value cardinality.
62       * 
63       * @param multiValue boolean
64       * @return PropertyDescriptor
65       */
66      protected PropertyDescriptor createProperty(boolean multiValue) {
67  
68          return multiValue ? new DoubleMultiProperty("testDouble", "Test double property", MIN, MAX, new Double[] { -1d,
69                  0d, 1d, 2d }, 1.0f) : new DoubleProperty("testDouble", "Test double property", MIN, MAX, 9.0, 1.0f);
70      }
71  
72      /**
73       * Attempts to create a property with invalid constructor arguments.
74       * 
75       * @param multiValue boolean
76       * @return PropertyDescriptor
77       */
78      protected PropertyDescriptor createBadProperty(boolean multiValue) {
79  
80          return multiValue ? new DoubleMultiProperty("testDouble", "Test double property", MIN, MAX, new Double[] {
81                  MIN - SHIFT, MIN, MIN + SHIFT, MAX + SHIFT }, 1.0f) : new DoubleProperty("testDouble",
82                  "Test double property", MAX, MIN, 9.0, 1.0f);
83      }
84  }