1 /***
2 * <copyright>
3 * Copyright 1997-2002 InfoEther, LLC
4 * under sponsorship of the Defense Advanced Research Projects Agency
5 (DARPA).
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Cougaar Open Source License as published
9 by
10 * DARPA on the Cougaar Open Source Website (www.cougaar.org).
11 *
12 * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
13 * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
14 * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
16 * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
17 * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
19 * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THE COUGAAR SOFTWARE.
21 * </copyright>
22 */
23 package test.net.sourceforge.pmd;
24
25 import junit.framework.TestCase;
26 import net.sourceforge.pmd.AbstractRule;
27 import net.sourceforge.pmd.PropertyDescriptor;
28 import net.sourceforge.pmd.Report;
29 import net.sourceforge.pmd.RuleContext;
30 import net.sourceforge.pmd.RuleViolation;
31 import net.sourceforge.pmd.ast.SimpleJavaNode;
32 import net.sourceforge.pmd.ast.SimpleNode;
33 import net.sourceforge.pmd.properties.StringProperty;
34 import net.sourceforge.pmd.symboltable.SourceFileScope;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 public class AbstractRuleTest extends TestCase {
40
41 private static class MyRule extends AbstractRule {
42 private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f);
43
44 private static final PropertyDescriptor xpath = new StringProperty("xpath", "xpath property", "", 2.0f);
45
46 private static final Map propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { pd, xpath });
47
48 protected Map propertiesByName() {
49 return propertyDescriptorsByName;
50 }
51
52 public MyRule() {
53 setName("MyRule");
54 setMessage("my rule msg");
55 setPriority(3);
56 setProperty(pd, "value");
57 }
58 }
59
60 private static class MyOtherRule extends AbstractRule {
61 private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f);
62
63 private static final Map propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { pd });
64
65 protected Map propertiesByName() {
66 return propertyDescriptorsByName;
67 }
68
69 public MyOtherRule() {
70 setName("MyOtherRule");
71 setMessage("my other rule");
72 setPriority(3);
73 setProperty(pd, "value");
74 }
75 }
76
77 public AbstractRuleTest(String name) {
78 super(name);
79 }
80
81 public void testCreateRV() {
82 MyRule r = new MyRule();
83 r.setRuleSetName("foo");
84 RuleContext ctx = new RuleContext();
85 ctx.setSourceCodeFilename("filename");
86 SimpleNode s = new SimpleJavaNode(1);
87 s.testingOnly__setBeginColumn(5);
88 s.testingOnly__setBeginLine(5);
89 s.setScope(new SourceFileScope("foo"));
90 RuleViolation rv = new RuleViolation(r, ctx, s);
91 assertEquals("Line number mismatch!", 5, rv.getBeginLine());
92 assertEquals("Filename mismatch!", "filename", rv.getFilename());
93 assertEquals("Rule object mismatch!", r, rv.getRule());
94 assertEquals("Rule msg mismatch!", "my rule msg", rv.getDescription());
95 assertEquals("RuleSet name mismatch!", "foo", rv.getRule().getRuleSetName());
96 }
97
98 public void testCreateRV2() {
99 MyRule r = new MyRule();
100 RuleContext ctx = new RuleContext();
101 ctx.setSourceCodeFilename("filename");
102 SimpleNode s = new SimpleJavaNode(1);
103 s.testingOnly__setBeginColumn(5);
104 s.testingOnly__setBeginLine(5);
105 s.setScope(new SourceFileScope("foo"));
106 RuleViolation rv = new RuleViolation(r, ctx, s, "specificdescription");
107 assertEquals("Line number mismatch!", 5, rv.getBeginLine());
108 assertEquals("Filename mismatch!", "filename", rv.getFilename());
109 assertEquals("Rule object mismatch!", r, rv.getRule());
110 assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription());
111 }
112
113 public void testRuleExclusion() {
114 MyRule r = new MyRule();
115 RuleContext ctx = new RuleContext();
116 Map m = new HashMap();
117 m.put(new Integer(5), "");
118 ctx.setReport(new Report());
119 ctx.excludeLines(m);
120 ctx.setSourceCodeFilename("filename");
121 SimpleNode n = new SimpleJavaNode(1);
122 n.testingOnly__setBeginColumn(5);
123 n.testingOnly__setBeginLine(5);
124 n.setScope(new SourceFileScope("foo"));
125 RuleViolation rv = new RuleViolation(r, ctx, n, "specificdescription");
126 ctx.getReport().addRuleViolation(rv);
127 assertTrue(ctx.getReport().isEmpty());
128 }
129
130 public void testEquals1() {
131 MyRule r = new MyRule();
132 assertFalse("A rule is never equals to null!", r.equals(null));
133 }
134
135 public void testEquals2() {
136 MyRule r = new MyRule();
137 assertEquals("A rule must be equals to itself", r, r);
138 }
139
140 public void testEquals3() {
141 MyRule r1 = new MyRule();
142 MyRule r2 = new MyRule();
143 assertEquals("2 instances of the same rule are equals", r1, r2);
144 assertEquals("hasCode for 2 instances of the same rule must be equals", r1.hashCode(), r2.hashCode());
145 }
146
147 public void testEquals4() {
148 MyRule myRule = new MyRule();
149 assertFalse("A rule cannot be equals to an object of another class", myRule.equals("MyRule"));
150 }
151
152 public void testEquals5() {
153 MyRule myRule = new MyRule();
154 MyOtherRule myOtherRule = new MyOtherRule();
155 assertFalse("2 rules of different classes cannot be equals", myRule.equals(myOtherRule));
156 assertFalse("Rules that are not equals should not have the same hashcode", myRule.hashCode() == myOtherRule.hashCode());
157 }
158
159 public void testEquals6() {
160 MyRule r1 = new MyRule();
161 MyRule r2 = new MyRule();
162 r2.setName("MyRule2");
163 assertFalse("Rules with different names cannot be equals", r1.equals(r2));
164 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode());
165 }
166
167 public void testEquals7() {
168 MyRule r1 = new MyRule();
169 MyRule r2 = new MyRule();
170 r2.setPriority(1);
171 assertFalse("Rules with different priority cannot be equals", r1.equals(r2));
172 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode());
173 }
174
175 public void testEquals8() {
176 MyRule r1 = new MyRule();
177 r1.setProperty(MyRule.xpath, "something");
178 MyRule r2 = new MyRule();
179 r2.setProperty(MyRule.xpath, "something else");
180 assertFalse("Rules with different properties values cannot be equals", r1.equals(r2));
181 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode());
182 }
183
184 public void testEquals9() {
185 MyRule r1 = new MyRule();
186 MyRule r2 = new MyRule();
187 r2.setProperty(MyRule.xpath, "something else");
188 assertFalse("Rules with different properties cannot be equals", r1.equals(r2));
189 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode());
190 }
191
192 public void testEquals10() {
193 MyRule r1 = new MyRule();
194 MyRule r2 = new MyRule();
195 r2.setMessage("another message");
196 assertTrue("Rules with different message are still equals", r1.equals(r2));
197 assertTrue("Rules that are equals must have the same hashcode", r1.hashCode() == r2.hashCode());
198 }
199
200 }