From 5f2a8350db3ab43615b00368f291b3d13702c3eb Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Tue, 14 Oct 2008 07:29:13 +0000 Subject: [PATCH] PMD 5.0 conversion of eclipse plugin git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6611 51baf565-9d33-0410-a72c-fc3788e3496d --- .../src/test/RuleSetsExtension.java | 28 ++++----- .../sourceforge/pmd/eclipse/BasicPMDTest.java | 58 +++++++++---------- .../pmd/eclipse/core/PMDCorePluginTest.java | 12 ++-- .../ext/RuleSetsExtensionProcessorTest.java | 8 +-- .../impl/RuleSetsManagerImplTest.java | 2 +- .../eclipse/core/rulesets/vo/TestManager.java | 2 +- .../ProjectPropertiesModelTest.java | 8 +-- .../UpdateProjectPropertiesCmdTest.java | 9 +-- 8 files changed, 64 insertions(+), 63 deletions(-) diff --git a/pmd-eclipse-plugin/pmd-eclipse-test-fragment/src/test/RuleSetsExtension.java b/pmd-eclipse-plugin/pmd-eclipse-test-fragment/src/test/RuleSetsExtension.java index 03474971ed..4d123a3424 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test-fragment/src/test/RuleSetsExtension.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test-fragment/src/test/RuleSetsExtension.java @@ -7,7 +7,7 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright @@ -20,7 +20,7 @@ * * Neither the name of "PMD for Eclipse Development Team" nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A @@ -38,18 +38,18 @@ package test; import java.util.Set; -import org.eclipse.core.runtime.IStatus; - import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; import net.sourceforge.pmd.eclipse.core.IRuleSetsExtension; import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; +import org.eclipse.core.runtime.IStatus; + /** * Sample of an RuleSets extension. * This will automatically registers our fragment rulesets into the core plugin. - * + * * @author Herlin * */ @@ -62,17 +62,17 @@ public class RuleSetsExtension implements IRuleSetsExtension { * Replace the core plugin fragment with our own rulesets * @see net.sourceforge.pmd.eclipse.core.IRuleSetsExtension#registerRuleSets(java.util.Set) */ - public void registerRuleSets(Set registeredRuleSets) { + public void registerRuleSets(Set registeredRuleSets) { try { RuleSet ruleSet1 = getRuleSet1(); RuleSet ruleSet2 = getRuleSet2(); - + // registeredRuleSets.clear(); // to remove all rulesets already registered registeredRuleSets.add(ruleSet1); registeredRuleSets.add(ruleSet2); } catch (RuleSetNotFoundException e) { PMDPlugin.getDefault().log(IStatus.ERROR, "Unable to load rulesets", e); - } + } } /** @@ -80,19 +80,19 @@ public class RuleSetsExtension implements IRuleSetsExtension { * (for instance when creating a new workspace) * @see net.sourceforge.pmd.eclipse.core.IRuleSetsExtension#registerDefaultRuleSets(java.util.Set) */ - public void registerDefaultRuleSets(Set defaultRuleSets) { + public void registerDefaultRuleSets(Set defaultRuleSets) { try { RuleSet ruleSet1 = getRuleSet1(); RuleSet ruleSet2 = getRuleSet2(); - + // registeredRuleSets.clear(); // to remove all rulesets already registered defaultRuleSets.add(ruleSet1); defaultRuleSets.add(ruleSet2); } catch (RuleSetNotFoundException e) { PMDPlugin.getDefault().log(IStatus.ERROR, "Unable to load rulesets", e); - } + } } - + /** * Load the 1st ruleset * @return the 1st ruleset @@ -103,7 +103,7 @@ public class RuleSetsExtension implements IRuleSetsExtension { RuleSetFactory factory = new RuleSetFactory(); this.ruleSet1 = factory.createRuleSets("rulesets/extra1.xml").getAllRuleSets()[0]; } - + return this.ruleSet1; } @@ -117,7 +117,7 @@ public class RuleSetsExtension implements IRuleSetsExtension { RuleSetFactory factory = new RuleSetFactory(); this.ruleSet2 = factory.createRuleSets("rulesets/extra2.xml").getAllRuleSets()[0]; } - + return this.ruleSet2; } diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java index 6f973c4e1a..77726c7216 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java @@ -7,7 +7,7 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright @@ -20,7 +20,7 @@ * * Neither the name of "PMD for Eclipse Development Team" nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A @@ -40,7 +40,6 @@ import java.io.StringReader; import java.util.Iterator; import junit.framework.TestCase; -import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PMDException; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.RuleContext; @@ -48,20 +47,21 @@ import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; import net.sourceforge.pmd.RuleViolation; -import net.sourceforge.pmd.SourceType; import net.sourceforge.pmd.eclipse.core.PluginConstants; +import net.sourceforge.pmd.eclipse.runtime.cmd.PMDEngine; +import net.sourceforge.pmd.lang.LanguageVersion; /** * Test if PMD can be run correctly - * + * * @author Philippe Herlin - * + * */ public class BasicPMDTest extends TestCase { /** * Test case constructor - * + * * @param name of the test case */ public BasicPMDTest(String name) { @@ -70,13 +70,13 @@ public class BasicPMDTest extends TestCase { /** * One first thing the plugin must be able to do is to run PMD - * + * */ public void testRunPmdJdk13() { try { - PMD pmd = new PMD(); - pmd.setJavaVersion(SourceType.JAVA_13); + PMDEngine pmd = new PMDEngine(); + pmd.setLanguageVersion(LanguageVersion.JAVA_13); String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}"; Reader input = new StringReader(sourceCode); @@ -85,13 +85,13 @@ public class BasicPMDTest extends TestCase { context.setSourceCodeFilename("foo.java"); context.setReport(new Report()); - RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml"); pmd.processFile(input, basicRuleSet, context); - Iterator iter = context.getReport().iterator(); + Iterator iter = context.getReport().iterator(); assertTrue("There should be at least one violation", iter.hasNext()); - RuleViolation violation = (RuleViolation) iter.next(); + RuleViolation violation = iter.next(); assertEquals(violation.getRule().getName(), "UnnecessaryReturn"); assertEquals(3, violation.getBeginLine()); @@ -106,13 +106,13 @@ public class BasicPMDTest extends TestCase { /** * Let see with Java 1.4 - * + * */ public void testRunPmdJdk14() { try { - PMD pmd = new PMD(); - pmd.setJavaVersion(SourceType.JAVA_14); + PMDEngine pmd = new PMDEngine(); + pmd.setLanguageVersion(LanguageVersion.JAVA_14); String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}"; Reader input = new StringReader(sourceCode); @@ -121,13 +121,13 @@ public class BasicPMDTest extends TestCase { context.setSourceCodeFilename("foo.java"); context.setReport(new Report()); - RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml"); pmd.processFile(input, basicRuleSet, context); - Iterator iter = context.getReport().iterator(); + Iterator iter = context.getReport().iterator(); assertTrue("There should be at least one violation", iter.hasNext()); - RuleViolation violation = (RuleViolation) iter.next(); + RuleViolation violation = iter.next(); assertEquals(violation.getRule().getName(), "UnnecessaryReturn"); assertEquals(3, violation.getBeginLine()); @@ -142,13 +142,13 @@ public class BasicPMDTest extends TestCase { /** * Let see with Java 1.5 - * + * */ public void testRunPmdJdk15() { try { - PMD pmd = new PMD(); - pmd.setJavaVersion(SourceType.JAVA_15); + PMDEngine pmd = new PMDEngine(); + pmd.setLanguageVersion(LanguageVersion.JAVA_15); String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}"; Reader input = new StringReader(sourceCode); @@ -157,13 +157,13 @@ public class BasicPMDTest extends TestCase { context.setSourceCodeFilename("foo.java"); context.setReport(new Report()); - RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml"); pmd.processFile(input, basicRuleSet, context); - Iterator iter = context.getReport().iterator(); + Iterator iter = context.getReport().iterator(); assertTrue("There should be at least one violation", iter.hasNext()); - RuleViolation violation = (RuleViolation) iter.next(); + RuleViolation violation = iter.next(); assertEquals(violation.getRule().getName(), "UnnecessaryReturn"); assertEquals(3, violation.getBeginLine()); @@ -178,17 +178,17 @@ public class BasicPMDTest extends TestCase { /** * Try to load all the plugin known rulesets - * + * */ public void testDefaulltRuleSets() { RuleSetFactory factory = new RuleSetFactory(); String allRuleSets[] = PluginConstants.PMD_RULESETS; - for (int i = 0; i < allRuleSets.length; i++) { + for (String allRuleSet : allRuleSets) { try { - RuleSet ruleSet = factory.createSingleRuleSet(allRuleSets[i]); + RuleSet ruleSet = factory.createRuleSet(allRuleSet); } catch (RuleSetNotFoundException e) { e.printStackTrace(); - fail("unable to load ruleset " + allRuleSets[i]); + fail("unable to load ruleset " + allRuleSet); } } } diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/PMDCorePluginTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/PMDCorePluginTest.java index f3a03f61f3..1a03115d45 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/PMDCorePluginTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/PMDCorePluginTest.java @@ -87,9 +87,9 @@ public class PMDCorePluginTest extends TestCase { assertFalse("No registered rulesets!", registeredRuleSets.isEmpty()); RuleSetFactory factory = new RuleSetFactory(); - for (int i = 0; i < PluginConstants.PMD_RULESETS.length; i++) { - RuleSet ruleSet = factory.createSingleRuleSet(PluginConstants.PMD_RULESETS[i]); - assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet, + for (String element : PluginConstants.PMD_RULESETS) { + RuleSet ruleSet = factory.createRuleSet(element); + assertTrue("RuleSet \"" + element + "\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets)); } } @@ -104,9 +104,9 @@ public class PMDCorePluginTest extends TestCase { assertFalse("No registered default rulesets!", defaultRuleSets.isEmpty()); RuleSetFactory factory = new RuleSetFactory(); - for (int i = 0; i < PluginConstants.PMD_RULESETS.length; i++) { - RuleSet ruleSet = factory.createSingleRuleSet(PluginConstants.PMD_RULESETS[i]); - assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet, + for (String element : PluginConstants.PMD_RULESETS) { + RuleSet ruleSet = factory.createRuleSet(element); + assertTrue("RuleSet \"" + element + "\" has not been registered", ruleSetRegistered(ruleSet, defaultRuleSets)); } } diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/ext/RuleSetsExtensionProcessorTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/ext/RuleSetsExtensionProcessorTest.java index aeb7d046de..10879baf3d 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/ext/RuleSetsExtensionProcessorTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/ext/RuleSetsExtensionProcessorTest.java @@ -62,10 +62,10 @@ public class RuleSetsExtensionProcessorTest extends TestCase { assertFalse("No registered rulesets!", registeredRuleSets.isEmpty()); RuleSetFactory factory = new RuleSetFactory(); - RuleSet ruleSet = factory.createSingleRuleSet("rulesets/extra1.xml"); + RuleSet ruleSet = factory.createRuleSet("rulesets/extra1.xml"); assertTrue("RuleSet \"rulesets/extra1.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets)); - ruleSet = factory.createSingleRuleSet("rulesets/extra2.xml"); + ruleSet = factory.createRuleSet("rulesets/extra2.xml"); assertTrue("RuleSet \"rulesets/extra2.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets)); } @@ -79,10 +79,10 @@ public class RuleSetsExtensionProcessorTest extends TestCase { assertFalse("No registered default rulesets!", registeredRuleSets.isEmpty()); RuleSetFactory factory = new RuleSetFactory(); - RuleSet ruleSet = factory.createSingleRuleSet("rulesets/extra1.xml"); + RuleSet ruleSet = factory.createRuleSet("rulesets/extra1.xml"); assertTrue("RuleSet \"rulesets/extra1.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets)); - ruleSet = factory.createSingleRuleSet("rulesets/extra2.xml"); + ruleSet = factory.createRuleSet("rulesets/extra2.xml"); assertTrue("RuleSet \"rulesets/extra2.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets)); } diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/impl/RuleSetsManagerImplTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/impl/RuleSetsManagerImplTest.java index 0d9642494b..141e164a57 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/impl/RuleSetsManagerImplTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/impl/RuleSetsManagerImplTest.java @@ -73,7 +73,7 @@ public class RuleSetsManagerImplTest extends TestCase { RuleSet ruleSet = rsm.valueOf(new String[] { "rulesets/basic.xml" }); Collection pmdRules = ruleSet.getPmdRuleSet().getRules(); - Collection basicRules = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml").getRules(); + Collection basicRules = new RuleSetFactory().createRuleSet("rulesets/basic.xml").getRules(); // dump("PMD Rules", pmdRules); // dump("Basic Rules", basicRules); diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/vo/TestManager.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/vo/TestManager.java index ada5980c13..b1b520a8a0 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/vo/TestManager.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/core/rulesets/vo/TestManager.java @@ -63,7 +63,7 @@ public class TestManager { public static RuleSet getRuleSet() throws RuleSetNotFoundException { if (basicRuleSet == null) { - basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml"); + basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml"); Iterator i = getRuleSet().getRules().iterator(); rules = new Rule[4]; for (int j = 0; j < 4; j++) { diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java index 2bf7de98dd..50f852927d 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java @@ -134,7 +134,7 @@ public class ProjectPropertiesModelTest extends TestCase { // use the basic ruleset because it should be included in the plugin // ruleset. - RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); // First set the project ruleset model.setProjectRuleSet(basicRuleSet); @@ -269,7 +269,7 @@ public class ProjectPropertiesModelTest extends TestCase { IProjectProperties model = mgr.loadProjectProperties(this.testProject); RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); model.setPmdEnabled(true); model.setRuleSetStoredInProject(false); model.setProjectWorkingSet(null); @@ -282,7 +282,7 @@ public class ProjectPropertiesModelTest extends TestCase { boolean b = model.isRuleSetStoredInProject(); IFile file = this.testProject.getFile(".ruleset"); - RuleSet projectRuleSet = factory.createSingleRuleSet(file.getLocation().toOSString()); + RuleSet projectRuleSet = factory.createRuleSet(file.getLocation().toOSString()); assertTrue("the ruleset should be stored in the project", b); assertEquals("The project ruleset must be equals to the one found in the project", model.getProjectRuleSet(), projectRuleSet); @@ -298,7 +298,7 @@ public class ProjectPropertiesModelTest extends TestCase { IProjectProperties model = mgr.loadProjectProperties(this.testProject); RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); model.setPmdEnabled(true); model.setRuleSetStoredInProject(false); model.setProjectWorkingSet(null); diff --git a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/ui/properties/UpdateProjectPropertiesCmdTest.java b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/ui/properties/UpdateProjectPropertiesCmdTest.java index e59604ca3a..a3b989b62d 100644 --- a/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/ui/properties/UpdateProjectPropertiesCmdTest.java +++ b/pmd-eclipse-plugin/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/ui/properties/UpdateProjectPropertiesCmdTest.java @@ -12,7 +12,6 @@ import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties; import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager; import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException; -import net.sourceforge.pmd.eclipse.ui.properties.UpdateProjectPropertiesCmd; import org.eclipse.core.resources.IProject; @@ -20,15 +19,17 @@ import org.eclipse.core.resources.IProject; public class UpdateProjectPropertiesCmdTest extends TestCase { private IProject testProject; + @Override protected void setUp() throws Exception { super.setUp(); // 1. Create a Java project this.testProject = EclipseUtils.createJavaProject("PMDTestProject"); - assertTrue("A test project cannot be created; the tests cannot be performed.", (this.testProject != null) + assertTrue("A test project cannot be created; the tests cannot be performed.", this.testProject != null && this.testProject.exists() && this.testProject.isAccessible()); } + @Override protected void tearDown() throws Exception { try { // 1. Delete the test project @@ -64,8 +65,8 @@ public class UpdateProjectPropertiesCmdTest extends TestCase { // 2. remove the first rule (keep its name for assertion) RuleSet newRuleSet = new RuleSet(); newRuleSet.addRuleSet(projectRuleSet); - Iterator i = newRuleSet.getRules().iterator(); - Rule removedRule = (Rule) i.next(); + Iterator i = newRuleSet.getRules().iterator(); + Rule removedRule = i.next(); i.remove(); UpdateProjectPropertiesCmd cmd = new UpdateProjectPropertiesCmd();