1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package test.net.sourceforge.pmd.rules.design;
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  
12  public class UnsynchronizedStaticDateFormatterTest extends SimpleAggregatorTst  {
13  
14      private Rule rule;
15  
16      public void setUp() {
17          rule = findRule("design", "UnsynchronizedStaticDateFormatter");
18      }
19  
20      public void testAll() throws Exception{
21          runTests(new TestDescriptor[] { 
22                  new TestDescriptor(TEST1, "Format called from non-synchronized block", 1, rule),
23                  new TestDescriptor(TEST2, "2, No call to format", 0, rule),
24                  new TestDescriptor(TEST3, "3, Inside synchronized, OK", 0, rule),
25                  new TestDescriptor(TEST4, "4, Inside synchronized, OK", 0, rule),
26                  new TestDescriptor(TEST5, "5, Use DateFormat, ok", 0, rule),
27                  new TestDescriptor(TEST6, "6, Use DateFormat, fail", 1, rule),
28          });
29      }
30  
31      private static final String TEST1 =
32          "public class Foo {" + PMD.EOL +
33          "    private static final SimpleDateFormat sdf = new SimpleDateFormat();" + PMD.EOL +
34          "    void bar() {" + PMD.EOL +
35          "        sdf.format();" + PMD.EOL +
36          "    }" + PMD.EOL +
37          "}";
38  
39      private static final String TEST2 =
40          "public class Foo {" + PMD.EOL +
41          "    private final SimpleDateFormat sdf = new SimpleDateFormat();" + PMD.EOL +
42          "    void bar() {" + PMD.EOL +
43          "    }" + PMD.EOL +
44          "}";
45  
46      private static final String TEST3 =
47          "public class Foo {" + PMD.EOL +
48          "    private static final SimpleDateFormat sdf = new SimpleDateFormat();" + PMD.EOL +
49          "    synchronized void bar() {" + PMD.EOL +
50          "        sdf.format();" + PMD.EOL +
51          "    }" + PMD.EOL +
52          "}";
53  
54  
55      private static final String TEST4 =
56          "public class Foo {" + PMD.EOL +
57          "    private static final SimpleDateFormat sdf = new SimpleDateFormat();" + PMD.EOL +
58          "    public void bar() {" + PMD.EOL +
59          "        synchronized (sdf) {" + PMD.EOL +
60          "            sdf.format();" + PMD.EOL +
61          "        }" + PMD.EOL +
62          "    }" + PMD.EOL +
63          "}";
64      
65  
66      private static final String TEST5 =
67          "public class Foo {" + PMD.EOL +
68          "    private static final DateFormat sdf = new DateFormat();" + PMD.EOL +
69          "    synchronized void bar() {" + PMD.EOL +
70          "        sdf.format();" + PMD.EOL +
71          "    }" + PMD.EOL +
72          "}";
73  
74      private static final String TEST6 =
75          "public class Foo {" + PMD.EOL +
76          "    private static final DateFormat sdf = new DateFormat();" + PMD.EOL +
77          "    void bar() {" + PMD.EOL +
78          "        sdf.format();" + PMD.EOL +
79          "    }" + PMD.EOL +
80          "}";
81  
82  }