pmd: plsql - added unit test for NPathComplexity and a initial PLSQL parser test

This commit is contained in:
Andreas Dangel committed 2012-12-13 18:22:43 +01:00
1 parent 2d5e59ba73
commit f88bcf144a
6 files changed
+120 -23

No files matched your search

@@ -31,29 +31,28 @@ PROCEDURE bar AS BEGIN -- this is something more complex than it needs to be,
while (f < 5 ) LOOP
anotherThing;
f := f - 27;
END LOOP
else
END LOOP;
else
tryThis();
END IF;
END LOOP
END IF;
if ( r - n > 45) THEN
while (doMagic) LOOP
findRabbits;
END LOOP;
END IF;
END IF;
END LOOP;
END IF;
if ( r - n > 45) THEN
while (doMagic) LOOP
findRabbits;
END LOOP;
END IF;
BEGIN
doSomethingDangerous();
EXCEPTION WHEN FooException THEN
makeAmends;
BEGIN
doSomethingDangerous();
EXCEPTION WHEN Exception THEN
makeAmends;
BEGIN
dontDoItAgain;
EXCEPTION
WHEN OTHERS THEN
log_problem;
END;
dontDoItAgain;
EXCEPTION
WHEN OTHERS THEN
log_problem;
END;
}
END;
END;
]]>
@@ -25,7 +25,7 @@ import net.sourceforge.pmd.lang.plsql.ast.PLSQLParserVisitor;
import net.sourceforge.pmd.lang.plsql.dfa.DataFlowFacade;
import net.sourceforge.pmd.lang.plsql.symboltable.SymbolFacade;
public abstract class ParserTst {
public abstract class AbstractPLSQLParserTst {
private class Collector<E> implements InvocationHandler {
private Class<E> clazz = null;
@@ -110,6 +110,10 @@ public abstract class ParserTst {
return (ASTInput)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
}
public ASTInput parsePLSQL(String code) {
return parsePLSQL(LanguageVersion.PLSQL, code);
}
public Node parseLanguage(LanguageVersion languageVersion, String code) {
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
return (Node)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
@@ -0,0 +1,23 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql;
import org.junit.Test;
public class PLSQLParserTest extends AbstractPLSQLParserTst {
@Test
public void testExceptions() {
parsePLSQL(
"CREATE OR REPLACE PROCEDURE bar IS BEGIN"
+ " doSomething;"
+ " EXCEPTION"
+ " WHEN FooException THEN"
+ " doSomethingElse;"
+ " WHEN OTHERS THEN"
+ " doSomethingElse;"
+ "END;");
}
}
@@ -9,7 +9,7 @@ import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.dfa.DataFlowNode;
import net.sourceforge.pmd.lang.dfa.NodeType;
import net.sourceforge.pmd.lang.plsql.ParserTst;
import net.sourceforge.pmd.lang.plsql.AbstractPLSQLParserTst;
import net.sourceforge.pmd.lang.plsql.ast.ASTExpression;
import net.sourceforge.pmd.lang.plsql.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit;
@@ -20,7 +20,7 @@ import org.junit.Test;
//import net.sourceforge.pmd.lang.plsql.ast.ASTConstructorDeclaration;
public class StatementAndBraceFinderTest extends ParserTst {
public class StatementAndBraceFinderTest extends AbstractPLSQLParserTst {
/**Java ASTStetamentExpressionequivalent is inferred as an Expression() which has
* an UnlabelledStatement as a parent.
@@ -0,0 +1,23 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule.codesize;
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import org.junit.Before;
public class CodesizeRulesTest extends SimpleAggregatorTst {
private static final String RULESET = "plsql-codesize";
@Before
public void setUp() {
addRule(RULESET, "NPathComplexity");
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(CodesizeRulesTest.class);
}
}
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
A too complex procedure which triggers NPathComplexityRule
]]></description>
<expected-problems>1</expected-problems>
<rule-property name="minimum">1.0</rule-property>
<code>
<![CDATA[
CREATE OR REPLACE
PROCEDURE bar AS BEGIN -- this is something more complex than it needs to be,
if (y) THEN -- it should be broken down into smaller methods or functions
for j IN 0 .. j-1 LOOP
if (j > r) THEN
doSomething;
while (f < 5 ) LOOP
anotherThing;
f := f - 27;
END LOOP;
else
tryThis();
END IF;
END LOOP;
END IF;
if ( r - n > 45) THEN
while (doMagic) LOOP
findRabbits;
END LOOP;
END IF;
BEGIN
doSomethingDangerous();
EXCEPTION
WHEN FooException THEN
makeAmends;
BEGIN
dontDoItAgain;
EXCEPTION
WHEN OTHERS THEN
log_problem;
END;
END;
END;
]]>
</code>
<source-type>plsql</source-type>
</test-code>
</test-data>