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.Report; 28 import net.sourceforge.pmd.RuleContext; 29 import net.sourceforge.pmd.RuleViolation; 30 import net.sourceforge.pmd.ast.SimpleNode; 31 import net.sourceforge.pmd.symboltable.SourceFileScope; 32 33 import java.util.HashSet; 34 import java.util.Set; 35 36 public class AbstractRuleTest extends TestCase { 37 38 private static class MyRule extends AbstractRule{ 39 public MyRule() { 40 setName("MyRule"); 41 setMessage("my rule"); 42 setPriority(3); 43 addProperty("foo", "value"); 44 } 45 } 46 47 private static class MyOtherRule extends AbstractRule{ 48 public MyOtherRule() { 49 setName("MyOtherRule"); 50 setMessage("my other rule"); 51 setPriority(3); 52 addProperty("foo", "value"); 53 } 54 } 55 56 public AbstractRuleTest(String name) { 57 super(name); 58 } 59 60 public void testCreateRV() { 61 MyRule r = new MyRule(); 62 r.setRuleSetName("foo"); 63 RuleContext ctx = new RuleContext(); 64 ctx.setSourceCodeFilename("filename"); 65 SimpleNode s = new SimpleNode(1); 66 s.testingOnly__setBeginColumn(5); 67 s.testingOnly__setBeginLine(5); 68 s.setScope(new SourceFileScope("foo")); 69 RuleViolation rv = r.createRuleViolation(ctx, s); 70 assertEquals("Line number mismatch!", 5, rv.getLine()); 71 assertEquals("Filename mismatch!", "filename", rv.getFilename()); 72 assertEquals("Rule object mismatch!", r, rv.getRule()); 73 assertEquals("Rule description mismatch!", "my rule", rv.getDescription()); 74 assertEquals("RuleSet name mismatch!", "foo", rv.getRule().getRuleSetName()); 75 } 76 77 public void testCreateRV2() { 78 MyRule r = new MyRule(); 79 RuleContext ctx = new RuleContext(); 80 ctx.setSourceCodeFilename("filename"); 81 SimpleNode s = new SimpleNode(1); 82 s.testingOnly__setBeginColumn(5); 83 s.testingOnly__setBeginLine(5); 84 s.setScope(new SourceFileScope("foo")); 85 RuleViolation rv = r.createRuleViolation(ctx, s, "specificdescription"); 86 assertEquals("Line number mismatch!", 5, rv.getLine()); 87 assertEquals("Filename mismatch!", "filename", rv.getFilename()); 88 assertEquals("Rule object mismatch!", r, rv.getRule()); 89 assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription()); 90 } 91 92 public void testRuleExclusion() { 93 MyRule r = new MyRule(); 94 RuleContext ctx = new RuleContext(); 95 Set s = new HashSet(); 96 s.add(new Integer(5)); 97 ctx.setReport(new Report()); 98 ctx.excludeLines(s); 99 ctx.setSourceCodeFilename("filename"); 100 SimpleNode n = new SimpleNode(1); 101 n.testingOnly__setBeginColumn(5); 102 n.testingOnly__setBeginLine(5); 103 n.setScope(new SourceFileScope("foo")); 104 r.createRuleViolation(ctx, n, "specificdescription"); 105 assertTrue(ctx.getReport().isEmpty()); 106 } 107 108 public void testEquals1() { 109 MyRule r = new MyRule(); 110 assertFalse("A rule is never equals to null!", r.equals(null)); 111 } 112 113 public void testEquals2() { 114 MyRule r = new MyRule(); 115 assertEquals("A rule must be equals to itself", r, r); 116 } 117 118 public void testEquals3() { 119 MyRule r1 = new MyRule(); 120 MyRule r2 = new MyRule(); 121 assertEquals("2 instances of the same rule are equals", r1, r2); 122 assertEquals("hasCode for 2 instances of the same rule must be equals", r1.hashCode(), r2.hashCode()); 123 } 124 125 public void testEquals4() { 126 MyRule myRule = new MyRule(); 127 assertFalse("A rule cannot be equals to an object of another class", myRule.equals("MyRule")); 128 } 129 130 public void testEquals5() { 131 MyRule myRule = new MyRule(); 132 MyOtherRule myOtherRule = new MyOtherRule(); 133 assertFalse("2 rules of different classes cannot be equals", myRule.equals(myOtherRule)); 134 assertFalse("Rules that are not equals should not have the same hashcode", myRule.hashCode() == myOtherRule.hashCode()); 135 } 136 137 public void testEquals6() { 138 MyRule r1 = new MyRule(); 139 MyRule r2 = new MyRule(); 140 r2.setName("MyRule2"); 141 assertFalse("Rules with different names cannot be equals", r1.equals(r2)); 142 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode()); 143 } 144 145 public void testEquals7() { 146 MyRule r1 = new MyRule(); 147 MyRule r2 = new MyRule(); 148 r2.setPriority(1); 149 assertFalse("Rules with different priority cannot be equals", r1.equals(r2)); 150 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode()); 151 } 152 153 public void testEquals8() { 154 MyRule r1 = new MyRule(); 155 r1.addProperty("xpath", "something"); 156 MyRule r2 = new MyRule(); 157 r2.addProperty("xpath", "something else"); 158 assertFalse("Rules with different properties values cannot be equals", r1.equals(r2)); 159 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode()); 160 } 161 162 public void testEquals9() { 163 MyRule r1 = new MyRule(); 164 MyRule r2 = new MyRule(); 165 r2.addProperty("xpath", "something else"); 166 assertFalse("Rules with different properties cannot be equals", r1.equals(r2)); 167 assertFalse("Rules that are not equals should not have the same hashcode", r1.hashCode() == r2.hashCode()); 168 } 169 170 public void testEquals10() { 171 MyRule r1 = new MyRule(); 172 MyRule r2 = new MyRule(); 173 r2.setMessage("another message"); 174 assertTrue("Rules with different message are still equals", r1.equals(r2)); 175 assertTrue("Rules that are equals must have the same hashcode", r1.hashCode() == r2.hashCode()); 176 } 177 178 }