Skip to content

Commit 8a8e539

Browse files
committed
Add unit test
1 parent 1212fb7 commit 8a8e539

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCmdTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sourceforge.pmd.eclipse.EclipseUtils;
2828
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
2929
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
30+
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences;
3031
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
3132
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
3233

@@ -174,4 +175,64 @@ public void testReviewCmdNullResource() {
174175
cmd.setResourceDelta(null);
175176
cmd.performExecute();
176177
}
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+
}
177238
}

net.sourceforge.pmd.eclipse.plugin.test/src/main/resources/test.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Test {
99

1010
public void foo() {
1111
try {
12+
// EmptyCatchBlock!!
1213
} catch (Exception e) {
1314

1415
}

0 commit comments

Comments
 (0)