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