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