    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 StringToStringRuleTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("strings", "StringToString");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "local var", 1, rule),
    22              new TestDescriptor(TEST2, "parameter", 1, rule),
    23              new TestDescriptor(TEST3, "field", 1, rule),
    24              new TestDescriptor(TEST4, "primitive", 0, rule),
    25              new TestDescriptor(TEST5, "multiple similar params", 0, rule),
    26              new TestDescriptor(TEST6, "string array", 1, rule)
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo {" + PMD.EOL +
    32              " private String baz() {" + PMD.EOL +
    33              "  String bar = \"howdy\";" + PMD.EOL +
    34              "  return bar.toString();" + PMD.EOL +
    35              " }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo {" + PMD.EOL +
    40              " private String baz(String bar) {" + PMD.EOL +
    41              "  return bar.toString();" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST3 =
    46              "public class Foo {" + PMD.EOL +
    47              " private String baz;" + PMD.EOL +
    48              " private int getBaz() {" + PMD.EOL +
    49              "  return baz.toString();" + PMD.EOL +
    50              " }" + PMD.EOL +
    51              "}";
    52
    53      private static final String TEST4 =
    54              "public class Foo {" + PMD.EOL +
    55              " private int baz;" + PMD.EOL +
    56              " private int getBaz() {" + PMD.EOL +
    57              "  return baz;" + PMD.EOL +
    58              " }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST5 =
    62              "public class Foo {" + PMD.EOL +
    63              " private String getBaz(String foo, StringBuffer buffer) {" + PMD.EOL +
    64              "  return buffer.toString();" + PMD.EOL +
    65              " }" + PMD.EOL +
    66              "}";
    67
    68      private static final String TEST6 =
    69              "public class Foo {" + PMD.EOL +
    70              " private String getBaz() {" + PMD.EOL +
    71              "  String[] foo = {\"hi\"};" + PMD.EOL +
    72              "  return foo[0].toString();" + PMD.EOL +
    73              " }" + PMD.EOL +
    74              "}";
    75
    76  }