Skip to content

Commit 6a50f48

Browse files
committed
Add test for violations details dialog
1 parent 84c7c6d commit 6a50f48

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.eclipse.ui.dialogs;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.io.InputStream;
10+
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.resources.IncrementalProjectBuilder;
15+
import org.eclipse.core.runtime.CoreException;
16+
import org.eclipse.core.runtime.NullProgressMonitor;
17+
import org.eclipse.jdt.ui.JavaUI;
18+
import org.eclipse.swt.widgets.Display;
19+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
20+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
21+
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
22+
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
23+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
24+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
25+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
26+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
27+
import org.eclipse.ui.IWorkbench;
28+
import org.eclipse.ui.IWorkbenchPage;
29+
import org.eclipse.ui.IWorkbenchWindow;
30+
import org.eclipse.ui.PlatformUI;
31+
import org.eclipse.ui.WorkbenchException;
32+
import org.eclipse.ui.navigator.resources.ProjectExplorer;
33+
import org.junit.After;
34+
import org.junit.AfterClass;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.BeforeClass;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
41+
import net.sourceforge.pmd.eclipse.EclipseUtils;
42+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
43+
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
44+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
45+
46+
@RunWith(SWTBotJunit4ClassRunner.class)
47+
public class ViolationDetailsDialogTest {
48+
private static SWTWorkbenchBot bot;
49+
private static final String PROJECT_NAME = ViolationDetailsDialogTest.class.getSimpleName();
50+
51+
private IProject testProject;
52+
53+
@BeforeClass
54+
public static void initBot() throws InterruptedException {
55+
bot = new SWTWorkbenchBot();
56+
for (SWTBotView view : bot.views()) {
57+
if ("Welcome".equals(view.getTitle())) {
58+
view.close();
59+
}
60+
}
61+
}
62+
63+
@AfterClass
64+
public static void afterClass() {
65+
try {
66+
bot.resetWorkbench();
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
72+
73+
@Before
74+
public void setUp() throws Exception {
75+
// 1. Create a Java project
76+
this.testProject = EclipseUtils.createJavaProject(PROJECT_NAME);
77+
Assert.assertTrue("A test project cannot be created; the tests cannot be performed.",
78+
this.testProject != null && this.testProject.exists() && this.testProject.isAccessible());
79+
80+
// 2. Create a test source file inside that project
81+
EclipseUtils.createTestSourceFile(testProject, "/src/MyInterface.java", "public interface MyInterface {\n public void run();\n}\n".replaceAll("\\R", System.lineSeparator()));
82+
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/MyInterface.java")) {
83+
Assert.assertNotNull("Cannot find the test source file", is);
84+
}
85+
86+
// 3. Enable PMD for the test project
87+
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()
88+
.loadProjectProperties(testProject);
89+
properties.setPmdEnabled(true);
90+
properties.sync();
91+
}
92+
93+
@After
94+
public void tearDown() throws Exception {
95+
try {
96+
if (this.testProject != null) {
97+
if (this.testProject.exists() && this.testProject.isAccessible()) {
98+
EclipseUtils.removePMDNature(this.testProject);
99+
this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
100+
this.testProject.delete(true, true, null);
101+
this.testProject = null;
102+
}
103+
}
104+
} catch (final Exception e) {
105+
System.out.println("Exception " + e.getClass().getName() + " when tearing down. Ignored.");
106+
}
107+
}
108+
109+
@Test
110+
public void openDialogViaProblemView() throws Exception {
111+
buildAndWaitForViolations();
112+
openJavaPerspective();
113+
114+
SWTBotView problemsView = bot.viewByPartName("Problems");
115+
problemsView.bot().tree().getTreeItem("Warnings (4 items)").expand();
116+
SWTBotTreeItem item = problemsView.bot().tree().getTreeItem("Warnings (4 items)").getNode("UnnecessaryModifier: Unnecessary modifier 'public' on method 'run': the method is declared in an interface type").select();
117+
item.contextMenu("Show details...").click();
118+
119+
assertDialog();
120+
}
121+
122+
@Test
123+
public void openDialogViaViolationOutlineView() throws Exception {
124+
buildAndWaitForViolations();
125+
openPMDPerspective();
126+
127+
SWTBotTree projectTree = bot.viewByPartName("Package Explorer").bot().tree();
128+
SWTBotTreeItem item = projectTree.getTreeItem("ViolationDetailsDialogTest").expand();
129+
item = item.getNode("src").expand();
130+
item = item.getNode("(default package)").expand();
131+
item = item.getNode("MyInterface.java").select();
132+
item.doubleClick();
133+
134+
SWTBotView outlineView = bot.viewByPartName("Violations Outline");
135+
int row = outlineView.bot().table().indexOf("UnnecessaryModifier", 3);
136+
SWTBotTableItem tableItem = outlineView.bot().table().getTableItem(row);
137+
tableItem.select();
138+
tableItem.contextMenu("Show details ...").click();
139+
140+
assertDialog();
141+
}
142+
143+
private void assertDialog() {
144+
SWTBotShell dialog = bot.shell("PMD Plugin: Violation Details");
145+
String message = dialog.bot().text(0).getText();
146+
dialog.bot().button("Close").click();
147+
148+
assertEquals("Unnecessary modifier 'public' on method 'run': the method is declared in an interface type", message);
149+
}
150+
151+
private void buildAndWaitForViolations() throws CoreException {
152+
testProject.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
153+
154+
bot.waitUntil(new DefaultCondition() {
155+
@Override
156+
public boolean test() throws Exception {
157+
IMarker[] markers = testProject.findMarkers(PMDRuntimeConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
158+
return markers.length > 0;
159+
}
160+
161+
@Override
162+
public String getFailureMessage() {
163+
return "At least one marker is expected";
164+
}
165+
});
166+
}
167+
168+
private static void openPMDPerspective() throws InterruptedException {
169+
Display.getDefault().syncExec(new Runnable() {
170+
@Override
171+
public void run() {
172+
try {
173+
IWorkbench workbench = PlatformUI.getWorkbench();
174+
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
175+
workbench.showPerspective(PMDRuntimeConstants.ID_PERSPECTIVE, activeWorkbenchWindow);
176+
} catch (WorkbenchException e) {
177+
e.printStackTrace();
178+
}
179+
}
180+
});
181+
}
182+
183+
private static void openJavaPerspective() throws InterruptedException {
184+
Display.getDefault().syncExec(new Runnable() {
185+
@Override
186+
public void run() {
187+
try {
188+
IWorkbench workbench = PlatformUI.getWorkbench();
189+
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
190+
workbench.showPerspective(JavaUI.ID_PERSPECTIVE, activeWorkbenchWindow);
191+
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
192+
activePage.showView(ProjectExplorer.VIEW_ID);
193+
} catch (WorkbenchException e) {
194+
e.printStackTrace();
195+
}
196+
}
197+
});
198+
}
199+
}

0 commit comments

Comments
 (0)