EnumeratedPropertyTest xref

View Javadoc
1 package net.sourceforge.pmd.properties;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 
6 import net.sourceforge.pmd.PropertyDescriptor;
7 import net.sourceforge.pmd.lang.rule.properties.EnumeratedMultiProperty;
8 import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
9 
10 /**
11  * Evaluates the functionality of the EnumeratedProperty descriptor by testing its ability to catch creation
12  * errors (illegal args), flag invalid selections, and serialize/deserialize selection options.
13  * 
14  * @author Brian Remedios
15  */
16 public class EnumeratedPropertyTest extends AbstractPropertyDescriptorTester {
17 
18 	private static final String[] keys = new String[] {
19 		"map",
20 		"emptyArray",
21 		"list",
22 		"string",
23 		};
24 
25 private static final Object[] values = new Object[] {
26 new HashMap(),
27 new Object[0],
28 new ArrayList(),
29 "Hello World!",
30 };
31 
32 	public EnumeratedPropertyTest() {
33 		super();
34 	}
35 
36 	/**
37 	 * Method createValue.
38 	 * @param count int
39 	 * @return Object
40 	 */
41 	protected Object createValue(int count) {
42 		
43 		if (count == 1) return randomChoice(values);
44 		
45 		Object[] values = new Object[count];
46 		for (int i=0; i<values.length; i++) values[i] = createValue(1);
47 		return values;
48 	}
49 
50 	/**
51 	 * Returns a (count) number of values that are not in the set of legal values.
52 	 * 
53 	 * @param count int
54 	 * @return Object
55 	 */
56 	protected Object createBadValue(int count) {
57 		
58 		if (count == 1) return Integer.toString(randomInt());		// not in the set of values
59 		
60 		Object[] values = new Object[count];
61 		for (int i=0; i<values.length; i++) values[i] = createBadValue(1);
62 		return values;
63 	}
64 	
65 	/**
66 	 * Method createProperty.
67 	 * @param multiValue boolean
68 	 * @return PropertyDescriptor
69 	 */
70 	protected PropertyDescriptor createProperty(boolean multiValue) {
71 		
72 		return multiValue ?
73 			new EnumeratedMultiProperty<Object>("testEnumerations", "Test enumerations with complex types", keys, values, new int[] {0,1}, 1.0f) :
74 			new EnumeratedProperty<Object>("testEnumerations", "Test enumerations with complex types", keys, values, 0, 1.0f);			
75 	}
76 
77 	/**
78 	 * Method createBadProperty.
79 	 * @param multiValue boolean
80 	 * @return PropertyDescriptor
81 	 */
82 	protected PropertyDescriptor createBadProperty(boolean multiValue) {
83 		
84 		return multiValue ?
85 			new EnumeratedMultiProperty<Object>("testEnumerations", "Test enumerations with complex types", keys, new Object[0], new int[] {99}, 1.0f) :
86 			new EnumeratedProperty<Object>("testEnumerations", "Test enumerations with complex types", new String[0], values, -1, 1.0f);
87 	}
88 	
89 public static junit.framework.Test suite() {
90 return new junit.framework.JUnit4TestAdapter(EnumeratedPropertyTest.class);
91 }
92 }

AltStyle によって変換されたページ (->オリジナル) /