View Javadoc
1 package test.net.sourceforge.pmd.rules; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.Rule; 5 import net.sourceforge.pmd.rules.XPathRule; 6 7 public class StringInstantiationRuleTest extends SimpleAggregatorTst { 8 9 private Rule rule; 10 11 public void setUp() { 12 rule = new XPathRule(); 13 rule.addProperty("xpath", "//AllocationExpression[Name/@Image='String'][count(.//Expression) < 2][not(ArrayDimsAndInits)]"); 14 } 15 16 public void testAll() { 17 runTests(new TestDescriptor[] { 18 new TestDescriptor(TEST1, "new 'new String's", 2, rule), 19 new TestDescriptor(TEST2, "new String array", 0, rule), 20 new TestDescriptor(TEST3, "using multiple parameter constructor", 0, rule), 21 new TestDescriptor(TEST4, "using 4 parameter constructor", 0, rule) 22 }); 23 } 24 25 private static final String TEST1 = 26 "public class StringInstantiation1 {" + PMD.EOL + 27 " private String bar = new String(\"bar\");" + PMD.EOL + 28 " private String baz = new String();" + PMD.EOL + 29 "}"; 30 31 private static final String TEST2 = 32 "public class StringInstantiation2 {" + PMD.EOL + 33 " private String[] bar = new String[5];" + PMD.EOL + 34 "}"; 35 36 private static final String TEST3 = 37 "public class StringInstantiation3 {" + PMD.EOL + 38 " public void foo() {" + PMD.EOL + 39 " byte[] bytes = new byte[50];" + PMD.EOL + 40 " String bar = new String(bytes, 0, bytes.length);" + PMD.EOL + 41 " }" + PMD.EOL + 42 "}"; 43 44 private static final String TEST4 = 45 "public class StringInstantiation4 {" + PMD.EOL + 46 " public void foo() {" + PMD.EOL + 47 " byte[] bytes = new byte[50];" + PMD.EOL + 48 " String bar = new String(bytes, 0, bytes.length, \"some-encoding\");" + PMD.EOL + 49 " }" + PMD.EOL + 50 "}"; 51 52 53 }

This page was automatically generated by Maven