    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 InefficientEmptyStringCheckTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("strings", "InefficientEmptyStringCheck");
    17      }
    18
    19      public void testAll() {
    20
    21          runTests(new TestDescriptor[]{new TestDescriptor(TEST1, "test is ok, ok", 0, rule),
    22                                        new TestDescriptor(TEST2, "String.trim.length is called, should have failed", 1, rule),
    23                                        new TestDescriptor(TEST3, "String.trim.length not is called, ok", 0, rule),
    24                                        new TestDescriptor(TEST4, "String.trim.length is called, should have failed", 1, rule),
    25                                        new TestDescriptor(TEST5, "String.trim.length is called, assigned to int, ok", 0, rule),
    26                                        new TestDescriptor(TEST6, "String.trim.length is called, assigned to boolean, should have failed", 1, rule),
    27                                        new TestDescriptor(TEST7, "Using trim.length to find the length and compare to 1, OK", 0, rule),
    28                                        new TestDescriptor(TEST8, "Passes trim().length() and 0 to another method", 0, rule),
    29                                        new TestDescriptor(TEST9, "Compares the length against a mathematical function", 0, rule),
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo {" + PMD.EOL +
    35              "    void bar() {" + PMD.EOL +
    36              "        String foo = \"foo\";" + PMD.EOL +
    37              "        if(foo.length() == 0) {" + PMD.EOL +
    38              "        // this is bad" + PMD.EOL +
    39              "        }" + PMD.EOL +
    40              "    }" + PMD.EOL +
    41              "}";
    42
    43      private static final String TEST2 =
    44              "public class Foo {" + PMD.EOL +
    45              "    void bar() {" + PMD.EOL +
    46              "        String foo = \"foo\";" + PMD.EOL +
    47              "        if(foo.trim().length() == 0) {" + PMD.EOL +
    48              "        // this is bad" + PMD.EOL +
    49              "        }" + PMD.EOL +
    50              "    }" + PMD.EOL +
    51              "}";
    52
    53      private static final String TEST3 =
    54              "public class Foo {" + PMD.EOL +
    55              "    void bar() {" + PMD.EOL +
    56              "        String foo = \"foo\";" + PMD.EOL +
    57              "        if(foo.trim().equals(\"\")) {" + PMD.EOL +
    58              "        }" + PMD.EOL +
    59              "    }" + PMD.EOL +
    60              "}";
    61
    62
    63      private static final String TEST4 =
    64              "public class Foo {" + PMD.EOL +
    65              "    void bar() {" + PMD.EOL +
    66              "        String foo = \"foo\";" + PMD.EOL +
    67              "        while(foo.trim().length()==0) {" + PMD.EOL +
    68              "        }" + PMD.EOL +
    69              "    }" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST5 =
    73              "public class Foo {" + PMD.EOL +
    74              "    void bar() {" + PMD.EOL +
    75              "        String foo = \"foo\";" + PMD.EOL +
    76              "        int i = foo.trim().length();" + PMD.EOL +
    77              "    }" + PMD.EOL +
    78              "}";
    79
    80      private static final String TEST6 =
    81              "public class Foo {" + PMD.EOL +
    82              "    void bar() {" + PMD.EOL +
    83              "        String foo = \"foo\";" + PMD.EOL +
    84              "        boolean b = foo.trim().length()==0;" + PMD.EOL +
    85              "    }" + PMD.EOL +
    86              "}";
    87
    88      private static final String TEST7 =
    89              "public class Foo {" + PMD.EOL +
    90              "    void bar() {" + PMD.EOL +
    91              "        String foo = \"foo\";" + PMD.EOL +
    92              "        boolean b = foo.trim().length()==1;" + PMD.EOL +
    93              "    }" + PMD.EOL +
    94              "}";
    95
    96      private static final String TEST8 =
    97              "public class Foo {" + PMD.EOL +
    98              "    void bar() {" + PMD.EOL +
    99              "        String foo = \"foo\";" + PMD.EOL +
    100             "        boolean b = foo(foo.trim().length(),0);" + PMD.EOL +
    101             "    }" + PMD.EOL +
    102             "}";
    103
    104     private static final String TEST9 =
    105             "public class Foo {" + PMD.EOL +
    106             "    void bar() {" + PMD.EOL +
    107             "        String foo = \"foo\";" + PMD.EOL +
    108             "        boolean b = foo(foo.trim().length()==(2-1));" + PMD.EOL +
    109             "    }" + PMD.EOL +
    110             "}";
    111
    112
    113 }