    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 InefficientStringBufferingTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("strings", "InefficientStringBuffering");
    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                 new TestDescriptor(TEST11, "looking too deep", 0, rule),
    32                 new TestDescriptor(TEST12, "concatenating two non-literals", 1, rule),
    33                 new TestDescriptor(TEST13, "concatenating method + int", 0, rule),
    34                 new TestDescriptor(TEST14, "JTextArea.append", 0, rule),
    35                 new TestDescriptor(TEST15, "don't get thrown off by a buried literal", 1, rule),
    36                 new TestDescriptor(TEST16, "sb.delete shouldn't trigger it", 0, rule),
    37                 new TestDescriptor(TEST17, "skip additions involving static finals, compiler will do constant folding for these", 0, rule),
    38                 new TestDescriptor(TEST18, "for statement without braces", 1, rule),
    39                 new TestDescriptor(TEST19, "if statement without braces", 1, rule),
    40                 new TestDescriptor(TEST20, "3 args version of StringBuffer.append", 0, rule),
    41                 new TestDescriptor(TEST21, "compile-time concats are ok", 0, rule),
    42                 new TestDescriptor(TEST22, "compile-time concats are ok, v2", 0, rule),
    43         });
    44      }
    45
    46     private static final String TEST1 =
    47      "public class Foo {" + PMD.EOL +
    48      " private void baz() {" + PMD.EOL +
    49      "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    50      "  sb.append(\"hello\"+ world()); "+
    51      " }" + PMD.EOL +
    52      "}";
    53
    54     private static final String TEST2 =
    55         "public class Foo {" + PMD.EOL +
    56         " private void baz() {" + PMD.EOL +
    57         "  StringBuffer sb = new StringBuffer(\"hello\"+ world());" + PMD.EOL +
    58         " }" + PMD.EOL +
    59         "}";
    60
    61     private static final String TEST3 =
    62         "public class Foo {" + PMD.EOL +
    63         " private void baz() {" + PMD.EOL +
    64         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    65         "  sb.append(\"hello\").append(world()); "+
    66         " }" + PMD.EOL +
    67         "}";
    68
    69     private static final String TEST4 =
    70         "public class Foo {" + PMD.EOL +
    71         " private void baz() {" + PMD.EOL +
    72         "  StringBuffer sb = new StringBuffer(\"hello\"+ \"world\");" + PMD.EOL +
    73         " }" + PMD.EOL +
    74         "}";
    75
    76     private static final String TEST5 =
    77         "public class Foo {" + PMD.EOL +
    78         " private void baz() {" + PMD.EOL +
    79         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    80         "  sb.append(\"hello\"+\"world\"); "+
    81         " }" + PMD.EOL +
    82         "}";
    83
    84     private static final String TEST6 =
    85         "public class Foo {" + PMD.EOL +
    86         " public Foo() {" + PMD.EOL +
    87         "  super(\"CauseMsg:\" + ex.getMessage(), ex); " + PMD.EOL +
    88         " }" + PMD.EOL +
    89         "}";
    90
    91      private static final String TEST7 =
    92          "public class Foo {" + PMD.EOL +
    93          " public void bar() {" + PMD.EOL +
    94          "  int t[] = new int[x+y+1];" + PMD.EOL +
    95          " }" + PMD.EOL +
    96          "}";
    97
    98
    99     private static final String TEST8 =
    100        "public class Foo {" + PMD.EOL +
    101        " public int foor() {return 2;}" + PMD.EOL +
    102        " public void bar(int x) {" + PMD.EOL +
    103        "  StringBuffer buf = new StringBuffer(1 + foo());" + PMD.EOL +
    104        " }" + PMD.EOL +
    105        "}";
    106
    107    private static final String TEST9 =
    108        "public class Foo {" + PMD.EOL +
    109        " public void bar(int x) {" + PMD.EOL +
    110        "  StringBuffer buf = new StringBuffer(x);" + PMD.EOL +
    111        " }" + PMD.EOL +
    112        "}";
    113
    114    private static final String TEST10 =
    115        "public class Foo {" + PMD.EOL +
    116        " public void bar() {" + PMD.EOL +
    117        "  if (foo) {" + PMD.EOL +
    118        "   StringBuffer buf = new StringBuffer();" + PMD.EOL +
    119        "   buf.append(\"hello\");" + PMD.EOL +
    120        "   Object x = a(\"world\" + x, buf.toString());" + PMD.EOL +
    121        "  }" + PMD.EOL +
    122        " }" + PMD.EOL +
    123        "}";
    124
    125    private static final String TEST11 =
    126        "public class Foo {" + PMD.EOL +
    127        " public void bar(int i) {" + PMD.EOL +
    128        "  StringBuffer buf = new StringBuffer();" + PMD.EOL +
    129        "  buf.append(getFoo(getBar(i + \"hi\")));" + PMD.EOL +
    130        " }" + PMD.EOL +
    131        "}";
    132
    133    private static final String TEST12 =
    134        "public class Foo {" + PMD.EOL +
    135        " public void bar(String a, String b) {" + PMD.EOL +
    136        "  StringBuffer buf = new StringBuffer();" + PMD.EOL +
    137        "  buf.append(a + b);" + PMD.EOL +
    138        " }" + PMD.EOL +
    139        "}";
    140
    141    private static final String TEST13 =
    142        "public class Foo {" + PMD.EOL +
    143        " public void bar(Date a) {" + PMD.EOL +
    144        "  StringBuffer buf = new StringBuffer();" + PMD.EOL +
    145        "  buf.append(a.getYear() + 1900);" + PMD.EOL +
    146        " }" + PMD.EOL +
    147        "}";
    148
    149    private static final String TEST14 =
    150        "public class Foo {" + PMD.EOL +
    151        " public void bar(JTextArea jta) {" + PMD.EOL +
    152        "  jta.append(f + \"hi\");" + PMD.EOL +
    153        " }" + PMD.EOL +
    154        "}";
    155
    156     private static final String TEST15 =
    157         "public class Foo {" + PMD.EOL +
    158         " private void baz() {" + PMD.EOL +
    159         "  StringBuffer sb = new StringBuffer(\"hello\"+ System.getProperty(\"blah\"));" + PMD.EOL +
    160         " }" + PMD.EOL +
    161         "}";
    162
    163     private static final String TEST16 =
    164         "public class Foo {" + PMD.EOL +
    165         " public void bar(StringBuffer sb) {" + PMD.EOL +
    166         "  sb.delete(x, y+z);" + PMD.EOL +
    167         " }" + PMD.EOL +
    168         "}";
    169
    170     private static final String TEST17 =
    171         "public class Foo {" + PMD.EOL +
    172         " public static final String FOO = \"bar\";" + PMD.EOL +
    173         " public void bar(StringBuffer sb) {" + PMD.EOL +
    174         "  sb.append(\"foo\" + FOO);" + PMD.EOL +
    175         " }" + PMD.EOL +
    176         "}";
    177
    178     private static final String TEST18 =
    179         "public class Foo {" + PMD.EOL +
    180         " private void baz() {" + PMD.EOL +
    181         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    182         "  for(int ix = 0; ix < 100; ix++) "+
    183         "      sb.append(\"hello\"+ world()); "+
    184         " }" + PMD.EOL +
    185         "}";
    186
    187     private static final String TEST19 =
    188         "public class Foo {" + PMD.EOL +
    189         " private void baz() {" + PMD.EOL +
    190         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    191         "  if(true) "+
    192         "      sb.append(\"hello\"+ world()); "+
    193         " }" + PMD.EOL +
    194         "}";
    195
    196     private static final String TEST20 =
    197         "public class Foo {" + PMD.EOL +
    198         " private void baz(StringBuffer s, char[] chars, int start, int end) {" + PMD.EOL +
    199         "  s.append(chars, start, start - end);" + PMD.EOL +
    200         " }" + PMD.EOL +
    201         "}";
    202
    203
    204     private static final String TEST21 =
    205             "public class Foo {" + PMD.EOL +
    206             " private void baz() {" + PMD.EOL +
    207             "StringBuffer buffer = new StringBuffer(" + PMD.EOL +
    208             "\"a\" + \"b\" + \"c\");" + PMD.EOL +
    209             "} }";
    210
    211     private static final String TEST22 =
    212             "public class Foo {" + PMD.EOL +
    213             "static final String BAR = \"foo\";" + PMD.EOL +
    214             " private void baz() {" + PMD.EOL +
    215             "StringBuffer buffer = new StringBuffer(" + PMD.EOL +
    216             "\"a\" + BAR + \"b\" + BAR);" + PMD.EOL +
    217             "} }";
    218 }