1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules.imports;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Report;
8   import net.sourceforge.pmd.Rule;
9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
11  
12  public class UnusedImportsRuleTest extends SimpleAggregatorTst {
13  
14      private Rule rule;
15  
16      public void setUp() {
17          rule = findRule("imports", "UnusedImports");
18      }
19  
20      public void testAll() {
21          runTests(new TestDescriptor[]{
22              new TestDescriptor(TEST1, "simple unused single type import", 1, rule),
23              new TestDescriptor(TEST2, "one used single type import", 0, rule),
24              new TestDescriptor(TEST3, "2 unused single-type imports", 2, rule),
25              new TestDescriptor(TEST4, "1 used single type import", 0, rule),
26              new TestDescriptor(TEST5, "1 import stmt, used only in throws clause", 0, rule),
27          });
28      }
29  
30      public void testForLoop() throws Throwable {
31          Report rpt = new Report();
32          runTestFromString15(TEST6, rule, rpt);
33          assertEquals(0, rpt.size());
34      }
35  
36      public void testGenerics() throws Throwable {
37          Report rpt = new Report();
38          runTestFromString15(TEST7, rule, rpt);
39          assertEquals(0, rpt.size());
40      }
41  
42      public void testAnnotations() throws Throwable {
43          Report rpt = new Report();
44          runTestFromString15(TEST8, rule, rpt);
45          assertEquals(0, rpt.size());
46      }
47  
48      public void testAnnotations2() throws Throwable {
49          Report rpt = new Report();
50          runTestFromString15(TEST9, rule, rpt);
51          assertEquals(0, rpt.size());
52      }
53  
54      private static final String TEST1 =
55              "import java.io.File;" + PMD.EOL +
56              "public class Foo {}";
57  
58      private static final String TEST2 =
59              "import java.io.File;" + PMD.EOL +
60              "public class Foo {" + PMD.EOL +
61              " private File file;" + PMD.EOL +
62              "}";
63  
64      private static final String TEST3 =
65              "import java.io.File;" + PMD.EOL +
66              "import java.util.List;" + PMD.EOL +
67              "public class Foo {" + PMD.EOL +
68              "}";
69  
70      private static final String TEST4 =
71              "import java.security.AccessController;" + PMD.EOL +
72              "public class Foo {" + PMD.EOL +
73              " public void foo() {" + PMD.EOL +
74              "  AccessController.doPrivileged(null);" + PMD.EOL +
75              " }" + PMD.EOL +
76              "}";
77  
78      private static final String TEST5 =
79              "import java.rmi.RemoteException;" + PMD.EOL +
80              "public class Foo {" + PMD.EOL +
81              " public void foo() throws RemoteException {}" + PMD.EOL +
82              "}";
83  
84      private static final String TEST6 =
85              "import java.util.ArrayList;" + PMD.EOL +
86              "public class Foo {" + PMD.EOL +
87              " public void foo(ArrayList list) {" + PMD.EOL +
88              "  for (String s : list) {}" + PMD.EOL +
89              " }" + PMD.EOL +
90              "}";
91  
92      private static final String TEST7 =
93              "import foo.TestInterfaceTwo;" + PMD.EOL +
94              "public class Foo {" + PMD.EOL +
95              " private List<TestInterfaceTwo> x = new ArrayList<TestInterfaceTwo>();" + PMD.EOL +
96              "}";
97  
98      private static final String TEST8 =
99              "import foo.annotation.Retention;" + PMD.EOL +
100             "import foo.annotation.RetentionPolicy;" + PMD.EOL +
101             "@Retention(RetentionPolicy.RUNTIME)" + PMD.EOL +
102             "public @interface Foo {" + PMD.EOL +
103             "}";
104 
105     private static final String TEST9 =
106             "import foo.FooAnnotation1;" + PMD.EOL +
107             "import foo.FooAnnotation2;" + PMD.EOL +
108             "@FooAnnotation1" + PMD.EOL +
109             "@FooAnnotation2" + PMD.EOL +
110             "public class Foo {}";
111 
112 }