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.Report;
8   import net.sourceforge.pmd.Rule;
9   import net.sourceforge.pmd.RuleSetNotFoundException;
10  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
11  import test.net.sourceforge.pmd.testframework.TestDescriptor;
12  
13  public class DoubleCheckedLockingTest extends SimpleAggregatorTst {
14  
15      private Rule rule;
16  
17      public void setUp() throws RuleSetNotFoundException {
18          rule = findRule("basic", "DoubleCheckedLocking");
19      }
20  
21      public void testAll() {
22          runTests(new TestDescriptor[]{
23              new TestDescriptor(TEST1, "simple ok", 0, rule),
24              new TestDescriptor(TEST2, "simple failure", 1, rule),
25              new TestDescriptor(TEST3, "skip interfaces", 0, rule),
26          });
27      }
28  
29      public void testGenerics() throws Throwable {
30          Report rpt = new Report();
31          runTestFromString15(TEST4, rule, rpt);
32          assertEquals(0, rpt.size());
33      }
34  
35      private static final String TEST1 =
36              "public class Foo {" + PMD.EOL +
37              " public void foo() {}" + PMD.EOL +
38              "}";
39  
40      private static final String TEST2 =
41              "public class Foo {" + PMD.EOL +
42              "      Object baz;" + PMD.EOL +
43              "      Object bar() {" + PMD.EOL +
44              "        if(baz == null) { //baz may be non-null yet not fully created" + PMD.EOL +
45              "          synchronized(this){" + PMD.EOL +
46              "            if(baz == null){" + PMD.EOL +
47              "              baz = new Object();" + PMD.EOL +
48              "            }" + PMD.EOL +
49              "          }" + PMD.EOL +
50              "        }" + PMD.EOL +
51              "        return baz;" + PMD.EOL +
52              "      }" + PMD.EOL +
53              "}";
54  
55      private static final String TEST3 =
56              "public interface Foo {" + PMD.EOL +
57              " void foo();" + PMD.EOL +
58              "}";
59  
60      private static final String TEST4 =
61              "public class Foo {" + PMD.EOL +
62              " public <T> Bar<T> foo() { /* blah */}" + PMD.EOL +
63              "}";
64  }