    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 AccessorClassGenerationTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("design", "AccessorClassGeneration");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "inner class has private constructor", 1, rule),
    23              new TestDescriptor(TEST2, "inner class has public constructor", 0, rule),
    24              new TestDescriptor(TEST3, "outer class has public constructor", 1, rule),
    25              new TestDescriptor(TEST4, "final inner class", 0, rule),
    26
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo1 {" + PMD.EOL +
    32              " public class InnerClass {" + PMD.EOL +
    33              "   private InnerClass(){" + PMD.EOL +
    34              "   }" + PMD.EOL +
    35              " }" + PMD.EOL +
    36              " void method(){" + PMD.EOL +
    37              "   new InnerClass();//Causes generation of accessor" + PMD.EOL +
    38              " }" + PMD.EOL +
    39              "}";
    40
    41      private static final String TEST2 =
    42              "public class Foo2 {" + PMD.EOL +
    43              " public class InnerClass {" + PMD.EOL +
    44              "   public InnerClass(){" + PMD.EOL +
    45              "   }" + PMD.EOL +
    46              " }" + PMD.EOL +
    47              " void method(){" + PMD.EOL +
    48              "   new InnerClass(); //OK, due to public constructor" + PMD.EOL +
    49              " }" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST3 =
    53              "public class Foo3 {" + PMD.EOL +
    54              "    public class InnerClass {" + PMD.EOL +
    55              "      void method(){" + PMD.EOL +
    56              "        new Foo3();//Causes generation of accessor" + PMD.EOL +
    57              "      }" + PMD.EOL +
    58              "    }" + PMD.EOL +
    59              "    private Foo3() {" + PMD.EOL +
    60              "    }" + PMD.EOL +
    61              "}";
    62
    63      private static final String TEST4 =
    64              "public class Foo {" + PMD.EOL +
    65              " void method() {" + PMD.EOL +
    66              "   final class Inner {}; " + PMD.EOL +
    67              "   Inner i = new Inner();" + PMD.EOL +
    68              " }" + PMD.EOL +
    69              "}";
    70  }