Skip to content

Commit 89b00ec

Browse files
committed
Refactor swtbot based tests
1 parent 137d1fe commit 89b00ec

File tree

3 files changed

+68
-102
lines changed

3 files changed

+68
-102
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
6+
package net.sourceforge.pmd.eclipse;
7+
8+
import org.eclipse.jdt.ui.JavaUI;
9+
import org.eclipse.swt.widgets.Display;
10+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
11+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
12+
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
13+
import org.eclipse.ui.IWorkbench;
14+
import org.eclipse.ui.IWorkbenchPage;
15+
import org.eclipse.ui.IWorkbenchWindow;
16+
import org.eclipse.ui.PlatformUI;
17+
import org.eclipse.ui.WorkbenchException;
18+
import org.eclipse.ui.navigator.resources.ProjectExplorer;
19+
import org.junit.AfterClass;
20+
import org.junit.BeforeClass;
21+
import org.junit.runner.RunWith;
22+
23+
@RunWith(SWTBotJunit4ClassRunner.class)
24+
public abstract class AbstractSWTBotTest {
25+
protected static SWTWorkbenchBot bot;
26+
27+
@BeforeClass
28+
public static void initBot() throws InterruptedException {
29+
bot = new SWTWorkbenchBot();
30+
for (SWTBotView view : bot.views()) {
31+
if ("Welcome".equals(view.getTitle())) {
32+
view.close();
33+
}
34+
}
35+
openJavaPerspective();
36+
}
37+
38+
@AfterClass
39+
public static void afterClass() {
40+
try {
41+
bot.resetWorkbench();
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
protected static void openJavaPerspective() throws InterruptedException {
48+
Display.getDefault().syncExec(new Runnable() {
49+
@Override
50+
public void run() {
51+
try {
52+
IWorkbench workbench = PlatformUI.getWorkbench();
53+
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
54+
workbench.showPerspective(JavaUI.ID_PERSPECTIVE, activeWorkbenchWindow);
55+
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
56+
activePage.showView(ProjectExplorer.VIEW_ID);
57+
} catch (WorkbenchException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
});
62+
}
63+
}

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/dialogs/ViolationDetailsDialogTest.java

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,33 @@
1414
import org.eclipse.core.resources.IncrementalProjectBuilder;
1515
import org.eclipse.core.runtime.CoreException;
1616
import org.eclipse.core.runtime.NullProgressMonitor;
17-
import org.eclipse.jdt.ui.JavaUI;
1817
import org.eclipse.swt.widgets.Display;
19-
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
2018
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
21-
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
2219
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
2320
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
2421
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
2522
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
2623
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
2724
import org.eclipse.ui.IWorkbench;
28-
import org.eclipse.ui.IWorkbenchPage;
2925
import org.eclipse.ui.IWorkbenchWindow;
3026
import org.eclipse.ui.PlatformUI;
3127
import org.eclipse.ui.WorkbenchException;
32-
import org.eclipse.ui.navigator.resources.ProjectExplorer;
3328
import org.junit.After;
34-
import org.junit.AfterClass;
3529
import org.junit.Assert;
3630
import org.junit.Before;
37-
import org.junit.BeforeClass;
3831
import org.junit.Test;
39-
import org.junit.runner.RunWith;
4032

33+
import net.sourceforge.pmd.eclipse.AbstractSWTBotTest;
4134
import net.sourceforge.pmd.eclipse.EclipseUtils;
4235
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
4336
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
4437
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
4538

46-
@RunWith(SWTBotJunit4ClassRunner.class)
47-
public class ViolationDetailsDialogTest {
48-
private static SWTWorkbenchBot bot;
39+
public class ViolationDetailsDialogTest extends AbstractSWTBotTest {
4940
private static final String PROJECT_NAME = ViolationDetailsDialogTest.class.getSimpleName();
5041

5142
private IProject testProject;
5243

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-
7344
@Before
7445
public void setUp() throws Exception {
7546
// 1. Create a Java project
@@ -179,21 +150,4 @@ public void run() {
179150
}
180151
});
181152
}
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-
}
199153
}

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/properties/PMDProjectPropertyPageTest.java

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,24 @@
1111

1212
import org.eclipse.core.resources.IProject;
1313
import org.eclipse.core.resources.IResource;
14-
import org.eclipse.jdt.ui.JavaUI;
15-
import org.eclipse.swt.widgets.Display;
16-
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
1714
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
1815
import org.eclipse.swtbot.swt.finder.SWTBot;
19-
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
2016
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
2117
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
2218
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
23-
import org.eclipse.ui.IWorkbench;
24-
import org.eclipse.ui.IWorkbenchPage;
25-
import org.eclipse.ui.IWorkbenchWindow;
26-
import org.eclipse.ui.PlatformUI;
27-
import org.eclipse.ui.WorkbenchException;
28-
import org.eclipse.ui.navigator.resources.ProjectExplorer;
2919
import org.junit.After;
30-
import org.junit.AfterClass;
3120
import org.junit.Before;
32-
import org.junit.BeforeClass;
3321
import org.junit.Test;
34-
import org.junit.runner.RunWith;
3522

23+
import net.sourceforge.pmd.eclipse.AbstractSWTBotTest;
3624
import net.sourceforge.pmd.eclipse.EclipseUtils;
3725
import net.sourceforge.pmd.eclipse.internal.ResourceUtil;
3826

39-
@RunWith(SWTBotJunit4ClassRunner.class)
40-
public class PMDProjectPropertyPageTest {
41-
private static SWTWorkbenchBot bot;
42-
private static final String PROJECT_NAME = PMDProjectPropertyPageTest.class.getName();
27+
public class PMDProjectPropertyPageTest extends AbstractSWTBotTest {
28+
private static final String PROJECT_NAME = PMDProjectPropertyPageTest.class.getSimpleName();
4329

4430
private IProject testProject;
4531

46-
@BeforeClass
47-
public static void initBot() throws InterruptedException {
48-
bot = new SWTWorkbenchBot();
49-
for (SWTBotView view : bot.views()) {
50-
if ("Welcome".equals(view.getTitle())) {
51-
view.close();
52-
}
53-
}
54-
openJavaPerspective();
55-
}
56-
57-
@AfterClass
58-
public static void afterClass() {
59-
try {
60-
bot.resetWorkbench();
61-
} catch (Exception e) {
62-
e.printStackTrace();
63-
}
64-
}
65-
6632
@Before
6733
public void setUp() throws Exception {
6834
this.testProject = EclipseUtils.createJavaProject(PROJECT_NAME);
@@ -135,21 +101,4 @@ private SWTBot openProjectProperties() {
135101
dialog.bot().tree().getTreeItem("PMD").select();
136102
return dialog.bot();
137103
}
138-
139-
private static void openJavaPerspective() throws InterruptedException {
140-
Display.getDefault().syncExec(new Runnable() {
141-
@Override
142-
public void run() {
143-
try {
144-
IWorkbench workbench = PlatformUI.getWorkbench();
145-
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
146-
workbench.showPerspective(JavaUI.ID_PERSPECTIVE, activeWorkbenchWindow);
147-
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
148-
activePage.showView(ProjectExplorer.VIEW_ID);
149-
} catch (WorkbenchException e) {
150-
e.printStackTrace();
151-
}
152-
}
153-
});
154-
}
155104
}

0 commit comments

Comments
 (0)