1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules.clone;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Rule;
8   import net.sourceforge.pmd.RuleSetNotFoundException;
9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
11  
12  public class ProperCloneImplementationTest extends SimpleAggregatorTst {
13  
14      private Rule rule;
15  
16      public void setUp() throws RuleSetNotFoundException {
17          rule = findRule("clone", "ProperCloneImplementation");
18      }
19  
20      public void testAll() {
21          runTests(new TestDescriptor[]{
22              new TestDescriptor(TEST1, "ok, calls super.clone", 0, rule),
23              new TestDescriptor(TEST2, "bad, Foo.clone() calls new Foo();", 1, rule),
24              new TestDescriptor(TEST3, "clone([whatever]) is fine", 0, rule),
25          });
26      }
27  
28      private static final String TEST1 =
29              "public class Foo {" + PMD.EOL +
30              " void clone() {" + PMD.EOL +
31              "  super.clone();" + PMD.EOL +
32              " }" + PMD.EOL +
33              "}";
34  
35      private static final String TEST2 =
36              "public class Foo {" + PMD.EOL +
37              " void clone() {" + PMD.EOL +
38              "  Foo f = new Foo();" + PMD.EOL +
39              " }" + PMD.EOL +
40              "}";
41  
42      private static final String TEST3 =
43              "public class Foo {" + PMD.EOL +
44              " void clone(String fiddle) {" + PMD.EOL +
45              "  Foo f = new Foo();" + PMD.EOL +
46              " }" + PMD.EOL +
47              "}";
48  }