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