1 package test.net.sourceforge.pmd.rules.finalize; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.Rule; 5 import net.sourceforge.pmd.RuleSetNotFoundException; 6 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 7 import test.net.sourceforge.pmd.testframework.TestDescriptor; 8 9 public class AvoidCallingFinalizeTest extends SimpleAggregatorTst { 10 11 private Rule rule; 12 13 public void setUp() throws RuleSetNotFoundException { 14 rule = findRule("finalizers", "AvoidCallingFinalize"); 15 } 16 17 public void testAll() { 18 runTests(new TestDescriptor[]{ 19 new TestDescriptor(TEST1, "simple failure case", 1, rule), 20 new TestDescriptor(TEST2, "calling finalize on an object", 1, rule), 21 new TestDescriptor(TEST3, "calling super.finalize", 1, rule), 22 new TestDescriptor(TEST4, "no call to finalize", 0, rule), 23 new TestDescriptor(TEST5, "it's ok in a finalizer", 0, rule), 24 new TestDescriptor(TEST6, "finalizer in anon inner class is OK too", 0, rule), 25 }); 26 } 27 28 private static final String TEST1 = 29 "public class Foo {" + PMD.EOL + 30 " void foo () {" + 31 " finalize();" + 32 " }" + PMD.EOL + 33 "}"; 34 35 private static final String TEST2 = 36 "public class Foo {" + PMD.EOL + 37 " void foo () {" + 38 " Foo f;" + 39 " f.finalize();" + 40 " }" + PMD.EOL + 41 "}"; 42 43 private static final String TEST3 = 44 "public class Foo {" + PMD.EOL + 45 " void foo () {" + 46 " super.finalize();" + 47 " }" + PMD.EOL + 48 "}"; 49 50 private static final String TEST4 = 51 "public class Foo {" + PMD.EOL + 52 " void finalize () {" + 53 " }" + PMD.EOL + 54 "}"; 55 56 private static final String TEST5 = 57 "public class Foo {" + PMD.EOL + 58 " void finalize () {" + 59 " super.finalize(); " + PMD.EOL + 60 " }" + PMD.EOL + 61 "}"; 62 63 private static final String TEST6 = 64 "public class Foo {" + PMD.EOL + 65 " void foo () {" + 66 " Foo myFoo = new Foo(new FooOtherInterface() { " + PMD.EOL + 67 " protected void finalize() { " + PMD.EOL + 68 " super.finalize(); " + PMD.EOL + 69 " } " + PMD.EOL + 70 " }); " + PMD.EOL + 71 " }" + PMD.EOL + 72 "}"; 73 }