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