|
27 | 27 | import net.sourceforge.pmd.eclipse.EclipseUtils; |
28 | 28 | import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
29 | 29 | import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants; |
| 30 | +import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences; |
30 | 31 | import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties; |
31 | 32 | import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil; |
32 | 33 |
|
@@ -174,4 +175,64 @@ public void testReviewCmdNullResource() { |
174 | 175 | cmd.setResourceDelta(null); |
175 | 176 | cmd.performExecute(); |
176 | 177 | } |
| 178 | + |
| 179 | + private static Rule findRuleByName(RuleSet ruleset, String ruleName, String language) { |
| 180 | + for (Rule rule : ruleset.getRules()) { |
| 181 | + if (rule.getName().equals(ruleName) && rule.getLanguage().getTerseName().equals(language)) { |
| 182 | + return rule; |
| 183 | + } |
| 184 | + } |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testReviewCmdBasicAllFilesDefault() throws Exception { |
| 190 | + // add a second file |
| 191 | + EclipseUtils.createTestSourceFile(testProject, "/src/Test.js", "function(arg) { notDeclaredVariable = 1; }"); |
| 192 | + testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 193 | + |
| 194 | + IProjectProperties projectProperties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject); |
| 195 | + RuleSet projectRuleSet = projectProperties.getProjectRuleSet(); |
| 196 | + Rule emptyCatchBlock = findRuleByName(projectRuleSet, "EmptyCatchBlock", "java"); |
| 197 | + projectRuleSet = RuleSetUtil.clearRules(projectRuleSet); |
| 198 | + projectRuleSet = RuleSetUtil.addRule(projectRuleSet, emptyCatchBlock); |
| 199 | + projectProperties.setProjectRuleSet(projectRuleSet); |
| 200 | + PMDPlugin.getDefault().getPropertiesManager().storeProjectProperties(projectProperties); |
| 201 | + |
| 202 | + final ReviewCodeCmd cmd = new ReviewCodeCmd(); |
| 203 | + cmd.addResource(this.testProject); |
| 204 | + cmd.performExecute(); |
| 205 | + cmd.join(); |
| 206 | + |
| 207 | + Assert.assertEquals(1, cmd.getFileCount()); // only one file analyzed |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testReviewCmdBasicAllFiles() throws Exception { |
| 212 | + try { |
| 213 | + // add a second file |
| 214 | + EclipseUtils.createTestSourceFile(testProject, "/src/Test.js", "function(arg) { notDeclaredVariable = 1; }"); |
| 215 | + testProject.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 216 | + |
| 217 | + PMDPlugin.getDefault().loadPreferences().setDetermineFiletypesAutomatically(false); |
| 218 | + |
| 219 | + IProjectProperties projectProperties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject); |
| 220 | + RuleSet projectRuleSet = projectProperties.getProjectRuleSet(); |
| 221 | + Rule emptyCatchBlock = findRuleByName(projectRuleSet, "EmptyCatchBlock", "java"); |
| 222 | + projectRuleSet = RuleSetUtil.clearRules(projectRuleSet); |
| 223 | + projectRuleSet = RuleSetUtil.addRule(projectRuleSet, emptyCatchBlock); |
| 224 | + projectProperties.setProjectRuleSet(projectRuleSet); |
| 225 | + PMDPlugin.getDefault().getPropertiesManager().storeProjectProperties(projectProperties); |
| 226 | + |
| 227 | + final ReviewCodeCmd cmd = new ReviewCodeCmd(); |
| 228 | + cmd.addResource(this.testProject); |
| 229 | + cmd.performExecute(); |
| 230 | + cmd.join(); |
| 231 | + |
| 232 | + Assert.assertEquals(2, cmd.getFileCount()); // both files analyzed, although unnecessary, since only |
| 233 | + // the one java rule is active and no javascript rules |
| 234 | + } finally { |
| 235 | + PMDPlugin.getDefault().loadPreferences().setDetermineFiletypesAutomatically(IPreferences.DETERMINE_FILETYPES_AUTOMATICALLY_DEFAULT); |
| 236 | + } |
| 237 | + } |
177 | 238 | } |
0 commit comments