Skip to content

Commit

Permalink
Add test for PMD Preferences / Rule Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Apr 19, 2024
1 parent 475c54b commit f010f9b
Showing 1 changed file with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/


package net.sourceforge.pmd.eclipse.ui.preferences.br;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withStyle;
import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForWidget;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.ILogListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.PlatformUI;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import net.sourceforge.pmd.eclipse.AbstractSWTBotTest;

public class PMDPreferencePage2Test extends AbstractSWTBotTest {
private List<IStatus> errors;
private ILogListener logListener;

@Before
public void setup() {
errors = new ArrayList<>();
logListener = new ILogListener() {
@Override
public void logging(IStatus status, String plugin) {
if (status.getSeverity() != IStatus.ERROR) {
return;
}

String statusText = status.toString().toLowerCase(Locale.ROOT);
if (statusText.contains("pmd") || statusText.contains("unhandled event loop exception")) {
errors.add(status);
}
}
};
Platform.getLog(PlatformUI.class).addLogListener(logListener);
}

@After
public void cleanup() {
Platform.getLog(PlatformUI.class).removeLogListener(logListener);
}

/**
* Simple smoke test to open the rule configuration preference pages.
* @see PMDPreferencePage2
*/
@Test
public void openPMDRuleConfiguration() {
bot.menu().menu("Window", "Preferences").click();
SWTBotShell preferencesDialog = bot.shell("Preferences");
SWTBotTreeItem pmdItem = preferencesDialog.bot().tree(0).getTreeItem("PMD");
pmdItem.click();
pmdItem.expand();
pmdItem.getNode("Rule Configuration").click();


Matcher<Button> matcher = allOf(widgetOfType(Button.class),
withMnemonic("Use global rule management"),
withStyle(SWT.CHECK, "SWT.CHECK"));
WaitForObjectCondition<Button> waitForWidget = waitForWidget(matcher);
preferencesDialog.bot().waitUntil(waitForWidget, TimeUnit.SECONDS.toMillis(15));
SWTBotCheckBox globalRuleManagementCheckbox = new SWTBotCheckBox(waitForWidget.get(0), matcher);
globalRuleManagementCheckbox.click();

SWTBotTree ruleTable = preferencesDialog.bot().tree(1);
int rowCount = ruleTable.rowCount();
assertTrue(rowCount > 0);

// select first rule
ruleTable.getAllItems()[0].click();

preferencesDialog.bot().button("Cancel").click();

if (!errors.isEmpty()) {
fail("There are " + errors.size() + " errors:\n"
+ errors.stream().map(IStatus::getMessage).collect(Collectors.joining("\n")));
}
}
}

0 comments on commit f010f9b

Please sign in to comment.