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