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.Collections; 37 import java.util.HashSet; 38 import java.util.Iterator; 39 import java.util.List; 40 import java.util.Random; 41 import java.util.Set; 42 43 public class RuleSetTest extends TestCase { 44 private String javaCode = "public class Test { }"; 45 46 public void testConstructor() { 47 new RuleSet(); 48 } 49 50 public void testAccessors() { 51 RuleSet rs = new RuleSet(); 52 rs.setName("foo"); 53 assertEquals("name mismatch", "foo", rs.getName()); 54 rs.setDescription("bar"); 55 assertEquals("description mismatch", "bar", rs.getDescription()); 56 } 57 58 public void testGetRuleByName() { 59 RuleSet rs = new RuleSet(); 60 MockRule mock = new MockRule("name", "desc", "msg"); 61 rs.addRule(mock); 62 assertEquals("unable to fetch rule by name", mock, rs.getRuleByName("name")); 63 } 64 65 public void testRuleList() { 66 RuleSet IUT = new RuleSet(); 67 68 assertEquals("Size of RuleSet isn't zero.", 0, IUT.size()); 69 70 MockRule rule = new MockRule("name", "desc", "msg"); 71 IUT.addRule(rule); 72 73 assertEquals("Size of RuleSet isn't one.", 1, IUT.size()); 74 75 Set rules = IUT.getRules(); 76 77 Iterator i = rules.iterator(); 78 assertTrue("Empty Set", i.hasNext()); 79 assertEquals("Returned set of wrong size.", 1, rules.size()); 80 assertEquals("Rule isn't in ruleset.", rule, i.next()); 81 } 82 83 public void testAddRuleSet() { 84 RuleSet set1 = new RuleSet(); 85 set1.addRule(new MockRule("name", "desc", "msg")); 86 RuleSet set2 = new RuleSet(); 87 set2.addRule(new MockRule("name", "desc", "msg")); 88 set1.addRuleSet(set2); 89 assertEquals("ruleset size wrong", 2, set1.size()); 90 } 91 92 public void testApply0Rules() throws Throwable { 93 RuleSet IUT = new RuleSet(); 94 verifyRuleSet(IUT, 0, new HashSet()); 95 } 96 97 public void testApply1Rule() throws Throwable { 98 RuleSet IUT = new RuleSet(); 99 100 MockRule rule = new MockRule("name", "desc", "msg"); 101 RuleContext ctx = new RuleContext(); 102 ctx.setSourceCodeFilename("filename"); 103 RuleViolation violation = new RuleViolation(rule, 1, ctx); 104 rule.addViolation(violation); 105 106 IUT.addRule(rule); 107 108 verifyRuleSet(IUT, 1, Collections.singleton(violation)); 109 } 110 111 public void testApplyNRule() throws Throwable { 112 RuleSet IUT = new RuleSet(); 113 114 Random rand = new Random(); 115 int numRules = rand.nextInt(10) + 1; 116 Set ruleViolations = new HashSet(); 117 118 for (int i = 0; i < numRules; i++) { 119 MockRule rule = new MockRule("name", "desc", "msg"); 120 RuleContext ctx = new RuleContext(); 121 ctx.setSourceCodeFilename("filename"); 122 RuleViolation violation = new RuleViolation(rule, i, ctx); 123 124 ruleViolations.add(violation); 125 rule.addViolation(violation); 126 127 IUT.addRule(rule); 128 } 129 130 verifyRuleSet(IUT, numRules, ruleViolations); 131 } 132 133 protected void verifyRuleSet(RuleSet IUT, int size, Set values) throws Throwable { 134 135 RuleContext context = new RuleContext(); 136 Set reportedValues = new HashSet(); 137 context.setReport(new Report()); 138 IUT.apply(makeCompilationUnits(), context); 139 140 assertEquals("Invalid number of Violations Reported", size, context.getReport().size()); 141 142 Iterator violations = context.getReport().iterator(); 143 while (violations.hasNext()) { 144 RuleViolation violation = (RuleViolation) violations.next(); 145 146 reportedValues.add(violation); 147 assertTrue("Unexpected Violation Returned: " + violation, values.contains(violation)); 148 } 149 150 Iterator expected = values.iterator(); 151 while (expected.hasNext()) { 152 RuleViolation violation = (RuleViolation) expected.next(); 153 assertTrue("Expected Violation not Returned: " + violation, reportedValues.contains(violation)); 154 } 155 } 156 157 158 protected List makeCompilationUnits() throws Throwable { 159 List RC = new ArrayList(); 160 JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode)); 161 RC.add(parser.CompilationUnit()); 162 return RC; 163 } 164 }