001: package test.net.sourceforge.pmd.properties;
002:
003: import static org.junit.Assert.assertEquals;
004: import static org.junit.Assert.assertFalse;
005: import static org.junit.Assert.assertTrue;
006: import net.sourceforge.pmd.AbstractRule;
007: import net.sourceforge.pmd.cpd.ReportException;
008: import net.sourceforge.pmd.util.CollectionUtil;
009:
010: import org.junit.Before;
011: import org.junit.Ignore;
012: import org.junit.Test;
013:
014: import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
015:
016: public class PropertyAccessorTest extends SimpleAggregatorTst {
017:
018: private AbstractRule rule;
019:
020: @Before
021: public void setUp() {
022: rule = new NonRuleWithAllPropertyTypes();
023: }
024:
025: public static boolean areEqual(int[] a, int[] b) {
026: if (a.length != b.length)
027: return false;
028: for (int i = 0; i < a.length; i++) {
029: if (a[i] != b[i])
030: return false;
031: }
032: return true;
033: }
034:
035: public static boolean areEqual(boolean[] a, boolean[] b) {
036: if (a.length != b.length)
037: return false;
038: for (int i = 0; i < a.length; i++) {
039: if (a[i] != b[i])
040: return false;
041: }
042: return true;
043: }
044:
045: @Test
046: public void testIntegers() {
047: rule.setProperty(NonRuleWithAllPropertyTypes.singleInt,
048: new Integer(0));
049: assertTrue(rule
050: .getIntProperty(NonRuleWithAllPropertyTypes.singleInt) == 0);
051:
052: rule.setProperties(NonRuleWithAllPropertyTypes.multiInt,
053: new Object[] { new Integer(0), new Integer(1) });
054: assertTrue(areEqual(
055: rule
056: .getIntProperties(NonRuleWithAllPropertyTypes.multiInt),
057: new int[] { 0, 1 }));
058: }
059:
060: @Test(expected=RuntimeException.class)
061: public void testIntegersSingle() {
062: rule.setProperties(NonRuleWithAllPropertyTypes.singleInt,
063: new Object[] { new Integer(0), new Integer(1) });
064: }
065:
066: @Test(expected=RuntimeException.class)
067: public void testIntegersMultiple() {
068: rule.setProperty(NonRuleWithAllPropertyTypes.multiInt,
069: new Integer(0));
070: }
071:
072: @Test
073: public void testBooleans() {
074:
075: rule.setProperty(NonRuleWithAllPropertyTypes.singleBool,
076: Boolean.FALSE);
077: assertFalse(rule
078: .getBooleanProperty(NonRuleWithAllPropertyTypes.singleBool));
079:
080: rule.setProperties(NonRuleWithAllPropertyTypes.multiBool,
081: new Boolean[] { Boolean.TRUE, Boolean.FALSE });
082: assertTrue(areEqual(
083: rule
084: .getBooleanProperties(NonRuleWithAllPropertyTypes.multiBool),
085: new boolean[] { true, false }));
086:
087: }
088:
089: @Test(expected=RuntimeException.class)
090: public void testBooleansSingle() {
091: rule.setProperties(NonRuleWithAllPropertyTypes.singleBool,
092: new Boolean[] { Boolean.TRUE, Boolean.FALSE });
093: }
094:
095: @Test(expected=RuntimeException.class)
096: public void testBooleansMultiple() {
097:
098: rule.setProperty(NonRuleWithAllPropertyTypes.multiBool,
099: Boolean.TRUE);
100: }
101:
102: @Ignore
103: @Test
104: public void testFloats() throws ReportException {
105: /*
106: rule.setProperty("singleFloat", new Float(0));
107: assertTrue(rule.getFloatProperty("singleFloat") == 0f);
108:
109: rule.setProperties("multiBool", new Boolean[] {Boolean.TRUE, Boolean.FALSE});
110: assertTrue(areEqual(rule.getBooleanProperties("multiBool"), new boolean[]{true, false}));
111:
112: boolean exceptionOccurred = false;
113: try {
114: rule.setProperties("singleBool", new Boolean[] {Boolean.TRUE, Boolean.FALSE});
115: } catch (Exception ex) {
116: exceptionOccurred = true;
117: }
118: assertTrue(exceptionOccurred);
119:
120: exceptionOccurred = false;
121: try {
122: rule.setProperty("multiBool", Boolean.TRUE);
123: } catch (Exception ex) {
124: exceptionOccurred = true;
125: }
126: assertTrue(exceptionOccurred);
127: */}
128:
129: @Test
130: public void testStrings() {
131: rule
132: .setProperty(NonRuleWithAllPropertyTypes.singleStr,
133: "brian");
134: assertEquals(
135: rule
136: .getStringProperty(NonRuleWithAllPropertyTypes.singleStr),
137: "brian");
138:
139: rule.setProperties(NonRuleWithAllPropertyTypes.multiStr,
140: new String[] { "hello", "world" });
141: assertTrue(CollectionUtil
142: .arraysAreEqual(
143: rule
144: .getStringProperties(NonRuleWithAllPropertyTypes.multiStr),
145: new String[] { "hello", "world" }));
146: }
147:
148: @Test(expected=RuntimeException.class)
149: public void testStringsSingle() {
150: rule.setProperties(NonRuleWithAllPropertyTypes.singleStr,
151: new String[] { "hello", "world" });
152: }
153:
154: @Test(expected=RuntimeException.class)
155: public void testStringsMultiple() {
156: rule.setProperty(NonRuleWithAllPropertyTypes.multiStr, "brian");
157: }
158:
159: public static junit.framework.Test suite() {
160: return new junit.framework.JUnit4TestAdapter(
161: PropertyAccessorTest.class);
162: }
163: }
|