55package net .sourceforge .pmd .eclipse ;
66
77import java .io .ByteArrayInputStream ;
8- import java .io .File ;
98import java .io .IOException ;
109import java .io .InputStream ;
11- import java .io . UnsupportedEncodingException ;
10+ import java .nio . charset . StandardCharsets ;
1211import java .util .ArrayList ;
1312import java .util .Collections ;
14- import java .util .Iterator ;
1513import java .util .List ;
1614
1715import org .junit .Assert ;
1816import org .junit .Test ;
1917
2018import net .sourceforge .pmd .PMD ;
2119import net .sourceforge .pmd .PMDConfiguration ;
22- import net .sourceforge .pmd .RuleContext ;
20+ import net .sourceforge .pmd .Report ;
2321import net .sourceforge .pmd .RuleSet ;
24- import net .sourceforge .pmd .RuleSetFactory ;
25- import net .sourceforge .pmd .RuleSetNotFoundException ;
22+ import net .sourceforge .pmd .RuleSetLoader ;
2623import net .sourceforge .pmd .RuleViolation ;
27- import net .sourceforge .pmd .RulesetsFactoryUtils ;
28- import net .sourceforge .pmd .ThreadSafeReportListener ;
2924import net .sourceforge .pmd .lang .LanguageRegistry ;
3025import net .sourceforge .pmd .renderers .Renderer ;
31- import net .sourceforge .pmd .stat .Metric ;
3226import net .sourceforge .pmd .util .datasource .DataSource ;
3327
3428/**
@@ -43,11 +37,7 @@ static class StringDataSource implements DataSource {
4337 private final ByteArrayInputStream is ;
4438
4539 StringDataSource (final String source ) {
46- try {
47- this .is = new ByteArrayInputStream (source .getBytes ("UTF-8" ));
48- } catch (UnsupportedEncodingException e ) {
49- throw new RuntimeException (e );
50- }
40+ this .is = new ByteArrayInputStream (source .getBytes (StandardCharsets .UTF_8 ));
5141 }
5242
5343 @ Override
@@ -72,49 +62,29 @@ public void close() throws IOException {
7262 */
7363 @ Test
7464 public void testDefaulltRuleSets () {
75- try {
76- final RuleSetFactory factory = RulesetsFactoryUtils .defaultFactory ();
77- final Iterator <RuleSet > iterator = factory .getRegisteredRuleSets ();
78- while (iterator .hasNext ()) {
79- iterator .next ();
80- }
81- } catch (final RuleSetNotFoundException e ) {
82- e .printStackTrace ();
83- Assert .fail ("unable to load registered rulesets " );
84- }
65+ RuleSetLoader rulesetloader = new RuleSetLoader ();
66+ List <RuleSet > standardRuleSets = rulesetloader .getStandardRuleSets ();
67+ Assert .assertFalse ("No Rulesets found" , standardRuleSets .isEmpty ());
8568 }
8669
8770 private void runPmd (String javaVersion ) {
8871 PMDConfiguration configuration = new PMDConfiguration ();
8972 configuration .setDefaultLanguageVersion (LanguageRegistry .findLanguageByTerseName ("java" ).getVersion (javaVersion ));
9073 configuration .setRuleSets ("category/java/codestyle.xml/UnnecessaryReturn" );
91- RuleSetFactory ruleSetFactory = RulesetsFactoryUtils .createFactory (configuration );
74+ configuration .setIgnoreIncrementalAnalysis (true );
75+ RuleSetLoader rulesetLoader = RuleSetLoader .fromPmdConfig (configuration );
76+ List <RuleSet > rulesets = rulesetLoader .loadFromResources (configuration .getRuleSets ());
9277
9378 List <DataSource > files = new ArrayList <>();
9479 final String sourceCode = "public class Foo {\n public void foo() {\n return;\n }}" ;
9580 files .add (new StringDataSource (sourceCode ));
9681
97- final List <RuleViolation > violations = new ArrayList <>();
98- final RuleContext ctx = new RuleContext ();
99- ctx .setSourceCodeFile (new File ("foo.java" ));
100- ctx .getReport ().addListener (new ThreadSafeReportListener () {
101- @ Override
102- public void ruleViolationAdded (RuleViolation ruleViolation ) {
103- violations .add (ruleViolation );
104- }
105-
106- @ Override
107- public void metricAdded (Metric metric ) {
108- }
109- });
110-
111-
112- PMD .processFiles (configuration , ruleSetFactory , files , ctx ,
82+ Report result = PMD .processFiles (configuration , rulesets , files ,
11383 Collections .<Renderer >emptyList ());
11484
115- Assert .assertFalse ("There should be at least one violation" , violations .isEmpty ());
85+ Assert .assertFalse ("There should be at least one violation" , result . getViolations () .isEmpty ());
11686
117- final RuleViolation violation = violations .get (0 );
87+ final RuleViolation violation = result . getViolations () .get (0 );
11888 Assert .assertEquals (violation .getRule ().getName (), "UnnecessaryReturn" );
11989 Assert .assertEquals (3 , violation .getBeginLine ());
12090 }
0 commit comments