More fixes - some tests simply moved to java module
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import junit.framework.JUnit4TestAdapter;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
import net.sourceforge.pmd.testframework.TestDescriptor;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ExcludeLinesTest extends RuleTst {
|
||||
private Rule rule;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
rule = findRule("java-unusedcode", "UnusedLocalVariable");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptance() {
|
||||
runTest(new TestDescriptor(TEST1, "NOPMD should work", 0, rule));
|
||||
runTest(new TestDescriptor(TEST2, "Should fail without exclude marker", 1, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlternateMarker() throws Throwable {
|
||||
PMD p = new PMD();
|
||||
p.getConfiguration().setSuppressMarker("FOOBAR");
|
||||
RuleContext ctx = new RuleContext();
|
||||
Report r = new Report();
|
||||
ctx.setReport(r);
|
||||
ctx.setSourceCodeFilename("n/a");
|
||||
ctx.setLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion());
|
||||
RuleSet rules = new RuleSet();
|
||||
rules.addRule(rule);
|
||||
p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx);
|
||||
assertTrue(r.isEmpty());
|
||||
assertEquals(r.getSuppressedRuleViolations().size(), 1);
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" void foo() {" + PMD.EOL +
|
||||
" int x; //NOPMD " + PMD.EOL +
|
||||
" } " + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST2 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" void foo() {" + PMD.EOL +
|
||||
" int x;" + PMD.EOL +
|
||||
" } " + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST3 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" void foo() {" + PMD.EOL +
|
||||
" int x; // FOOBAR" + PMD.EOL +
|
||||
" } " + PMD.EOL +
|
||||
"}";
|
||||
|
||||
public static junit.framework.Test suite() {
|
||||
return new JUnit4TestAdapter(ExcludeLinesTest.class);
|
||||
}
|
||||
}
|
36
pmd-java/src/test/java/net/sourceforge/pmd/FooRule.java
Normal file
36
pmd-java/src/test/java/net/sourceforge/pmd/FooRule.java
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
|
||||
public class FooRule extends AbstractJavaRule {
|
||||
|
||||
public FooRule() {
|
||||
setMessage("No Foo allowed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
|
||||
if (c.getImage().equalsIgnoreCase("Foo")) {
|
||||
addViolation(ctx, c);
|
||||
}
|
||||
return super.visit(c, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTVariableDeclaratorId c, Object ctx) {
|
||||
if (c.getImage().equalsIgnoreCase("Foo")) {
|
||||
addViolation(ctx, c);
|
||||
}
|
||||
return super.visit(c, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "NoFoo";
|
||||
}
|
||||
}
|
84
pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java
Normal file
84
pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java
Normal file
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ReportTest extends RuleTst {
|
||||
|
||||
private LanguageVersion defaultLanguage = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
|
||||
|
||||
@Test
|
||||
public void testBasic() throws Throwable {
|
||||
Report r = new Report();
|
||||
runTestFromString(TEST1, new FooRule(), r, defaultLanguage);
|
||||
assertFalse(r.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionsInReportWithRuleViolationSuppressRegex() throws Throwable {
|
||||
Report rpt = new Report();
|
||||
Rule rule = new FooRule();
|
||||
rule.setProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR, ".*No Foo.*");
|
||||
runTestFromString(TEST1, rule, rpt, defaultLanguage);
|
||||
assertTrue(rpt.isEmpty());
|
||||
assertEquals(1, rpt.getSuppressedRuleViolations().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionsInReportWithRuleViolationSuppressXPath() throws Throwable {
|
||||
Report rpt = new Report();
|
||||
Rule rule = new FooRule();
|
||||
rule.setProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR, ".[@Image = 'Foo']");
|
||||
runTestFromString(TEST1, rule, rpt, defaultLanguage);
|
||||
assertTrue(rpt.isEmpty());
|
||||
assertEquals(1, rpt.getSuppressedRuleViolations().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionsInReportWithAnnotations() throws Throwable {
|
||||
Report rpt = new Report();
|
||||
runTestFromString(TEST2, new FooRule(), rpt, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"));
|
||||
assertTrue(rpt.isEmpty());
|
||||
assertEquals(1, rpt.getSuppressedRuleViolations().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionsInReportWithAnnotationsFullName() throws Throwable {
|
||||
Report rpt = new Report();
|
||||
runTestFromString(TEST2_FULL, new FooRule(), rpt, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"));
|
||||
assertTrue(rpt.isEmpty());
|
||||
assertEquals(1, rpt.getSuppressedRuleViolations().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusionsInReportWithNOPMD() throws Throwable {
|
||||
Report rpt = new Report();
|
||||
runTestFromString(TEST3, new FooRule(), rpt, defaultLanguage);
|
||||
assertTrue(rpt.isEmpty());
|
||||
assertEquals(1, rpt.getSuppressedRuleViolations().size());
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
"public class Foo {}" + PMD.EOL;
|
||||
|
||||
private static final String TEST2 =
|
||||
"@SuppressWarnings(\"PMD\")" + PMD.EOL +
|
||||
"public class Foo {}";
|
||||
private static final String TEST2_FULL =
|
||||
"@java.lang.SuppressWarnings(\"PMD\")" + PMD.EOL +
|
||||
"public class Foo {}";
|
||||
|
||||
private static final String TEST3 =
|
||||
"public class Foo {} // NOPMD";
|
||||
}
|
@ -5,12 +5,12 @@ package net.sourceforge.pmd.lang.java;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import junit.framework.JUnit4TestAdapter;
|
||||
import net.sourceforge.pmd.FooRule;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
|
||||
@ -19,29 +19,6 @@ import org.junit.Test;
|
||||
|
||||
public class SuppressWarningsTest extends RuleTst {
|
||||
|
||||
private static class FooRule extends AbstractJavaRule {
|
||||
@Override
|
||||
public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
|
||||
if (c.getImage().equalsIgnoreCase("Foo")) {
|
||||
addViolation(ctx, c);
|
||||
}
|
||||
return super.visit(c, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTVariableDeclaratorId c, Object ctx) {
|
||||
if (c.getImage().equalsIgnoreCase("Foo")) {
|
||||
addViolation(ctx, c);
|
||||
}
|
||||
return super.visit(c, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "NoFoo";
|
||||
}
|
||||
}
|
||||
|
||||
private static class BarRule extends AbstractJavaRule {
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit cu, Object ctx) {
|
||||
|
Reference in New Issue
Block a user