Fix more tests like StatisticalRuleTest, SourceCodeTest
MatchAlgorithmTest moved to java
This commit is contained in:
@ -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<Match> 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<TokenEntry> 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<Match> matches = matchAlgorithm.matches();
|
||||
Match match = matches.next();
|
||||
assertFalse(matches.hasNext());
|
||||
|
||||
Iterator marks = match.iterator();
|
||||
Iterator<TokenEntry> 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);
|
||||
}
|
||||
}
|
@ -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();
|
@ -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");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<String>();
|
||||
}
|
||||
};
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -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<? extends Node> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Metric> 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<Node> list = new ArrayList<Node>();
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user