View Javadoc

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