Skip to content

Commit f010f9b

Browse files
committed
Add test for PMD Preferences / Rule Configuration
1 parent 475c54b commit f010f9b

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
6+
package net.sourceforge.pmd.eclipse.ui.preferences.br;
7+
8+
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
9+
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
10+
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
11+
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withStyle;
12+
import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForWidget;
13+
import static org.junit.Assert.assertTrue;
14+
import static org.junit.Assert.fail;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Locale;
19+
import java.util.concurrent.TimeUnit;
20+
import java.util.stream.Collectors;
21+
22+
import org.eclipse.core.runtime.ILogListener;
23+
import org.eclipse.core.runtime.IStatus;
24+
import org.eclipse.core.runtime.Platform;
25+
import org.eclipse.swt.SWT;
26+
import org.eclipse.swt.widgets.Button;
27+
import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
28+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
29+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
30+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
31+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
32+
import org.eclipse.ui.PlatformUI;
33+
import org.hamcrest.Matcher;
34+
import org.junit.After;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
38+
import net.sourceforge.pmd.eclipse.AbstractSWTBotTest;
39+
40+
public class PMDPreferencePage2Test extends AbstractSWTBotTest {
41+
private List<IStatus> errors;
42+
private ILogListener logListener;
43+
44+
@Before
45+
public void setup() {
46+
errors = new ArrayList<>();
47+
logListener = new ILogListener() {
48+
@Override
49+
public void logging(IStatus status, String plugin) {
50+
if (status.getSeverity() != IStatus.ERROR) {
51+
return;
52+
}
53+
54+
String statusText = status.toString().toLowerCase(Locale.ROOT);
55+
if (statusText.contains("pmd") || statusText.contains("unhandled event loop exception")) {
56+
errors.add(status);
57+
}
58+
}
59+
};
60+
Platform.getLog(PlatformUI.class).addLogListener(logListener);
61+
}
62+
63+
@After
64+
public void cleanup() {
65+
Platform.getLog(PlatformUI.class).removeLogListener(logListener);
66+
}
67+
68+
/**
69+
* Simple smoke test to open the rule configuration preference pages.
70+
* @see PMDPreferencePage2
71+
*/
72+
@Test
73+
public void openPMDRuleConfiguration() {
74+
bot.menu().menu("Window", "Preferences").click();
75+
SWTBotShell preferencesDialog = bot.shell("Preferences");
76+
SWTBotTreeItem pmdItem = preferencesDialog.bot().tree(0).getTreeItem("PMD");
77+
pmdItem.click();
78+
pmdItem.expand();
79+
pmdItem.getNode("Rule Configuration").click();
80+
81+
82+
Matcher<Button> matcher = allOf(widgetOfType(Button.class),
83+
withMnemonic("Use global rule management"),
84+
withStyle(SWT.CHECK, "SWT.CHECK"));
85+
WaitForObjectCondition<Button> waitForWidget = waitForWidget(matcher);
86+
preferencesDialog.bot().waitUntil(waitForWidget, TimeUnit.SECONDS.toMillis(15));
87+
SWTBotCheckBox globalRuleManagementCheckbox = new SWTBotCheckBox(waitForWidget.get(0), matcher);
88+
globalRuleManagementCheckbox.click();
89+
90+
SWTBotTree ruleTable = preferencesDialog.bot().tree(1);
91+
int rowCount = ruleTable.rowCount();
92+
assertTrue(rowCount > 0);
93+
94+
// select first rule
95+
ruleTable.getAllItems()[0].click();
96+
97+
preferencesDialog.bot().button("Cancel").click();
98+
99+
if (!errors.isEmpty()) {
100+
fail("There are " + errors.size() + " errors:\n"
101+
+ errors.stream().map(IStatus::getMessage).collect(Collectors.joining("\n")));
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)