From 9aa113572b21214d9c9e2b72336ba9aa90bd8143 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Fri, 10 Jun 2005 17:48:45 +0000 Subject: [PATCH] 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 --- pmd/regress/test/net/sourceforge/pmd/RuleSetTest.java | 7 +++++++ pmd/src/net/sourceforge/pmd/RuleSet.java | 11 ++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/RuleSetTest.java b/pmd/regress/test/net/sourceforge/pmd/RuleSetTest.java index 42791100dd..d0815e341f 100644 --- a/pmd/regress/test/net/sourceforge/pmd/RuleSetTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/RuleSetTest.java @@ -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(); diff --git a/pmd/src/net/sourceforge/pmd/RuleSet.java b/pmd/src/net/sourceforge/pmd/RuleSet.java index 408558fe60..5f5366b50d 100644 --- a/pmd/src/net/sourceforge/pmd/RuleSet.java +++ b/pmd/src/net/sourceforge/pmd/RuleSet.java @@ -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; } /**