View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.testframework;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import net.sourceforge.pmd.Rule;
10  
11  import org.junit.runner.RunWith;
12  
13  /**
14   * Standard methods for (simple) testcases.
15   */
16  @RunWith(PMDTestRunner.class)
17  public abstract class SimpleAggregatorTst extends RuleTst {
18  
19      /**
20       * Configure the rule tests to be executed. Implement this method in
21       * subclasses by calling adRule.
22       * 
23       * @see #addRule(String, String)
24       */
25      protected void setUp() {
26          // empty, to be overridden
27      }
28  
29      /**
30       * Run a set of tests defined in an XML test-data file for a rule. The file
31       * should be ./xml/RuleName.xml relative to the test-class. The format is
32       * defined in test-data.xsd.
33       */
34      public void runTests(Rule rule) {
35          runTests(extractTestsFromXml(rule));
36      }
37  
38      /**
39       * Run a set of tests defined in a XML test-data file. The file should be
40       * ./xml/[testsFileName].xml relative to the test-class. The format is
41       * defined in test-data.xsd.
42       */
43      public void runTests(Rule rule, String testsFileName) {
44          runTests(extractTestsFromXml(rule, testsFileName));
45      }
46  
47      /**
48       * Run a set of tests of a certain sourceType.
49       */
50      public void runTests(TestDescriptor[] tests) {
51          for (int i = 0; i < tests.length; i++) {
52              runTest(tests[i]);
53          }
54      }
55  
56      private List<Rule> rules = new ArrayList<>();
57  
58      /**
59       * Add new XML tests associated with the rule to the test suite. This should
60       * be called from the setup method.
61       */
62      protected void addRule(String ruleSet, String ruleName) {
63          rules.add(findRule(ruleSet, ruleName));
64      }
65  
66      /**
67       * Gets all configured rules.
68       * 
69       * @return all configured rules.
70       */
71      protected List<Rule> getRules() {
72          return rules;
73      }
74  }