View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.properties;
5   
6   import static org.junit.Assert.assertArrayEquals;
7   import static org.junit.Assert.assertEquals;
8   import static org.junit.Assert.assertFalse;
9   import static org.junit.Assert.assertSame;
10  import static org.junit.Assert.assertTrue;
11  import net.sourceforge.pmd.Rule;
12  import net.sourceforge.pmd.cpd.ReportException;
13  import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
14  import net.sourceforge.pmd.util.CollectionUtil;
15  import net.sourceforge.pmd.util.NumericConstants;
16  
17  import org.junit.Before;
18  import org.junit.Ignore;
19  import org.junit.Test;
20  
21  /**
22   * @author Brian Remedios
23   */
24  public class PropertyAccessorTest extends SimpleAggregatorTst {
25  
26      private Rule rule;
27  
28      @Before
29      public void setUpSingleRule() {
30          rule = new NonRuleWithAllPropertyTypes();
31      }
32  
33      @Test
34      public void testIntegers() {
35          rule.setProperty(NonRuleWithAllPropertyTypes.singleInt, NumericConstants.ZERO);
36          assertSame(rule.getProperty(NonRuleWithAllPropertyTypes.singleInt), 0);
37  
38          rule.setProperty(NonRuleWithAllPropertyTypes.multiInt, new Integer[] { NumericConstants.ZERO,
39                  NumericConstants.ONE });
40          assertArrayEquals(rule.getProperty(NonRuleWithAllPropertyTypes.multiInt), new Integer[] { 0, 1 });
41      }
42  
43      @Test
44      public void testBooleans() {
45  
46          rule.setProperty(NonRuleWithAllPropertyTypes.singleBool, Boolean.FALSE);
47          assertFalse(rule.getProperty(NonRuleWithAllPropertyTypes.singleBool));
48  
49          rule.setProperty(NonRuleWithAllPropertyTypes.multiBool, new Boolean[] { Boolean.TRUE, Boolean.FALSE });
50          assertArrayEquals(rule.getProperty(NonRuleWithAllPropertyTypes.multiBool), new Boolean[] { true, false });
51      }
52  
53      @Ignore
54      @Test
55      public void testFloats() throws ReportException {
56          /*
57           * rule.setProperty("singleFloat", new Float(0));
58           * assertTrue(rule.getFloatProperty("singleFloat") == 0f);
59           * 
60           * rule.setProperties("multiBool", new Boolean[] {Boolean.TRUE,
61           * Boolean.FALSE});
62           * assertTrue(areEqual(rule.getBooleanProperties("multiBool"), new
63           * boolean[]{true, false}));
64           * 
65           * boolean exceptionOccurred = false; try {
66           * rule.setProperties("singleBool", new Boolean[] {Boolean.TRUE,
67           * Boolean.FALSE}); } catch (Exception ex) { exceptionOccurred = true; }
68           * assertTrue(exceptionOccurred);
69           * 
70           * exceptionOccurred = false; try { rule.setProperty("multiBool",
71           * Boolean.TRUE); } catch (Exception ex) { exceptionOccurred = true; }
72           * assertTrue(exceptionOccurred);
73           */}
74  
75      @Test
76      public void testStrings() {
77          rule.setProperty(NonRuleWithAllPropertyTypes.singleStr, "brian");
78          assertEquals(rule.getProperty(NonRuleWithAllPropertyTypes.singleStr), "brian");
79  
80          rule.setProperty(NonRuleWithAllPropertyTypes.multiStr, new String[] { "hello", "world" });
81          assertTrue(CollectionUtil.arraysAreEqual(rule.getProperty(NonRuleWithAllPropertyTypes.multiStr), new String[] {
82                  "hello", "world" }));
83      }
84  }