|
| 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.properties; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertFalse; |
| 9 | +import static org.junit.Assert.assertNotNull; |
| 10 | +import static org.junit.Assert.assertTrue; |
| 11 | + |
| 12 | +import org.eclipse.core.resources.IProject; |
| 13 | +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; |
| 17 | +import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
| 18 | +import org.eclipse.swtbot.swt.finder.SWTBot; |
| 19 | +import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
| 20 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; |
| 21 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
| 22 | +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; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import net.sourceforge.pmd.eclipse.EclipseUtils; |
| 37 | +import net.sourceforge.pmd.eclipse.internal.ResourceUtil; |
| 38 | + |
| 39 | +@RunWith(SWTBotJunit4ClassRunner.class) |
| 40 | +public class PMDProjectPropertyPageTest { |
| 41 | + private static SWTWorkbenchBot bot; |
| 42 | + private static final String PROJECT_NAME = PMDProjectPropertyPageTest.class.getName(); |
| 43 | + |
| 44 | + private IProject testProject; |
| 45 | + |
| 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 | + |
| 66 | + @Before |
| 67 | + public void setUp() throws Exception { |
| 68 | + this.testProject = EclipseUtils.createJavaProject(PROJECT_NAME); |
| 69 | + assertTrue("A test project cannot be created; the tests cannot be performed.", |
| 70 | + this.testProject != null && this.testProject.exists() && this.testProject.isAccessible()); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void tearDown() throws Exception { |
| 75 | + if (this.testProject != null) { |
| 76 | + if (this.testProject.exists() && this.testProject.isAccessible()) { |
| 77 | + EclipseUtils.removePMDNature(this.testProject); |
| 78 | + this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 79 | + this.testProject.delete(true, true, null); |
| 80 | + this.testProject = null; |
| 81 | + } else { |
| 82 | + System.out.println("WARNING: Test Project has not been deleted!"); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void twoRadioButtonsForRuleSelectionAreShown() { |
| 89 | + SWTBot dialogBot = openProjectProperties(); |
| 90 | + SWTBotCheckBox enablePmdCheckbox = dialogBot.checkBox("Enable PMD"); |
| 91 | + assertFalse("PMD should not enabled by default", enablePmdCheckbox.isChecked()); |
| 92 | + enablePmdCheckbox.click(); |
| 93 | + assertNotNull(dialogBot.radio("Use local rules")); |
| 94 | + assertTrue("local rules should be used by default", dialogBot.radio("Use local rules").isSelected()); |
| 95 | + assertNotNull(dialogBot.radio("Use the ruleset configured in a project file")); |
| 96 | + dialogBot.button("Cancel").click(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * When switching between local rules and ruleset file, only one option of the two |
| 101 | + * should be persisted in the ".pmd" file. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + public void verifyProjectPropertiesAfterSwitchingFromRulesetFileToLocal() throws Exception { |
| 105 | + // first enable PMD with local rules (default) |
| 106 | + SWTBot dialogBot = openProjectProperties(); |
| 107 | + dialogBot.checkBox("Enable PMD").click(); |
| 108 | + dialogBot.button("Apply and Close").click(); |
| 109 | + |
| 110 | + String projectProperties = ResourceUtil.getResourceAsString(testProject, ".pmd"); |
| 111 | + assertFalse("No ruleSetFile should be in the properties", projectProperties.contains("<ruleSetFile>")); |
| 112 | + assertTrue("rules should be in the properties", projectProperties.contains("<rules>")); |
| 113 | + assertTrue(projectProperties.contains("<useProjectRuleSet>false</useProjectRuleSet>")); |
| 114 | + |
| 115 | + // now enable ruleset file |
| 116 | + dialogBot = openProjectProperties(); |
| 117 | + dialogBot.radio("Use the ruleset configured in a project file").click(); |
| 118 | + dialogBot.button("Apply and Close").click(); |
| 119 | + |
| 120 | + bot.shell("PMD Question").bot().button("Yes").click(); |
| 121 | + |
| 122 | + projectProperties = ResourceUtil.getResourceAsString(testProject, ".pmd"); |
| 123 | + assertTrue("ruleSetFile should be in the properties", projectProperties.contains("<ruleSetFile>")); |
| 124 | + assertFalse("No rules should be in the properties", projectProperties.contains("<rules>")); |
| 125 | + assertTrue(projectProperties.contains("<useProjectRuleSet>true</useProjectRuleSet>")); |
| 126 | + } |
| 127 | + |
| 128 | + private SWTBot openProjectProperties() { |
| 129 | + SWTBotView projectExplorer = bot.viewByTitle("Project Explorer"); |
| 130 | + projectExplorer.show(); |
| 131 | + SWTBotTreeItem projectItem = projectExplorer.bot().tree().getTreeItem(PROJECT_NAME); |
| 132 | + projectItem.select(); |
| 133 | + projectItem.contextMenu().menu("Properties", false, 0).click(); |
| 134 | + SWTBotShell dialog = bot.shell("Properties for " + PROJECT_NAME); |
| 135 | + dialog.bot().tree().getTreeItem("PMD").select(); |
| 136 | + return dialog.bot(); |
| 137 | + } |
| 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 | + } |
| 155 | +} |
0 commit comments