001: /**
002: * <copyright>
003: * Copyright 1997-2002 InfoEther, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency
005: (DARPA).
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the Cougaar Open Source License as published
009: by
010: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
013: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
014: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
015: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
016: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
017: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
018: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
019: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
020: * PERFORMANCE OF THE COUGAAR SOFTWARE.
021: * </copyright>
022: */package test.net.sourceforge.pmd;
023:
024: import static org.junit.Assert.assertEquals;
025: import static org.junit.Assert.assertFalse;
026: import static org.junit.Assert.assertTrue;
027: import net.sourceforge.pmd.AbstractRule;
028: import net.sourceforge.pmd.PropertyDescriptor;
029: import net.sourceforge.pmd.Report;
030: import net.sourceforge.pmd.RuleContext;
031: import net.sourceforge.pmd.RuleViolation;
032: import net.sourceforge.pmd.ast.SimpleJavaNode;
033: import net.sourceforge.pmd.ast.SimpleNode;
034: import net.sourceforge.pmd.properties.StringProperty;
035: import net.sourceforge.pmd.symboltable.SourceFileScope;
036:
037: import org.junit.Test;
038:
039: import java.util.HashMap;
040: import java.util.Map;
041:
042: public class AbstractRuleTest {
043:
044: private static class MyRule extends AbstractRule {
045: private static final PropertyDescriptor pd = new StringProperty(
046: "foo", "foo property", "x", 1.0f);
047:
048: private static final PropertyDescriptor xpath = new StringProperty(
049: "xpath", "xpath property", "", 2.0f);
050:
051: private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] {
052: pd, xpath });
053:
054: protected Map<String, PropertyDescriptor> propertiesByName() {
055: return propertyDescriptorsByName;
056: }
057:
058: public MyRule() {
059: setName("MyRule");
060: setMessage("my rule msg");
061: setPriority(3);
062: setProperty(pd, "value");
063: }
064: }
065:
066: private static class MyOtherRule extends AbstractRule {
067: private static final PropertyDescriptor pd = new StringProperty(
068: "foo", "foo property", "x", 1.0f);
069:
070: private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { pd });
071:
072: protected Map<String, PropertyDescriptor> propertiesByName() {
073: return propertyDescriptorsByName;
074: }
075:
076: public MyOtherRule() {
077: setName("MyOtherRule");
078: setMessage("my other rule");
079: setPriority(3);
080: setProperty(pd, "value");
081: }
082: }
083:
084: @Test
085: public void testCreateRV() {
086: MyRule r = new MyRule();
087: r.setRuleSetName("foo");
088: RuleContext ctx = new RuleContext();
089: ctx.setSourceCodeFilename("filename");
090: SimpleNode s = new SimpleJavaNode(1);
091: s.testingOnly__setBeginColumn(5);
092: s.testingOnly__setBeginLine(5);
093: s.setScope(new SourceFileScope("foo"));
094: RuleViolation rv = new RuleViolation(r, ctx, s);
095: assertEquals("Line number mismatch!", 5, rv.getBeginLine());
096: assertEquals("Filename mismatch!", "filename", rv.getFilename());
097: assertEquals("Rule object mismatch!", r, rv.getRule());
098: assertEquals("Rule msg mismatch!", "my rule msg", rv
099: .getDescription());
100: assertEquals("RuleSet name mismatch!", "foo", rv.getRule()
101: .getRuleSetName());
102: }
103:
104: @Test
105: public void testCreateRV2() {
106: MyRule r = new MyRule();
107: RuleContext ctx = new RuleContext();
108: ctx.setSourceCodeFilename("filename");
109: SimpleNode s = new SimpleJavaNode(1);
110: s.testingOnly__setBeginColumn(5);
111: s.testingOnly__setBeginLine(5);
112: s.setScope(new SourceFileScope("foo"));
113: RuleViolation rv = new RuleViolation(r, ctx, s,
114: "specificdescription");
115: assertEquals("Line number mismatch!", 5, rv.getBeginLine());
116: assertEquals("Filename mismatch!", "filename", rv.getFilename());
117: assertEquals("Rule object mismatch!", r, rv.getRule());
118: assertEquals("Rule description mismatch!",
119: "specificdescription", rv.getDescription());
120: }
121:
122: @Test
123: public void testRuleExclusion() {
124: MyRule r = new MyRule();
125: RuleContext ctx = new RuleContext();
126: Map<Integer, String> m = new HashMap<Integer, String>();
127: m.put(new Integer(5), "");
128: ctx.setReport(new Report());
129: ctx.excludeLines(m);
130: ctx.setSourceCodeFilename("filename");
131: SimpleNode n = new SimpleJavaNode(1);
132: n.testingOnly__setBeginColumn(5);
133: n.testingOnly__setBeginLine(5);
134: n.setScope(new SourceFileScope("foo"));
135: RuleViolation rv = new RuleViolation(r, ctx, n,
136: "specificdescription");
137: ctx.getReport().addRuleViolation(rv);
138: assertTrue(ctx.getReport().isEmpty());
139: }
140:
141: @Test
142: public void testEquals1() {
143: MyRule r = new MyRule();
144: assertFalse("A rule is never equals to null!", r.equals(null));
145: }
146:
147: @Test
148: public void testEquals2() {
149: MyRule r = new MyRule();
150: assertEquals("A rule must be equals to itself", r, r);
151: }
152:
153: @Test
154: public void testEquals3() {
155: MyRule r1 = new MyRule();
156: MyRule r2 = new MyRule();
157: assertEquals("Two instances of the same rule are equal", r1, r2);
158: assertEquals(
159: "Hashcode for two instances of the same rule must be equal",
160: r1.hashCode(), r2.hashCode());
161: }
162:
163: @Test
164: public void testEquals4() {
165: MyRule myRule = new MyRule();
166: assertFalse(
167: "A rule cannot be equal to an object of another class",
168: myRule.equals("MyRule"));
169: }
170:
171: @Test
172: public void testEquals5() {
173: MyRule myRule = new MyRule();
174: MyOtherRule myOtherRule = new MyOtherRule();
175: assertFalse("Two rules from different classes cannot be equal",
176: myRule.equals(myOtherRule));
177: }
178:
179: @Test
180: public void testEquals6() {
181: MyRule r1 = new MyRule();
182: MyRule r2 = new MyRule();
183: r2.setName("MyRule2");
184: assertFalse("Rules with different names cannot be equal", r1
185: .equals(r2));
186: }
187:
188: @Test
189: public void testEquals7() {
190: MyRule r1 = new MyRule();
191: MyRule r2 = new MyRule();
192: r2.setPriority(1);
193: assertFalse(
194: "Rules with different priority levels cannot be equal",
195: r1.equals(r2));
196: }
197:
198: @Test
199: public void testEquals8() {
200: MyRule r1 = new MyRule();
201: r1.setProperty(MyRule.xpath, "something");
202: MyRule r2 = new MyRule();
203: r2.setProperty(MyRule.xpath, "something else");
204: assertFalse(
205: "Rules with different properties values cannot be equal",
206: r1.equals(r2));
207: }
208:
209: @Test
210: public void testEquals9() {
211: MyRule r1 = new MyRule();
212: MyRule r2 = new MyRule();
213: r2.setProperty(MyRule.xpath, "something else");
214: assertFalse("Rules with different properties cannot be equal",
215: r1.equals(r2));
216: }
217:
218: @Test
219: public void testEquals10() {
220: MyRule r1 = new MyRule();
221: MyRule r2 = new MyRule();
222: r2.setMessage("another message");
223: assertTrue("Rules with different messages are still equal", r1
224: .equals(r2));
225: assertTrue("Rules that are equal must have the same hashcode",
226: r1.hashCode() == r2.hashCode());
227: }
228:
229: public static junit.framework.Test suite() {
230: return new junit.framework.JUnit4TestAdapter(
231: AbstractRuleTest.class);
232: }
233: }
|