Skip to content

Commit b964498

Browse files
committed
Merge branch 'test-for-issue-65' into pr-66
2 parents 1ed14d0 + 993dee8 commit b964498

File tree

12 files changed

+251
-12
lines changed

12 files changed

+251
-12
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# PMD Eclipse Plugin
22

33
[![Build Status](https://travis-ci.org/pmd/pmd-eclipse-plugin.svg?branch=master)](https://travis-ci.org/pmd/pmd-eclipse-plugin)
4+
[![Eclipse Marketplace](https://img.shields.io/eclipse-marketplace/v/pmd-eclipse-plugin.svg)](https://marketplace.eclipse.org/content/pmd-eclipse-plugin)
45

56
Release Notes: <https://github.com/pmd/pmd-eclipse-plugin/blob/master/ReleaseNotes.md>
67

@@ -9,6 +10,8 @@ Eclipse Update Site:
910
* Releases: <https://dl.bintray.com/pmd/pmd-eclipse-plugin/updates/>
1011
* Snapshots: <https://dl.bintray.com/pmd/pmd-eclipse-plugin/snapshots/updates/>
1112

13+
Marketplace: [![Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client](https://marketplace.eclipse.org/sites/all/themes/solstice/public/images/marketplace/btn-install.png)](http://marketplace.eclipse.org/marketplace-client-intro?mpc_install=2755329 "Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client")
14+
1215
## How to contribute
1316

1417
You can contribute by testing the latest version, creating bug reports, or even forking
@@ -146,6 +149,13 @@ The release happens in two phases:
146149
git branch -D pmd-eclipse-plugin-rb-$VERSION
147150
git push origin master
148151
git push origin tag $VERSION.$BUILDQUALIFIER
152+
echo
153+
echo
154+
155+
echo
156+
echo Update the marketplace entry with the new version:
157+
echo https://marketplace.eclipse.org/content/pmd-eclipse-plugin
158+
echo
149159

150160
echo Done.
151161

ReleaseNotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Eclipse Update Site:
1111

1212
### New and noteworthy
1313

14-
* Updated PMD to 6.7.0
14+
* Updated PMD to 6.9.0
1515
* Eclipse SimRel 2018-09 is supported.
1616
To do this, the plugin doesn't expose log4j for other plugins anymore
1717
(the package `org.apache.log4j` is not exported anymore). In case you used this in a fragment,
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.eclipse.runtime.cmd;
6+
7+
import java.nio.charset.StandardCharsets;
8+
9+
import org.apache.commons.io.IOUtils;
10+
import org.eclipse.core.resources.IFolder;
11+
import org.eclipse.core.resources.IMarker;
12+
import org.eclipse.core.resources.IProject;
13+
import org.eclipse.core.resources.IResource;
14+
import org.eclipse.core.runtime.CoreException;
15+
import org.eclipse.core.runtime.Path;
16+
import org.eclipse.jdt.core.IClasspathEntry;
17+
import org.eclipse.jdt.core.IJavaProject;
18+
import org.eclipse.jdt.core.JavaCore;
19+
import org.junit.After;
20+
import org.junit.Assert;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import net.sourceforge.pmd.eclipse.EclipseUtils;
25+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
26+
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
27+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
28+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager;
29+
30+
import name.herlin.command.CommandException;
31+
32+
/**
33+
* This tests execution of PMD with multiple rulesets
34+
*/
35+
public class MultipleRulesetsTest {
36+
private IProject testProject;
37+
38+
@Before
39+
public void setUp() throws Exception {
40+
41+
// 1. Create a Java project
42+
this.testProject = EclipseUtils.createJavaProject("PMDTestProject");
43+
Assert.assertTrue("A test project cannot be created; the tests cannot be performed.",
44+
this.testProject != null && this.testProject.exists() && this.testProject.isAccessible());
45+
46+
// 2. Setup test folder
47+
this.testProject.getFolder("src/main/java").getFullPath().toFile().mkdirs();
48+
IFolder folder = this.testProject.getFolder("/src/main");
49+
folder.create(true, true, null);
50+
folder = folder.getFolder("java");
51+
folder.create(true, true, null);
52+
IJavaProject javaProject = JavaCore.create(this.testProject);
53+
javaProject.setRawClasspath(
54+
new IClasspathEntry[] { JavaCore.newSourceEntry(testProject.getFolder("src/main/java").getFullPath()),
55+
JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")) },
56+
null);
57+
58+
// 3. Create test sources
59+
folder.getFolder("first").create(true, true, null);
60+
EclipseUtils.createTestSourceFile(testProject, "/src/main/java/first/First.java",
61+
IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/First.java"),
62+
StandardCharsets.UTF_8.name()));
63+
folder.getFolder("second").create(true, true, null);
64+
EclipseUtils.createTestSourceFile(testProject, "/src/main/java/second/Second.java",
65+
IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/Second.java"),
66+
StandardCharsets.UTF_8.name()));
67+
folder.getFolder("third").create(true, true, null);
68+
EclipseUtils.createTestSourceFile(testProject, "/src/main/java/third/Third.java",
69+
IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/Third.java"),
70+
StandardCharsets.UTF_8.name()));
71+
72+
// 4. Copy rulesets
73+
EclipseUtils.createTestSourceFile(testProject, "/ruleset1.xml",
74+
IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/ruleset1.xml"),
75+
StandardCharsets.UTF_8.name()));
76+
EclipseUtils.createTestSourceFile(testProject, "/ruleset2.xml",
77+
IOUtils.toString(MultipleRulesetsTest.class.getResourceAsStream("multiplerulesets/ruleset2.xml"),
78+
StandardCharsets.UTF_8.name()));
79+
80+
// 5. Enable and configure PMD for the test project
81+
IProjectPropertiesManager propertiesManager = PMDPlugin.getDefault().getPropertiesManager();
82+
IProjectProperties properties = propertiesManager.loadProjectProperties(testProject);
83+
properties.setPmdEnabled(true);
84+
properties.setRuleSetStoredInProject(true);
85+
properties.setRuleSetFile("ruleset1.xml,ruleset2.xml");
86+
propertiesManager.storeProjectProperties(properties);
87+
}
88+
89+
@After
90+
public void tearDown() throws Exception {
91+
try {
92+
if (this.testProject != null) {
93+
if (this.testProject.exists() && this.testProject.isAccessible()) {
94+
EclipseUtils.removePMDNature(this.testProject);
95+
this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
96+
this.testProject.delete(true, true, null);
97+
this.testProject = null;
98+
}
99+
}
100+
} catch (final Exception e) {
101+
System.out.println("Exception " + e.getClass().getName() + " when tearing down. Ignored.");
102+
}
103+
}
104+
105+
/**
106+
* Test the basic usage of the processor command
107+
*/
108+
@Test
109+
public void testReviewCmdBasic() throws CommandException, CoreException {
110+
final ReviewCodeCmd cmd = new ReviewCodeCmd();
111+
cmd.addResource(this.testProject);
112+
cmd.performExecute();
113+
cmd.join();
114+
115+
IMarker[] markersFirst = this.testProject.getFile("src/main/java/first/First.java")
116+
.findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1);
117+
Assert.assertEquals(2, markersFirst.length);
118+
assertHasRuleViolation(markersFirst, "ClassNamingConventions");
119+
assertHasRuleViolation(markersFirst, "UseUtilityClass");
120+
121+
IMarker[] markersSecond = this.testProject.getFile("src/main/java/second/Second.java")
122+
.findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1);
123+
Assert.assertEquals(1, markersSecond.length);
124+
assertHasRuleViolation(markersSecond, "UseUtilityClass");
125+
126+
IMarker[] markersThird = this.testProject.getFile("src/main/java/third/Third.java")
127+
.findMarkers(PMDRuntimeConstants.PMD_MARKER_1, false, 1);
128+
Assert.assertEquals(1, markersThird.length);
129+
assertHasRuleViolation(markersThird, "ClassNamingConventions");
130+
}
131+
132+
private void assertHasRuleViolation(IMarker[] markers, String rulename) throws CoreException {
133+
boolean found = false;
134+
for (IMarker marker : markers) {
135+
if (marker.getAttribute(PMDRuntimeConstants.KEY_MARKERATT_RULENAME).equals(rulename)) {
136+
found = true;
137+
break;
138+
}
139+
}
140+
if (!found) {
141+
Assert.fail("Expected violation for rule " + rulename + ", but no violation found!");
142+
}
143+
}
144+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
package first;
3+
4+
public class First {
5+
6+
public static boolean isBoolean() {
7+
return true;
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
package second;
3+
4+
public class Second {
5+
6+
public static boolean isBoolean() {
7+
return true;
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
package third;
3+
4+
public class Third {
5+
6+
public static boolean isBoolean() {
7+
return true;
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
name="PMD Custom Ruleset"
4+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
5+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
7+
http://pmd.sf.net/ruleset_xml_schema.xsd" >
8+
9+
<description>Rules</description>
10+
11+
<exclude-pattern>.*/src/main/java/third/.*</exclude-pattern>
12+
13+
<rule ref="category/java/codestyle.xml/ClassNamingConventions">
14+
<priority>1</priority>
15+
</rule>
16+
17+
<rule ref="category/java/design.xml" >
18+
<exclude name="UseUtilityClass"/>
19+
</rule>
20+
21+
</ruleset>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
name="Custom Ruleset"
4+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
5+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
7+
http://pmd.sf.net/ruleset_xml_schema.xsd" >
8+
9+
<description>Custom rule</description>
10+
11+
<exclude-pattern>.*/src/main/java/second/.*</exclude-pattern>
12+
13+
<rule ref="category/java/design.xml/UseUtilityClass">
14+
<priority>1</priority>
15+
</rule>
16+
</ruleset>

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,16 @@ public interface IProjectProperties {
122122
/**
123123
* @return Returns the resolved RuleSet File suitable for loading a rule
124124
* set.
125+
* @deprecated use {@link #getResolvedRuleSetFiles()} instead
125126
*/
126-
List<File> getResolvedRuleSetFile() throws PropertiesException;
127+
@Deprecated
128+
File getResolvedRuleSetFile() throws PropertiesException;
129+
130+
/**
131+
* @return Returns the resolved RuleSet Files suitable for loading multiple
132+
* rulesets.
133+
*/
134+
List<File> getResolvedRuleSetFiles() throws PropertiesException;
127135

128136
/**
129137
* @return Returns the project Working Set.

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void setProjectRuleSets(final RuleSets newProjectRuleSets) throws Propert
201201
this.needRebuild = !this.projectRuleSets.getAllRules().equals(newProjectRuleSets.getAllRules());
202202
this.projectRuleSets = newProjectRuleSets;
203203
if (this.ruleSetStoredInProject) {
204-
for (File f : getResolvedRuleSetFile()) {
204+
for (File f : getResolvedRuleSetFiles()) {
205205
if (f != null) {
206206
if (projectRuleFileLastModified < f.lastModified()) {
207207
projectRuleFileLastModified = f.lastModified();
@@ -232,7 +232,7 @@ public void setRuleSetStoredInProject(final boolean ruleSetStoredInProject) thro
232232
throw new PropertiesException(
233233
"The project ruleset file cannot be found for project " + this.project.getName());
234234
}
235-
for (File f : getResolvedRuleSetFile()) {
235+
for (File f : getResolvedRuleSetFiles()) {
236236
if (f != null) {
237237
if (projectRuleFileLastModified < f.lastModified()) {
238238
projectRuleFileLastModified = f.lastModified();
@@ -262,7 +262,7 @@ public void setRuleSetFile(String ruleSetFile) throws PropertiesException {
262262
throw new PropertiesException(
263263
"The project ruleset file cannot be found for project " + project.getName());
264264
}
265-
for (File f : getResolvedRuleSetFile()) {
265+
for (File f : getResolvedRuleSetFiles()) {
266266
if (f != null) {
267267
if (projectRuleFileLastModified < f.lastModified()) {
268268
projectRuleFileLastModified = f.lastModified();
@@ -294,12 +294,13 @@ public void setProjectWorkingSet(final IWorkingSet projectWorkingSet) {
294294
/**
295295
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties#isNeedRebuild()
296296
*/
297+
@Override
297298
public boolean isNeedRebuild() {
298299
LOG.debug("Query if project " + project.getName() + " need rebuild : " + (pmdEnabled && needRebuild));
299300
LOG.debug(" PMD Enabled = " + pmdEnabled);
300301
LOG.debug(" Project need rebuild = " + needRebuild);
301302
if (ruleSetStoredInProject) {
302-
for (File f : getResolvedRuleSetFile()) {
303+
for (File f : getResolvedRuleSetFiles()) {
303304
if (f != null) {
304305
needRebuild |= f.lastModified() > projectRuleFileLastModified;
305306
}
@@ -321,18 +322,28 @@ public void setNeedRebuild(final boolean needRebuild) {
321322
*/
322323
public final boolean isRuleSetFileExist() {
323324
boolean ret = true;
324-
for (File f : getResolvedRuleSetFile()) {
325+
for (File f : getResolvedRuleSetFiles()) {
325326
if (!f.exists()) {
326327
ret = false;
327328
}
328329
}
329330
return ret;
330331
}
331332

333+
@Deprecated
334+
@Override
335+
public File getResolvedRuleSetFile() throws PropertiesException {
336+
if (isRuleSetFileExist()) {
337+
return getResolvedRuleSetFiles().get(0);
338+
}
339+
return null;
340+
}
341+
332342
/**
333-
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties#getResolvedRuleSetFile()
343+
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties#getResolvedRuleSetFiles()
334344
*/
335-
public List<File> getResolvedRuleSetFile() {
345+
@Override
346+
public List<File> getResolvedRuleSetFiles() {
336347
// Check as project-relative path
337348
List<File> files = new ArrayList<File>();
338349
for (String ruleSetFile : getRuleSetFile().split(",")) {

0 commit comments

Comments
 (0)