1 package test.net.sourceforge.pmd; 2 3 import net.sourceforge.pmd.AbstractRule; 4 import net.sourceforge.pmd.PMD; 5 import net.sourceforge.pmd.Report; 6 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; 7 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; 8 import test.net.sourceforge.pmd.testframework.RuleTst; 9 10 public class SuppressWarningsTest extends RuleTst { 11 12 private static class FooRule extends AbstractRule { 13 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) { 14 if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx, c); 15 return super.visit(c, ctx); 16 } 17 18 public Object visit(ASTVariableDeclaratorId c, Object ctx) { 19 if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx, c); 20 return super.visit(c, ctx); 21 } 22 } 23 24 public void testClassLevelSuppression() throws Throwable { 25 Report rpt = new Report(); 26 runTestFromString15(TEST1, new FooRule(), rpt); 27 assertEquals(0, rpt.size()); 28 runTestFromString15(TEST2, new FooRule(), rpt); 29 assertEquals(0, rpt.size()); 30 } 31 32 public void testInheritedSuppression() throws Throwable { 33 Report rpt = new Report(); 34 runTestFromString15(TEST3, new FooRule(), rpt); 35 assertEquals(0, rpt.size()); 36 } 37 38 public void testMethodLevelSuppression() throws Throwable { 39 Report rpt = new Report(); 40 runTestFromString15(TEST4, new FooRule(), rpt); 41 assertEquals(1, rpt.size()); 42 } 43 44 public void testConstructorLevelSuppression() throws Throwable { 45 Report rpt = new Report(); 46 runTestFromString15(TEST5, new FooRule(), rpt); 47 assertEquals(0, rpt.size()); 48 } 49 50 public void testFieldLevelSuppression() throws Throwable { 51 Report rpt = new Report(); 52 runTestFromString15(TEST6, new FooRule(), rpt); 53 assertEquals(1, rpt.size()); 54 } 55 56 public void testParameterLevelSuppression() throws Throwable { 57 Report rpt = new Report(); 58 runTestFromString15(TEST7, new FooRule(), rpt); 59 assertEquals(1, rpt.size()); 60 } 61 62 public void testLocalVariableLevelSuppression() throws Throwable { 63 Report rpt = new Report(); 64 runTestFromString15(TEST8, new FooRule(), rpt); 65 assertEquals(1, rpt.size()); 66 } 67 68 private static final String TEST1 = 69 "@SuppressWarnings(\"\")" + PMD.EOL + 70 "public class Foo {}"; 71 72 private static final String TEST2 = 73 "@SuppressWarnings(\"\")" + PMD.EOL + 74 "public class Foo {" + PMD.EOL + 75 " void bar() {" + PMD.EOL + 76 " int foo;" + PMD.EOL + 77 " }" + PMD.EOL + 78 "}"; 79 80 private static final String TEST3 = 81 "public class Baz {" + PMD.EOL + 82 " @SuppressWarnings(\"\")" + PMD.EOL + 83 " public class Bar {" + PMD.EOL + 84 " void bar() {" + PMD.EOL + 85 " int foo;" + PMD.EOL + 86 " }" + PMD.EOL + 87 " }" + PMD.EOL + 88 "}"; 89 90 private static final String TEST4 = 91 "public class Foo {" + PMD.EOL + 92 " @SuppressWarnings(\"\")" + PMD.EOL + 93 " void bar() {" + PMD.EOL + 94 " int foo;" + PMD.EOL + 95 " }" + PMD.EOL + 96 "}"; 97 98 private static final String TEST5 = 99 "public class Bar {" + PMD.EOL + 100 " @SuppressWarnings(\"\")" + PMD.EOL + 101 " public Bar() {" + PMD.EOL + 102 " int foo;" + PMD.EOL + 103 " }" + PMD.EOL + 104 "}"; 105 106 private static final String TEST6 = 107 "public class Bar {" + PMD.EOL + 108 " @SuppressWarnings(\"\")" + PMD.EOL + 109 " int foo;" + PMD.EOL + 110 " void bar() {" + PMD.EOL + 111 " int foo;" + PMD.EOL + 112 " }" + PMD.EOL + 113 "}"; 114 115 private static final String TEST7 = 116 "public class Bar {" + PMD.EOL + 117 " int foo;" + PMD.EOL + 118 " void bar(@SuppressWarnings(\"\") int foo) {}" + PMD.EOL + 119 "}"; 120 121 private static final String TEST8 = 122 "public class Bar {" + PMD.EOL + 123 " int foo;" + PMD.EOL + 124 " void bar() {" + PMD.EOL + 125 " @SuppressWarnings(\"\") int foo;" + PMD.EOL + 126 " }" + PMD.EOL + 127 "}"; 128 129 }