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

This page was automatically generated by Maven