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 test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 9 import test.net.sourceforge.pmd.testframework.TestDescriptor; 10 11 public class UncommentedEmptyConstructorRuleTest extends SimpleAggregatorTst { 12 13 private Rule defaultRuleRule; 14 15 private Rule ignoredConstructorInvocationRule; 16 17 public void setUp() { 18 defaultRuleRule = findRule("design", "UncommentedEmptyConstructor"); 19 ignoredConstructorInvocationRule = findRule("design", "UncommentedEmptyConstructor"); 20 ignoredConstructorInvocationRule.addProperty("ignoreExplicitConstructorInvocation", "true"); 21 } 22 23 public void testDefault() { 24 runTests(new TestDescriptor[]{ 25 new TestDescriptor(TEST1, "simple failure", 1, defaultRuleRule), 26 new TestDescriptor(TEST2, "only 'this(...)' is OK", 0, defaultRuleRule), 27 new TestDescriptor(TEST3, "only 'super(...)' is OK", 0, defaultRuleRule), 28 new TestDescriptor(TEST4, "single-line comment is OK", 0, defaultRuleRule), 29 new TestDescriptor(TEST5, "multiple-line comment is OK", 0, defaultRuleRule), 30 new TestDescriptor(TEST6, "Javadoc comment is OK", 0, defaultRuleRule), 31 new TestDescriptor(TEST7, "ok", 0, defaultRuleRule), 32 new TestDescriptor(TEST8, "with 'this(...)' ok", 0, defaultRuleRule), 33 new TestDescriptor(TEST9, "with 'super(...)' ok", 0, defaultRuleRule), 34 }); 35 } 36 37 public void testIgnoredConstructorInvocation() { 38 runTests(new TestDescriptor[]{ 39 new TestDescriptor(TEST1, "simple failure", 1, ignoredConstructorInvocationRule), 40 new TestDescriptor(TEST2, "only 'this(...)' failure", 1, ignoredConstructorInvocationRule), 41 new TestDescriptor(TEST3, "only 'super(...)' failure", 1, ignoredConstructorInvocationRule), 42 new TestDescriptor(TEST4, "single-line comment is OK", 0, ignoredConstructorInvocationRule), 43 new TestDescriptor(TEST5, "multiple-line comment is OK", 0, ignoredConstructorInvocationRule), 44 new TestDescriptor(TEST6, "Javadoc comment is OK", 0, ignoredConstructorInvocationRule), 45 new TestDescriptor(TEST7, "ok", 0, ignoredConstructorInvocationRule), 46 new TestDescriptor(TEST8, "with 'this(...)' ok", 0, ignoredConstructorInvocationRule), 47 new TestDescriptor(TEST9, "with 'super(...)' ok", 0, ignoredConstructorInvocationRule), 48 }); 49 } 50 51 public static final String TEST1 = 52 "public class Foo {" + PMD.EOL + 53 " Foo() {" + PMD.EOL + 54 " }" + PMD.EOL + 55 "}"; 56 57 public static final String TEST2 = 58 "public class Foo {" + PMD.EOL + 59 " Foo() {" + PMD.EOL + 60 " this();" + PMD.EOL + 61 " }" + PMD.EOL + 62 "}"; 63 64 public static final String TEST3 = 65 "public class Foo {" + PMD.EOL + 66 " Foo() {" + PMD.EOL + 67 " super();" + PMD.EOL + 68 " }" + PMD.EOL + 69 "}"; 70 71 public static final String TEST4 = 72 "public class Foo {" + PMD.EOL + 73 " Foo() {" + PMD.EOL + 74 " // Comment" + PMD.EOL + 75 " }" + PMD.EOL + 76 "}"; 77 78 public static final String TEST5 = 79 "public class Foo {" + PMD.EOL + 80 " Foo() {" + PMD.EOL + 81 " this();" + PMD.EOL + 82 " /* Comment */" + PMD.EOL + 83 " }" + PMD.EOL + 84 "}"; 85 86 public static final String TEST6 = 87 "public class Foo {" + PMD.EOL + 88 " Foo() {" + PMD.EOL + 89 " super();" + PMD.EOL + 90 " /** Comment */" + PMD.EOL + 91 " }" + PMD.EOL + 92 "}"; 93 94 public static final String TEST7 = 95 "public class Foo {" + PMD.EOL + 96 " Foo() {" + PMD.EOL + 97 " int bar;" + PMD.EOL + 98 " }" + PMD.EOL + 99 "}"; 100 101 public static final String TEST8 = 102 "public class Foo {" + PMD.EOL + 103 " Foo() {" + PMD.EOL + 104 " this();" + PMD.EOL + 105 " bar();" + PMD.EOL + 106 " }" + PMD.EOL + 107 "}"; 108 109 public static final String TEST9 = 110 "public class Foo {" + PMD.EOL + 111 " Foo() {" + PMD.EOL + 112 " super();" + PMD.EOL + 113 " bar++;" + PMD.EOL + 114 " }" + PMD.EOL + 115 "}"; 116 }