    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
    12  public class ConsecutiveLiteralAppendsTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws Exception {
    17          rule = findRule("strings", "ConsecutiveLiteralAppends");
    18      }
    19
    20      public void testAll() {
    21
    22          // first run the legal tests
    23          runTests(new TestDescriptor[]{
    24
    25              new TestDescriptor(TEST1, "1, Single append, should be ok", 0, rule),
    26              new TestDescriptor(TEST3, "3, Appends broken up by variable", 0, rule),
    27              new TestDescriptor(TEST7, "7, Appends, then a variable", 0, rule),
    28              new TestDescriptor(TEST8, "8, Appends, then a while", 0, rule),
    29              new TestDescriptor(TEST12, "12, Two loops, not concurrent appends though", 0, rule),
    30              new TestDescriptor(TEST13, "13, A bunch of loops, but nothing concurrent", 0, rule),
    31              new TestDescriptor(TEST15, "15, A bunch of loops, none concurrent, separated by else", 0, rule),
    32              new TestDescriptor(TEST16, "16, Additive Expression 1", 0, rule),
    33              new TestDescriptor(TEST17, "17, Additive Expression 2", 0, rule),
    34              new TestDescriptor(TEST18, "18, End with literal append", 0, rule),
    35              new TestDescriptor(TEST19, "19, A bunch of appends", 0, rule),
    36              new TestDescriptor(TEST21, "21, Appends separated by an if", 0, rule),
    37              new TestDescriptor(TEST22, "22, calls to methods in append", 0, rule),
    38              new TestDescriptor(TEST24, "24, Appends from within switch statement", 0, rule),
    39              new TestDescriptor(TEST25, "25, Appends from within several different ifs", 0, rule),
    40              new TestDescriptor(TEST26, "26, One append in if, one in else", 0, rule),
    41              new TestDescriptor(TEST28, "28, Additive inside an if statement", 0, rule),
    42              new TestDescriptor(TEST29, "29, Adding two strings only", 0, rule),
    43              new TestDescriptor(TEST30, "30, Method call in append", 0, rule),
    44              new TestDescriptor(TEST34, "34, Additive in the constructor", 0, rule),
    45              new TestDescriptor(TEST35, "35, For block without braces", 0, rule),
    46              new TestDescriptor(TEST36, "36, Appends broken up by method call", 0, rule),
    47              new TestDescriptor(TEST39, "39, Buffer as class variable, accessed in 2 methods", 0, rule),
    48              new TestDescriptor(TEST40, "40, Call to external method with subtraction", 0, rule),
    49              new TestDescriptor(TEST41, "41, Call to external method with addition, not string appending", 0, rule),
    50          });
    51
    52          // Then run the failure tests
    53          runTests(new TestDescriptor[]{
    54              new TestDescriptor(TEST2_FAIL, "2, Back to back append, not ok", 1, rule),
    55              new TestDescriptor(TEST4_FAIL, "4, Appends with literal appends", 1, rule),
    56              new TestDescriptor(TEST5_FAIL, "5, Appends broken up by while loop", 1, rule),
    57              new TestDescriptor(TEST6_FAIL, "6, Appends, then a variable", 1, rule),
    58              new TestDescriptor(TEST9_FAIL, "9, Multiple appends in same while", 1, rule),
    59              new TestDescriptor(TEST10_FAIL, "10, Multiple appends in same while, with multiple outside that while", 2, rule),
    60              new TestDescriptor(TEST11_FAIL, "11, Multiple appends in same while, none outside the loop", 1, rule),
    61              new TestDescriptor(TEST14_FAIL, "14, A bunch of loops, one concurrent", 1, rule),
    62              new TestDescriptor(TEST20_FAIL, "20, Suffix append follwed by real append", 1, rule),
    63              new TestDescriptor(TEST23_FAIL, "23, force 2 failures on 3 lines", 2, rule),
    64              new TestDescriptor(TEST27_FAIL, "27, Concurrent Appends from within switch statement", 2, rule),
    65              new TestDescriptor(TEST31_FAIL, "31, Adding two strings together then another append", 1, rule),
    66              new TestDescriptor(TEST32_FAIL, "32, Including the constructor's string", 1, rule),
    67              new TestDescriptor(TEST33_FAIL, "33, Additive in the constructor", 1, rule),
    68              new TestDescriptor(TEST37_FAIL, "37, Intervening method call not related to append", 1, rule),
    69              new TestDescriptor(TEST38_FAIL, "38, Intervening method call not related to append", 1, rule),
    70          });
    71
    72          // test threshold
    73          rule.addProperty("threshold", "2");
    74          runTests(new TestDescriptor[]{
    75              new TestDescriptor(TEST2_FAIL, "2, re-running with threshold", 0, rule),
    76              new TestDescriptor(TEST23_FAIL, "23, re-running with threshold", 1, rule),
    77
    78          });
    79      }
    80
    81      private static final String TEST1 =
    82              "public class Foo {" + PMD.EOL +
    83              "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    84              "   public void bar() {" + PMD.EOL +
    85              "       StringBuffer sb = new StringBuffer(15);" + PMD.EOL +
    86              "       sb.append(\"foo\");" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              "}";
    89
    90      private static final String TEST2_FAIL =
    91              "public class Foo {" + PMD.EOL +
    92              " public void bar() {" + PMD.EOL +
    93              "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    94              "  sb.append(\"Hello\");" + PMD.EOL +
    95              "  sb.append(\"World\");" + PMD.EOL +
    96              " }" + PMD.EOL +
    97              "}";
    98
    99      private static final String TEST3 =
    100             "public class Foo {" + PMD.EOL +
    101             " public void bar() {" + PMD.EOL +
    102             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    103             "  String foo = \"Hello\";" + PMD.EOL +
    104             "  sb.append(\"Hello\");" + PMD.EOL +
    105             "  sb.append(foo);" + PMD.EOL +
    106             "  sb.append(\"World\");" + PMD.EOL +
    107             " }" + PMD.EOL +
    108             "}";
    109
    110
    111     private static final String TEST4_FAIL =
    112             "public class Foo {" + PMD.EOL +
    113             " public void bar() {" + PMD.EOL +
    114             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    115             "  sb.append(\"Hello\").append(\"World\");" + PMD.EOL +
    116             " }" + PMD.EOL +
    117             "}";
    118
    119     private static final String TEST5_FAIL =
    120             "public class Foo {" + PMD.EOL +
    121             " public void bar() {" + PMD.EOL +
    122             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    123             "  sb.append(\"Hello\");" + PMD.EOL +
    124             "  while(true){" + PMD.EOL +
    125             "  }" + PMD.EOL +
    126             "  sb.append(\"World\");" + PMD.EOL +
    127             " }" + PMD.EOL +
    128             "}";
    129
    130     private static final String TEST6_FAIL =
    131             "public class Foo {" + PMD.EOL +
    132             " public void bar() {" + PMD.EOL +
    133             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    134             "  String foo = \"Hello\";" + PMD.EOL +
    135             "  sb.append(\"Hello\");" + PMD.EOL +
    136             "  sb.append(\"World\");" + PMD.EOL +
    137             "  sb.append(foo);" + PMD.EOL +
    138             "  sb.append(\"World\");" + PMD.EOL +
    139             " }" + PMD.EOL +
    140             "}";
    141
    142     private static final String TEST7 =
    143             "public class Foo {" + PMD.EOL +
    144             " public void bar() {" + PMD.EOL +
    145             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    146             "  sb.append(\"Hello\");" + PMD.EOL +
    147             "  while(true){" + PMD.EOL +
    148             "   sb.append(foo);" + PMD.EOL +
    149             "  }" + PMD.EOL +
    150             "  sb.append(\"World\");" + PMD.EOL +
    151             " }" + PMD.EOL +
    152             "}";
    153
    154
    155     private static final String TEST8 =
    156             "public class Foo {" + PMD.EOL +
    157             " public void bar() {" + PMD.EOL +
    158             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    159             "  sb.append(\"Hello\");" + PMD.EOL +
    160             "  while(true){" + PMD.EOL +
    161             "       sb.append(\"World\");" + PMD.EOL +
    162             "  }" + PMD.EOL +
    163             "  sb.append(\"World\");" + PMD.EOL +
    164             " }" + PMD.EOL +
    165             "}";
    166
    167
    168     private static final String TEST9_FAIL =
    169             "public class Foo {" + PMD.EOL +
    170             " public void bar() {" + PMD.EOL +
    171             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    172             "  sb.append(\"Hello\");" + PMD.EOL +
    173             "  while(true){" + PMD.EOL +
    174             "       sb.append(\"World\");" + PMD.EOL +
    175             "       sb.append(\"World\");" + PMD.EOL +
    176             "  }" + PMD.EOL +
    177             "  sb.append(\"World\");" + PMD.EOL +
    178             " }" + PMD.EOL +
    179             "}";
    180
    181
    182     private static final String TEST10_FAIL =
    183             "public class Foo {" + PMD.EOL +
    184             " public void bar() {" + PMD.EOL +
    185             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    186             "  sb.append(\"Hello\");" + PMD.EOL +
    187             "  sb.append(\"Hello\");" + PMD.EOL +
    188             "  while(true){" + PMD.EOL +
    189             "       sb.append(\"World\");" + PMD.EOL +
    190             "       sb.append(\"World\");" + PMD.EOL +
    191             "  }" + PMD.EOL +
    192             "  sb.append(\"World\");" + PMD.EOL +
    193             " }" + PMD.EOL +
    194             "}";
    195
    196     private static final String TEST11_FAIL =
    197             "public class Foo {" + PMD.EOL +
    198             " public void bar() {" + PMD.EOL +
    199             "  while(true){" + PMD.EOL +
    200             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    201             "       sb.append(\"World\");" + PMD.EOL +
    202             "       sb.append(\"World\");" + PMD.EOL +
    203             "  }" + PMD.EOL +
    204             " }" + PMD.EOL +
    205             "}";
    206
    207     private static final String TEST12 =
    208             "public class Foo {" + PMD.EOL +
    209             " public void bar() {" + PMD.EOL +
    210             "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    211             "  while(true){" + PMD.EOL +
    212             "       sb.append(\"World\");" + PMD.EOL +
    213             "  }" + PMD.EOL +
    214             "       sb.append(\"World\");" + PMD.EOL +
    215             "  for(int ix = 0; ix < 2; ix++){" + PMD.EOL +
    216             "       sb.append(\"World\");" + PMD.EOL +
    217             " }" + PMD.EOL +
    218             "       sb.append(\"World\");" + PMD.EOL +
    219             " }" + PMD.EOL +
    220             "}";
    221
    222     private static final String TEST13 =
    223             "public class Foo {" + PMD.EOL +
    224             "   public void bar(List l) {" + PMD.EOL +
    225             "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    226             "       sb.append(\"Hello cruel world\");" + PMD.EOL +
    227             "       Iterator iter = l.iterator();" + PMD.EOL +
    228             "       while (iter.hasNext()) {" + PMD.EOL +
    229             "           List innerList = (List) iter.next();" + PMD.EOL +
    230             "           sb.append(\",\");" + PMD.EOL +
    231             "           for (Iterator ixtor = innerList.iterator(); ixtor.hasNext();) {" + PMD.EOL +
    232             "               Integer integer = (Integer) ixtor.next();" + PMD.EOL +
    233             "               sb.append(\"\");" + PMD.EOL +
    234             "               if (ixtor.hasNext()) {" + PMD.EOL +
    235             "                   sb.append(\",\");" + PMD.EOL +
    236             "               }" + PMD.EOL +
    237             "           }" + PMD.EOL +
    238             "           sb.append(\"foo\");" + PMD.EOL +
    239             "       }" + PMD.EOL +
    240             "   }" + PMD.EOL +
    241             "}";
    242
    243     private static final String TEST14_FAIL =
    244             "public class Foo {" + PMD.EOL +
    245             "   public void bar(List l) {" + PMD.EOL +
    246             "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    247             "       sb.append(\"Hello cruel world\");" + PMD.EOL +
    248             "       Iterator iter = l.iterator();" + PMD.EOL +
    249             "       while (iter.hasNext()) {" + PMD.EOL +
    250             "           List innerList = (List) iter.next();" + PMD.EOL +
    251             "           sb.append(\",\");" + PMD.EOL +
    252             "           for (Iterator ixtor = innerList.iterator(); ixtor.hasNext();) {" + PMD.EOL +
    253             "               Integer integer = (Integer) ixtor.next();" + PMD.EOL +
    254             "               sb.append(\"\");" + PMD.EOL +
    255             "               if (ixtor.hasNext()) {" + PMD.EOL +
    256             "                   sb.append(\",\");" + PMD.EOL +
    257             "                   sb.append(\",\");" + PMD.EOL +
    258             "               }" + PMD.EOL +
    259             "           }" + PMD.EOL +
    260             "           sb.append(\"foo\");" + PMD.EOL +
    261             "       }" + PMD.EOL +
    262             "   }" + PMD.EOL +
    263             "}";
    264
    265     private static final String TEST15 =
    266             "public class Foo {" + PMD.EOL +
    267             "   public void bar(List l) {" + PMD.EOL +
    268             "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    269             "       sb.append(\"Hello cruel world\");" + PMD.EOL +
    270             "       Iterator iter = l.iterator();" + PMD.EOL +
    271             "       while (iter.hasNext()) {" + PMD.EOL +
    272             "           List innerList = (List) iter.next();" + PMD.EOL +
    273             "           sb.append(\",\");" + PMD.EOL +
    274             "           for (Iterator ixtor = innerList.iterator(); ixtor.hasNext();) {" + PMD.EOL +
    275             "               Integer integer = (Integer) ixtor.next();" + PMD.EOL +
    276             "               sb.append(\"\");" + PMD.EOL +
    277             "               if (ixtor.hasNext()) {" + PMD.EOL +
    278             "                   sb.append(\",\");" + PMD.EOL +
    279             "               } " +
    280             "           }" + PMD.EOL +
    281             "           sb.append(\"foo\");" + PMD.EOL +
    282             "       }" + PMD.EOL +
    283             "   }" + PMD.EOL +
    284             "}";
    285
    286     private static final String TEST16 =
    287             "public class Foo {" + PMD.EOL +
    288             " public void bar() {" + PMD.EOL +
    289             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    290             "  String foo = \"World\";" + PMD.EOL +
    291             "  sb.append(\"Hello\" + foo);" + PMD.EOL +
    292             " }" + PMD.EOL +
    293             "}";
    294
    295     private static final String TEST17 =
    296             "public class Foo {" + PMD.EOL +
    297             " public void bar() {" + PMD.EOL +
    298             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    299             "  String foo = \"World\";" + PMD.EOL +
    300             "  sb.append(\"Hello\" + foo);" + PMD.EOL +
    301             "  sb.append(\"World\");" + PMD.EOL +
    302             " }" + PMD.EOL +
    303             "}";
    304
    305     private static final String TEST18 =
    306             "public class Foo {" + PMD.EOL +
    307             " public void bar() {" + PMD.EOL +
    308             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    309             "  String foo = \"Hello\";" + PMD.EOL +
    310             "  sb.append(\"Hello\");" + PMD.EOL +
    311             "  sb.append(foo);" + PMD.EOL +
    312             " }" + PMD.EOL +
    313             "}";
    314
    315     private static final String TEST19 =
    316             "public class Foo {" + PMD.EOL +
    317             " public void bar() {" + PMD.EOL +
    318             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    319             "  String foo = \"World\";" + PMD.EOL +
    320             "  sb.append(foo).append(\"World\");" + PMD.EOL +
    321             "  sb.append(foo).append(\"World\");" + PMD.EOL +
    322             " }" + PMD.EOL +
    323             "}";
    324
    325     private static final String TEST20_FAIL =
    326             "public class Foo {" + PMD.EOL +
    327             " public void bar() {" + PMD.EOL +
    328             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    329             "  String foo = \"AAA\";" + PMD.EOL +
    330             "  sb.append(foo).append(\"BBB\");" + PMD.EOL +
    331             "  sb.append(\"CCC\");" + PMD.EOL +
    332             " }" + PMD.EOL +
    333             "}";
    334
    335     private static final String TEST21 =
    336             "public class Foo {" + PMD.EOL +
    337             " public void bar() {" + PMD.EOL +
    338             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    339             "  sb.append(\"BBB\");" + PMD.EOL +
    340             "  if(true){" + PMD.EOL +
    341             "   sb.append(\"CCC\");" + PMD.EOL +
    342             "  }" + PMD.EOL +
    343             "  sb.append(\"DDD\");" + PMD.EOL +
    344             " }" + PMD.EOL +
    345             "}";
    346
    347     private static final String TEST22 =
    348             "public class Foo {" + PMD.EOL +
    349             " public void bar() {" + PMD.EOL +
    350             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    351             "  sb.append(String.valueOf(\"2\"));" + PMD.EOL +
    352             "  sb.append(String.valueOf(\"3\"));" + PMD.EOL +
    353             " }" + PMD.EOL +
    354             "}";
    355
    356     private static final String TEST23_FAIL =
    357             "public class Foo {" + PMD.EOL +
    358             " public void bar() {" + PMD.EOL +
    359             "  String somevar = \"\";" + PMD.EOL +
    360             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    361             "  sb.append(\"<bignode>\");" + PMD.EOL +
    362             "  sb.append(\"<somenode>\").append(somevar).append(\"</somenode>\");" + PMD.EOL +
    363             "  sb.append(\"</bignode>\");" + PMD.EOL +
    364             "  sb.append(\"</bignode>\");" + PMD.EOL +
    365             " }" + PMD.EOL +
    366             "}";
    367
    368     private static final String TEST24 =
    369             "public class Foo {" + PMD.EOL +
    370             "    public String foo(int in) {" + PMD.EOL +
    371             "        StringBuffer retval = new StringBuffer();" + PMD.EOL +
    372             "        for (int i = 0; i < in; i++) {" + PMD.EOL +
    373             "          switch (in){" + PMD.EOL +
    374             "             case 0 :" + PMD.EOL +
    375             "                continue;" + PMD.EOL +
    376             "             case 1:" + PMD.EOL +
    377             "                retval.append(\"0\");" + PMD.EOL +
    378             "                continue;" + PMD.EOL +
    379             "             case 2:" + PMD.EOL +
    380             "                retval.append(\"1\");" + PMD.EOL +
    381             "                continue;" + PMD.EOL +
    382             "             case 3:" + PMD.EOL +
    383             "                retval.append(\"2\");" + PMD.EOL +
    384             "                continue;" + PMD.EOL +
    385             "             default:" + PMD.EOL +
    386             "                retval.append(\"3\");" + PMD.EOL +
    387             "                continue;" + PMD.EOL +
    388             "          }" + PMD.EOL +
    389             "        }" + PMD.EOL +
    390             "        return retval.toString();" + PMD.EOL +
    391             "     }" + PMD.EOL +
    392             "}";
    393     private static final String TEST25 =
    394             "public class Foo {" + PMD.EOL +
    395             " public void bar() {" + PMD.EOL +
    396             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    397             "  if(true){" + PMD.EOL +
    398             "   sb.append(\"CCC\");" + PMD.EOL +
    399             "  }" + PMD.EOL +
    400             "  if(true){" + PMD.EOL +
    401             "   sb.append(\"CCC\");" + PMD.EOL +
    402             "  }" + PMD.EOL +
    403             "  if(true){" + PMD.EOL +
    404             "   sb.append(\"CCC\");" + PMD.EOL +
    405             "  }" + PMD.EOL +
    406             " }" + PMD.EOL +
    407             "}";
    408     private static final String TEST26 =
    409             "public class Foo {" + PMD.EOL +
    410             " public void bar() {" + PMD.EOL +
    411             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    412             "  sb.append(\"CCC\");" + PMD.EOL +
    413             "  if(true){" + PMD.EOL +
    414             "   sb.append(\"CCC\");" + PMD.EOL +
    415             "  } else if (sb.length() == 2){" + PMD.EOL +
    416             "   sb.append(\"CCC\");" + PMD.EOL +
    417             "  } else {" + PMD.EOL +
    418             "   sb.append(\"CCC\");" + PMD.EOL +
    419             "  }" + PMD.EOL +
    420             " }" + PMD.EOL +
    421             "}";
    422
    423     private static final String TEST27_FAIL =
    424             "public class Foo {" + PMD.EOL +
    425             "    public String foo(int in) {" + PMD.EOL +
    426             "        StringBuffer retval = new StringBuffer();" + PMD.EOL +
    427             "        for (int i = 0; i < in; i++) {" + PMD.EOL +
    428             "          switch (in){" + PMD.EOL +
    429             "             case 0 :" + PMD.EOL +
    430             "                continue;" + PMD.EOL +
    431             "             case 1:" + PMD.EOL +
    432             "                retval.append(\"0\");" + PMD.EOL +
    433             "                continue;" + PMD.EOL +
    434             "             case 2:" + PMD.EOL +
    435             "                retval.append(\"1\");" + PMD.EOL +
    436             "                retval.append(\"1\");" + PMD.EOL +
    437             "                continue;" + PMD.EOL +
    438             "             case 3:" + PMD.EOL +
    439             "                retval.append(\"2\");" + PMD.EOL +
    440             "                continue;" + PMD.EOL +
    441             "             default:" + PMD.EOL +
    442             "                retval.append(\"3\");" + PMD.EOL +
    443             "                retval.append(\"3\");" + PMD.EOL +
    444             "                continue;" + PMD.EOL +
    445             "          }" + PMD.EOL +
    446             "        }" + PMD.EOL +
    447             "        return retval.toString();" + PMD.EOL +
    448             "     }" + PMD.EOL +
    449             "}";
    450
    451
    452     private static final String TEST28 =
    453             "public class Foo {" + PMD.EOL +
    454             " public void bar() {" + PMD.EOL +
    455             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    456             "  String foo = \"blah\";" + PMD.EOL +
    457             "  int count = 0;" + PMD.EOL +
    458             "  if(true){" + PMD.EOL +
    459             "   sb.append(\"CCC\" + (++count) + \"Ffalsd\");" + PMD.EOL +
    460             "  }else if(foo.length() == 2){" + PMD.EOL +
    461             "   sb.append(\"CCC\" + (++count) + \"Ffalsd\");" + PMD.EOL +
    462             "  }" + PMD.EOL +
    463             " }" + PMD.EOL +
    464             "}";
    465
    466     private static final String TEST29 =
    467             "public class Foo {" + PMD.EOL +
    468             " public void bar() {" + PMD.EOL +
    469             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    470             "   sb.append(\"CCC\" + \"Ffalsd\");" + PMD.EOL +
    471             " }" + PMD.EOL +
    472             "}";
    473     private static final String TEST30 =
    474             "public class Foo {" + PMD.EOL +
    475             " public void bar() {" + PMD.EOL +
    476             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    477             "  int count = 0;" + PMD.EOL +
    478             "   sb.append(\"CCC\" + String.valueOf(count++));" + PMD.EOL +
    479             " }" + PMD.EOL +
    480             "}";
    481
    482     private static final String TEST31_FAIL =
    483             "public class Foo {" + PMD.EOL +
    484             " public void bar() {" + PMD.EOL +
    485             "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    486             "   sb.append(\"CCC\" + \"Ffalsd\");" + PMD.EOL +
    487             "   sb.append(\"CCC\");" + PMD.EOL +
    488             " }" + PMD.EOL +
    489             "}";
    490
    491     private static final String TEST32_FAIL =
    492             "public class Foo {" + PMD.EOL +
    493             " public void bar() {" + PMD.EOL +
    494             "  StringBuffer sb = new StringBuffer(\"CCC\");" + PMD.EOL +
    495             "   sb.append(\"CCC\");" + PMD.EOL +
    496             " }" + PMD.EOL +
    497             "}";
    498
    499     private static final String TEST33_FAIL =
    500             "public class Foo {" + PMD.EOL +
    501             " public void bar() {" + PMD.EOL +
    502             "  StringBuffer sb = new StringBuffer(foo + \"CCC\");" + PMD.EOL +
    503             "   sb.append(\"CCC\");" + PMD.EOL +
    504             " }" + PMD.EOL +
    505             "}";
    506
    507     private static final String TEST34 =
    508             "public class Foo {" + PMD.EOL +
    509             " public void bar() {" + PMD.EOL +
    510             "  StringBuffer sb = new StringBuffer(\"CCC\" + foo);" + PMD.EOL +
    511             "   sb.append(\"CCC\");" + PMD.EOL +
    512             " }" + PMD.EOL +
    513             "}";
    514
    515     private static final String TEST35 =
    516             "public class Foo {" + PMD.EOL +
    517             " public void bar() {" + PMD.EOL +
    518             "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    519             " sb.append(\"World\");" + PMD.EOL +
    520             "  for(int ix = 0; ix < 2; ix++)" + PMD.EOL +
    521             "     sb.append(\"World\");" + PMD.EOL +
    522             " " + PMD.EOL +
    523             " sb.append(\"World\");" + PMD.EOL +
    524             " }" + PMD.EOL +
    525             "}";
    526
    527
    528     private static final String TEST36 =
    529             "public class Foo {" + PMD.EOL +
    530             "    public void bar() {" + PMD.EOL +
    531             "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    532             "        sb.append(\"World\");" + PMD.EOL +
    533             "        foo(sb);" + PMD.EOL +
    534             "        sb.append(\"World\");" + PMD.EOL +
    535             "    }" + PMD.EOL +
    536             "}";
    537
    538     private static final String TEST37_FAIL =
    539             "public class Foo {" + PMD.EOL +
    540             "    public void bar() {" + PMD.EOL +
    541             "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    542             "        sb.append(\"World\");" + PMD.EOL +
    543             "        sb.toString();" + PMD.EOL +
    544             "        sb.append(\"World\");" + PMD.EOL +
    545             "    }" + PMD.EOL +
    546             "}";
    547
    548     private static final String TEST38_FAIL =
    549             "public class Foo {" + PMD.EOL +
    550             "    public void bar() {" + PMD.EOL +
    551             "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    552             "        sb.append(\"World\");" + PMD.EOL +
    553             "        foo(sb.toString());" + PMD.EOL +
    554             "        sb.append(\"World\");" + PMD.EOL +
    555             "    }" + PMD.EOL +
    556             "}";
    557
    558     private static final String TEST39 =
    559             "public class Foo {" + PMD.EOL +
    560             "    StringBuffer sb = new StringBuffer();" + PMD.EOL +
    561             "    public void foo() {" + PMD.EOL +
    562             "        sb.append(\"World\");" + PMD.EOL +
    563             "    }" + PMD.EOL +
    564             "    public void bar() {" + PMD.EOL +
    565             "        sb.append(\"World\");" + PMD.EOL +
    566             "    }" + PMD.EOL +
    567             "}";
    568
    569     private static final String TEST40 =
    570             "public class Foo {" + PMD.EOL +
    571             "    public void foo(int i) {" + PMD.EOL +
    572             "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    573             "        sb.append(foo(i-2));" + PMD.EOL +
    574             "        sb.append(\"World\");" + PMD.EOL +
    575             "    }" + PMD.EOL +
    576             "}";
    577
    578     private static final String TEST41 =
    579             "public class Foo {" + PMD.EOL +
    580             "    public void foo() {" + PMD.EOL +
    581             "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    582             "        sb.append(foo(\"\".length()+2));" + PMD.EOL +
    583             "        sb.append(\"World\");" + PMD.EOL +
    584             "    }" + PMD.EOL +
    585             "}";
    586
    587 }