    1   package test.net.sourceforge.pmd.rules.strings;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.Rule;
    5   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    6   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    7
    8   public class UseStringBufferLengthTest extends SimpleAggregatorTst {
    9
    10      private Rule rule;
    11
    12      public void setUp() throws Exception {
    13          rule = findRule("rulesets/strings.xml", "UseStringBufferLength");
    14      }
    15
    16      public void testAll() {
    17          runTests(new TestDescriptor[]{
    18              new TestDescriptor(TEST1, "Using length properly", 0, rule),
    19              new TestDescriptor(TEST2, "StringBuffer.toString.equals(\"\"), bad", 1, rule),
    20              new TestDescriptor(TEST3, "StringBuffer.toString.equals(\"foo\"), ok", 0, rule),
    21              new TestDescriptor(TEST4, "StringBuffer.toString.length(), bad", 1, rule),
    22              new TestDescriptor(TEST5, "no literals", 0, rule),
    23          });
    24      }
    25
    26      private static final String TEST1 =
    27              "public class Foo {" + PMD.EOL +
    28              "void bar() {" + PMD.EOL +
    29              "    StringBuffer sb = new StringBuffer();" + PMD.EOL +
    30              "     if ( sb.length() == 0 ){" + PMD.EOL +
    31              "           sb.append(\",\");" + PMD.EOL +
    32              "     }" + PMD.EOL +
    33              "    sb.append( \"whatever\" );" + PMD.EOL +
    34              "}" + PMD.EOL +
    35              "}";
    36
    37      private static final String TEST2 =
    38              "public class Foo {" + PMD.EOL +
    39              "void bar() {" + PMD.EOL +
    40              "    StringBuffer sb = new StringBuffer();" + PMD.EOL +
    41              "     if ( sb.toString().equals(\"\") ){" + PMD.EOL +
    42              "           sb.append(\",\");" + PMD.EOL +
    43              "     }" + PMD.EOL +
    44              "    sb.append( \"whatever\" );" + PMD.EOL +
    45              "}" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class Foo {" + PMD.EOL +
    50              "void bar() {" + PMD.EOL +
    51              "    StringBuffer sb = new StringBuffer();" + PMD.EOL +
    52              "     if ( sb.toString().equals(\"foo\") ){" + PMD.EOL +
    53              "           sb.append(\",\");" + PMD.EOL +
    54              "     }" + PMD.EOL +
    55              "    sb.append( \"whatever\" );" + PMD.EOL +
    56              "}" + PMD.EOL +
    57              "}";
    58
    59      private static final String TEST4 =
    60              "public class Foo {" + PMD.EOL +
    61              "void bar() {" + PMD.EOL +
    62              "    StringBuffer sb = new StringBuffer();" + PMD.EOL +
    63              "     if ( sb.toString().length() == 0 ){" + PMD.EOL +
    64              "           sb.append(\",\");" + PMD.EOL +
    65              "     }" + PMD.EOL +
    66              "    sb.append( \"whatever\" );" + PMD.EOL +
    67              "}" + PMD.EOL +
    68              "}";
    69
    70      private static final String TEST5 =
    71              "public class Foo {" + PMD.EOL +
    72              " boolean bar(Object foo) {" + PMD.EOL +
    73              "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    74              "  return sb.toString().equals(foo);" + PMD.EOL +
    75              " }" + PMD.EOL +
    76              "}";
    77  }