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  /**
23   * @author Brian Remedios
24   */
25  public class PropertyAccessorTest extends SimpleAggregatorTst {
26  
27      private Rule rule;
28  
29      @Before
30      public void setUp() {
31          rule = new NonRuleWithAllPropertyTypes();
32      }
33  
34      @Test
35      public void testIntegers() {
36      	rule.setProperty(NonRuleWithAllPropertyTypes.singleInt, NumericConstants.ZERO);
37          assertSame(rule.getProperty(NonRuleWithAllPropertyTypes.singleInt), 0);
38  
39      	rule.setProperty(NonRuleWithAllPropertyTypes.multiInt, new Integer[] {NumericConstants.ZERO, 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, Boolean.FALSE});
61          assertTrue(areEqual(rule.getBooleanProperties("multiBool"), new boolean[]{true, false}));
62  
63          boolean exceptionOccurred = false;
64          try {
65          	rule.setProperties("singleBool", new Boolean[] {Boolean.TRUE, Boolean.FALSE});
66          	} catch (Exception ex) {
67          		exceptionOccurred = true;
68          	}
69          assertTrue(exceptionOccurred);
70  
71          exceptionOccurred = false;
72          try {
73          	rule.setProperty("multiBool", Boolean.TRUE);
74          	} catch (Exception ex) {
75          		exceptionOccurred = true;
76          	}
77          assertTrue(exceptionOccurred);
78  */    }
79  
80      @Test
81      public void testStrings() {
82      	rule.setProperty(NonRuleWithAllPropertyTypes.singleStr, "brian");
83          assertEquals(rule.getProperty(NonRuleWithAllPropertyTypes.singleStr), "brian");
84  
85      	rule.setProperty(NonRuleWithAllPropertyTypes.multiStr, new String[] {"hello", "world"});
86      	assertTrue(CollectionUtil.arraysAreEqual(rule.getProperty(NonRuleWithAllPropertyTypes.multiStr),  new String[] {"hello", "world"}));
87      }
88  
89      public static junit.framework.Test suite() {
90          return new junit.framework.JUnit4TestAdapter(PropertyAccessorTest.class);
91      }
92  }