View Javadoc

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