    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 AppendCharacterWithCharTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("strings", "AppendCharacterWithChar");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "appending single character string, should fail", 1, rule),
    22              new TestDescriptor(TEST2, "appending single char, should be ok", 0, rule),
    23              new TestDescriptor(TEST3, "this is probably wrong, but shouldn't fail", 0, rule),
    24              new TestDescriptor(TEST4, "concatenates a three character int", 0, rule),
    25              new TestDescriptor(TEST5, "concatenates a string explicitly set to 1 character, not explicitly checking right now", 0, rule),
    26              new TestDescriptor(TEST6, "for statement", 1, rule),
    27              new TestDescriptor(TEST7, "concatenates an escaped character", 1, rule),
    28              new TestDescriptor(TEST8, "concatenates a single upper case", 1, rule),
    29              new TestDescriptor(TEST9, "concatenates a single number", 1, rule),
    30              new TestDescriptor(TEST10, "concatenates a single character &", 1, rule),
    31              new TestDescriptor(TEST11, "concatenates two characters", 0, rule),
    32          });
    33      }
    34
    35      private static final String TEST1 =
    36              "public class Foo {" + PMD.EOL +
    37              " public void bar(StringBuffer sb) {" + PMD.EOL +
    38              "  sb.append(\"a\");" + PMD.EOL +
    39              " }" + PMD.EOL +
    40              "}";
    41
    42      private static final String TEST2 =
    43              "public class Foo {" + PMD.EOL +
    44              " public void bar(StringBuffer sb) {" + PMD.EOL +
    45              "  sb.append('a');" + PMD.EOL +
    46              " }" + PMD.EOL +
    47              "}";
    48
    49
    50      private static final String TEST3 =
    51              "public class Foo {" + PMD.EOL +
    52              " public void bar(StringBuffer sb) {" + PMD.EOL +
    53              "  sb.append(\"a\" + \"foo\");" + PMD.EOL +
    54              " }" + PMD.EOL +
    55              "}";
    56
    57
    58      private static final String TEST4 =
    59              "public class Foo {" + PMD.EOL +
    60              " public void bar(StringBuffer sb) {" + PMD.EOL +
    61              "  sb.append(123);" + PMD.EOL +
    62              " }" + PMD.EOL +
    63              "}";
    64
    65      private static final String TEST5 =
    66              "public class Foo {" + PMD.EOL +
    67              " public void bar(StringBuffer sb) {" + PMD.EOL +
    68              "  String str = \"a\";" + PMD.EOL +
    69              "  sb.append(str);" + PMD.EOL +
    70              " }" + PMD.EOL +
    71              "}";
    72
    73      private static final String TEST6 =
    74              "public class Foo {" + PMD.EOL +
    75              " public void bar(StringBuffer sb) {" + PMD.EOL +
    76              "  for(int ix = 0; ix < 100; ix++) " +
    77              "      sb.append(\"a\");" + PMD.EOL +
    78              " }" + PMD.EOL +
    79              "}";
    80
    81      private static final String TEST7 =
    82              "public class Foo {" + PMD.EOL +
    83              " public void bar(StringBuffer sb) {" + PMD.EOL +
    84              "  sb.append(\"//t\");" + PMD.EOL +
    85              " }" + PMD.EOL +
    86              "}";
    87
    88      private static final String TEST8 =
    89              "public class Foo {" + PMD.EOL +
    90              " public void bar(StringBuffer sb) {" + PMD.EOL +
    91              "  sb.append(\"A\");" + PMD.EOL +
    92              " }" + PMD.EOL +
    93              "}";
    94
    95      private static final String TEST9 =
    96              "public class Foo {" + PMD.EOL +
    97              " public void bar(StringBuffer sb) {" + PMD.EOL +
    98              "  sb.append(\"3\");" + PMD.EOL +
    99              " }" + PMD.EOL +
    100             "}";
    101
    102     private static final String TEST10 =
    103             "public class Foo {" + PMD.EOL +
    104             " public void bar(StringBuffer sb) {" + PMD.EOL +
    105             "  sb.append(\"&\");" + PMD.EOL +
    106             " }" + PMD.EOL +
    107             "}";
    108
    109     private static final String TEST11 =
    110             "public class Foo {" + PMD.EOL +
    111             " public void bar(StringBuffer sb) {" + PMD.EOL +
    112             "  sb.append(\"/t\");" + PMD.EOL +
    113             " }" + PMD.EOL +
    114             "}";
    115
    116 }