View Javadoc
1   package net.sourceforge.pmd.properties;
2   
3   import net.sourceforge.pmd.PropertyDescriptor;
4   import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty;
5   import net.sourceforge.pmd.lang.rule.properties.StringProperty;
6   
7   /**
8    * Evaluates the functionality of the StringProperty descriptor by testing its
9    * ability to catch creation errors (illegal args), flag invalid strings per any
10   * specified expressions, and serialize/deserialize groups of strings onto/from
11   * a string buffer.
12   * 
13   * @author Brian Remedios
14   */
15  public class StringPropertyTest extends AbstractPropertyDescriptorTester {
16  
17      private static final int maxStringLength = 52;
18      private static final char delimiter = '|';
19      private static final char[] charSet = filter(allChars.toCharArray(), delimiter);
20  
21      public StringPropertyTest() {
22          super("String");
23      }
24  
25      /**
26       * Method createValue.
27       * 
28       * @param count int
29       * @return Object
30       */
31      protected Object createValue(int count) {
32  
33          if (count == 1)
34              return newString();
35  
36          String[] values = new String[count];
37          for (int i = 0; i < count; i++)
38              values[i] = (String) createValue(1);
39          return values;
40      }
41  
42      /**
43       * Method createBadValue.
44       * 
45       * @param count int
46       * @return Object
47       */
48      protected Object createBadValue(int count) {
49  
50          if (count == 1)
51              return null;
52  
53          Object[] values = new Object[count];
54          for (int i = 0; i < count; i++)
55              values[i] = createBadValue(1);
56          return values;
57      }
58  
59      /**
60       * Method newString.
61       * 
62       * @return String
63       */
64      private String newString() {
65  
66          int strLength = randomInt(0, maxStringLength);
67  
68          char[] chars = new char[strLength];
69          for (int i = 0; i < chars.length; i++)
70              chars[i] = randomCharIn(charSet);
71          return new String(chars);
72      }
73  
74      /**
75       * Method randomCharIn.
76       * 
77       * @param chars char[]
78       * @return char
79       */
80      private char randomCharIn(char[] chars) {
81          return randomChar(chars);
82      }
83  
84      /**
85       * Method createProperty.
86       * 
87       * @param multiValue boolean
88       * @return PropertyDescriptor
89       */
90      protected PropertyDescriptor createProperty(boolean multiValue) {
91          return multiValue ? new StringMultiProperty("testString", "Test string property", new String[] { "hello",
92                  "world" }, 1.0f, delimiter) : new StringProperty("testString", "Test string property", "brian", 1.0f);
93      }
94  
95      /**
96       * Method createBadProperty.
97       * 
98       * @param multiValue boolean
99       * @return PropertyDescriptor
100      */
101     protected PropertyDescriptor createBadProperty(boolean multiValue) {
102         return multiValue ? new StringMultiProperty("testString", "Test string property", new String[] { "hello",
103                 "world", "a" + delimiter + "b" }, 1.0f, delimiter) : new StringProperty("", "Test string property",
104                 "brian", 1.0f);
105     }
106 }