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.rules.design.UseSingletonRule;
12  import net.sourceforge.pmd.stat.Metric;
13  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
14  import test.net.sourceforge.pmd.testframework.TestDescriptor;
15  
16  public class UseSingletonRuleTest extends SimpleAggregatorTst implements ReportListener {
17  
18      private int callbacks;
19  
20      public void testAll() {
21         runTests(new TestDescriptor[] {
22             new TestDescriptor(TEST1, "should be singleton since all static, public constructor", 1, new UseSingletonRule()),
23             new TestDescriptor(TEST2, "ok, uses non-static", 0, new UseSingletonRule()),
24             new TestDescriptor(TEST3, "should be singleton, couple of statics, no constructor", 1, new UseSingletonRule()),
25             new TestDescriptor(TEST4, "no constructor, one static - ok", 0, new UseSingletonRule()),
26             new TestDescriptor(TEST5, "classic singleton - ok", 0, new UseSingletonRule()),
27             new TestDescriptor(TEST6, "abstract, so ok", 0, new UseSingletonRule()),
28             new TestDescriptor(TEST7, "has no fields, so ok", 0, new UseSingletonRule()),
29         });
30      }
31  
32      public void testResetState() throws Throwable {
33          callbacks = 0;
34          Rule rule = new UseSingletonRule();
35          Report report = new Report();
36          report.addListener(this);
37          runTestFromString(TEST3, rule, report);
38          runTestFromString(TEST4, rule, report);
39          assertEquals(1, callbacks);
40      }
41  
42      public void ruleViolationAdded(RuleViolation ruleViolation) {
43          callbacks++;
44      }
45      public void metricAdded(Metric metric) {}
46  
47      private static final String TEST1 =
48      "public class Foo {" + PMD.EOL +
49      " public Foo() { }" + PMD.EOL +
50      " public static void doSomething() {}" + PMD.EOL +
51      " public static void main(String args[]) {" + PMD.EOL +
52      "  doSomething();" + PMD.EOL +
53      " }" + PMD.EOL +
54      "}";
55  
56      private static final String TEST2 =
57      "public class Foo {" + PMD.EOL +
58      "    public Foo() { }" + PMD.EOL +
59      "    public void doSomething() { }" + PMD.EOL +
60      "    public static void main(String args[]) { }" + PMD.EOL +
61      "}";
62  
63      private static final String TEST3 =
64      "public class Foo {" + PMD.EOL +
65      "    public static void doSomething1() { }" + PMD.EOL +
66      "    public static void doSomething2() { }" + PMD.EOL +
67      "}";
68  
69      private static final String TEST4 =
70      "public class Foo {" + PMD.EOL +
71      "    public Foo() { }" + PMD.EOL +
72      "}";
73  
74      private static final String TEST5 =
75      "public class Foo {" + PMD.EOL +
76      " private Foo() {}" + PMD.EOL +
77      " public static Foo get() {" + PMD.EOL +
78      "  return null;" + PMD.EOL +
79      " }     " + PMD.EOL +
80      "}";
81  
82      private static final String TEST6 =
83      "public abstract class Foo {" + PMD.EOL +
84      "    public static void doSomething1() { }" + PMD.EOL +
85      "    public static void doSomething2() { }" + PMD.EOL +
86      "    public static void doSomething3() { }" + PMD.EOL +
87      "}";
88  
89      private static final String TEST7 =
90      "public class Foo {" + PMD.EOL +
91      " public Foo() { }" + PMD.EOL +
92      " private int x;" + PMD.EOL +
93      " public static void doSomething() {}" + PMD.EOL +
94      "}";
95  }