    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.testframework;
    5
    6   import junit.framework.TestCase;
    7   import net.sourceforge.pmd.PMD;
    8   import net.sourceforge.pmd.Report;
    9   import net.sourceforge.pmd.Rule;
    10  import net.sourceforge.pmd.RuleContext;
    11  import net.sourceforge.pmd.RuleSet;
    12  import net.sourceforge.pmd.RuleSetFactory;
    13  import net.sourceforge.pmd.RuleSetNotFoundException;
    14  import net.sourceforge.pmd.RuleSets;
    15  import net.sourceforge.pmd.SimpleRuleSetNameMapper;
    16  import net.sourceforge.pmd.SourceType;
    17  import net.sourceforge.pmd.SourceTypeToRuleLanguageMapper;
    18  import net.sourceforge.pmd.TargetJDKVersion;
    19
    20  import java.io.StringReader;
    21
    22  public class RuleTst extends TestCase {
    23      public static final SourceType DEFAULT_SOURCE_TYPE = SourceType.JAVA_14;
    24
    25      public void runTestFromString(String code, int expectedResults, Rule rule) throws Throwable {
    26          runTestFromString(code, expectedResults, rule, DEFAULT_SOURCE_TYPE);
    27      }
    28
    29      public Rule findRule(String rs, String r) {
    30          try {
    31              Rule rule = new RuleSetFactory().createRuleSets(new SimpleRuleSetNameMapper(rs).getRuleSets()).getRuleByName(r);
    32              if (rule == null) {
    33                  fail("Rule " + r + " not found in ruleset " + rs);
    34              }
    35              return rule;
    36          } catch (RuleSetNotFoundException e) {
    37              e.printStackTrace();
    38              fail("Couldn't find ruleset " + rs);
    39              return null;
    40          }
    41      }
    42
    43
    44      /***
    45       * Run the rule on the given code, and check the expected number of violations.
    46       *
    47       * @param code
    48       * @param expectedResults
    49       * @param rule
    50       * @throws Throwable
    51       */
    52      public void runTestFromString(String code, int expectedResults, Rule rule,
    53                                    SourceType sourceType) throws Throwable {
    54          int res = processUsingStringReader(code, rule, sourceType).size();
    55          assertEquals("Expected " + expectedResults + " failures, got " + res + ".",
    56                  expectedResults, res);
    57      }
    58
    59      private Report processUsingStringReader(String code, Rule rule,
    60                                              SourceType sourceType) throws Throwable {
    61          Report report = new Report();
    62          runTestFromString(code, rule, report, sourceType);
    63          return report;
    64      }
    65
    66      /***
    67       * Run the rule on the given code, and put the violations in the given report.
    68       *
    69       * @param code
    70       * @param expectedResults
    71       * @param rule
    72       * @throws Throwable
    73       */
    74      public void runTestFromString(String code, Rule rule, Report report,
    75                                    SourceType sourceType) throws Throwable {
    76          PMD p = new PMD();
    77          RuleContext ctx = new RuleContext();
    78          ctx.setReport(report);
    79          ctx.setSourceCodeFilename("n/a");
    80          RuleSet rules = new RuleSet();
    81          rules.addRule(rule);
    82          rules.setLanguage(SourceTypeToRuleLanguageMapper.getMappedLanguage(sourceType));
    83          p.processFile(new StringReader(code), new RuleSets(rules), ctx, sourceType);
    84      }
    85
    86
    87      public void runTestFromString(String code, Rule rule, Report report) throws Throwable {
    88          runTestFromString(code, rule, report, DEFAULT_SOURCE_TYPE);
    89      }
    90
    91      public void runTestFromString15(String code, Rule rule, Report report) throws Throwable {
    92          runTestFromString(code, rule, report, SourceType.JAVA_15);
    93      }
    94
    95      public void runTestFromString13(String code, Rule rule, Report report) throws Throwable {
    96          runTestFromString(code, rule, report, SourceType.JAVA_13);
    97      }
    98
    99      public void runTestFromString(String code, Rule rule, Report report, TargetJDKVersion jdk)
    100             throws Throwable {
    101         PMD p = new PMD(jdk);
    102         RuleContext ctx = new RuleContext();
    103         ctx.setReport(report);
    104         ctx.setSourceCodeFilename("n/a");
    105         RuleSet rules = new RuleSet();
    106         rules.addRule(rule);
    107         p.processFile(new StringReader(code), rules, ctx);
    108     }
    109 }