    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.design;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Report;
    8   import net.sourceforge.pmd.ReportListener;
    9   import net.sourceforge.pmd.Rule;
    10  import net.sourceforge.pmd.RuleViolation;
    11  import net.sourceforge.pmd.IRuleViolation;
    12  import net.sourceforge.pmd.rules.design.UseSingleton;
    13  import net.sourceforge.pmd.stat.Metric;
    14  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    15  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    16
    17  public class UseSingletonTest extends SimpleAggregatorTst implements ReportListener {
    18
    19      private int callbacks;
    20      private Rule rule;
    21
    22      public void setUp() {
    23          rule = findRule("design", "UseSingleton");
    24      }
    25
    26      public void testAll() {
    27          runTests(new TestDescriptor[]{
    28              new TestDescriptor(TEST1, "should be singleton since all static, public constructor", 1, rule),
    29              new TestDescriptor(TEST2, "ok, uses non-static", 0, rule),
    30              new TestDescriptor(TEST3, "should be singleton, couple of statics, no constructor", 1, rule),
    31              new TestDescriptor(TEST4, "no constructor, one static - ok", 0, rule),
    32              new TestDescriptor(TEST5, "classic singleton - ok", 0, rule),
    33              new TestDescriptor(TEST6, "abstract, so ok", 0, rule),
    34              new TestDescriptor(TEST7, "has no fields, so ok", 0, rule),
    35              new TestDescriptor(TEST8, "has public static field, so need to check", 1, rule),
    36              new TestDescriptor(TEST9, "junit 'suite' method is OK", 0, rule),
    37          });
    38      }
    39
    40      public void testResetState() throws Throwable {
    41          callbacks = 0;
    42          Rule rule = new UseSingleton();
    43          Report report = new Report();
    44          report.addListener(this);
    45          runTestFromString(TEST3, rule, report);
    46          runTestFromString(TEST4, rule, report);
    47          assertEquals(1, callbacks);
    48      }
    49
    50      public void ruleViolationAdded(IRuleViolation ruleViolation) {
    51          callbacks++;
    52      }
    53
    54      public void metricAdded(Metric metric) {
    55      }
    56
    57      private static final String TEST1 =
    58              "public class Foo {" + PMD.EOL +
    59              " public Foo() { }" + PMD.EOL +
    60              " public static void doSomething() {}" + PMD.EOL +
    61              " public static void main(String args[]) {" + PMD.EOL +
    62              "  doSomething();" + PMD.EOL +
    63              " }" + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST2 =
    67              "public class Foo {" + PMD.EOL +
    68              "    public Foo() { }" + PMD.EOL +
    69              "    public void doSomething() { }" + PMD.EOL +
    70              "    public static void main(String args[]) { }" + PMD.EOL +
    71              "}";
    72
    73      private static final String TEST3 =
    74              "public class Foo {" + PMD.EOL +
    75              "    public static void doSomething1() { }" + PMD.EOL +
    76              "    public static void doSomething2() { }" + PMD.EOL +
    77              "}";
    78
    79      private static final String TEST4 =
    80              "public class Foo {" + PMD.EOL +
    81              "    public Foo() { }" + PMD.EOL +
    82              "}";
    83
    84      private static final String TEST5 =
    85              "public class Foo {" + PMD.EOL +
    86              " private Foo() {}" + PMD.EOL +
    87              " public static Foo get() {" + PMD.EOL +
    88              "  return null;" + PMD.EOL +
    89              " }     " + PMD.EOL +
    90              "}";
    91
    92      private static final String TEST6 =
    93              "public abstract class Foo {" + PMD.EOL +
    94              "    public static void doSomething1() { }" + PMD.EOL +
    95              "    public static void doSomething2() { }" + PMD.EOL +
    96              "    public static void doSomething3() { }" + PMD.EOL +
    97              "}";
    98
    99      private static final String TEST7 =
    100             "public class Foo {" + PMD.EOL +
    101             " public Foo() { }" + PMD.EOL +
    102             " private int x;" + PMD.EOL +
    103             " public static void doSomething() {}" + PMD.EOL +
    104             "}";
    105
    106     private static final String TEST8 =
    107             "public class Foo {" + PMD.EOL +
    108             " public static final int x = 5;" + PMD.EOL +
    109             " public static void doSomething() {}" + PMD.EOL +
    110             "}";
    111
    112     private static final String TEST9 =
    113             "public class FooTest {" + PMD.EOL +
    114             " public static Test suite() {" + PMD.EOL +
    115             "  return new TestSuite();" + PMD.EOL +
    116             " }" + PMD.EOL +
    117             "}";
    118 }