1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package test.net.sourceforge.pmd.rules.strings;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Rule;
8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
10  
11  public class AvoidConcatenatingNonLiteralsInStringBufferTest extends SimpleAggregatorTst {
12  
13      private Rule rule;
14  
15      public void setUp() throws Exception {
16          rule = findRule("strings", "AvoidConcatenatingNonLiteralsInStringBuffer");
17      }
18  
19      public void testAll() {
20         runTests(new TestDescriptor[] {
21                 new TestDescriptor(TEST1, "concatenating a literal to a method return value", 1, rule),
22                 new TestDescriptor(TEST2, "same as TEST1, but in SB constructor", 1, rule),
23                 new TestDescriptor(TEST3, "chained appends", 0, rule),
24                 new TestDescriptor(TEST4, "concatenating two literals in SB constructor", 0, rule),
25                 new TestDescriptor(TEST5, "concatenating two literals post-construction", 0, rule),
26                 new TestDescriptor(TEST6, "case where concatenation is not a child of a BlockStatement, but instead is a child of an ExplicitConstructorInvocation", 0, rule),
27                 new TestDescriptor(TEST7, "don't error out on array instantiation", 0, rule),
28                 new TestDescriptor(TEST8, "usage of the StringBuffer constructor that takes an int", 0, rule),
29                 new TestDescriptor(TEST9, "nested", 0, rule),
30                 new TestDescriptor(TEST10, "looking up too high", 0, rule),
31         });
32      }
33  
34     private static final String TEST1 =
35      "public class Foo {" + PMD.EOL +
36      " private void baz() {" + PMD.EOL +
37      "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
38      "  sb.append(\"hello\"+ world()); "+
39      " }" + PMD.EOL +
40      "}";
41  
42     private static final String TEST2 =
43         "public class Foo {" + PMD.EOL +
44         " private void baz() {" + PMD.EOL +
45         "  StringBuffer sb = new StringBuffer(\"hello\"+ world());" + PMD.EOL +
46         " }" + PMD.EOL +
47         "}";
48  
49     private static final String TEST3 =
50         "public class Foo {" + PMD.EOL +
51         " private void baz() {" + PMD.EOL +
52         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
53         "  sb.append(\"hello\").append(world()); "+
54         " }" + PMD.EOL +
55         "}";
56  
57     private static final String TEST4 =
58         "public class Foo {" + PMD.EOL +
59         " private void baz() {" + PMD.EOL +
60         "  StringBuffer sb = new StringBuffer(\"hello\"+ \"world\");" + PMD.EOL +
61         " }" + PMD.EOL +
62         "}";
63     
64     private static final String TEST5 =
65         "public class Foo {" + PMD.EOL +
66         " private void baz() {" + PMD.EOL +
67         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
68         "  sb.append(\"hello\"+\"world\"); "+
69         " }" + PMD.EOL +
70         "}";
71  
72     private static final String TEST6 =
73         "public class Foo {" + PMD.EOL +
74         " public Foo() {" + PMD.EOL +
75         "  super(\"CauseMsg:\" + ex.getMessage(), ex); " + PMD.EOL +
76         " }" + PMD.EOL +
77         "}";
78  
79      private static final String TEST7 =
80          "public class Foo {" + PMD.EOL +
81          " public void bar() {" + PMD.EOL +
82          "  int t[] = new int[x+y+1];" + PMD.EOL +
83          " }" + PMD.EOL +
84          "}";
85  
86  
87     private static final String TEST8 =
88         "public class Foo {" + PMD.EOL +
89         " public int foor() {return 2;}" + PMD.EOL +
90         " public void bar(int x) {" + PMD.EOL +
91         "  StringBuffer buf = new StringBuffer(1 + foo());" + PMD.EOL +
92         " }" + PMD.EOL +
93         "}";
94  
95     private static final String TEST9 =
96         "public class Foo {" + PMD.EOL +
97         " public void bar(int x) {" + PMD.EOL +
98         "  StringBuffer buf = new StringBuffer(x);" + PMD.EOL +
99         " }" + PMD.EOL +
100        "}";
101 
102    private static final String TEST10 =
103        "public class Foo {" + PMD.EOL +
104        " public void bar() {" + PMD.EOL +
105        "  if (foo) {" + PMD.EOL +
106        "   StringBuffer buf = new StringBuffer();" + PMD.EOL +
107        "   buf.append(\"hello\");" + PMD.EOL +
108        "   Object x = a(\"world\" + x, buf.toString());" + PMD.EOL +
109        "  }" + PMD.EOL +
110        " }" + PMD.EOL +
111        "}";
112 
113 }