1
2
3
4 package 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
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import net.sourceforge.pmd.lang.DummyLanguageModule;
15 import net.sourceforge.pmd.lang.LanguageRegistry;
16 import net.sourceforge.pmd.lang.ast.DummyNode;
17 import net.sourceforge.pmd.lang.ast.Node;
18 import net.sourceforge.pmd.lang.rule.AbstractRule;
19 import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
20 import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
21 import net.sourceforge.pmd.lang.rule.properties.StringProperty;
22
23 import org.junit.Test;
24
25 public class AbstractRuleTest {
26
27 private static class MyRule extends AbstractRule {
28 private static final StringProperty pd = new StringProperty("foo", "foo property", "x", 1.0f);
29
30 private static final StringProperty xpath = new StringProperty("xpath", "xpath property", "", 2.0f);
31
32 public MyRule() {
33 definePropertyDescriptor(pd);
34 definePropertyDescriptor(xpath);
35 setName("MyRule");
36 setMessage("my rule msg");
37 setPriority(RulePriority.MEDIUM);
38 setProperty(pd, "value");
39 }
40
41 @Override
42 public void apply(List<? extends Node> nodes, RuleContext ctx) {
43 }
44 }
45
46 private static class MyOtherRule extends AbstractRule {
47 private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f);
48
49 public MyOtherRule() {
50 definePropertyDescriptor(pd);
51 setName("MyOtherRule");
52 setMessage("my other rule");
53 setPriority(RulePriority.MEDIUM);
54 setProperty(pd, "value");
55 }
56
57 @Override
58 public void apply(List<? extends Node> nodes, RuleContext ctx) {
59 }
60 }
61
62 @Test
63 public void testCreateRV() {
64 MyRule r = new MyRule();
65 r.setRuleSetName("foo");
66 RuleContext ctx = new RuleContext();
67 ctx.setSourceCodeFilename("filename");
68 DummyNode s = new DummyNode(1);
69 s.testingOnly__setBeginColumn(5);
70 s.testingOnly__setBeginLine(5);
71 RuleViolation rv = new ParametricRuleViolation(r, ctx, s, r.getMessage());
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 DummyNode s = new DummyNode(1);
85 s.testingOnly__setBeginColumn(5);
86 s.testingOnly__setBeginLine(5);
87 RuleViolation rv = new ParametricRuleViolation(r, ctx, s, "specificdescription");
88 assertEquals("Line number mismatch!", 5, rv.getBeginLine());
89 assertEquals("Filename mismatch!", "filename", rv.getFilename());
90 assertEquals("Rule object mismatch!", r, rv.getRule());
91 assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription());
92 }
93
94 @Test
95 public void testRuleWithVariableInMessage() {
96 MyRule r = new MyRule();
97 r.definePropertyDescriptor(new IntegerProperty("testInt", "description", 0, 100, 10, 0));
98 r.setMessage("Message ${packageName} ${className} ${methodName} ${variableName} ${testInt} ${noSuchProperty}");
99 RuleContext ctx = new RuleContext();
100 ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion());
101 ctx.setReport(new Report());
102 ctx.setSourceCodeFilename("filename");
103 DummyNode s = new DummyNode(1);
104 s.testingOnly__setBeginColumn(5);
105 s.testingOnly__setBeginLine(5);
106 s.setImage("TestImage");
107 r.addViolation(ctx, s);
108 RuleViolation rv = ctx.getReport().getViolationTree().iterator().next();
109 assertEquals("Message foo 10 ${noSuchProperty}", rv.getDescription());
110 }
111
112 @Test
113 public void testRuleSuppress() {
114 MyRule r = new MyRule();
115 RuleContext ctx = new RuleContext();
116 Map<Integer, String> m = new HashMap<Integer, String>();
117 m.put(Integer.valueOf(5), "");
118 ctx.setReport(new Report());
119 ctx.getReport().suppress(m);
120 ctx.setSourceCodeFilename("filename");
121 DummyNode n = new DummyNode(1);
122 n.testingOnly__setBeginColumn(5);
123 n.testingOnly__setBeginLine(5);
124 RuleViolation rv = new ParametricRuleViolation(r, ctx, n, "specificdescription");
125 ctx.getReport().addRuleViolation(rv);
126 assertTrue(ctx.getReport().isEmpty());
127 }
128
129 @Test
130 public void testEquals1() {
131 MyRule r = new MyRule();
132 assertFalse("A rule is never equals to null!", r.equals(null));
133 }
134
135 @Test
136 public void testEquals2() {
137 MyRule r = new MyRule();
138 assertEquals("A rule must be equals to itself", r, r);
139 }
140
141 @Test
142 public void testEquals3() {
143 MyRule r1 = new MyRule();
144 MyRule r2 = new MyRule();
145 assertEquals("Two instances of the same rule are equal", r1, r2);
146 assertEquals("Hashcode for two instances of the same rule must be equal", r1.hashCode(), r2.hashCode());
147 }
148
149 @Test
150 public void testEquals4() {
151 MyRule myRule = new MyRule();
152 assertFalse("A rule cannot be equal to an object of another class", myRule.equals("MyRule"));
153 }
154
155 @Test
156 public void testEquals5() {
157 MyRule myRule = new MyRule();
158 MyOtherRule myOtherRule = new MyOtherRule();
159 assertFalse("Two rules from different classes cannot be equal", myRule.equals(myOtherRule));
160 }
161
162 @Test
163 public void testEquals6() {
164 MyRule r1 = new MyRule();
165 MyRule r2 = new MyRule();
166 r2.setName("MyRule2");
167 assertFalse("Rules with different names cannot be equal", r1.equals(r2));
168 }
169
170 @Test
171 public void testEquals7() {
172 MyRule r1 = new MyRule();
173 MyRule r2 = new MyRule();
174 r2.setPriority(RulePriority.HIGH);
175 assertFalse("Rules with different priority levels cannot be equal", r1.equals(r2));
176 }
177
178 @Test
179 public void testEquals8() {
180 MyRule r1 = new MyRule();
181 r1.setProperty(MyRule.xpath, "something");
182 MyRule r2 = new MyRule();
183 r2.setProperty(MyRule.xpath, "something else");
184 assertFalse("Rules with different properties values cannot be equal", r1.equals(r2));
185 }
186
187 @Test
188 public void testEquals9() {
189 MyRule r1 = new MyRule();
190 MyRule r2 = new MyRule();
191 r2.setProperty(MyRule.xpath, "something else");
192 assertFalse("Rules with different properties cannot be equal", r1.equals(r2));
193 }
194
195 @Test
196 public void testEquals10() {
197 MyRule r1 = new MyRule();
198 MyRule r2 = new MyRule();
199 r2.setMessage("another message");
200 assertEquals("Rules with different messages are still equal", r1, r2);
201 assertEquals("Rules that are equal must have the an equal hashcode", r1.hashCode(), r2.hashCode());
202 }
203
204 public static junit.framework.Test suite() {
205 return new junit.framework.JUnit4TestAdapter(AbstractRuleTest.class);
206 }
207 }