1   package test.net.sourceforge.pmd.properties;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertTrue;
6   import net.sourceforge.pmd.AbstractRule;
7   import net.sourceforge.pmd.cpd.ReportException;
8   import net.sourceforge.pmd.util.CollectionUtil;
9   
10  import org.junit.Before;
11  import org.junit.Ignore;
12  import org.junit.Test;
13  
14  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
15  
16  public class PropertyAccessorTest extends SimpleAggregatorTst {
17  
18      private AbstractRule rule;
19  
20      @Before
21      public void setUp() {
22          rule = new NonRuleWithAllPropertyTypes();
23      }
24  
25      public static boolean areEqual(int[] a, int[] b) {
26      	if (a.length != b.length) return false;
27      	for (int i=0; i<a.length; i++) {
28      		if (a[i] != b[i]) return false;
29      	}
30      	return true;
31      }
32     
33      public static boolean areEqual(boolean[] a, boolean[] b) {
34      	if (a.length != b.length) return false;
35      	for (int i=0; i<a.length; i++) {
36      		if (a[i] != b[i]) return false;
37      	}
38      	return true;
39      }
40      
41      @Test
42      public void testIntegers() {
43      	rule.setProperty(NonRuleWithAllPropertyTypes.singleInt, new Integer(0));
44          assertTrue(rule.getIntProperty(NonRuleWithAllPropertyTypes.singleInt) == 0);
45          
46      	rule.setProperties(NonRuleWithAllPropertyTypes.multiInt, new Object[] {new Integer(0), new Integer(1)});
47          assertTrue(areEqual(rule.getIntProperties(NonRuleWithAllPropertyTypes.multiInt), new int[]{0, 1}));
48      }
49      
50      @Test(expected = RuntimeException.class)
51      public void testIntegersSingle() {
52          rule.setProperties(NonRuleWithAllPropertyTypes.singleInt, new Object[] { new Integer(0), new Integer(1) });
53      }
54  
55      @Test(expected=RuntimeException.class)
56      public void testIntegersMultiple() {
57          rule.setProperty(NonRuleWithAllPropertyTypes.multiInt, new Integer(0));
58      }
59       
60      @Test
61      public void testBooleans() {
62  
63      	rule.setProperty(NonRuleWithAllPropertyTypes.singleBool, Boolean.FALSE);
64          assertFalse(rule.getBooleanProperty(NonRuleWithAllPropertyTypes.singleBool));
65          
66      	rule.setProperties(NonRuleWithAllPropertyTypes.multiBool, new Boolean[] {Boolean.TRUE, Boolean.FALSE});
67          assertTrue(areEqual(rule.getBooleanProperties(NonRuleWithAllPropertyTypes.multiBool), new boolean[]{true, false}));
68          
69      }
70      
71      @Test(expected = RuntimeException.class)
72      public void testBooleansSingle() {
73          rule.setProperties(NonRuleWithAllPropertyTypes.singleBool, new Boolean[] { Boolean.TRUE, Boolean.FALSE });
74      }
75  
76      @Test(expected = RuntimeException.class)
77      public void testBooleansMultiple() {
78  
79          rule.setProperty(NonRuleWithAllPropertyTypes.multiBool, Boolean.TRUE);
80      }
81      
82      @Ignore
83      @Test
84      public void testFloats() throws ReportException {
85  /*
86      	rule.setProperty("singleFloat", new Float(0));
87          assertTrue(rule.getFloatProperty("singleFloat") == 0f);
88          
89      	rule.setProperties("multiBool", new Boolean[] {Boolean.TRUE, Boolean.FALSE});
90          assertTrue(areEqual(rule.getBooleanProperties("multiBool"), new boolean[]{true, false}));
91          
92          boolean exceptionOccurred = false;
93          try {
94          	rule.setProperties("singleBool", new Boolean[] {Boolean.TRUE, Boolean.FALSE});
95          	} catch (Exception ex) {
96          		exceptionOccurred = true;
97          	}
98          assertTrue(exceptionOccurred);
99          
100         exceptionOccurred = false;
101         try {
102         	rule.setProperty("multiBool", Boolean.TRUE);
103         	} catch (Exception ex) {
104         		exceptionOccurred = true;
105         	}
106         assertTrue(exceptionOccurred);
107 */    }
108     
109     @Test
110     public void testStrings() {
111     	rule.setProperty(NonRuleWithAllPropertyTypes.singleStr, "brian");
112         assertEquals(rule.getStringProperty(NonRuleWithAllPropertyTypes.singleStr), "brian");
113         
114     	rule.setProperties(NonRuleWithAllPropertyTypes.multiStr, new String[] {"hello", "world"});
115     	assertTrue(CollectionUtil.arraysAreEqual(rule.getStringProperties(NonRuleWithAllPropertyTypes.multiStr),  new String[] {"hello", "world"}));
116     }
117 
118     @Test(expected = RuntimeException.class)
119     public void testStringsSingle() {
120         rule.setProperties(NonRuleWithAllPropertyTypes.singleStr, new String[] { "hello", "world" });
121     }
122 
123     @Test(expected = RuntimeException.class)
124     public void testStringsMultiple() {
125         rule.setProperty(NonRuleWithAllPropertyTypes.multiStr, "brian");
126     }
127 
128     public static junit.framework.Test suite() {
129         return new junit.framework.JUnit4TestAdapter(PropertyAccessorTest.class);
130     }
131 }