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 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 rule.setMessage("Avoid this stuff -> ''{0}''"); 19 } 20 21 public void testAll() { 22 runTests(new TestDescriptor[] { 23 new TestDescriptor(TEST1, "simple unused single type import", 1, rule), 24 new TestDescriptor(TEST2, "one used single type import", 0, rule), 25 new TestDescriptor(TEST3, "2 unused single-type imports", 2, rule), 26 new TestDescriptor(TEST4, "1 used single type import", 0, rule), 27 new TestDescriptor(TEST5, "1 import stmt, used only in throws clause", 0, rule), 28 }); 29 } 30 31 public void testForLoop() throws Throwable { 32 Report rpt = new Report(); 33 runTestFromString15(TEST6, rule, rpt); 34 assertEquals(0, rpt.size()); 35 } 36 37 public void testGenerics() throws Throwable { 38 Report rpt = new Report(); 39 runTestFromString15(TEST7, rule, rpt); 40 assertEquals(0, rpt.size()); 41 } 42 43 public void testAnnotations() throws Throwable { 44 Report rpt = new Report(); 45 runTestFromString15(TEST8, rule, rpt); 46 assertEquals(0, rpt.size()); 47 } 48 49 public void testAnnotations2() throws Throwable { 50 Report rpt = new Report(); 51 runTestFromString15(TEST9, rule, rpt); 52 assertEquals(0, rpt.size()); 53 } 54 55 private static final String TEST1 = 56 "import java.io.File;" + PMD.EOL + 57 "public class Foo {}"; 58 59 private static final String TEST2 = 60 "import java.io.File;" + PMD.EOL + 61 "public class Foo {" + PMD.EOL + 62 " private File file;" + PMD.EOL + 63 "}"; 64 65 private static final String TEST3 = 66 "import java.io.File;" + PMD.EOL + 67 "import java.util.List;" + PMD.EOL + 68 "public class Foo {" + PMD.EOL + 69 "}"; 70 71 private static final String TEST4 = 72 "import java.security.AccessController;" + PMD.EOL + 73 "public class Foo {" + PMD.EOL + 74 " public void foo() {" + PMD.EOL + 75 " AccessController.doPrivileged(null);" + PMD.EOL + 76 " }" + PMD.EOL + 77 "}"; 78 79 private static final String TEST5 = 80 "import java.rmi.RemoteException;" + PMD.EOL + 81 "public class Foo {" + PMD.EOL + 82 " public void foo() throws RemoteException {}" + PMD.EOL + 83 "}"; 84 85 private static final String TEST6 = 86 "import java.util.ArrayList;" + PMD.EOL + 87 "public class Foo {" + PMD.EOL + 88 " public void foo(ArrayList list) {" + PMD.EOL + 89 " for (String s : list) {}" + PMD.EOL + 90 " }" + PMD.EOL + 91 "}"; 92 93 private static final String TEST7 = 94 "import foo.TestInterfaceTwo;" + PMD.EOL + 95 "public class Foo {" + PMD.EOL + 96 " private List<TestInterfaceTwo> x = new ArrayList<TestInterfaceTwo>();" + PMD.EOL + 97 "}"; 98 99 private static final String TEST8 = 100 "import foo.annotation.Retention;" + PMD.EOL + 101 "import foo.annotation.RetentionPolicy;" + PMD.EOL + 102 "@Retention(RetentionPolicy.RUNTIME)" + PMD.EOL + 103 "public @interface Foo {" + PMD.EOL + 104 "}"; 105 106 private static final String TEST9 = 107 "import foo.FooAnnotation1;" + PMD.EOL + 108 "import foo.FooAnnotation2;" + PMD.EOL + 109 "@FooAnnotation1" + PMD.EOL + 110 "@FooAnnotation2" + PMD.EOL + 111 "public class Foo {}"; 112 113 }