1 package test.net.sourceforge.pmd.properties;
2
3 import static org.junit.Assert.assertTrue;
4 import net.sourceforge.pmd.PropertyDescriptor;
5 import net.sourceforge.pmd.util.CollectionUtil;
6
7 import org.junit.Test;
8 /**
9 *
10 * @author Brian Remedios
11 */
12 public abstract class AbstractPropertyDescriptorTester {
13
14 private static final int maxCardinality = 10;
15
16 public static final String punctuationChars = "!@#$%^&*()_-+=[]{}\\|;:'\",.<>/?`~";
17 public static final String whitespaceChars = " \t\n";
18 public static final String digitChars = "0123456789";
19 public static final String alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmniopqrstuvwxyz";
20 public static final String alphaNumericChars = digitChars + alphaChars;
21 public static final String allChars = punctuationChars + whitespaceChars + alphaNumericChars;
22
23
24 /**
25 * Method createValue.
26 * @param count int
27 * @return Object
28 */
29 protected abstract Object createValue(int count);
30 /**
31 * Method createProperty.
32 * @param maxCount int
33 * @return PropertyDescriptor
34 */
35 protected abstract PropertyDescriptor createProperty(int maxCount);
36
37 @Test
38 public void testAsDelimitedString() {
39
40 Object testValue = createValue(maxCardinality);
41 PropertyDescriptor pmdProp = createProperty(maxCardinality);
42
43 String storeValue = pmdProp.asDelimitedString(testValue);
44
45 Object returnedValue = pmdProp.valueFrom(storeValue);
46
47 assertTrue(CollectionUtil.areEqual(returnedValue, testValue));
48 }
49
50 @Test
51 public void testValueFrom() {
52
53 Object testValue = createValue(1);
54 PropertyDescriptor pmdProp = createProperty(1);
55
56 String storeValue = pmdProp.asDelimitedString(testValue);
57
58 Object returnedValue = pmdProp.valueFrom(storeValue);
59
60 assertTrue(CollectionUtil.areEqual(returnedValue, testValue));
61 }
62
63
64 @Test
65 public void testErrorFor() {
66
67 Object testValue = createValue(1);
68 PropertyDescriptor pmdProp = createProperty(1);
69 String errorMsg = pmdProp.errorFor(testValue);
70 assertTrue(errorMsg == null);
71
72 testValue = createValue(maxCardinality);
73 pmdProp = createProperty(maxCardinality);
74 errorMsg = pmdProp.errorFor(testValue);
75 assertTrue(errorMsg == null);
76 }
77
78 @Test
79 public void testType() {
80
81 PropertyDescriptor pmdProp = createProperty(1);
82
83 assertTrue(pmdProp.type() != null);
84 }
85
86 /**
87 * Method randomInt.
88 * @return int
89 */
90 public static int randomInt() {
91
92 int randomVal = (int) (Math.random() * 100 + 1D);
93 return randomVal + (int) (Math.random() * 100000D);
94 }
95
96 /**
97 * Method randomInt.
98 * @param min int
99 * @param max int
100 * @return int
101 */
102 public static int randomInt(int min, int max) {
103 if (max < min) max = min;
104 int range = Math.abs(max - min);
105 int x = (int) ((range * Math.random()) + .5);
106 return x + min;
107 }
108
109 /**
110 * Method randomChar.
111 * @param characters char[]
112 * @return char
113 */
114 public static char randomChar(char[] characters) {
115 return characters[randomInt(0, characters.length-1)];
116 }
117
118 /**
119 * Method randomChoice.
120 * @param items Object[]
121 * @return Object
122 */
123 public static Object randomChoice(Object[] items) {
124 return items[randomInt(0, items.length-1)];
125 }
126
127 /**
128 * Method filter.
129 * @param chars char[]
130 * @param removeChar char
131 * @return char[]
132 */
133 protected static final char[] filter(char[] chars, char removeChar) {
134 int count = 0;
135 for (int i=0; i<chars.length; i++) if (chars[i] == removeChar) count++;
136 char[] results = new char[chars.length - count];
137
138 int index = 0;
139 for (int i=0; i<chars.length; i++) {
140 if (chars[i] != removeChar) results[index++] = chars[i];
141 }
142 return results;
143 }
144
145 public static junit.framework.Test suite() {
146 return new junit.framework.JUnit4TestAdapter(AbstractPropertyDescriptorTester.class);
147 }
148 }