55package net .sourceforge .pmd .eclipse ;
66
77import java .io .ByteArrayInputStream ;
8+ import java .io .File ;
89import java .io .IOException ;
910import java .io .InputStream ;
1011import java .io .UnsupportedEncodingException ;
12+ import java .util .ArrayList ;
13+ import java .util .Collections ;
1114import java .util .Iterator ;
15+ import java .util .List ;
1216
1317import org .junit .Assert ;
1418import org .junit .Test ;
1519
20+ import net .sourceforge .pmd .PMD ;
1621import net .sourceforge .pmd .PMDConfiguration ;
17- import net .sourceforge .pmd .PMDException ;
18- import net .sourceforge .pmd .Report ;
1922import net .sourceforge .pmd .RuleContext ;
2023import net .sourceforge .pmd .RuleSet ;
2124import net .sourceforge .pmd .RuleSetFactory ;
2225import net .sourceforge .pmd .RuleSetNotFoundException ;
23- import net .sourceforge .pmd .RuleSets ;
2426import net .sourceforge .pmd .RuleViolation ;
25- import net .sourceforge .pmd .SourceCodeProcessor ;
27+ import net .sourceforge .pmd .RulesetsFactoryUtils ;
28+ import net .sourceforge .pmd .ThreadSafeReportListener ;
2629import net .sourceforge .pmd .lang .LanguageRegistry ;
30+ import net .sourceforge .pmd .renderers .Renderer ;
31+ import net .sourceforge .pmd .stat .Metric ;
2732import net .sourceforge .pmd .util .datasource .DataSource ;
2833
2934/**
@@ -62,13 +67,13 @@ public void close() throws IOException {
6267 }
6368
6469 /**
65- * Try to load all the plugin known rulesets
70+ * Try to load all the plugin known rulesets.
6671 *
6772 */
6873 @ Test
6974 public void testDefaulltRuleSets () {
7075 try {
71- final RuleSetFactory factory = new RuleSetFactory ();
76+ final RuleSetFactory factory = RulesetsFactoryUtils . defaultFactory ();
7277 final Iterator <RuleSet > iterator = factory .getRegisteredRuleSets ();
7378 while (iterator .hasNext ()) {
7479 iterator .next ();
@@ -79,117 +84,65 @@ public void testDefaulltRuleSets() {
7984 }
8085 }
8186
82- /**
83- * One first thing the plugin must be able to do is to run PMD
84- *
85- */
86- @ Test
87- public void testRunPmdJdk13 () {
88-
89- try {
90- PMDConfiguration configuration = new PMDConfiguration ();
91- configuration .setDefaultLanguageVersion (LanguageRegistry .findLanguageVersionByTerseName ("java 1.3" ));
87+ private void runPmd (String javaVersion ) {
88+ PMDConfiguration configuration = new PMDConfiguration ();
89+ configuration .setDefaultLanguageVersion (LanguageRegistry .findLanguageByTerseName ("java" ).getVersion (javaVersion ));
90+ configuration .setRuleSets ("category/java/codestyle.xml/UnnecessaryReturn" );
91+ RuleSetFactory ruleSetFactory = RulesetsFactoryUtils .createFactory (configuration );
92+
93+ List <DataSource > files = new ArrayList <>();
94+ final String sourceCode = "public class Foo {\n public void foo() {\n return;\n }}" ;
95+ files .add (new StringDataSource (sourceCode ));
96+
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+ }
92105
93- final String sourceCode = "public class Foo {\n public void foo() {\n return;\n }}" ;
106+ @ Override
107+ public void metricAdded (Metric metric ) {
108+ }
109+ });
94110
95- final RuleContext context = new RuleContext ();
96- context .setSourceCodeFilename ("foo.java" );
97- context .setReport (new Report ());
98111
99- final RuleSet codeStyleRuleSet = new RuleSetFactory ().createRuleSet ("category/java/codestyle.xml/UnnecessaryReturn" );
100- RuleSets rSets = new RuleSets (codeStyleRuleSet );
101- new SourceCodeProcessor (configuration ).processSourceCode (new StringDataSource (sourceCode ).getInputStream (),
102- rSets , context );
112+ PMD .processFiles (configuration , ruleSetFactory , files , ctx ,
113+ Collections .<Renderer >emptyList ());
103114
104- final Iterator <RuleViolation > iter = context .getReport ().iterator ();
105- Assert .assertTrue ("There should be at least one violation" , iter .hasNext ());
115+ Assert .assertFalse ("There should be at least one violation" , violations .isEmpty ());
106116
107- final RuleViolation violation = iter .next ();
108- Assert .assertEquals (violation .getRule ().getName (), "UnnecessaryReturn" );
109- Assert .assertEquals (3 , violation .getBeginLine ());
117+ final RuleViolation violation = violations .get (0 );
118+ Assert .assertEquals (violation .getRule ().getName (), "UnnecessaryReturn" );
119+ Assert .assertEquals (3 , violation .getBeginLine ());
120+ }
110121
111- } catch ( final RuleSetNotFoundException e ) {
112- e . printStackTrace ();
113- Assert . fail ();
114- } catch ( final PMDException e ) {
115- e . printStackTrace ();
116- Assert . fail ();
117- }
122+ /**
123+ * One first thing the plugin must be able to do is to run PMD.
124+ *
125+ */
126+ @ Test
127+ public void testRunPmdJdk13 () {
128+ runPmd ( "1.3" );
118129 }
119130
120131 /**
121- * Let see with Java 1.4
132+ * Let see with Java 1.4.
122133 *
123134 */
124135 @ Test
125136 public void testRunPmdJdk14 () {
126-
127- try {
128- PMDConfiguration configuration = new PMDConfiguration ();
129- configuration .setDefaultLanguageVersion (LanguageRegistry .findLanguageVersionByTerseName ("java 1.4" ));
130-
131- final String sourceCode = "public class Foo {\n public void foo() {\n return;\n }}" ;
132-
133- final RuleContext context = new RuleContext ();
134- context .setSourceCodeFilename ("foo.java" );
135- context .setReport (new Report ());
136-
137- final RuleSet codeStyleRuleSet = new RuleSetFactory ().createRuleSet ("category/java/codestyle.xml/UnnecessaryReturn" );
138- RuleSets rSets = new RuleSets (codeStyleRuleSet );
139- new SourceCodeProcessor (configuration ).processSourceCode (new StringDataSource (sourceCode ).getInputStream (),
140- rSets , context );
141-
142- final Iterator <RuleViolation > iter = context .getReport ().iterator ();
143- Assert .assertTrue ("There should be at least one violation" , iter .hasNext ());
144-
145- final RuleViolation violation = iter .next ();
146- Assert .assertEquals (violation .getRule ().getName (), "UnnecessaryReturn" );
147- Assert .assertEquals (3 , violation .getBeginLine ());
148-
149- } catch (final RuleSetNotFoundException e ) {
150- e .printStackTrace ();
151- Assert .fail ();
152- } catch (final PMDException e ) {
153- e .printStackTrace ();
154- Assert .fail ();
155- }
137+ runPmd ("1.4" );
156138 }
157139
158140 /**
159- * Let see with Java 1.5
141+ * Let see with Java 1.5.
160142 *
161143 */
162144 @ Test
163145 public void testRunPmdJdk15 () {
164-
165- try {
166- PMDConfiguration configuration = new PMDConfiguration ();
167- configuration .setDefaultLanguageVersion (LanguageRegistry .findLanguageVersionByTerseName ("java 1.5" ));
168-
169- final String sourceCode = "public class Foo {\n public void foo() {\n return;\n }}" ;
170-
171- final RuleContext context = new RuleContext ();
172- context .setSourceCodeFilename ("foo.java" );
173- context .setReport (new Report ());
174-
175- final RuleSet codeStyleRuleSet = new RuleSetFactory ().createRuleSet ("category/java/codestyle.xml/UnnecessaryReturn" );
176- RuleSets rSets = new RuleSets (codeStyleRuleSet );
177- new SourceCodeProcessor (configuration ).processSourceCode (new StringDataSource (sourceCode ).getInputStream (),
178- rSets , context );
179-
180- final Iterator <RuleViolation > iter = context .getReport ().iterator ();
181- Assert .assertTrue ("There should be at least one violation" , iter .hasNext ());
182-
183- final RuleViolation violation = iter .next ();
184- Assert .assertEquals (violation .getRule ().getName (), "UnnecessaryReturn" );
185- Assert .assertEquals (3 , violation .getBeginLine ());
186-
187- } catch (final RuleSetNotFoundException e ) {
188- e .printStackTrace ();
189- Assert .fail ();
190- } catch (final PMDException e ) {
191- e .printStackTrace ();
192- Assert .fail ();
193- }
146+ runPmd ("1.5" );
194147 }
195148}
0 commit comments