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