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 test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
7 import test.net.sourceforge.pmd.testframework.TestDescriptor;
8 import net.sourceforge.pmd.Rule;
9 import net.sourceforge.pmd.PMD;
10 import net.sourceforge.pmd.rules.XPathRule;
11
12 public class ProperCloneImplementationRuleTest extends SimpleAggregatorTst{
13
14 private Rule rule;
15
16 public void setUp() {
17 rule = new XPathRule();
18 rule.addProperty("xpath", "//ClassDeclaration//MethodDeclarator[@Image = 'clone'][count(FormalParameters/*) = 0][count(../Block//*[ (self::AllocationExpression) and (./Name/@Image = ancestor::UnmodifiedClassDeclaration[position()=last()]/@Image) ])> 0]");
19 }
20
21 public void testAll() {
22 runTests(new TestDescriptor[] {
23 new TestDescriptor(TEST1, "ok, calls super.clone", 0, rule),
24 new TestDescriptor(TEST2, "bad, Foo.clone() calls new Foo();", 1, rule),
25 new TestDescriptor(TEST3, "clone([whatever]) is fine", 0, rule),
26 });
27 }
28
29 private static final String TEST1 =
30 "public class Foo {" + PMD.EOL +
31 " void clone() {" + PMD.EOL +
32 " super.clone();" + PMD.EOL +
33 " }" + PMD.EOL +
34 "}";
35
36 private static final String TEST2 =
37 "public class Foo {" + PMD.EOL +
38 " void clone() {" + PMD.EOL +
39 " Foo f = new Foo();" + PMD.EOL +
40 " }" + PMD.EOL +
41 "}";
42
43 private static final String TEST3 =
44 "public class Foo {" + PMD.EOL +
45 " void clone(String fiddle) {" + PMD.EOL +
46 " Foo f = new Foo();" + PMD.EOL +
47 " }" + PMD.EOL +
48 "}";
49 }
This page was automatically generated by Maven