From a44b75cbcc3e23a9cfe289b8c7d5d01c30e7d5a7 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Wed, 27 Apr 2022 16:40:16 +0900 Subject: [PATCH] - Added DRLParserWrapper to store errors using DRLErrorListener (#5) - Added MiscDRLParserTest which is being ported from RuleParserTest to enhance coverage. --- drools-drl/drools-drl10-parser/pom.xml | 6 +++ .../org/drools/parser/DRLErrorListener.java | 28 ++++++++++ .../org/drools/parser/DRLParserError.java | 54 +++++++++++++++++++ .../org/drools/parser/DRLParserWrapper.java | 45 ++++++++++++++++ .../org/drools/parser/MiscDRLParserTest.java | 51 ++++++++++++++++++ .../src/test/resources/logback-test.xml | 16 ++++++ 6 files changed, 200 insertions(+) create mode 100644 drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLErrorListener.java create mode 100644 drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserError.java create mode 100644 drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserWrapper.java create mode 100644 drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java create mode 100644 drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml diff --git a/drools-drl/drools-drl10-parser/pom.xml b/drools-drl/drools-drl10-parser/pom.xml index 2b88baa153b..599ec8ab07f 100644 --- a/drools-drl/drools-drl10-parser/pom.xml +++ b/drools-drl/drools-drl10-parser/pom.xml @@ -47,6 +47,12 @@ ${version.junit} test + + ch.qos.logback + logback-classic + ${version.ch.qos.logback} + test + diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLErrorListener.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLErrorListener.java new file mode 100644 index 00000000000..a15d12adddb --- /dev/null +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLErrorListener.java @@ -0,0 +1,28 @@ +package org.drools.parser; + +import java.util.ArrayList; +import java.util.List; + +import org.antlr.v4.runtime.BaseErrorListener; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; + +public class DRLErrorListener extends BaseErrorListener { + + private final List errors = new ArrayList<>(); + + public List getErrors() { + return errors; + } + + @Override + public void syntaxError(Recognizer recognizer, + Object offendingSymbol, + int line, + int charPositionInLine, + String msg, + RecognitionException e) { + + errors.add(new DRLParserError(line, charPositionInLine, msg)); + } +} diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserError.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserError.java new file mode 100644 index 00000000000..6b9fe116943 --- /dev/null +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserError.java @@ -0,0 +1,54 @@ +package org.drools.parser; + +public class DRLParserError { + + private int lineNumber; + private int column; + private String message; + + private Exception exception; + + public DRLParserError(int lineNumber, int column, String message) { + this.lineNumber = lineNumber; + this.column = column; + this.message = message; + } + + public DRLParserError(Exception exception) { + this.exception = exception; + } + + public int getLineNumber() { + return lineNumber; + } + + public void setLineNumber(int lineNumber) { + this.lineNumber = lineNumber; + } + + public int getColumn() { + return column; + } + + public void setColumn(int column) { + this.column = column; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "DRLParserError{" + + "lineNumber=" + lineNumber + + ", column=" + column + + ", message='" + message + '\'' + + ", exception=" + exception + + '}'; + } +} diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserWrapper.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserWrapper.java new file mode 100644 index 00000000000..4b948cffca5 --- /dev/null +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserWrapper.java @@ -0,0 +1,45 @@ +package org.drools.parser; + +import java.util.ArrayList; +import java.util.List; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.drools.drl.ast.descr.PackageDescr; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DRLParserWrapper { + + private static final Logger LOGGER = LoggerFactory.getLogger(DRLParserWrapper.class); + + private final List errors = new ArrayList<>(); + + public DRLParserWrapper() { + } + + public PackageDescr parse(String drl) { + DRLParser drlParser = DRLParserHelper.createDrlParser(drl); + DRLErrorListener errorListener = new DRLErrorListener(); + drlParser.addErrorListener(errorListener); + + ParseTree parseTree = drlParser.compilationUnit(); + + errors.addAll(errorListener.getErrors()); + + try { + return DRLParserHelper.parseTree2PackageDescr(parseTree); + } catch (Exception e) { + LOGGER.error("Exception while creating PackageDescr", e); + errors.add(new DRLParserError(e)); + return null; + } + } + + public List getErrors() { + return errors; + } + + public boolean hasErrors() { + return !errors.isEmpty(); + } +} diff --git a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java new file mode 100644 index 00000000000..9a6b31f2b7f --- /dev/null +++ b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java @@ -0,0 +1,51 @@ +package org.drools.parser; + +import junit.framework.TestCase; +import org.drools.drl.ast.descr.PackageDescr; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/* + * This test class is being ported from org.drools.mvel.compiler.lang.RuleParserTest + */ +public class MiscDRLParserTest extends TestCase { + + private DRLParserWrapper parser; + + @Before + protected void setUp() throws Exception { + super.setUp(); + parser = new DRLParserWrapper(); + } + + @After + protected void tearDown() throws Exception { + super.tearDown(); + } + + @Test + public void testPackage() throws Exception { + final String source = "package foo.bar.baz"; + final PackageDescr pkg = parser.parse(source); + assertEquals("foo.bar.baz", pkg.getName()); + } + + @Test + public void testPackageWithErrorNode() throws Exception { + final String source = "package 12 foo.bar.baz"; + final PackageDescr pkg = parser.parse(source); + assertTrue(parser.hasErrors()); + // getText() combines an ErrorNode "12" so the result is different from DRL6Parser. + assertEquals("12foo.bar.baz", pkg.getName()); + } + + @Test + public void testPackageWithAllErrorNode() throws Exception { + final String source = "package 12 12312 231"; + final PackageDescr pkg = parser.parse(source); + assertTrue(parser.hasErrors()); + // NPE inside DRLVisitorImpl.visitIdentifier(). So pkg is null. Different from DRL6Parser. + assertNull(pkg); + } +} diff --git a/drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml b/drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..7740901e7d3 --- /dev/null +++ b/drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml @@ -0,0 +1,16 @@ + + + + + + %date{HH:mm:ss.SSS} [%thread] %-5level %class{36}.%method:%line - %msg%n + + + + + + + + + + \ No newline at end of file