1 package test.net.sourceforge.pmd.rules.design;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.Report;
5 import net.sourceforge.pmd.ReportListener;
6 import net.sourceforge.pmd.Rule;
7 import net.sourceforge.pmd.RuleViolation;
8 import net.sourceforge.pmd.rules.design.UseSingletonRule;
9 import net.sourceforge.pmd.stat.Metric;
10 import test.net.sourceforge.pmd.rules.RuleTst;
11
12 public class UseSingletonRuleTest extends RuleTst implements ReportListener {
13
14 public void testAllStaticsPublicConstructor() throws Throwable {
15 runTestFromString(TEST1, 1, new UseSingletonRule());
16 }
17
18 public void testOKDueToNonStaticMethod() throws Throwable {
19 runTestFromString(TEST2, 0, new UseSingletonRule());
20 }
21
22 public void testNoConstructorCoupleOfStatics() throws Throwable {
23 runTestFromString(TEST3, 1, new UseSingletonRule());
24 }
25
26 public void testNoConstructorOneStatic() throws Throwable {
27 runTestFromString(TEST4, 0, new UseSingletonRule());
28 }
29
30 public void testClassicSingleton() throws Throwable {
31 runTestFromString(TEST5, 0, new UseSingletonRule());
32 }
33
34 public void testAbstractSingleton() throws Throwable {
35 runTestFromString(TEST6, 0, new UseSingletonRule());
36 }
37
38
39 public void testResetState() throws Throwable {
40 callbacks = 0;
41 Rule rule = new UseSingletonRule();
42 Report report = new Report();
43 report.addListener(this);
44 runTestFromString(TEST3, rule, report);
45 runTestFromString(TEST4, rule, report);
46 assertEquals(1, callbacks);
47 }
48
49 private int callbacks;
50
51 public void ruleViolationAdded(RuleViolation ruleViolation) {
52 callbacks++;
53 }
54
55 public void metricAdded(Metric metric) {
56 }
57
58 private static final String TEST1 =
59 "public class Foo {" + PMD.EOL +
60 " // Should trigger UseSingleton rule?" + PMD.EOL +
61 " public Foo() { }" + PMD.EOL +
62 " public static void doSomething() {}" + PMD.EOL +
63 " public static void main(String args[]) {" + PMD.EOL +
64 " doSomething();" + PMD.EOL +
65 " }" + PMD.EOL +
66 "}";
67
68 private static final String TEST2 =
69 "public class UseSingleton2" + PMD.EOL +
70 "{" + PMD.EOL +
71 " // Should not trigger UseSingleton rule." + PMD.EOL +
72 " public UseSingleton2() { }" + PMD.EOL +
73 " public void doSomething() { }" + PMD.EOL +
74 " public static void main(String args[]) { }" + PMD.EOL +
75 "}";
76
77 private static final String TEST3 =
78 "public class UseSingleton3" + PMD.EOL +
79 "{" + PMD.EOL +
80 " // Should trigger it." + PMD.EOL +
81 " public static void doSomething1() { }" + PMD.EOL +
82 " public static void doSomething2() { }" + PMD.EOL +
83 " public static void doSomething3() { }" + PMD.EOL +
84 "}";
85
86 private static final String TEST4 =
87 "public class UseSingleton4" + PMD.EOL +
88 "{" + PMD.EOL +
89 " public UseSingleton4() { }" + PMD.EOL +
90 "}";
91
92 private static final String TEST5 =
93 "public class UseSingleton5 {" + PMD.EOL +
94 " private UseSingleton5() {}" + PMD.EOL +
95 " public static UseSingleton5 get() {" + PMD.EOL +
96 " return null;" + PMD.EOL +
97 " } " + PMD.EOL +
98 "}";
99
100 private static final String TEST6 =
101 "public abstract class Foo {" + PMD.EOL +
102 " public static void doSomething1() { }" + PMD.EOL +
103 " public static void doSomething2() { }" + PMD.EOL +
104 " public static void doSomething3() { }" + PMD.EOL +
105 "}";
106
107 }
This page was automatically generated by Maven