PMD 5.0 conversion of eclipse plugin

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6611 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2008-10-14 07:29:13 +00:00
parent a8ddb4bb9e
commit 5f2a8350db
8 changed files with 64 additions and 63 deletions

View File

@ -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<RuleSet> 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<RuleSet> 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;
}

View File

@ -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<RuleViolation> 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<RuleViolation> 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<RuleViolation> 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);
}
}
}

View File

@ -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));
}
}

View File

@ -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));
}

View File

@ -73,7 +73,7 @@ public class RuleSetsManagerImplTest extends TestCase {
RuleSet ruleSet = rsm.valueOf(new String[] { "rulesets/basic.xml" });
Collection<Rule> pmdRules = ruleSet.getPmdRuleSet().getRules();
Collection<Rule> basicRules = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml").getRules();
Collection<Rule> basicRules = new RuleSetFactory().createRuleSet("rulesets/basic.xml").getRules();
// dump("PMD Rules", pmdRules);
// dump("Basic Rules", basicRules);

View File

@ -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<Rule> i = getRuleSet().getRules().iterator();
rules = new Rule[4];
for (int j = 0; j < 4; j++) {

View File

@ -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);

View File

@ -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<Rule> i = newRuleSet.getRules().iterator();
Rule removedRule = i.next();
i.remove();
UpdateProjectPropertiesCmd cmd = new UpdateProjectPropertiesCmd();