01: package test.net.sourceforge.pmd.rules;
02:
03: import net.sourceforge.pmd.Rule;
04: import net.sourceforge.pmd.RuleSet;
05: import net.sourceforge.pmd.RuleSetFactory;
06: import net.sourceforge.pmd.RuleSetNotFoundException;
07: import net.sourceforge.pmd.RuleSets;
08: import net.sourceforge.pmd.util.ResourceLoader;
09:
10: import org.junit.Ignore;
11: import org.junit.Test;
12: import org.junit.runner.RunWith;
13: import org.junit.runners.Parameterized;
14: import org.junit.runners.Parameterized.Parameters;
15:
16: import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
17: import test.net.sourceforge.pmd.testframework.TestDescriptor;
18:
19: import java.io.IOException;
20: import java.util.ArrayList;
21: import java.util.Collection;
22: import java.util.Iterator;
23: import java.util.List;
24: import java.util.Properties;
25: import java.util.StringTokenizer;
26:
27: @RunWith(Parameterized.class)
28: public class DynamicRuleTest extends SimpleAggregatorTst {
29:
30: private Rule rule;
31:
32: private String strRuleset;
33:
34: public DynamicRuleTest(String strRuleset, Rule rule) {
35: this .rule = rule;
36: this .strRuleset = strRuleset;
37: }
38:
39: @Parameters
40: public static Collection data() throws IOException,
41: RuleSetNotFoundException {
42: List<Object[]> allRules = new ArrayList<Object[]>();
43: RuleSetFactory rsf = new RuleSetFactory();
44: String rulesetFilenames = null;
45: Properties props = new Properties();
46: props.load(ResourceLoader
47: .loadResourceAsStream("rulesets/rulesets.properties"));
48: rulesetFilenames = props.getProperty("rulesets.testnames");
49: StringTokenizer st = new StringTokenizer(rulesetFilenames, ",");
50: while (st.hasMoreTokens()) {
51: String strRule = st.nextToken();
52: String strCleanRule = cleanRulesetName(strRule);
53: RuleSets ruleSets = rsf.createRuleSets(strRule);
54: for (Iterator<RuleSet> iter = ruleSets
55: .getRuleSetsIterator(); iter.hasNext();) {
56: RuleSet ruleSet = iter.next();
57: for (Rule rule : ruleSet.getRules()) {
58: allRules.add(new Object[] { strCleanRule, rule });
59: }
60: }
61: }
62:
63: return allRules;
64: }
65:
66: private static String cleanRulesetName(String strRule) {
67: return strRule.substring(strRule.indexOf('/') + 1,
68: strRule.indexOf('.')).replaceAll("-", "")
69: + "/xml/";
70: }
71:
72: @Ignore
73: @Test
74: public void testAll() {
75: TestDescriptor[] td = extractTestsFromXml(rule,
76: getCleanRuleName(rule), strRuleset);
77: runTests(td);
78: }
79:
80: public static junit.framework.Test suite() {
81: return new junit.framework.JUnit4TestAdapter(
82: DynamicRuleTest.class);
83: }
84: }
|