View Javadoc
1   package net.sourceforge.pmd.properties;
2   
3   import java.util.Comparator;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.Observer;
7   import java.util.Set;
8   
9   import net.sourceforge.pmd.PropertyDescriptor;
10  import net.sourceforge.pmd.lang.rule.properties.TypeMultiProperty;
11  import net.sourceforge.pmd.lang.rule.properties.TypeProperty;
12  
13  /**
14   * Evaluates the functionality of the TypeProperty descriptor by testing its
15   * ability to catch creation errors (illegal args), flag invalid Type values per
16   * the allowable packages, and serialize/deserialize groups of types onto/from a
17   * string buffer.
18   * 
19   * We're using java.lang classes for 'normal' constructors and applying
20   * java.util types as ones we expect to fail.
21   * 
22   * @author Brian Remedios
23   */
24  public class TypePropertyTest extends AbstractPropertyDescriptorTester {
25  
26      private static final Class[] javaLangClasses = new Class[] { String.class, Integer.class, Thread.class,
27              Object.class, Runtime.class };
28      private static final Class[] javaUtilTypes = new Class[] { HashMap.class, Map.class, Comparator.class, Set.class,
29              Observer.class };
30  
31      public TypePropertyTest() {
32          super("Class");
33      }
34  
35      /**
36       * Method createValue.
37       * 
38       * @param count int
39       * @return Object
40       */
41      protected Object createValue(int count) {
42  
43          if (count == 1)
44              return randomChoice(javaLangClasses);
45  
46          Object[] values = new Object[count];
47          for (int i = 0; i < values.length; i++)
48              values[i] = createValue(1);
49          return values;
50      }
51  
52      /**
53       * Method createBadValue.
54       * 
55       * @param count int
56       * @return Object
57       */
58      protected Object createBadValue(int count) {
59  
60          if (count == 1)
61              return randomChoice(javaUtilTypes);
62  
63          Object[] values = new Object[count];
64          for (int i = 0; i < values.length; i++)
65              values[i] = createBadValue(1);
66          return values;
67      }
68  
69      /**
70       * Method createProperty.
71       * 
72       * @param multiValue boolean
73       * @return PropertyDescriptor
74       */
75      protected PropertyDescriptor createProperty(boolean multiValue) {
76  
77          return multiValue ? new TypeMultiProperty("testType", "Test type property", javaLangClasses,
78                  new String[] { "java.lang" }, 1.0f) : new TypeProperty("testType", "Test type property",
79                  javaLangClasses[0], new String[] { "java.lang" }, 1.0f);
80      }
81  
82      /**
83       * Method createProperty.
84       * 
85       * @param multiValue boolean
86       * @return PropertyDescriptor
87       */
88      protected PropertyDescriptor createBadProperty(boolean multiValue) {
89  
90          return multiValue ? new TypeMultiProperty("testType", "Test type property", new Class[] { Set.class },
91                  new String[] { "java.lang" }, 1.0f) : new TypeProperty("testType", "Test type property",
92                  javaLangClasses[0], new String[] { "java.util" }, 1.0f);
93      }
94  
95  }