001: package test.net.sourceforge.pmd.properties;
002:
003: import static org.junit.Assert.assertTrue;
004: import net.sourceforge.pmd.PropertyDescriptor;
005: import net.sourceforge.pmd.util.CollectionUtil;
006:
007: import org.junit.Test;
008:
009: /**
010: *
011: * @author Brian Remedios
012: */
013: public abstract class AbstractPropertyDescriptorTester {
014:
015: private static final int maxCardinality = 10;
016:
017: public static final String punctuationChars = "!@#$%^&*()_-+=[]{}\\|;:'\",.<>/?`~";
018: public static final String whitespaceChars = " \t\n";
019: public static final String digitChars = "0123456789";
020: public static final String alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmniopqrstuvwxyz";
021: public static final String alphaNumericChars = digitChars
022: + alphaChars;
023: public static final String allChars = punctuationChars
024: + whitespaceChars + alphaNumericChars;
025:
026: /**
027: * Method createValue.
028: * @param count int
029: * @return Object
030: */
031: protected abstract Object createValue(int count);
032:
033: /**
034: * Method createProperty.
035: * @param maxCount int
036: * @return PropertyDescriptor
037: */
038: protected abstract PropertyDescriptor createProperty(int maxCount);
039:
040: @Test
041: public void testAsDelimitedString() {
042:
043: Object testValue = createValue(maxCardinality);
044: PropertyDescriptor pmdProp = createProperty(maxCardinality);
045:
046: String storeValue = pmdProp.asDelimitedString(testValue);
047:
048: Object returnedValue = pmdProp.valueFrom(storeValue);
049:
050: assertTrue(CollectionUtil.areEqual(returnedValue, testValue));
051: }
052:
053: @Test
054: public void testValueFrom() {
055:
056: Object testValue = createValue(1);
057: PropertyDescriptor pmdProp = createProperty(1);
058:
059: String storeValue = pmdProp.asDelimitedString(testValue);
060:
061: Object returnedValue = pmdProp.valueFrom(storeValue);
062:
063: assertTrue(CollectionUtil.areEqual(returnedValue, testValue));
064: }
065:
066: @Test
067: public void testErrorFor() {
068:
069: Object testValue = createValue(1);
070: PropertyDescriptor pmdProp = createProperty(1);
071: String errorMsg = pmdProp.errorFor(testValue);
072: assertTrue(errorMsg == null);
073:
074: testValue = createValue(maxCardinality);
075: pmdProp = createProperty(maxCardinality);
076: errorMsg = pmdProp.errorFor(testValue);
077: assertTrue(errorMsg == null);
078: }
079:
080: @Test
081: public void testType() {
082:
083: PropertyDescriptor pmdProp = createProperty(1);
084:
085: assertTrue(pmdProp.type() != null);
086: }
087:
088: /**
089: * Method randomInt.
090: * @return int
091: */
092: public static int randomInt() {
093:
094: int randomVal = (int) (Math.random() * 100 + 1D);
095: return randomVal + (int) (Math.random() * 100000D);
096: }
097:
098: /**
099: * Method randomInt.
100: * @param min int
101: * @param max int
102: * @return int
103: */
104: public static int randomInt(int min, int max) {
105: if (max < min)
106: max = min;
107: int range = Math.abs(max - min);
108: int x = (int) ((range * Math.random()) + .5);
109: return x + min;
110: }
111:
112: /**
113: * Method randomChar.
114: * @param characters char[]
115: * @return char
116: */
117: public static char randomChar(char[] characters) {
118: return characters[randomInt(0, characters.length - 1)];
119: }
120:
121: /**
122: * Method randomChoice.
123: * @param items Object[]
124: * @return Object
125: */
126: public static Object randomChoice(Object[] items) {
127: return items[randomInt(0, items.length - 1)];
128: }
129:
130: /**
131: * Method filter.
132: * @param chars char[]
133: * @param removeChar char
134: * @return char[]
135: */
136: protected static final char[] filter(char[] chars, char removeChar) {
137: int count = 0;
138: for (int i = 0; i < chars.length; i++)
139: if (chars[i] == removeChar)
140: count++;
141: char[] results = new char[chars.length - count];
142:
143: int index = 0;
144: for (int i = 0; i < chars.length; i++) {
145: if (chars[i] != removeChar)
146: results[index++] = chars[i];
147: }
148: return results;
149: }
150:
151: public static junit.framework.Test suite() {
152: return new junit.framework.JUnit4TestAdapter(
153: AbstractPropertyDescriptorTester.class);
154: }
155: }
|