    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.Report;
    8   import net.sourceforge.pmd.Rule;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class ConstructorCallsOverridableMethodTest extends SimpleAggregatorTst {
    13      private Rule rule;
    14
    15      public void setUp() {
    16          rule = findRule("design", "ConstructorCallsOverridableMethod");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "calling public method from constructor", 1, rule),
    22              new TestDescriptor(TEST2, "calling protected method from constructor", 1, rule),
    23              new TestDescriptor(TEST3, "calling package private method from constructor", 1, rule),
    24              new TestDescriptor(TEST4, "calling private method, ok", 0, rule),
    25              new TestDescriptor(TEST5, "overloaded constructors, calling public method", 1, rule),
    26              new TestDescriptor(TEST6, "calling method on literal bug", 0, rule),
    27              new TestDescriptor(TEST7, "method in anonymous inner class is ok", 0, rule),
    28              new TestDescriptor(TEST8, "bug report 975407", 0, rule),
    29              new TestDescriptor(TEST9, "ignore abstract methods", 0, rule),
    30              //FIXME new TestDescriptor(BUG_985989, "bug report 985989, ", 1, rule),
    31          });
    32      }
    33
    34      public void testGenerics() throws Throwable {
    35          Report rpt = new Report();
    36          runTestFromString15(TEST10, rule, rpt);
    37          assertEquals(0, rpt.size());
    38      }
    39
    40      private static final String TEST1 =
    41              "public class Foo {" + PMD.EOL +
    42              " public Foo() {" + PMD.EOL +
    43              "  bar();" + PMD.EOL +
    44              " }" + PMD.EOL +
    45              " public void bar() {}" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST2 =
    49              "public class Foo {" + PMD.EOL +
    50              " public Foo() {" + PMD.EOL +
    51              "  bar();" + PMD.EOL +
    52              " }" + PMD.EOL +
    53              " protected void bar() {}" + PMD.EOL +
    54              "}";
    55
    56      private static final String TEST3 =
    57              "public class Foo {" + PMD.EOL +
    58              " public Foo() {" + PMD.EOL +
    59              "  bar();" + PMD.EOL +
    60              " }" + PMD.EOL +
    61              " void bar() {}" + PMD.EOL +
    62              "}";
    63
    64      private static final String TEST4 =
    65              "public class Foo {" + PMD.EOL +
    66              " public Foo() {" + PMD.EOL +
    67              "  bar();" + PMD.EOL +
    68              " }" + PMD.EOL +
    69              " private void bar() {}" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST5 =
    73              "public class Foo {" + PMD.EOL +
    74              " public Foo() {" + PMD.EOL +
    75              "  this(\"Bar\");" + PMD.EOL +
    76              " }" + PMD.EOL +
    77              " private Foo(String bar) {" + PMD.EOL +
    78              "  bar();" + PMD.EOL +
    79              " }" + PMD.EOL +
    80              " public void bar() {}" + PMD.EOL +
    81              "}";
    82
    83      private static final String TEST6 =
    84              "public class Foo {" + PMD.EOL +
    85              " public Foo(String s) {" + PMD.EOL +
    86              "  \"foo\".equals(s);" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              " public void equals(String bar) {}" + PMD.EOL +
    89              "}";
    90
    91      private static final String TEST7 =
    92              "public class Foo {" + PMD.EOL +
    93              " public Foo(String s) {" + PMD.EOL +
    94              "  addActionListener(new ActionListener() {" + PMD.EOL +
    95              "   public void actionPerformed(ActionEvent e) {bar();}" + PMD.EOL +
    96              "  });" + PMD.EOL +
    97              " }" + PMD.EOL +
    98              " public void bar() {}" + PMD.EOL +
    99              "}";
    100
    101     private static final String TEST8 =
    102             "public class Foo {" + PMD.EOL +
    103             " public Foo() {" + PMD.EOL +
    104             "  bar();" + PMD.EOL +
    105             " }" + PMD.EOL +
    106             " private void bar() {}" + PMD.EOL +
    107             "}";
    108
    109     private static final String TEST9 =
    110             "public class Foo {" + PMD.EOL +
    111             " public Foo() {" + PMD.EOL +
    112             "  bar();" + PMD.EOL +
    113             " }" + PMD.EOL +
    114             " abstract void bar() {}" + PMD.EOL +
    115             "}";
    116
    117     private static final String TEST10 =
    118             "package foo.bar;" + PMD.EOL +
    119             "public enum Buz {" + PMD.EOL +
    120             " FOO(2);" + PMD.EOL +
    121             " private Buz(String s) {}" + PMD.EOL +
    122             "}";
    123
    124     private static final String BUG_985989 =
    125             "public class Test {" + PMD.EOL +
    126             "public static class SeniorClass {" + PMD.EOL +
    127             "  public SeniorClass(){" + PMD.EOL +
    128             "    toString(); //may throw NullPointerException if overridden" + PMD.EOL +
    129             "  }" + PMD.EOL +
    130             "  public String toString(){" + PMD.EOL +
    131             "    return \"IAmSeniorClass\";" + PMD.EOL +
    132             "  }" + PMD.EOL +
    133             "}" + PMD.EOL +
    134             "public static class JuniorClass extends SeniorClass {" + PMD.EOL +
    135             "  private String name;" + PMD.EOL +
    136             "  public JuniorClass(){" + PMD.EOL +
    137             "    super(); //Automatic call leads to NullPointerException" + PMD.EOL +
    138             "    name = \"JuniorClass\";" + PMD.EOL +
    139             "  }" + PMD.EOL +
    140             "  public String toString(){" + PMD.EOL +
    141             "    return name.toUpperCase();" + PMD.EOL +
    142             "  }" + PMD.EOL +
    143             "}" + PMD.EOL +
    144             "public static void main (String[] args) {" + PMD.EOL +
    145             "  System.out.println(\": \"+new SeniorClass());" + PMD.EOL +
    146             "  System.out.println(\": \"+new JuniorClass());" + PMD.EOL +
    147             "}" + PMD.EOL +
    148             "}";
    149 }
    150
    151
    152