1 /** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package test.net.sourceforge.pmd; 5 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertFalse; 8 import static org.junit.Assert.assertTrue; 9 import net.sourceforge.pmd.AbstractRule; 10 import net.sourceforge.pmd.PropertyDescriptor; 11 import net.sourceforge.pmd.Report; 12 import net.sourceforge.pmd.RuleContext; 13 import net.sourceforge.pmd.RuleViolation; 14 import net.sourceforge.pmd.ast.SimpleJavaNode; 15 import net.sourceforge.pmd.ast.SimpleNode; 16 import net.sourceforge.pmd.properties.StringProperty; 17 import net.sourceforge.pmd.symboltable.SourceFileScope; 18 19 import org.junit.Test; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 public class AbstractRuleTest { 24 25 private static class MyRule extends AbstractRule { 26 private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f); 27 28 private static final PropertyDescriptor xpath = new StringProperty("xpath", "xpath property", "", 2.0f); 29 30 private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { pd, xpath }); 31 32 protected Map<String, PropertyDescriptor> propertiesByName() { 33 return propertyDescriptorsByName; 34 } 35 36 public MyRule() { 37 setName("MyRule"); 38 setMessage("my rule msg"); 39 setPriority(3); 40 setProperty(pd, "value"); 41 } 42 } 43 44 private static class MyOtherRule extends AbstractRule { 45 private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f); 46 47 private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { pd }); 48 49 protected Map<String, PropertyDescriptor> propertiesByName() { 50 return propertyDescriptorsByName; 51 } 52 53 public MyOtherRule() { 54 setName("MyOtherRule"); 55 setMessage("my other rule"); 56 setPriority(3); 57 setProperty(pd, "value"); 58 } 59 } 60 61 @Test 62 public void testCreateRV() { 63 MyRule r = new MyRule(); 64 r.setRuleSetName("foo"); 65 RuleContext ctx = new RuleContext(); 66 ctx.setSourceCodeFilename("filename"); 67 SimpleNode s = new SimpleJavaNode(1); 68 s.testingOnly__setBeginColumn(5); 69 s.testingOnly__setBeginLine(5); 70 s.setScope(new SourceFileScope("foo")); 71 RuleViolation rv = new RuleViolation(r, ctx, s); 72 assertEquals("Line number mismatch!", 5, rv.getBeginLine()); 73 assertEquals("Filename mismatch!", "filename", rv.getFilename()); 74 assertEquals("Rule object mismatch!", r, rv.getRule()); 75 assertEquals("Rule msg mismatch!", "my rule msg", rv.getDescription()); 76 assertEquals("RuleSet name mismatch!", "foo", rv.getRule().getRuleSetName()); 77 } 78 79 @Test 80 public void testCreateRV2() { 81 MyRule r = new MyRule(); 82 RuleContext ctx = new RuleContext(); 83 ctx.setSourceCodeFilename("filename"); 84 SimpleNode s = new SimpleJavaNode(1); 85 s.testingOnly__setBeginColumn(5); 86 s.testingOnly__setBeginLine(5); 87 s.setScope(new SourceFileScope("foo")); 88 RuleViolation rv = new RuleViolation(r, ctx, s, "specificdescription"); 89 assertEquals("Line number mismatch!", 5, rv.getBeginLine()); 90 assertEquals("Filename mismatch!", "filename", rv.getFilename()); 91 assertEquals("Rule object mismatch!", r, rv.getRule()); 92 assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription()); 93 } 94 95 @Test 96 public void testRuleExclusion() { 97 MyRule r = new MyRule(); 98 RuleContext ctx = new RuleContext(); 99 Map<Integer, String> m = new HashMap<Integer, String>(); 100 m.put(new Integer(5), ""); 101 ctx.setReport(new Report()); 102 ctx.excludeLines(m); 103 ctx.setSourceCodeFilename("filename"); 104 SimpleNode n = new SimpleJavaNode(1); 105 n.testingOnly__setBeginColumn(5); 106 n.testingOnly__setBeginLine(5); 107 n.setScope(new SourceFileScope("foo")); 108 RuleViolation rv = new RuleViolation(r, ctx, n, "specificdescription"); 109 ctx.getReport().addRuleViolation(rv); 110 assertTrue(ctx.getReport().isEmpty()); 111 } 112 113 @Test 114 public void testEquals1() { 115 MyRule r = new MyRule(); 116 assertFalse("A rule is never equals to null!", r.equals(null)); 117 } 118 119 @Test 120 public void testEquals2() { 121 MyRule r = new MyRule(); 122 assertEquals("A rule must be equals to itself", r, r); 123 } 124 125 @Test 126 public void testEquals3() { 127 MyRule r1 = new MyRule(); 128 MyRule r2 = new MyRule(); 129 assertEquals("Two instances of the same rule are equal", r1, r2); 130 assertEquals("Hashcode for two instances of the same rule must be equal", r1.hashCode(), r2.hashCode()); 131 } 132 133 @Test 134 public void testEquals4() { 135 MyRule myRule = new MyRule(); 136 assertFalse("A rule cannot be equal to an object of another class", myRule.equals("MyRule")); 137 } 138 139 @Test 140 public void testEquals5() { 141 MyRule myRule = new MyRule(); 142 MyOtherRule myOtherRule = new MyOtherRule(); 143 assertFalse("Two rules from different classes cannot be equal", myRule.equals(myOtherRule)); 144 } 145 146 @Test 147 public void testEquals6() { 148 MyRule r1 = new MyRule(); 149 MyRule r2 = new MyRule(); 150 r2.setName("MyRule2"); 151 assertFalse("Rules with different names cannot be equal", r1.equals(r2)); 152 } 153 154 @Test 155 public void testEquals7() { 156 MyRule r1 = new MyRule(); 157 MyRule r2 = new MyRule(); 158 r2.setPriority(1); 159 assertFalse("Rules with different priority levels cannot be equal", r1.equals(r2)); 160 } 161 162 @Test 163 public void testEquals8() { 164 MyRule r1 = new MyRule(); 165 r1.setProperty(MyRule.xpath, "something"); 166 MyRule r2 = new MyRule(); 167 r2.setProperty(MyRule.xpath, "something else"); 168 assertFalse("Rules with different properties values cannot be equal", r1.equals(r2)); 169 } 170 171 @Test 172 public void testEquals9() { 173 MyRule r1 = new MyRule(); 174 MyRule r2 = new MyRule(); 175 r2.setProperty(MyRule.xpath, "something else"); 176 assertFalse("Rules with different properties cannot be equal", r1.equals(r2)); 177 } 178 179 @Test 180 public void testEquals10() { 181 MyRule r1 = new MyRule(); 182 MyRule r2 = new MyRule(); 183 r2.setMessage("another message"); 184 assertTrue("Rules with different messages are still equal", r1.equals(r2)); 185 assertTrue("Rules that are equal must have the same hashcode", r1.hashCode() == r2.hashCode()); 186 } 187 188 189 public static junit.framework.Test suite() { 190 return new junit.framework.JUnit4TestAdapter(AbstractRuleTest.class); 191 } 192 }