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