001: package test.net.sourceforge.pmd;
002:
003: import static org.junit.Assert.assertEquals;
004: import net.sourceforge.pmd.AbstractRule;
005: import net.sourceforge.pmd.PMD;
006: import net.sourceforge.pmd.Report;
007: import net.sourceforge.pmd.SourceType;
008: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
009: import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
010:
011: import org.junit.Test;
012:
013: import test.net.sourceforge.pmd.testframework.RuleTst;
014: import junit.framework.JUnit4TestAdapter;
015:
016: public class SuppressWarningsTest extends RuleTst {
017:
018: private static class FooRule extends AbstractRule {
019: public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
020: if (c.getImage().equalsIgnoreCase("Foo"))
021: addViolation(ctx, c);
022: return super .visit(c, ctx);
023: }
024:
025: public Object visit(ASTVariableDeclaratorId c, Object ctx) {
026: if (c.getImage().equalsIgnoreCase("Foo"))
027: addViolation(ctx, c);
028: return super .visit(c, ctx);
029: }
030:
031: public String getName() {
032: return "NoFoo";
033: }
034: }
035:
036: @Test
037: public void testClassLevelSuppression() throws Throwable {
038: Report rpt = new Report();
039: runTestFromString(TEST1, new FooRule(), rpt, SourceType.JAVA_15);
040: assertEquals(0, rpt.size());
041: runTestFromString(TEST2, new FooRule(), rpt, SourceType.JAVA_15);
042: assertEquals(0, rpt.size());
043: }
044:
045: @Test
046: public void testInheritedSuppression() throws Throwable {
047: Report rpt = new Report();
048: runTestFromString(TEST3, new FooRule(), rpt, SourceType.JAVA_15);
049: assertEquals(0, rpt.size());
050: }
051:
052: @Test
053: public void testMethodLevelSuppression() throws Throwable {
054: Report rpt = new Report();
055: runTestFromString(TEST4, new FooRule(), rpt, SourceType.JAVA_15);
056: assertEquals(1, rpt.size());
057: }
058:
059: @Test
060: public void testConstructorLevelSuppression() throws Throwable {
061: Report rpt = new Report();
062: runTestFromString(TEST5, new FooRule(), rpt, SourceType.JAVA_15);
063: assertEquals(0, rpt.size());
064: }
065:
066: @Test
067: public void testFieldLevelSuppression() throws Throwable {
068: Report rpt = new Report();
069: runTestFromString(TEST6, new FooRule(), rpt, SourceType.JAVA_15);
070: assertEquals(1, rpt.size());
071: }
072:
073: @Test
074: public void testParameterLevelSuppression() throws Throwable {
075: Report rpt = new Report();
076: runTestFromString(TEST7, new FooRule(), rpt, SourceType.JAVA_15);
077: assertEquals(1, rpt.size());
078: }
079:
080: @Test
081: public void testLocalVariableLevelSuppression() throws Throwable {
082: Report rpt = new Report();
083: runTestFromString(TEST8, new FooRule(), rpt, SourceType.JAVA_15);
084: assertEquals(1, rpt.size());
085: }
086:
087: @Test
088: public void testSpecificSuppression() throws Throwable {
089: Report rpt = new Report();
090: runTestFromString(TEST9, new FooRule(), rpt, SourceType.JAVA_15);
091: assertEquals(1, rpt.size());
092: }
093:
094: @Test
095: public void testNoSuppressionBlank() throws Throwable {
096: Report rpt = new Report();
097: runTestFromString(TEST10, new FooRule(), rpt,
098: SourceType.JAVA_15);
099: assertEquals(2, rpt.size());
100: }
101:
102: @Test
103: public void testNoSuppressionSomethingElseS() throws Throwable {
104: Report rpt = new Report();
105: runTestFromString(TEST11, new FooRule(), rpt,
106: SourceType.JAVA_15);
107: assertEquals(2, rpt.size());
108: }
109:
110: private static final String TEST1 = "@SuppressWarnings(\"PMD\")"
111: + PMD.EOL + "public class Foo {}";
112:
113: private static final String TEST2 = "@SuppressWarnings(\"PMD\")"
114: + PMD.EOL + "public class Foo {" + PMD.EOL
115: + " void bar() {" + PMD.EOL + " int foo;" + PMD.EOL + " }"
116: + PMD.EOL + "}";
117:
118: private static final String TEST3 = "public class Baz {" + PMD.EOL
119: + " @SuppressWarnings(\"PMD\")" + PMD.EOL
120: + " public class Bar {" + PMD.EOL + " void bar() {"
121: + PMD.EOL + " int foo;" + PMD.EOL + " }" + PMD.EOL
122: + " }" + PMD.EOL + "}";
123:
124: private static final String TEST4 = "public class Foo {" + PMD.EOL
125: + " @SuppressWarnings(\"PMD\")" + PMD.EOL + " void bar() {"
126: + PMD.EOL + " int foo;" + PMD.EOL + " }" + PMD.EOL + "}";
127:
128: private static final String TEST5 = "public class Bar {" + PMD.EOL
129: + " @SuppressWarnings(\"PMD\")" + PMD.EOL
130: + " public Bar() {" + PMD.EOL + " int foo;" + PMD.EOL
131: + " }" + PMD.EOL + "}";
132:
133: private static final String TEST6 = "public class Bar {" + PMD.EOL
134: + " @SuppressWarnings(\"PMD\")" + PMD.EOL + " int foo;"
135: + PMD.EOL + " void bar() {" + PMD.EOL + " int foo;"
136: + PMD.EOL + " }" + PMD.EOL + "}";
137:
138: private static final String TEST7 = "public class Bar {" + PMD.EOL
139: + " int foo;" + PMD.EOL
140: + " void bar(@SuppressWarnings(\"PMD\") int foo) {}"
141: + PMD.EOL + "}";
142:
143: private static final String TEST8 = "public class Bar {" + PMD.EOL
144: + " int foo;" + PMD.EOL + " void bar() {" + PMD.EOL
145: + " @SuppressWarnings(\"PMD\") int foo;" + PMD.EOL + " }"
146: + PMD.EOL + "}";
147:
148: private static final String TEST9 = "public class Bar {" + PMD.EOL
149: + " int foo;" + PMD.EOL + " void bar() {" + PMD.EOL
150: + " @SuppressWarnings(\"PMD.NoFoo\") int foo;" + PMD.EOL
151: + " }" + PMD.EOL + "}";
152:
153: private static final String TEST10 = "public class Bar {" + PMD.EOL
154: + " int foo;" + PMD.EOL + " void bar() {" + PMD.EOL
155: + " @SuppressWarnings(\"\") int foo;" + PMD.EOL + " }"
156: + PMD.EOL + "}";
157:
158: private static final String TEST11 = "public class Bar {" + PMD.EOL
159: + " int foo;" + PMD.EOL + " void bar() {" + PMD.EOL
160: + " @SuppressWarnings(\"SomethingElse\") int foo;"
161: + PMD.EOL + " }" + PMD.EOL + "}";
162:
163: public static junit.framework.Test suite() {
164: return new JUnit4TestAdapter(SuppressWarningsTest.class);
165: }
166: }
|