getRuleByName return null instead of throwing RuntimeException when the rule is not found

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3556 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2005-06-10 17:48:45 +00:00
parent d009e9be61
commit 9aa113572b
2 changed files with 13 additions and 5 deletions

View File

@ -72,6 +72,13 @@ public class RuleSetTest extends TestCase {
assertEquals("unable to fetch rule by name", mock, rs.getRuleByName("name"));
}
public void testGetRuleByName2() {
RuleSet rs = new RuleSet();
MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
rs.addRule(mock);
assertNull("the rule FooRule must not be found!", rs.getRuleByName("FooRule"));
}
public void testRuleList() {
RuleSet IUT = new RuleSet();

View File

@ -59,17 +59,18 @@ public class RuleSet {
* Returns the Rule with the given name
*
* @param ruleName the name of the rule to find
* @return
* @throws RuntimeException when the rule with the given name cannot be found
* @return the rule or null if not found
*/
public Rule getRuleByName(String ruleName) {
for (Iterator i = rules.iterator(); i.hasNext();) {
Rule rule = null;
for (Iterator i = rules.iterator(); i.hasNext() && (rule == null);) {
Rule r = (Rule) i.next();
if (r.getName().equals(ruleName)) {
return r;
rule = r;
}
}
throw new RuntimeException("Couldn't find rule named " + ruleName + " in the ruleset " + name);
return rule;
}
/**