Fix more tests like StatisticalRuleTest, SourceCodeTest
MatchAlgorithmTest moved to java
This commit is contained in:
@ -0,0 +1,93 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MatchAlgorithmTest {
|
||||
|
||||
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 = "}";
|
||||
|
||||
private static String getSampleCode() {
|
||||
return
|
||||
LINE_1 + PMD.EOL +
|
||||
LINE_2 + PMD.EOL +
|
||||
LINE_3 + PMD.EOL +
|
||||
LINE_4 + PMD.EOL +
|
||||
LINE_5 + PMD.EOL +
|
||||
LINE_6 + PMD.EOL +
|
||||
LINE_7 + PMD.EOL +
|
||||
LINE_8;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimple() throws Throwable {
|
||||
JavaTokenizer tokenizer = new JavaTokenizer();
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
|
||||
Tokens tokens = new Tokens();
|
||||
TokenEntry.clearImages();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
assertEquals(41, tokens.size());
|
||||
Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
|
||||
codeMap.put("Foo.java", sourceCode);
|
||||
|
||||
MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
|
||||
matchAlgorithm.findMatches();
|
||||
Iterator<Match> matches = matchAlgorithm.matches();
|
||||
Match match = matches.next();
|
||||
assertFalse(matches.hasNext());
|
||||
|
||||
Iterator<TokenEntry> marks = match.iterator();
|
||||
TokenEntry mark1 = marks.next();
|
||||
TokenEntry mark2 = marks.next();
|
||||
assertFalse(marks.hasNext());
|
||||
|
||||
assertEquals(3, mark1.getBeginLine());
|
||||
assertEquals(4, mark2.getBeginLine());
|
||||
assertTrue("Foo.java" == mark1.getTokenSrcID() && "Foo.java" == mark2.getTokenSrcID());
|
||||
assertEquals(LINE_3, match.getSourceCodeSlice());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnore() throws Throwable {
|
||||
JavaTokenizer tokenizer = new JavaTokenizer();
|
||||
tokenizer.setIgnoreLiterals(true);
|
||||
tokenizer.setIgnoreIdentifiers(true);
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(), "Foo.java"));
|
||||
Tokens tokens = new Tokens();
|
||||
TokenEntry.clearImages();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
|
||||
codeMap.put("Foo.java", sourceCode);
|
||||
|
||||
MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
|
||||
matchAlgorithm.findMatches();
|
||||
Iterator<Match> matches = matchAlgorithm.matches();
|
||||
Match match = matches.next();
|
||||
assertFalse(matches.hasNext());
|
||||
|
||||
Iterator<TokenEntry> marks = match.iterator();
|
||||
marks.next();
|
||||
marks.next();
|
||||
marks.next();
|
||||
assertFalse(marks.hasNext());
|
||||
}
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.jaxen;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Iterator;
|
||||
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;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
|
||||
import org.jaxen.BaseXPath;
|
||||
import org.jaxen.JaxenException;
|
||||
import org.jaxen.UnsupportedAxisException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class DocumentNavigatorTest extends RuleTst {
|
||||
|
||||
|
||||
private TestRule rule;
|
||||
|
||||
private class TestRule extends AbstractJavaRule {
|
||||
|
||||
private Node compilationUnit;
|
||||
private Node importDeclaration;
|
||||
private Node statement;
|
||||
private Node primaryPrefix;
|
||||
private Node primaryExpression;
|
||||
|
||||
/**
|
||||
* @see net.sourceforge.pmd.lang.java.ast.JavaParserVisitor#visit(ASTCompilationUnit, Object)
|
||||
*/
|
||||
public Object visit(ASTCompilationUnit node, Object data) {
|
||||
this.compilationUnit = node;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
public Object visit(ASTImportDeclaration node, Object data) {
|
||||
this.importDeclaration = node;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
public Object visit(ASTStatement node, Object data) {
|
||||
this.statement = node;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
public Object visit(ASTPrimaryPrefix node, Object data) {
|
||||
this.primaryPrefix = node;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
public Object visit(ASTPrimaryExpression node, Object data) {
|
||||
this.primaryExpression = node;
|
||||
return super.visit(node, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
try {
|
||||
rule = new TestRule();
|
||||
runTestFromString(TEST, rule, new Report(),
|
||||
LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion());
|
||||
} catch (Throwable xx) {
|
||||
xx.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildAxisIterator() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getChildAxisIterator(rule.compilationUnit);
|
||||
assertSame(rule.compilationUnit.jjtGetChild(0), iter.next());
|
||||
assertSame(rule.compilationUnit.jjtGetChild(1), iter.next());
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentAxisIterator() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getParentAxisIterator(rule.importDeclaration);
|
||||
assertSame(rule.importDeclaration.jjtGetParent(), iter.next());
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentAxisIterator2() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getParentAxisIterator(rule.compilationUnit);
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescendantAxisIterator() throws UnsupportedAxisException {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getDescendantAxisIterator(rule.statement);
|
||||
Node statementExpression = rule.statement.jjtGetChild(0);
|
||||
assertSame(statementExpression, iter.next());
|
||||
Node primaryExpression = statementExpression.jjtGetChild(0);
|
||||
assertSame(primaryExpression, iter.next());
|
||||
Node primaryPrefix = primaryExpression.jjtGetChild(0);
|
||||
assertSame(primaryPrefix, iter.next());
|
||||
Node primarySuffix = primaryExpression.jjtGetChild(1);
|
||||
// assertSame(primarySuffix, iter.next());
|
||||
Node name = primaryPrefix.jjtGetChild(0);
|
||||
// assertSame(name, iter.next());
|
||||
Node arguments = primarySuffix.jjtGetChild(0);
|
||||
// assertSame(arguments, iter.next());
|
||||
// assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescendantAxisIterator2() throws UnsupportedAxisException {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getDescendantAxisIterator(rule.primaryPrefix);
|
||||
Node name = rule.primaryPrefix.jjtGetChild(0);
|
||||
assertSame(name, iter.next());
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFollowingSiblingAxisIterator() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getFollowingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(0));
|
||||
assertSame(rule.primaryExpression.jjtGetChild(1), iter.next());
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFollowingSiblingAxisIterator2() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getFollowingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(1));
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrecedingSiblingAxisIterator() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getPrecedingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(1));
|
||||
assertSame(rule.primaryExpression.jjtGetChild(0), iter.next());
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrecedingSiblingAxisIterator2() {
|
||||
DocumentNavigator nav = new DocumentNavigator();
|
||||
Iterator iter = nav.getPrecedingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(0));
|
||||
assertFalse(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXPath() throws JaxenException {
|
||||
BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
|
||||
List matches = xPath.selectNodes(rule.statement);
|
||||
assertEquals(6, matches.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXPath2() throws JaxenException {
|
||||
BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
|
||||
List matches = xPath.selectNodes(rule.importDeclaration);
|
||||
assertEquals(1, matches.size());
|
||||
}
|
||||
|
||||
|
||||
public static final String TEST =
|
||||
"import java.io.*;" + PMD.EOL +
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" public Foo() {" + PMD.EOL +
|
||||
" try {" + PMD.EOL +
|
||||
" FileReader fr = new FileReader(\"/dev/null\");" + PMD.EOL +
|
||||
" } catch (Exception e) {}" + PMD.EOL +
|
||||
" try {" + PMD.EOL +
|
||||
" FileReader fr = new FileReader(\"/dev/null\");" + PMD.EOL +
|
||||
" } catch (Exception e) {" + PMD.EOL +
|
||||
" e.printStackTrace();" + PMD.EOL +
|
||||
" // this shouldn't show up on the report" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
public static junit.framework.Test suite() {
|
||||
return new junit.framework.JUnit4TestAdapter(DocumentNavigatorTest.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.jaxen;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
||||
import net.sourceforge.pmd.lang.rule.XPathRule;
|
||||
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
|
||||
import net.sourceforge.pmd.testframework.TestDescriptor;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegexpAcceptanceTest extends SimpleAggregatorTst {
|
||||
|
||||
private static final String xPath = "//ClassOrInterfaceDeclaration[matches(@Image, 'F?o')]";
|
||||
|
||||
@Test
|
||||
public void testSimple() throws Throwable {
|
||||
Rule r = new XPathRule(xPath);
|
||||
r.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
|
||||
r.setMessage("");
|
||||
TestDescriptor[] testDescriptors = extractTestsFromXml(r, "RegexpAcceptance");
|
||||
for (TestDescriptor testDescriptor : testDescriptors) {
|
||||
testDescriptor.setReinitializeRule(false);
|
||||
}
|
||||
runTests(testDescriptors);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
XPath should match Foo
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
XPath should not match Bar
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Bar {}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
XPath should match Flo
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Flo {}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
Reference in New Issue
Block a user