Skip to content

Commit 866377c

Browse files
committed
Merge pull request #166 from adangel:issue-109-report-duplicated-entries
Fix duplicate entries in reports (#109) #166
2 parents b097667 + 17bc4c9 commit 866377c

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This is a minor release.
1515

1616
### Fixed Issues
1717

18+
* [#109](https://github.com/pmd/pmd-eclipse-plugin/issues/109): Duplicate entries in reports
19+
1820
### API Changes
1921

2022
### External Contributions

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/RenderReportCmdTest.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package net.sourceforge.pmd.eclipse.runtime.cmd;
66

77
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.io.Reader;
810

911
import org.eclipse.core.resources.IFile;
1012
import org.eclipse.core.resources.IFolder;
13+
import org.eclipse.core.resources.IMarker;
1114
import org.eclipse.core.resources.IProject;
1215
import org.eclipse.core.resources.IResource;
1316
import org.eclipse.core.runtime.CoreException;
@@ -17,8 +20,12 @@
1720
import org.junit.Test;
1821

1922
import net.sourceforge.pmd.eclipse.EclipseUtils;
23+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
2024
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
25+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
26+
import net.sourceforge.pmd.eclipse.util.internal.IOUtil;
2127
import net.sourceforge.pmd.renderers.HTMLRenderer;
28+
import net.sourceforge.pmd.renderers.TextRenderer;
2229

2330
/**
2431
* Test the report rendering
@@ -42,6 +49,11 @@ public void setUp() throws Exception {
4249
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java")) {
4350
Assert.assertNotNull("Cannot find the test source file", is);
4451
}
52+
53+
// 3. Enable PMD for the test project
54+
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()
55+
.loadProjectProperties(testProject);
56+
properties.setPmdEnabled(true);
4557
}
4658

4759
@After
@@ -59,14 +71,14 @@ public void tearDown() throws Exception {
5971
}
6072

6173
/**
62-
* Test the basic usage of the report rendering command
63-
*
74+
* Test the basic usage of the report rendering command.
6475
*/
6576
@Test
6677
public void testRenderReportCmdBasic() throws CoreException {
6778
final ReviewCodeCmd reviewCmd = new ReviewCodeCmd();
6879
reviewCmd.addResource(this.testProject);
6980
reviewCmd.performExecute();
81+
reviewCmd.join();
7082

7183
final RenderReportsCmd cmd = new RenderReportsCmd();
7284
cmd.setProject(this.testProject);
@@ -91,6 +103,58 @@ public void testRenderReportCmdBasic() throws CoreException {
91103
}
92104
}
93105

106+
/**
107+
* Test text format renderer.
108+
*/
109+
@Test
110+
public void testRenderReportCmdText() throws Exception {
111+
final ReviewCodeCmd reviewCmd = new ReviewCodeCmd();
112+
reviewCmd.addResource(this.testProject);
113+
reviewCmd.performExecute();
114+
reviewCmd.join();
115+
116+
IMarker[] markers = this.testProject.findMarkers(PMDRuntimeConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
117+
Assert.assertEquals(14, markers.length);
118+
119+
final RenderReportsCmd cmd = new RenderReportsCmd();
120+
cmd.setProject(this.testProject);
121+
cmd.registerRenderer(new TextRenderer(), PMDRuntimeConstants.TXT_REPORT_NAME);
122+
cmd.performExecute();
123+
cmd.join();
124+
125+
final IFolder reportFolder = this.testProject.getFolder(PMDRuntimeConstants.REPORT_FOLDER);
126+
Assert.assertTrue(reportFolder.exists());
127+
128+
final IFile reportFile = reportFolder.getFile(PMDRuntimeConstants.TXT_REPORT_NAME);
129+
Assert.assertTrue(reportFile.exists());
130+
try (Reader reader = new InputStreamReader(reportFile.getContents(), reportFile.getCharset())) {
131+
String report = IOUtil.toString(reader);
132+
Assert.assertEquals(1, countMatches(report, "src/Test.java:5:\tNoPackage:\tNoPackage: All classes, interfaces, enums and annotations must belong to a named package"));
133+
String[] lines = report.split("\r\n|\n");
134+
Assert.assertEquals(markers.length, lines.length);
135+
}
136+
137+
this.testProject.deleteMarkers(PMDRuntimeConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
138+
139+
if (reportFile.exists()) {
140+
reportFile.delete(true, false, null);
141+
}
142+
143+
if (reportFolder.exists()) {
144+
reportFolder.delete(true, false, null);
145+
}
146+
}
147+
148+
private static int countMatches(String s, String pattern) {
149+
int count = 0;
150+
int index = s.indexOf(pattern);
151+
while (index != -1) {
152+
count++;
153+
index = s.indexOf(pattern, index + pattern.length());
154+
}
155+
return count;
156+
}
157+
94158
/**
95159
* Test robustness #1
96160
*/

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/builder/MarkerUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public static void deleteMarkersIn(IResource resource, String[] markerTypes) thr
238238
}
239239

240240
public static IMarker[] findAllMarkers(IResource resource) throws CoreException {
241-
return findMarkers(resource, PMDRuntimeConstants.ALL_MARKER_TYPES);
241+
return findMarkers(resource, PMDRuntimeConstants.PMD_MARKER);
242242
}
243243

244244
public static IMarker[] findMarkers(IResource resource, String markerType) throws CoreException {

0 commit comments

Comments
 (0)