    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.Report;
    27  import net.sourceforge.pmd.RuleContext;
    28  import net.sourceforge.pmd.RuleSet;
    29  import net.sourceforge.pmd.RuleViolation;
    30  import net.sourceforge.pmd.TargetJDK1_4;
    31  import net.sourceforge.pmd.ast.JavaParser;
    32  import test.net.sourceforge.pmd.testframework.MockRule;
    33
    34  import java.io.StringReader;
    35  import java.util.ArrayList;
    36  import java.util.Collection;
    37  import java.util.HashSet;
    38  import java.util.Iterator;
    39  import java.util.List;
    40  import java.util.Set;
    41
    42  public class RuleSetTest extends TestCase {
    43
    44      private String javaCode = "public class Test { }";
    45
    46      public void testNoDFA() {
    47          RuleSet rs = new RuleSet();
    48          MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    49          rs.addRule(mock);
    50          assertFalse(rs.usesDFA());
    51      }
    52
    53      public void testIncludesRuleWithDFA() {
    54          RuleSet rs = new RuleSet();
    55          MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    56          mock.setUsesDFA();
    57          rs.addRule(mock);
    58          assertTrue(rs.usesDFA());
    59      }
    60
    61      public void testAccessors() {
    62          RuleSet rs = new RuleSet();
    63          rs.setName("foo");
    64          assertEquals("name mismatch", "foo", rs.getName());
    65          rs.setDescription("bar");
    66          assertEquals("description mismatch", "bar", rs.getDescription());
    67      }
    68
    69      public void testGetRuleByName() {
    70          RuleSet rs = new RuleSet();
    71          MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    72          rs.addRule(mock);
    73          assertEquals("unable to fetch rule by name", mock, rs.getRuleByName("name"));
    74      }
    75
    76      public void testGetRuleByName2() {
    77          RuleSet rs = new RuleSet();
    78          MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    79          rs.addRule(mock);
    80          assertNull("the rule FooRule must not be found!", rs.getRuleByName("FooRule"));
    81      }
    82
    83      public void testRuleList() {
    84          RuleSet IUT = new RuleSet();
    85
    86          assertEquals("Size of RuleSet isn't zero.", 0, IUT.size());
    87
    88          MockRule rule = new MockRule("name", "desc", "msg", "rulesetname");
    89          IUT.addRule(rule);
    90
    91          assertEquals("Size of RuleSet isn't one.", 1, IUT.size());
    92
    93          Collection rules = IUT.getRules();
    94
    95          Iterator i = rules.iterator();
    96          assertTrue("Empty Set", i.hasNext());
    97          assertEquals("Returned set of wrong size.", 1, rules.size());
    98          assertEquals("Rule isn't in ruleset.", rule, i.next());
    99      }
    100
    101     public void testAddRuleSet() {
    102         RuleSet set1 = new RuleSet();
    103         set1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    104         RuleSet set2 = new RuleSet();
    105         set2.addRule(new MockRule("name2", "desc", "msg", "rulesetname"));
    106         set1.addRuleSet(set2);
    107         assertEquals("ruleset size wrong", 2, set1.size());
    108     }
    109
    110     public void testApply0Rules() throws Throwable {
    111         RuleSet IUT = new RuleSet();
    112         verifyRuleSet(IUT, 0, new HashSet());
    113     }
    114
    115     public void testEquals1() {
    116         RuleSet s = new RuleSet();
    117         assertFalse("A ruleset cannot be equals to null", s.equals(null));
    118     }
    119
    120     public void testEquals2() {
    121         RuleSet s = new RuleSet();
    122         assertTrue("A rulset must be equals to itself", s.equals(s));
    123     }
    124
    125     public void testEquals3() {
    126         RuleSet s = new RuleSet();
    127         s.setName("basic rules");
    128         assertFalse("A ruleset cannot be equals to another kind of object", s.equals("basic rules"));
    129     }
    130
    131     public void testEquals4() {
    132         RuleSet s1 = new RuleSet();
    133         s1.setName("my ruleset");
    134         s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    135
    136         RuleSet s2 = new RuleSet();
    137         s2.setName("my ruleset");
    138         s2.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    139
    140         assertEquals("2 rulesets with same name and rules must be equals", s1, s2);
    141         assertEquals("Equals rulesets must have the same hashcode", s1.hashCode(), s2.hashCode());
    142     }
    143
    144     public void testEquals5() {
    145         RuleSet s1 = new RuleSet();
    146         s1.setName("my ruleset");
    147         s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    148
    149         RuleSet s2 = new RuleSet();
    150         s2.setName("my other ruleset");
    151         s2.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    152
    153         assertFalse("2 rulesets with different name but same rules must not be equals", s1.equals(s2));
    154         assertFalse("Rulesets that are not equals should not have the same hashcode", s1.hashCode() == s2.hashCode());
    155     }
    156
    157     public void testEquals6() {
    158         RuleSet s1 = new RuleSet();
    159         s1.setName("my ruleset");
    160         s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
    161
    162         RuleSet s2 = new RuleSet();
    163         s2.setName("my ruleset");
    164         s2.addRule(new MockRule("other rule", "desc", "msg", "rulesetname"));
    165
    166         assertFalse("2 rulesets with same name but different rules must not be equals", s1.equals(s2));
    167         assertFalse("Rulesets that are not equals should not have the same hashcode", s1.hashCode() == s2.hashCode());
    168     }
    169
    170     protected void verifyRuleSet(RuleSet IUT, int size, Set values) throws Throwable {
    171
    172         RuleContext context = new RuleContext();
    173         Set reportedValues = new HashSet();
    174         context.setReport(new Report());
    175         IUT.apply(makeCompilationUnits(), context);
    176
    177         assertEquals("Invalid number of Violations Reported", size, context.getReport().size());
    178
    179         Iterator violations = context.getReport().iterator();
    180         while (violations.hasNext()) {
    181             RuleViolation violation = (RuleViolation) violations.next();
    182
    183             reportedValues.add(violation);
    184             assertTrue("Unexpected Violation Returned: " + violation, values.contains(violation));
    185         }
    186
    187         Iterator expected = values.iterator();
    188         while (expected.hasNext()) {
    189             RuleViolation violation = (RuleViolation) expected.next();
    190             assertTrue("Expected Violation not Returned: " + violation, reportedValues.contains(violation));
    191         }
    192     }
    193
    194
    195     protected List makeCompilationUnits() throws Throwable {
    196         List RC = new ArrayList();
    197         JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode));
    198         RC.add(parser.CompilationUnit());
    199         return RC;
    200     }
    201 }