View Javadoc
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.Rule; 27 import net.sourceforge.pmd.RuleSet; 28 import net.sourceforge.pmd.RuleSetFactory; 29 import net.sourceforge.pmd.RuleSetNotFoundException; 30 31 import java.io.ByteArrayInputStream; 32 import java.util.HashSet; 33 import java.util.Iterator; 34 import java.util.Set; 35 36 public class RuleSetFactoryTest extends TestCase { 37 38 private static final String EOL = System.getProperty("line.separator", "\n"); 39 40 private static final String EMPTY_RULE_SET = 41 "<?xml version=\"1.0\"?>" + EOL + 42 "<ruleset name=\"test\">" + EOL + 43 "<description>testdesc</description>" + EOL + 44 "</ruleset>"; 45 46 private static final String SINGLE_RULE_SET = 47 "<?xml version=\"1.0\"?>" + EOL + 48 "<ruleset name=\"test\">" + EOL + 49 "<description>" + EOL + 50 "testdesc" + EOL + 51 "</description>" + EOL + 52 "<rule " + EOL + 53 "name=\"MockRuleName\" " + EOL + 54 "message=\"avoid the mock rule\" " + EOL + 55 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + 56 "</rule></ruleset>"; 57 58 private static final String MULTIPLE_RULE_SET = 59 "<?xml version=\"1.0\"?>" + EOL + 60 "<ruleset name=\"test\">" + EOL + 61 "<description>" + EOL + 62 "testdesc" + EOL + "</description>" + EOL + 63 "<rule name=\"MockRuleName1\" " + EOL + 64 "message=\"avoid the mock rule\" " + EOL + 65 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + EOL + 66 "</rule>" + EOL + 67 "<rule name=\"MockRuleName2\" " + EOL + 68 "message=\"avoid the mock rule\" " + EOL + 69 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + EOL + 70 "</rule></ruleset>"; 71 72 private static final String RULE_WITH_PROPERTIES = 73 "<?xml version=\"1.0\"?>" + EOL + 74 "<ruleset name=\"test\">" + EOL + 75 "<description>" + EOL + 76 "testdesc" + EOL + 77 "</description>" + EOL + 78 "<rule name=\"MockRuleName\" " + EOL + 79 "message=\"avoid the mock rule\" " + EOL + 80 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + EOL + 81 "<description>" + EOL + "testdesc2" + EOL + 82 "</description>" + EOL + 83 "<properties>" + EOL + 84 "<property name=\"fooBoolean\" value=\"true\"/>" + EOL + 85 "<property name=\"fooDouble\" value=\"1.0\" />" + EOL + 86 "<property name=\"foo\" value=\"bar\"/>" + EOL + 87 "<property name=\"fooint\" value=\"2\"/>" + EOL + 88 "</properties>" + EOL + 89 "</rule></ruleset>"; 90 91 private static final String RULE_WITH_XPATH = 92 "<?xml version=\"1.0\"?>" + EOL + 93 "<ruleset name=\"test\">" + EOL + 94 "<description>" + EOL + 95 "testdesc" + EOL + 96 "</description>" + EOL + 97 "<priority>3</priority>" + EOL + 98 "<rule name=\"MockRuleName\" " + EOL + 99 "message=\"avoid the mock rule\" " + EOL + 100 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + EOL + 101 "<description>" + EOL + 102 "testdesc2" + EOL + 103 "</description>" + EOL + 104 "<properties>" + EOL + 105 "<property name=\"xpath\">" + EOL + 106 "<value>" + EOL + 107 "<![CDATA[ //Block ]]>" + EOL + 108 "</value>" + EOL + 109 "</property>" + EOL + 110 "</properties>" + EOL + 111 "</rule></ruleset>"; 112 113 114 private static final String SINGLE_RULE_SET_WITH_PRIORITY = 115 "<?xml version=\"1.0\"?>" + EOL + 116 "<ruleset name=\"test\">" + EOL + 117 "<description>" + EOL + 118 "testdesc" + EOL + 119 "</description>" + EOL + 120 "<rule " + EOL + 121 "name=\"MockRuleName\" " + EOL + 122 "message=\"avoid the mock rule\" " + EOL + 123 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + 124 "<priority>3</priority>" + EOL + 125 "</rule></ruleset>"; 126 127 public void testSingleRuleWithPriority() { 128 RuleSetFactory rsf = new RuleSetFactory(); 129 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(SINGLE_RULE_SET_WITH_PRIORITY.getBytes())); 130 Rule r = (Rule)rs.getRules().iterator().next(); 131 assertEquals(3, r.getPriority()); 132 } 133 134 public void testRuleSetNotFound() { 135 RuleSetFactory rsf = new RuleSetFactory(); 136 try { 137 rsf.createRuleSet("fooooo"); 138 throw new RuntimeException("Should have thrown a RuleSetNotFoundException"); 139 } catch (RuleSetNotFoundException rsnfe) { 140 // cool 141 } 142 } 143 144 public void testCreateEmptyRuleSet() { 145 RuleSetFactory rsf = new RuleSetFactory(); 146 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(EMPTY_RULE_SET.getBytes())); 147 assertEquals("test", rs.getName()); 148 assertEquals(0, rs.size()); 149 } 150 151 public void testSingleRule() { 152 RuleSetFactory rsf = new RuleSetFactory(); 153 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(SINGLE_RULE_SET.getBytes())); 154 assertEquals(1, rs.size()); 155 Rule r = (Rule)rs.getRules().iterator().next(); 156 assertEquals("MockRuleName", r.getName()); 157 assertEquals("avoid the mock rule", r.getMessage()); 158 } 159 160 public void testMultipleRules() { 161 RuleSetFactory rsf = new RuleSetFactory(); 162 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(MULTIPLE_RULE_SET.getBytes())); 163 assertEquals(2, rs.size()); 164 Set expected = new HashSet(); 165 expected.add("MockRuleName1"); 166 expected.add("MockRuleName2"); 167 for (Iterator i = rs.getRules().iterator(); i.hasNext();) { 168 assertTrue(expected.contains(((Rule) i.next()).getName())); 169 } 170 } 171 172 public void testProps() { 173 RuleSetFactory rsf = new RuleSetFactory(); 174 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(RULE_WITH_PROPERTIES.getBytes())); 175 Rule r = (Rule) rs.getRules().iterator().next(); 176 assertTrue(r.hasProperty("foo")); 177 assertEquals("bar", r.getStringProperty("foo")); 178 assertEquals(2, r.getIntProperty("fooint")); 179 assertTrue(r.hasProperty("fooBoolean")); 180 assertTrue(r.getBooleanProperty("fooBoolean")); 181 assertTrue(r.hasProperty("fooDouble")); 182 assertEquals(1.0, r.getDoubleProperty("fooDouble"), 0.05); 183 assertTrue(!r.hasProperty("BuggleFish")); 184 assertTrue(r.getDescription().indexOf("testdesc2") != -1); 185 } 186 187 public void testXPath() { 188 RuleSetFactory rsf = new RuleSetFactory(); 189 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(RULE_WITH_XPATH.getBytes())); 190 Rule r = (Rule) rs.getRules().iterator().next(); 191 assertTrue(r.hasProperty("xpath")); 192 assertTrue(r.getStringProperty("xpath").indexOf(" //Block ") != -1); 193 } 194 195 /* 196 public void testExternalReferences() { 197 RuleSetFactory rsf = new RuleSetFactory(); 198 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(EXTERNAL_REFERENCE_RULE_SET.getBytes())); 199 assertEquals(1, rs.size()); 200 } 201 private static final String EXTERNAL_REFERENCE_RULE_SET = "<?xml version=\"1.0\"?>" + 202 "<ruleset name=\"test\">\r\n<description>testdesc</description><rule ref=\"rulesets/basic.xml/EmptyCatchBlock\"/></ruleset>"; 203 private static final String SINGLE_RULE_NO_PROPS = "<?xml version=\"1.0\"?>" + 204 "<ruleset name=\"test\">\r\n<description>testdesc</description>" + 205 "<rule name=\"MockRuleName\" message=\"avoid the mock rule\" class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + 206 "<properties></properties>" + 207 "</rule></ruleset>"; 208 */ 209 }

This page was automatically generated by Maven