1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules;
5   
6   import net.sourceforge.pmd.Rule;
7   import net.sourceforge.pmd.RuleSet;
8   import net.sourceforge.pmd.RuleSetFactory;
9   import net.sourceforge.pmd.RuleSetNotFoundException;
10  import net.sourceforge.pmd.RuleSets;
11  import net.sourceforge.pmd.util.ResourceLoader;
12  
13  import org.junit.Ignore;
14  import org.junit.Test;
15  import org.junit.runner.RunWith;
16  import org.junit.runners.Parameterized;
17  import org.junit.runners.Parameterized.Parameters;
18  
19  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
20  import test.net.sourceforge.pmd.testframework.TestDescriptor;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Properties;
28  import java.util.StringTokenizer;
29  
30  @RunWith(Parameterized.class)
31  public class DynamicRuleTest extends SimpleAggregatorTst {
32  
33      private Rule rule;
34  
35      private String strRuleset;
36  
37      public DynamicRuleTest(String strRuleset, Rule rule) {
38          this.rule = rule;
39          this.strRuleset = strRuleset;
40      }
41  
42      @Parameters
43      public static Collection data() throws IOException, RuleSetNotFoundException {
44          List<Object[]> allRules = new ArrayList<Object[]>();
45          RuleSetFactory rsf = new RuleSetFactory();
46          String rulesetFilenames = null;
47          Properties props = new Properties();
48          props.load(ResourceLoader.loadResourceAsStream("rulesets/rulesets.properties"));
49          rulesetFilenames = props.getProperty("rulesets.testnames");
50          StringTokenizer st = new StringTokenizer(rulesetFilenames, ",");
51          while (st.hasMoreTokens()) {
52              String strRule = st.nextToken();
53              String strCleanRule = cleanRulesetName(strRule);
54              RuleSets ruleSets = rsf.createRuleSets(strRule);
55              for (Iterator<RuleSet> iter = ruleSets.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, strRule.indexOf('.')).replaceAll("-", "") + "/xml/";
68      }
69  
70      @Ignore
71      @Test
72      public void testAll() {
73          TestDescriptor[] td = extractTestsFromXml(rule, getCleanRuleName(rule), strRuleset);
74          runTests(td);
75      }
76  
77      public static junit.framework.Test suite() {
78          return new junit.framework.JUnit4TestAdapter(DynamicRuleTest.class);
79      }
80  }