1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package test.net.sourceforge.pmd.rules; 5 6 import net.sourceforge.pmd.PMD; 7 import net.sourceforge.pmd.Rule; 8 import net.sourceforge.pmd.RuleSetNotFoundException; 9 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 10 import test.net.sourceforge.pmd.testframework.TestDescriptor; 11 12 public class StringInstantiationRuleTest extends SimpleAggregatorTst { 13 14 private Rule rule; 15 16 public void setUp() throws RuleSetNotFoundException { 17 rule = findRule("rulesets/strings.xml", "StringInstantiation"); 18 } 19 20 public void testAll() { 21 runTests(new TestDescriptor[] { 22 new TestDescriptor(TEST1, "new 'new String's", 2, rule), 23 new TestDescriptor(TEST2, "new String array", 0, rule), 24 new TestDescriptor(TEST3, "using multiple parameter constructor", 0, rule), 25 new TestDescriptor(TEST4, "using 4 parameter constructor", 0, rule) 26 }); 27 } 28 29 private static final String TEST1 = 30 "public class Foo {" + PMD.EOL + 31 " private String bar = new String(\"bar\");" + PMD.EOL + 32 " private String baz = new String();" + PMD.EOL + 33 "}"; 34 35 private static final String TEST2 = 36 "public class Foo {" + PMD.EOL + 37 " private String[] bar = new String[5];" + PMD.EOL + 38 "}"; 39 40 private static final String TEST3 = 41 "public class Foo {" + PMD.EOL + 42 " void foo() {" + PMD.EOL + 43 " byte[] bytes = new byte[50];" + PMD.EOL + 44 " String bar = new String(bytes, 0, bytes.length);" + PMD.EOL + 45 " }" + PMD.EOL + 46 "}"; 47 48 private static final String TEST4 = 49 "public class Foo {" + PMD.EOL + 50 " void foo() {" + PMD.EOL + 51 " byte[] bytes = new byte[50];" + PMD.EOL + 52 " String bar = new String(bytes, 0, bytes.length, \"some-encoding\");" + PMD.EOL + 53 " }" + PMD.EOL + 54 "}"; 55 56 57 }