From af1dc32cd166e016cf827006bd6adf4110cf41d1 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Tue, 29 Apr 2003 17:11:13 +0000 Subject: [PATCH] Test code refactorings git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1878 51baf565-9d33-0410-a72c-fc3788e3496d --- .../rules/StringInstantiationRuleTest.java | 37 +++++++-------- .../pmd/rules/StringToStringRuleTest.java | 38 ++++++---------- .../SwitchStmtsShouldHaveDefaultRuleTest.java | 31 +++++++------ .../rules/UnnecessaryConstructorRuleTest.java | 45 ++++++++----------- 4 files changed, 64 insertions(+), 87 deletions(-) diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/StringInstantiationRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/StringInstantiationRuleTest.java index 66403d24ec..45d0428e93 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/StringInstantiationRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/StringInstantiationRuleTest.java @@ -4,7 +4,23 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.rules.XPathRule; -public class StringInstantiationRuleTest extends RuleTst { +public class StringInstantiationRuleTest extends SimpleAggregatorTst { + + private Rule rule; + + public void setUp() { + rule = new XPathRule(); + rule.addProperty("xpath", "//AllocationExpression[Name/@Image='String'][count(.//Expression) < 2][not(ArrayDimsAndInits)]"); + } + + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(TEST1, "new 'new String's", 2, rule), + new TestDescriptor(TEST2, "new String array", 0, rule), + new TestDescriptor(TEST3, "using multiple parameter constructor", 0, rule), + new TestDescriptor(TEST4, "using 4 parameter constructor", 0, rule) + }); + } private static final String TEST1 = "public class StringInstantiation1 {" + CPD.EOL + @@ -34,23 +50,4 @@ public class StringInstantiationRuleTest extends RuleTst { "}"; - private Rule rule; - - public void setUp() { - rule = new XPathRule(); - rule.addProperty("xpath", "//AllocationExpression[Name/@Image='String'][count(.//Expression) < 2][not(ArrayDimsAndInits)]"); - } - - public void test1() throws Throwable { - runTestFromString(TEST1, 2, rule); - } - public void test2() throws Throwable { - runTestFromString(TEST2, 0, rule); - } - public void test3() throws Throwable { - runTestFromString(TEST3, 0, rule); - } - public void test4() throws Throwable { - runTestFromString(TEST4, 0, rule); - } } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/StringToStringRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/StringToStringRuleTest.java index adf79effea..79ea576a9c 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/StringToStringRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/StringToStringRuleTest.java @@ -3,9 +3,20 @@ package test.net.sourceforge.pmd.rules; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.rules.StringToStringRule; -public class StringToStringRuleTest extends RuleTst { +public class StringToStringRuleTest extends SimpleAggregatorTst { - private static final String TEST1 = + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(TEST1, "local var", 1, new StringToStringRule()), + new TestDescriptor(TEST2, "parameter", 1, new StringToStringRule()), + new TestDescriptor(TEST3, "field", 1, new StringToStringRule()), + new TestDescriptor(TEST4, "primitive", 0, new StringToStringRule()), + new TestDescriptor(TEST5, "multiple similar params", 0, new StringToStringRule()), + new TestDescriptor(TEST6, "string array", 1, new StringToStringRule()) + }); + } + + private static final String TEST1 = "public class StringToString1 {" + CPD.EOL + " private String baz() {" + CPD.EOL + " String bar = \"howdy\";" + CPD.EOL + @@ -51,27 +62,4 @@ public class StringToStringRuleTest extends RuleTst { " }" + CPD.EOL + "}"; - public void testLocalVar() throws Throwable { - runTestFromString(TEST1, 1, new StringToStringRule()); - } - - public void testParam() throws Throwable { - runTestFromString(TEST2, 1, new StringToStringRule()); - } - - public void testInstanceVar() throws Throwable { - runTestFromString(TEST3, 1, new StringToStringRule()); - } - - public void testPrimitiveType() throws Throwable { - runTestFromString(TEST4, 0, new StringToStringRule()); - } - - public void testMultipleSimilarParams() throws Throwable { - runTestFromString(TEST5, 0, new StringToStringRule()); - } - - public void testStringArray() throws Throwable { - runTestFromString(TEST6, 1, new StringToStringRule()); - } } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/SwitchStmtsShouldHaveDefaultRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/SwitchStmtsShouldHaveDefaultRuleTest.java index b0776790bf..9bc20cb7c6 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/SwitchStmtsShouldHaveDefaultRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/SwitchStmtsShouldHaveDefaultRuleTest.java @@ -4,7 +4,21 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.rules.XPathRule; -public class SwitchStmtsShouldHaveDefaultRuleTest extends RuleTst { +public class SwitchStmtsShouldHaveDefaultRuleTest extends SimpleAggregatorTst { + + private Rule rule; + + public void setUp() { + rule = new XPathRule(); + rule.addProperty("xpath", "//SwitchStatement[not(SwitchLabel[count(*) = 0])]"); + } + + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(TEST1, "simple failure case", 1, rule), + new TestDescriptor(TEST2, "simple ok case", 0, rule) + }); + } private static final String TEST1 = "public class SwitchStmtsShouldHaveDefault1 {" + CPD.EOL + @@ -28,19 +42,4 @@ public class SwitchStmtsShouldHaveDefaultRuleTest extends RuleTst { "}"; - private Rule rule; - - public void setUp() { - rule = new XPathRule(); - rule.addProperty("xpath", "//SwitchStatement[not(SwitchLabel[count(*) = 0])]"); - } - - public void test1() throws Throwable { - runTestFromString(TEST1, 1, rule); - } - - public void test2() throws Throwable { - runTestFromString(TEST2, 0, rule); - } - } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/UnnecessaryConstructorRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/UnnecessaryConstructorRuleTest.java index 3c47490201..cc0c3c0c75 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/UnnecessaryConstructorRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/UnnecessaryConstructorRuleTest.java @@ -4,7 +4,25 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.rules.XPathRule; -public class UnnecessaryConstructorRuleTest extends RuleTst { +public class UnnecessaryConstructorRuleTest extends SimpleAggregatorTst { + + private Rule rule; + + public void setUp() { + rule = new XPathRule(); + rule.addProperty("xpath", "//ConstructorDeclaration[1][count(//ConstructorDeclaration)=1][@Public='true'][not(FormalParameters/*)][not(BlockStatement)][not(NameList)]"); + } + + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(TEST1, "simple failure case", 1, rule), + new TestDescriptor(TEST2, "private constructor", 0, rule), + new TestDescriptor(TEST3, "constructor with arguments", 0, rule), + new TestDescriptor(TEST4, "constructor with contents", 0, rule), + new TestDescriptor(TEST5, "constructor throws exception", 0, rule), + new TestDescriptor(TEST6, "two constructors", 0, rule) + }); + } private static final String TEST1 = "public class UnnecessaryConstructor1 {" + CPD.EOL + @@ -42,29 +60,4 @@ public class UnnecessaryConstructorRuleTest extends RuleTst { "}"; - private Rule rule; - - public void setUp() { - rule = new XPathRule(); - rule.addProperty("xpath", "//ConstructorDeclaration[1][count(//ConstructorDeclaration)=1][@Public='true'][not(FormalParameters/*)][not(BlockStatement)][not(NameList)]"); - } - - public void testSimpleFailureCase() throws Throwable { - runTestFromString(TEST1, 1, rule); - } - public void testPrivate() throws Throwable { - runTestFromString(TEST2, 0, rule); - } - public void testHasArgs() throws Throwable { - runTestFromString(TEST3, 0, rule); - } - public void testHasBody() throws Throwable { - runTestFromString(TEST4, 0, rule); - } - public void testHasExceptions() throws Throwable { - runTestFromString(TEST5, 0, rule); - } - public void testMultipleConstructors() throws Throwable { - runTestFromString(TEST6, 0, rule); - } }