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.rules.UnusedImportsRule; 8 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 9 import test.net.sourceforge.pmd.testframework.TestDescriptor; 10 11 public class UnusedImportsRuleTest extends SimpleAggregatorTst { 12 13 private UnusedImportsRule rule; 14 15 public void setUp() { 16 rule = new UnusedImportsRule(); 17 rule.setMessage("Avoid this stuff -> ''{0}''"); 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 private static final String TEST1 = 31 "import java.io.File;" + PMD.EOL + 32 "public class Foo {}"; 33 34 private static final String TEST2 = 35 "import java.io.File;" + PMD.EOL + 36 "public class Foo {" + PMD.EOL + 37 " private File file;" + PMD.EOL + 38 "}"; 39 40 private static final String TEST3 = 41 "import java.io.File;" + PMD.EOL + 42 "import java.util.List;" + PMD.EOL + 43 "public class Foo {" + PMD.EOL + 44 "}"; 45 46 private static final String TEST4 = 47 "import java.security.AccessController;" + PMD.EOL + 48 "public class Foo {" + PMD.EOL + 49 " public void foo() {" + PMD.EOL + 50 " AccessController.doPrivileged(null);" + PMD.EOL + 51 " }" + PMD.EOL + 52 "}"; 53 54 private static final String TEST5 = 55 "import java.rmi.RemoteException;" + PMD.EOL + 56 "public class Foo {" + PMD.EOL + 57 " public void foo() throws RemoteException {}" + PMD.EOL + 58 "}"; 59 60 61 }