    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class UnnecessaryConstructorTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("controversial", "UnnecessaryConstructor");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "simple failure case", 1, rule),
    23              new TestDescriptor(TEST2, "private constructor", 0, rule),
    24              new TestDescriptor(TEST3, "constructor with arguments", 0, rule),
    25              new TestDescriptor(TEST4, "constructor with contents", 0, rule),
    26              new TestDescriptor(TEST5, "constructor throws exception", 0, rule),
    27              new TestDescriptor(TEST6, "two constructors", 0, rule),
    28              new TestDescriptor(TEST7, "inner class with unnecessary constructor", 1, rule),
    29              new TestDescriptor(TEST8, "inner and outer both have unnecessary constructors", 2, rule),
    30              new TestDescriptor(TEST9, "inner and outer, both ok", 0, rule),
    31              new TestDescriptor(TEST10, "inner ok, outer bad", 1, rule),
    32              new TestDescriptor(TEST11, "inner ok due to nonpublic constructor", 0, rule),
    33              new TestDescriptor(TEST12, "constructor calls super", 0, rule),
    34              new TestDescriptor(TEST13, "constructor calls super, no args", 1, rule),
    35          });
    36      }
    37
    38      private static final String TEST1 =
    39              "public class Foo {" + PMD.EOL +
    40              " public Foo() {}" + PMD.EOL +
    41              "}";
    42
    43      private static final String TEST2 =
    44              "public class Foo {" + PMD.EOL +
    45              " private Foo() {}" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class Foo {" + PMD.EOL +
    50              " public Foo(int x) {}" + PMD.EOL +
    51              "}";
    52
    53      private static final String TEST4 =
    54              "public class Foo {" + PMD.EOL +
    55              " public Foo() {  " + PMD.EOL +
    56              "  int x = 2;" + PMD.EOL +
    57              " }" + PMD.EOL +
    58              "}";
    59
    60      private static final String TEST5 =
    61              "public class Foo {" + PMD.EOL +
    62              " public Foo() throws IOException {  " + PMD.EOL +
    63              " }" + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST6 =
    67              "public class Foo {" + PMD.EOL +
    68              " public Foo() {" + PMD.EOL +
    69              " }" + PMD.EOL +
    70              " public Foo(String foo) {}" + PMD.EOL +
    71              "}";
    72
    73      private static final String TEST7 =
    74              "public class Foo {" + PMD.EOL +
    75              " public class Inner {" + PMD.EOL +
    76              "  public Inner() {}" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              "}";
    79
    80      private static final String TEST8 =
    81              "public class Foo {" + PMD.EOL +
    82              " public class Inner {" + PMD.EOL +
    83              "  public Inner() {}" + PMD.EOL +
    84              " }" + PMD.EOL +
    85              " public Foo() {}" + PMD.EOL +
    86              "}";
    87
    88      private static final String TEST9 =
    89              "public class Foo {" + PMD.EOL +
    90              " public class Inner {" + PMD.EOL +
    91              " }" + PMD.EOL +
    92              "}";
    93
    94      private static final String TEST10 =
    95              "public class Foo {" + PMD.EOL +
    96              " public class Inner {" + PMD.EOL +
    97              " }" + PMD.EOL +
    98              " public Foo() {}" + PMD.EOL +
    99              "}";
    100
    101     private static final String TEST11 =
    102             "public class Foo {" + PMD.EOL +
    103             " public class Inner {" + PMD.EOL +
    104             "  private Inner() {}" + PMD.EOL +
    105             " }" + PMD.EOL +
    106             "}";
    107
    108     private static final String TEST12 =
    109             "public class Foo {" + PMD.EOL +
    110             "  public Foo() {super(7);}" + PMD.EOL +
    111             "}";
    112
    113     private static final String TEST13 =
    114             "public class Foo {" + PMD.EOL +
    115             "  public Foo() {super();}" + PMD.EOL +
    116             "}";
    117 }