Skip to content

Commit 2fd502b

Browse files
committed
Add unit test for PMDCheckAction
1 parent 4a2a844 commit 2fd502b

File tree

1 file changed

+91
-0
lines changed
  • net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/actions

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.eclipse.ui.actions;
6+
7+
import java.io.InputStream;
8+
9+
import org.eclipse.core.resources.IMarker;
10+
import org.eclipse.core.resources.IProject;
11+
import org.eclipse.core.resources.IResource;
12+
import org.eclipse.core.resources.WorkspaceJob;
13+
import org.eclipse.core.runtime.CoreException;
14+
import org.eclipse.core.runtime.jobs.Job;
15+
import org.eclipse.jface.viewers.ISelection;
16+
import org.eclipse.jface.viewers.StructuredSelection;
17+
import org.junit.After;
18+
import org.junit.Assert;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import net.sourceforge.pmd.eclipse.EclipseUtils;
23+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
24+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
25+
26+
public class PMDCheckActionTest {
27+
private IProject testProject;
28+
29+
@Before
30+
public void setUp() throws Exception {
31+
// 1. Create a Java project
32+
this.testProject = EclipseUtils.createJavaProject("PMDCheckActionTest");
33+
Assert.assertTrue("A test project cannot be created; the tests cannot be performed.",
34+
this.testProject != null && this.testProject.exists() && this.testProject.isAccessible());
35+
36+
// 2. Create a test source file inside that project
37+
EclipseUtils.createTestSourceFile(this.testProject);
38+
final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
39+
Assert.assertNotNull("Cannot find the test source file", is);
40+
is.close();
41+
42+
// 3. Enable PMD for the test project
43+
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()
44+
.loadProjectProperties(testProject);
45+
properties.setPmdEnabled(true);
46+
}
47+
48+
@After
49+
public void tearDown() throws Exception {
50+
try {
51+
if (this.testProject != null) {
52+
if (this.testProject.exists() && this.testProject.isAccessible()) {
53+
EclipseUtils.removePMDNature(this.testProject);
54+
this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
55+
this.testProject.delete(true, true, null);
56+
this.testProject = null;
57+
}
58+
}
59+
} catch (final Exception e) {
60+
System.out.println("Exception " + e.getClass().getName() + " when tearing down. Ignored.");
61+
}
62+
}
63+
64+
@Test
65+
public void runCheckAction() throws CoreException, InterruptedException {
66+
IMarker[] markers = testProject.findMarkers("net.sourceforge.pmd.eclipse.plugin.pmdMarker", true, IResource.DEPTH_INFINITE);
67+
Assert.assertEquals(0, markers.length);
68+
69+
ISelection selection = new StructuredSelection(testProject);
70+
PMDCheckAction action = new PMDCheckAction();
71+
action.selectionChanged(null, selection);
72+
action.run(null);
73+
74+
while (isReviewCodeJobStillRunning()) {
75+
Thread.sleep(500);
76+
}
77+
78+
markers = testProject.findMarkers("net.sourceforge.pmd.eclipse.plugin.pmdMarker", true, IResource.DEPTH_INFINITE);
79+
Assert.assertTrue("at least one marker is expected", markers.length > 0);
80+
}
81+
82+
private boolean isReviewCodeJobStillRunning() {
83+
Job[] jobs = WorkspaceJob.getJobManager().find(null);
84+
for (Job job : jobs) {
85+
if ("ReviewCode".equals(job.getName())) {
86+
return true;
87+
}
88+
}
89+
return false;
90+
}
91+
}

0 commit comments

Comments
 (0)