|
| 1 | +/* |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | +package net.sourceforge.pmd.eclipse.ui.properties; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.InputStream; |
| 9 | + |
| 10 | +import org.eclipse.core.resources.IFile; |
| 11 | +import org.eclipse.core.resources.IProject; |
| 12 | +import org.eclipse.core.runtime.CoreException; |
| 13 | +import org.junit.After; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Test; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import net.sourceforge.pmd.RuleSet; |
| 20 | +import net.sourceforge.pmd.RuleSets; |
| 21 | +import net.sourceforge.pmd.eclipse.EclipseUtils; |
| 22 | +import net.sourceforge.pmd.eclipse.internal.ResourceUtil; |
| 23 | +import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
| 24 | +import net.sourceforge.pmd.eclipse.runtime.cmd.BuildProjectCommand; |
| 25 | +import net.sourceforge.pmd.eclipse.runtime.cmd.JobCommandProcessor; |
| 26 | +import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties; |
| 27 | +import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager; |
| 28 | +import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil; |
| 29 | + |
| 30 | +public class ExternalRuleSetFileTest { |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(ExternalRuleSetFileTest.class); |
| 32 | + |
| 33 | + private static final String PMD_PROPERTIES_FILENAME = ".pmd"; |
| 34 | + |
| 35 | + private static final String PROJECT_RULESET_FILENAME = ".pmd-ruleset.xml"; |
| 36 | + |
| 37 | + private IProject testProject; |
| 38 | + |
| 39 | + |
| 40 | + private void setUpProject(String projectName) { |
| 41 | + try { |
| 42 | + this.testProject = EclipseUtils.createJavaProject(projectName); |
| 43 | + Assert.assertTrue("A test project cannot be created; the tests cannot be performed.", |
| 44 | + this.testProject != null && this.testProject.exists() && this.testProject.isAccessible()); |
| 45 | + } catch (CoreException e) { |
| 46 | + throw new RuntimeException(e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() throws Exception { |
| 52 | + try { |
| 53 | + if (this.testProject != null) { |
| 54 | + if (this.testProject.exists() && this.testProject.isAccessible()) { |
| 55 | + this.testProject.delete(true, true, null); |
| 56 | + this.testProject = null; |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (CoreException e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void changedExternalRulesetShouldBeReloaded() throws Exception { |
| 66 | + setUpProject("ExternalRulesetTest"); |
| 67 | + InputStream ruleset1 = ExternalRuleSetFileTest.class.getResourceAsStream("ruleset1.xml"); |
| 68 | + |
| 69 | + // create the ruleset file in the project |
| 70 | + IFile ruleSetFile = this.testProject.getFile(PROJECT_RULESET_FILENAME); |
| 71 | + if (ruleSetFile.exists()) { |
| 72 | + Assert.fail("File " + PROJECT_RULESET_FILENAME + " already exists!"); |
| 73 | + } |
| 74 | + try { |
| 75 | + ruleSetFile.create(ruleset1, true, null); |
| 76 | + } finally { |
| 77 | + ruleset1.close(); |
| 78 | + } |
| 79 | + |
| 80 | + // configure the project to use this |
| 81 | + final UpdateProjectPropertiesCmd cmd = new UpdateProjectPropertiesCmd(); |
| 82 | + cmd.setPmdEnabled(true); |
| 83 | + cmd.setProject(this.testProject); |
| 84 | + cmd.setProjectWorkingSet(null); |
| 85 | + cmd.setProjectRuleSets(new RuleSets(RuleSetUtil.newEmpty("empty", "empty"))); |
| 86 | + cmd.setRuleSetStoredInProject(true); |
| 87 | + cmd.setRuleSetFile(PROJECT_RULESET_FILENAME); |
| 88 | + cmd.execute(); |
| 89 | + |
| 90 | + // load the project settings - it should have this ruleset now active (1 rule) |
| 91 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 92 | + final IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 93 | + RuleSet projectRuleSet = model.getProjectRuleSet(); |
| 94 | + Assert.assertEquals(1, projectRuleSet.getRules().size()); |
| 95 | + |
| 96 | + // now let's change the ruleSetFile without eclipse knowing about it ("externally") |
| 97 | + File ruleSetFileReal = ruleSetFile.getLocation().toFile(); |
| 98 | + ResourceUtil.copyResource(this, "ruleset2.xml", ruleSetFileReal); |
| 99 | + |
| 100 | + // the file has changed, this should be detected |
| 101 | + Assert.assertTrue(model.isNeedRebuild()); |
| 102 | + // the model is not updated yet... |
| 103 | + Assert.assertEquals(1, model.getProjectRuleSet().getRules().size()); |
| 104 | + // but it will be when requesting the project properties again |
| 105 | + final IProjectProperties model2 = mgr.loadProjectProperties(testProject); |
| 106 | + Assert.assertEquals(2, model2.getProjectRuleSet().getRules().size()); |
| 107 | + // the model is still cached, but the rules are updated |
| 108 | + Assert.assertSame(model, model2); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void changedExternalRulesetShouldBeReloadedForPmdDisabledProjects() throws Exception { |
| 113 | + setUpProject("ExternalRulesetTestPmdDisabled"); |
| 114 | + |
| 115 | + // load the project properties - pmd is not enabled, it uses the default ruleset (more than 1 active rule) |
| 116 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 117 | + final IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 118 | + RuleSet projectRuleSet = model.getProjectRuleSet(); |
| 119 | + Assert.assertNotEquals(1, projectRuleSet.getRules().size()); |
| 120 | + |
| 121 | + // now let's change the ruleSetFile without eclipse knowing about it ("externally") |
| 122 | + IFile ruleSetFile = this.testProject.getFile(PROJECT_RULESET_FILENAME); |
| 123 | + if (ruleSetFile.exists()) { |
| 124 | + Assert.fail("File " + PROJECT_RULESET_FILENAME + " already exists!"); |
| 125 | + } |
| 126 | + File ruleSetFileReal = ruleSetFile.getLocation().toFile(); |
| 127 | + ResourceUtil.copyResource(this, "ruleset1.xml", ruleSetFileReal); |
| 128 | + |
| 129 | + // now create the .pmd project properties without eclipse knowing about it ("externally") |
| 130 | + IFile projectPropertiesFile = this.testProject.getFile(PMD_PROPERTIES_FILENAME); |
| 131 | + if (projectPropertiesFile.exists()) { |
| 132 | + Assert.fail("File .pmd does already exist!"); |
| 133 | + } |
| 134 | + File projectPropertiesFileReal = projectPropertiesFile.getLocation().toFile(); |
| 135 | + ResourceUtil.copyResource(this, "pmd-properties", projectPropertiesFileReal); |
| 136 | + |
| 137 | + // the files have been created, but the .pmd file is not reloaded yet, so: |
| 138 | + Assert.assertFalse(model.isNeedRebuild()); |
| 139 | + // the model is not updated yet... |
| 140 | + Assert.assertNotEquals(1, model.getProjectRuleSet().getRules().size()); |
| 141 | + // but it will be when requesting the project properties again |
| 142 | + final IProjectProperties model2 = mgr.loadProjectProperties(testProject); |
| 143 | + // PMD is still not enabled |
| 144 | + Assert.assertFalse(model.isPmdEnabled()); |
| 145 | + // but rebuild is needed (because ruleset changed) |
| 146 | + Assert.assertTrue(model.isNeedRebuild()); |
| 147 | + // and the ruleset is loaded |
| 148 | + Assert.assertEquals(1, model2.getProjectRuleSet().getRules().size()); |
| 149 | + // the model is still cached, but the rules are updated |
| 150 | + Assert.assertSame(model, model2); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void externallyChangedProjectPropertiesShouldBeReloaded() throws Exception { |
| 155 | + setUpProject("ProjectPropertiesChanged"); |
| 156 | + // configure the project to use PMD |
| 157 | + final UpdateProjectPropertiesCmd cmd = new UpdateProjectPropertiesCmd(); |
| 158 | + cmd.setPmdEnabled(true); |
| 159 | + cmd.setProject(this.testProject); |
| 160 | + cmd.setProjectWorkingSet(null); |
| 161 | + RuleSets rulesets = new RuleSets(PMDPlugin.getDefault().getPreferencesManager().getRuleSet()); |
| 162 | + cmd.setProjectRuleSets(rulesets); |
| 163 | + cmd.setRuleSetStoredInProject(false); |
| 164 | + cmd.execute(); |
| 165 | + |
| 166 | + if (cmd.isNeedRebuild()) { |
| 167 | + final BuildProjectCommand rebuildCmd = new BuildProjectCommand(); |
| 168 | + rebuildCmd.setProject(this.testProject); |
| 169 | + rebuildCmd.setUserInitiated(true); |
| 170 | + rebuildCmd.execute(); |
| 171 | + JobCommandProcessor.getInstance().waitCommandToFinish(null); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + // load the project settings - it should have this ruleset now active (many rules) |
| 176 | + final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager(); |
| 177 | + final IProjectProperties model = mgr.loadProjectProperties(this.testProject); |
| 178 | + RuleSet projectRuleSet = model.getProjectRuleSet(); |
| 179 | + int numberOfRules = rulesets.getAllRules().size(); |
| 180 | + Assert.assertEquals(numberOfRules, projectRuleSet.getRules().size()); |
| 181 | + // after the rebuild above, the project should be in a consistent state |
| 182 | + Assert.assertFalse(model.isNeedRebuild()); |
| 183 | + |
| 184 | + // now create a .pmd-ruleset.xml file without eclipse knowing about it ("externally") |
| 185 | + IFile ruleSetFile = this.testProject.getFile(PROJECT_RULESET_FILENAME); |
| 186 | + if (ruleSetFile.exists()) { |
| 187 | + Assert.fail("File " + PROJECT_RULESET_FILENAME + " already exists!"); |
| 188 | + } |
| 189 | + File ruleSetFileReal = ruleSetFile.getLocation().toFile(); |
| 190 | + ResourceUtil.copyResource(this, "ruleset1.xml", ruleSetFileReal); |
| 191 | + |
| 192 | + // now overwrite and change the .pmd project properties without eclipse knowing about it ("externally") |
| 193 | + IFile projectPropertiesFile = this.testProject.getFile(PMD_PROPERTIES_FILENAME); |
| 194 | + if (!projectPropertiesFile.exists()) { |
| 195 | + Assert.fail("File .pmd does not exist!"); |
| 196 | + } |
| 197 | + File projectPropertiesFileReal = projectPropertiesFile.getLocation().toFile(); |
| 198 | + ResourceUtil.copyResource(this, "pmd-properties", projectPropertiesFileReal); |
| 199 | + LOG.debug("Overwritten {}", projectPropertiesFile); |
| 200 | + |
| 201 | + // the model is not updated yet... |
| 202 | + Assert.assertEquals(numberOfRules, model.getProjectRuleSet().getRules().size()); |
| 203 | + // but it will be when requesting the project properties again |
| 204 | + final IProjectProperties model2 = mgr.loadProjectProperties(testProject); |
| 205 | + // the file and the ruleset have changed, this should be detected |
| 206 | + Assert.assertTrue(model2.isNeedRebuild()); |
| 207 | + // the new rule set should be active now |
| 208 | + Assert.assertEquals(1, model2.getProjectRuleSet().getRules().size()); |
| 209 | + // the model is still cached, but the rules are updated |
| 210 | + Assert.assertSame(model, model2); |
| 211 | + |
| 212 | + // rebuild again |
| 213 | + final BuildProjectCommand rebuildCmd = new BuildProjectCommand(); |
| 214 | + rebuildCmd.setProject(this.testProject); |
| 215 | + rebuildCmd.setUserInitiated(true); |
| 216 | + rebuildCmd.execute(); |
| 217 | + JobCommandProcessor.getInstance().waitCommandToFinish(null); |
| 218 | + // need rebuild flag should be reset now |
| 219 | + Assert.assertFalse(model.isNeedRebuild()); |
| 220 | + } |
| 221 | +} |
0 commit comments