1 package test.net.sourceforge.pmd.properties; 2 3 import net.sourceforge.pmd.AbstractRule; 4 import net.sourceforge.pmd.cpd.ReportException; 5 import net.sourceforge.pmd.util.CollectionUtil; 6 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 7 8 public class PropertyAccessorTest extends SimpleAggregatorTst { 9 10 private AbstractRule rule; 11 12 public void setUp() { 13 rule = new NonRuleWithAllPropertyTypes(); 14 } 15 16 public static boolean areEqual(int[] a, int[] b) { 17 if (a.length != b.length) return false; 18 for (int i=0; i<a.length; i++) { 19 if (a[i] != b[i]) return false; 20 } 21 return true; 22 } 23 24 public static boolean areEqual(boolean[] a, boolean[] b) { 25 if (a.length != b.length) return false; 26 for (int i=0; i<a.length; i++) { 27 if (a[i] != b[i]) return false; 28 } 29 return true; 30 } 31 32 public void testIntegers() throws ReportException { 33 34 rule.setProperty(NonRuleWithAllPropertyTypes.singleInt, new Integer (0)); 35 assertTrue(rule.getIntProperty(NonRuleWithAllPropertyTypes.singleInt) == 0); 36 37 rule.setProperties(NonRuleWithAllPropertyTypes.multiInt, new Object [] {new Integer (0), new Integer (1)}); 38 assertTrue(areEqual(rule.getIntProperties(NonRuleWithAllPropertyTypes.multiInt), new int[]{0, 1})); 39 40 boolean exceptionOccurred = false; 41 try { 42 rule.setProperties(NonRuleWithAllPropertyTypes.singleInt, new Object [] {new Integer (0), new Integer (1)}); 43 } catch (Exception ex) { 44 exceptionOccurred = true; 45 } 46 assertTrue(exceptionOccurred); 47 48 exceptionOccurred = false; 49 try { 50 rule.setProperty(NonRuleWithAllPropertyTypes.multiInt, new Integer (0)); 51 } catch (Exception ex) { 52 exceptionOccurred = true; 53 } 54 assertTrue(exceptionOccurred); 55 } 56 57 public void testBooleans() throws ReportException { 58 59 rule.setProperty(NonRuleWithAllPropertyTypes.singleBool, Boolean.FALSE); 60 assertFalse(rule.getBooleanProperty(NonRuleWithAllPropertyTypes.singleBool)); 61 62 rule.setProperties(NonRuleWithAllPropertyTypes.multiBool, new Boolean [] {Boolean.TRUE, Boolean.FALSE}); 63 assertTrue(areEqual(rule.getBooleanProperties(NonRuleWithAllPropertyTypes.multiBool), new boolean[]{true, false})); 64 65 boolean exceptionOccurred = false; 66 try { 67 rule.setProperties(NonRuleWithAllPropertyTypes.singleBool, new Boolean [] {Boolean.TRUE, Boolean.FALSE}); 68 } catch (Exception ex) { 69 exceptionOccurred = true; 70 } 71 assertTrue(exceptionOccurred); 72 73 exceptionOccurred = false; 74 try { 75 rule.setProperty(NonRuleWithAllPropertyTypes.multiBool, Boolean.TRUE); 76 } catch (Exception ex) { 77 exceptionOccurred = true; 78 } 79 assertTrue(exceptionOccurred); 80 } 81 82 107 public void testStrings() throws ReportException { 108 109 rule.setProperty(NonRuleWithAllPropertyTypes.singleStr, "brian"); 110 assertEquals(rule.getStringProperty(NonRuleWithAllPropertyTypes.singleStr), "brian"); 111 112 rule.setProperties(NonRuleWithAllPropertyTypes.multiStr, new String [] {"hello", "world"}); 113 assertTrue(CollectionUtil.arraysAreEqual(rule.getStringProperties(NonRuleWithAllPropertyTypes.multiStr), new String [] {"hello", "world"})); 114 115 boolean exceptionOccurred = false; 116 try { 117 rule.setProperties(NonRuleWithAllPropertyTypes.singleStr, new String [] {"hello", "world"}); 118 } catch (Exception ex) { 119 exceptionOccurred = true; 120 } 121 assertTrue(exceptionOccurred); 122 123 exceptionOccurred = false; 124 try { 125 rule.setProperty(NonRuleWithAllPropertyTypes.multiStr, "brian"); 126 } catch (Exception ex) { 127 exceptionOccurred = true; 128 } 129 assertTrue(exceptionOccurred); 130 } 131 } 132 | Popular Tags |