From 8ce1607b236bf1c5dd85ef520bb6cc2f35bee91c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Mon, 6 Oct 2014 19:19:52 +0200 Subject: [PATCH] Fix more tests like StatisticalRuleTest, SourceCodeTest MatchAlgorithmTest moved to java --- .../pmd/cpd/MatchAlgorithmTest.java | 38 +++++++++---------- .../pmd/jaxen/DocumentNavigatorTest.java | 5 ++- .../pmd/jaxen/RegexpAcceptanceTest.java | 0 .../pmd/jaxen/xml/RegexpAcceptance.xml | 0 .../sourceforge/pmd/cpd/CpddummyLanguage.java | 15 ++++++++ .../pmd/cpd/LanguageFactoryTest.java | 12 +----- .../sourceforge/pmd/cpd/SourceCodeTest.java | 20 +++++----- .../pmd/stat/MockStatisticalRule.java | 37 +++++++++++++++++- .../pmd/stat/StatisticalRuleTest.java | 29 ++++++-------- 9 files changed, 94 insertions(+), 62 deletions(-) rename {pmd => pmd-java}/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java (70%) rename {pmd => pmd-java}/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java (96%) rename {pmd => pmd-java}/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java (100%) rename {pmd => pmd-java}/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml (100%) create mode 100644 pmd/src/test/java/net/sourceforge/pmd/cpd/CpddummyLanguage.java diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java similarity index 70% rename from pmd/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java index 867dc9ea10..f82e86a0eb 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/MatchAlgorithmTest.java @@ -17,16 +17,16 @@ import org.junit.Test; public class MatchAlgorithmTest { - public static final String LINE_1 = "public class Foo { "; - public static final String LINE_2 = " public void bar() {"; - public static final String LINE_3 = " System.out.println(\"hello\");"; - public static final String LINE_4 = " System.out.println(\"hello\");"; - public static final String LINE_5 = " int i = 5"; - public static final String LINE_6 = " System.out.print(\"hello\");"; - public static final String LINE_7 = " }"; - public static final String LINE_8 = "}"; + private static final String LINE_1 = "public class Foo { "; + private static final String LINE_2 = " public void bar() {"; + private static final String LINE_3 = " System.out.println(\"hello\");"; + private static final String LINE_4 = " System.out.println(\"hello\");"; + private static final String LINE_5 = " int i = 5"; + private static final String LINE_6 = " System.out.print(\"hello\");"; + private static final String LINE_7 = " }"; + private static final String LINE_8 = "}"; - public static String getSampleCode() { + private static String getSampleCode() { return LINE_1 + PMD.EOL + LINE_2 + PMD.EOL + @@ -51,13 +51,13 @@ public class MatchAlgorithmTest { MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5); matchAlgorithm.findMatches(); - Iterator matches = matchAlgorithm.matches(); - Match match = (Match) matches.next(); + Iterator matches = matchAlgorithm.matches(); + Match match = matches.next(); assertFalse(matches.hasNext()); - Iterator marks = match.iterator(); - TokenEntry mark1 = (TokenEntry) marks.next(); - TokenEntry mark2 = (TokenEntry) marks.next(); + Iterator marks = match.iterator(); + TokenEntry mark1 = marks.next(); + TokenEntry mark2 = marks.next(); assertFalse(marks.hasNext()); assertEquals(3, mark1.getBeginLine()); @@ -80,18 +80,14 @@ public class MatchAlgorithmTest { MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5); matchAlgorithm.findMatches(); - Iterator matches = matchAlgorithm.matches(); - Match match = (Match) matches.next(); + Iterator matches = matchAlgorithm.matches(); + Match match = matches.next(); assertFalse(matches.hasNext()); - Iterator marks = match.iterator(); + Iterator marks = match.iterator(); marks.next(); marks.next(); marks.next(); assertFalse(marks.hasNext()); } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(MatchAlgorithmTest.class); - } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java similarity index 96% rename from pmd/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java index 9a5be300b6..8d0af01f0d 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/jaxen/DocumentNavigatorTest.java @@ -13,8 +13,10 @@ import java.util.List; import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; +import net.sourceforge.pmd.lang.java.JavaLanguageModule; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; @@ -76,7 +78,8 @@ public class DocumentNavigatorTest extends RuleTst { public void setUp() throws Exception { try { rule = new TestRule(); - runTestFromString(TEST, rule, new Report()); + runTestFromString(TEST, rule, new Report(), + LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion()); } catch (Throwable xx) { xx.printStackTrace(); fail(); diff --git a/pmd/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java similarity index 100% rename from pmd/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java diff --git a/pmd/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml similarity index 100% rename from pmd/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml rename to pmd-java/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/CpddummyLanguage.java b/pmd/src/test/java/net/sourceforge/pmd/cpd/CpddummyLanguage.java new file mode 100644 index 0000000000..f0a28a7667 --- /dev/null +++ b/pmd/src/test/java/net/sourceforge/pmd/cpd/CpddummyLanguage.java @@ -0,0 +1,15 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.cpd; + +/** + * Sample language for testing LanguageFactory. + * + */ +public class CpddummyLanguage extends AnyLanguage { + + public CpddummyLanguage() { + super("dummy"); + } +} diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/LanguageFactoryTest.java b/pmd/src/test/java/net/sourceforge/pmd/cpd/LanguageFactoryTest.java index c8d1e14616..eaf41a6599 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/cpd/LanguageFactoryTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/cpd/LanguageFactoryTest.java @@ -11,15 +11,7 @@ public class LanguageFactoryTest { @Test public void testSimple() { LanguageFactory f = new LanguageFactory(); - assertTrue(f.createLanguage("java") instanceof JavaLanguage); - assertTrue(f.createLanguage("cpp") instanceof CPPLanguage); - assertTrue(f.createLanguage("c") instanceof CPPLanguage); - assertTrue(f.createLanguage("php") instanceof PHPLanguage); - assertTrue(f.createLanguage("ruby") instanceof RubyLanguage); - assertTrue(f.createLanguage("plsql") instanceof PLSQLLanguage); - } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(LanguageFactoryTest.class); + assertTrue(f.createLanguage("Cpddummy") instanceof CpddummyLanguage); + assertTrue(f.createLanguage("not_existing_language") instanceof AnyLanguage); } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/SourceCodeTest.java b/pmd/src/test/java/net/sourceforge/pmd/cpd/SourceCodeTest.java index 5423b3bdaf..aed39e8963 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/cpd/SourceCodeTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/cpd/SourceCodeTest.java @@ -7,12 +7,16 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; -import net.sourceforge.pmd.PMD; - import org.junit.Test; public class SourceCodeTest { + private static final String SAMPLE_CODE = + "Line 1\n" + + "Line 2\n" + + "Line 3\n" + + "Line 4\n"; + @Test public void testSimple() throws Throwable { Tokenizer tokenizer = new AbstractTokenizer() { @@ -22,16 +26,12 @@ public class SourceCodeTest { this.ignorableStmt = new ArrayList(); } }; - SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(MatchAlgorithmTest.getSampleCode(), "Foo.java")); + SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(SAMPLE_CODE, "Foo.java")); assertEquals("Foo.java", sourceCode.getFileName()); tokenizer.tokenize(sourceCode, new Tokens()); - assertEquals(MatchAlgorithmTest.LINE_1, sourceCode.getSlice(1, 1)); - assertEquals(MatchAlgorithmTest.LINE_2, sourceCode.getSlice(2, 2)); - assertEquals(MatchAlgorithmTest.LINE_1 + PMD.EOL + MatchAlgorithmTest.LINE_2, sourceCode.getSlice(1, 2)); - } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(SourceCodeTest.class); + assertEquals("Line 1", sourceCode.getSlice(1, 1)); + assertEquals("Line 2", sourceCode.getSlice(2, 2)); + assertEquals("Line 1\nLine 2", sourceCode.getSlice(1, 2)); } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/stat/MockStatisticalRule.java b/pmd/src/test/java/net/sourceforge/pmd/stat/MockStatisticalRule.java index 52e29f4045..2841c1492f 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/stat/MockStatisticalRule.java +++ b/pmd/src/test/java/net/sourceforge/pmd/stat/MockStatisticalRule.java @@ -22,7 +22,40 @@ */ package net.sourceforge.pmd.stat; -import net.sourceforge.pmd.lang.java.rule.AbstractStatisticalJavaRule; +import java.util.List; -public class MockStatisticalRule extends AbstractStatisticalJavaRule { +import net.sourceforge.pmd.FooRule; +import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.rule.stat.StatisticalRule; +import net.sourceforge.pmd.lang.rule.stat.StatisticalRuleHelper; + +public class MockStatisticalRule extends FooRule implements StatisticalRule { + + private StatisticalRuleHelper helper; + + public MockStatisticalRule() { + helper = new StatisticalRuleHelper(this); + } + + @Override + public String getName() { + return this.getClass().getName(); + } + + @Override + public void apply(List nodes, RuleContext ctx) { + super.apply(nodes, ctx); + helper.apply(ctx); + } + + @Override + public void addDataPoint(DataPoint point) { + helper.addDataPoint(point); + } + + @Override + public Object[] getViolationParameters(DataPoint point) { + return null; + } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/stat/StatisticalRuleTest.java b/pmd/src/test/java/net/sourceforge/pmd/stat/StatisticalRuleTest.java index c2d95ed517..6be0cc7a75 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/stat/StatisticalRuleTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/stat/StatisticalRuleTest.java @@ -38,10 +38,10 @@ import junit.framework.AssertionFailedError; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.lang.DummyLanguageModule; import net.sourceforge.pmd.lang.LanguageRegistry; -import net.sourceforge.pmd.lang.java.JavaLanguageModule; -import net.sourceforge.pmd.lang.java.ast.DummyJavaNode; -import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope; +import net.sourceforge.pmd.lang.ast.DummyNode; +import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.rule.stat.StatisticalRule; import org.junit.Before; @@ -112,8 +112,7 @@ public class StatisticalRuleTest { for (int i = 0; i < POINTS; i++) { points[i] = new DataPoint(); points[i].setScore(1.0 * i); - DummyJavaNode s = new DummyJavaNode(1); - s.setScope(new SourceFileScope("foo")); + DummyNode s = new DummyNode(1); s.testingOnly__setBeginLine(i); s.testingOnly__setBeginColumn(1); points[i].setNode(s); @@ -125,8 +124,7 @@ public class StatisticalRuleTest { for (int i = POINTS - 1; i >= 0; i--) { points[i] = new DataPoint(); points[i].setScore(1.0 * i); - DummyJavaNode s = new DummyJavaNode(1); - s.setScope(new SourceFileScope("foo")); + DummyNode s = new DummyNode(1); s.testingOnly__setBeginLine(i); s.testingOnly__setBeginColumn(1); points[i].setNode(s); @@ -139,8 +137,7 @@ public class StatisticalRuleTest { for (int i = 0; i < POINTS; i++) { points[i] = new DataPoint(); points[i].setScore(1.0 * i); - DummyJavaNode s = new DummyJavaNode(1); - s.setScope(new SourceFileScope("foo")); + DummyNode s = new DummyNode(1); s.testingOnly__setBeginLine(i); s.testingOnly__setBeginColumn(1); s.testingOnly__setBeginColumn(1); @@ -165,13 +162,10 @@ public class StatisticalRuleTest { @Test public void testMetrics() throws Throwable { Report report = makeReport(IUT); - Iterator metrics = report.metrics(); + Iterator metrics = report.metrics(); assertTrue(metrics.hasNext()); - Object o = metrics.next(); - - assertTrue(o instanceof Metric); - Metric m = (Metric) o; + Metric m = metrics.next(); assertEquals("net.sourceforge.pmd.stat.MockStatisticalRule", m.getMetricName()); @@ -282,8 +276,7 @@ public class StatisticalRuleTest { DataPoint point = new DataPoint(); point.setScore(POINTS + 1.0); - DummyJavaNode s = new DummyJavaNode(1); - s.setScope(new SourceFileScope("foo")); + DummyNode s = new DummyNode(1); s.testingOnly__setBeginLine(POINTS + 1); s.testingOnly__setBeginColumn(1); point.setNode(s); @@ -856,13 +849,13 @@ public class StatisticalRuleTest { } public Report makeReport(Rule IUT) { - List list = new ArrayList(); + List list = new ArrayList(); Report report = new Report(); RuleContext ctx = new RuleContext(); ctx.setReport(report); ctx.setSourceCodeFilename(testName); - ctx.setLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion()); + ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); IUT.apply(list, ctx);