    1   package test.net.sourceforge.pmd.rules;
    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 UselessOverridingMethodTest extends SimpleAggregatorTst {
    9       private Rule rule;
    10
    11      public void setUp() {
    12          rule = findRule("basic", "UselessOverridingMethod");
    13      }
    14
    15      public void testAll() {
    16          runTests(new TestDescriptor[]{
    17              new TestDescriptor(TEST1, "call super", 1, rule),
    18              new TestDescriptor(TEST2, "call super with same argument", 1, rule),
    19              new TestDescriptor(TEST3, "call super with different argument", 0, rule),
    20              new TestDescriptor(TEST4, "call super with different argument 2", 0, rule),
    21              new TestDescriptor(TEST5, "call super with different argument 3", 0, rule),
    22              new TestDescriptor(TEST6, "call super with inverted arguments", 0, rule),
    23              new TestDescriptor(TEST7, "return value of super", 1, rule),
    24              new TestDescriptor(TEST8, "return value of super with argument", 1, rule),
    25              new TestDescriptor(TEST9, "return value of super after adding a string", 0, rule),
    26              new TestDescriptor(TEST10, "do not crash on abstract methods", 0, rule),
    27              new TestDescriptor(TEST11, "do not crash on interfaces", 0, rule),
    28              new TestDescriptor(TEST12, "do not crash on empty returns", 0, rule),
    29              new TestDescriptor(TEST13, "do not crash on super", 0, rule)
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo extends Bar {" + PMD.EOL +
    35              "public void foo() {" + PMD.EOL +
    36              "    super.foo();" + PMD.EOL +
    37              "}" + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST2 =
    41              "public class Foo extends Bar {" + PMD.EOL +
    42              "public void foo(String bar) {" + PMD.EOL +
    43              "    super.foo(bar);" + PMD.EOL +
    44              "}" + PMD.EOL +
    45              "}";
    46
    47      private static final String TEST3 =
    48              "public class Foo extends Bar {" + PMD.EOL +
    49              "String text = \"text\";" + PMD.EOL +
    50              "public void foo(String bar) {" + PMD.EOL +
    51              "    super.foo(text);" + PMD.EOL +
    52              "}" + PMD.EOL +
    53              "}";
    54
    55      private static final String TEST4 =
    56              "public class Foo extends Bar {" + PMD.EOL +
    57              "public void foo(String bar) {" + PMD.EOL +
    58              "    super.foo(boe(bar));" + PMD.EOL +
    59              "}" + PMD.EOL +
    60              "}";
    61
    62      private static final String TEST5 =
    63              "public class Buz{" + PMD.EOL +
    64              " public String foo(String bar) {" + PMD.EOL +
    65              "    super.foo(\"\" + bar); " + PMD.EOL +
    66              " }" + PMD.EOL +
    67              "}";
    68
    69      private static final String TEST6 =
    70              "public class Foo extends Bar {" + PMD.EOL +
    71              "public void foo(String bar, String stool) {" + PMD.EOL +
    72              "    super.foo(stool, bar);" + PMD.EOL +
    73              "}" + PMD.EOL +
    74              "}";
    75
    76      private static final String TEST7 =
    77              "public class Foo extends Bar {" + PMD.EOL +
    78              "public String foo() {" + PMD.EOL +
    79              "    return super.foo();" + PMD.EOL +
    80              "}" + PMD.EOL +
    81              "}";
    82
    83      private static final String TEST8 =
    84              "public class Foo extends Bar {" + PMD.EOL +
    85              "public String foo(String bar) {" + PMD.EOL +
    86              "    return super.foo(bar);" + PMD.EOL +
    87              "}" + PMD.EOL +
    88              "}";
    89
    90      private static final String TEST9 =
    91              "public class Foo extends Bar {" + PMD.EOL +
    92              "public String foo(String bar) {" + PMD.EOL +
    93              "    return super.foo(bar) + \"is foo\";" + PMD.EOL +
    94              "}" + PMD.EOL +
    95              "}";
    96
    97      private static final String TEST10 =
    98              "public class Foo extends Bar {" + PMD.EOL +
    99              "public abstract String foo();" + PMD.EOL +
    100             "}";
    101
    102     private static final String TEST11 =
    103             "public interface Foo extends Bar {" + PMD.EOL +
    104             "public String foo();" + PMD.EOL +
    105             "}";
    106
    107     private static final String TEST12 =
    108             "public class Foo {" + PMD.EOL +
    109             "public void foo(String bar) {" + PMD.EOL +
    110             "    return;" + PMD.EOL +
    111             "}" + PMD.EOL +
    112             "}";
    113
    114     private static final String TEST13 =
    115             "public class Foo {" + PMD.EOL +
    116             "public void init(String[] args) {" + PMD.EOL +
    117             "    super.init( args, Application.NO_MODULES );" + PMD.EOL +
    118             "}" + PMD.EOL +
    119             "}";
    120
    121 }