Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit tests not working with Drools 5.4.0.Final and JDK 8

Tags:

I recently started upgrading my application JDK version from jdk1.7.0_121_x64 to jdk1.8.0_202_x64. I have some legacy code using Drools 5.4.0.Final. This code is working with JDK version jdk1.7.0_121_x64 without any issue.

Maven dependencies are:

<dependency>     <groupId>org.drools</groupId>     <artifactId>drools-core</artifactId>     <version>5.4.0.Final</version> </dependency> <dependency>     <groupId>org.drools</groupId>     <artifactId>drools-decisiontables</artifactId>     <version>5.4.0.Final</version> </dependency> 

DRL files are loaded as:

final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(resource, ResourceType.DRL); knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages()); 

I was aware that there are issues using Drools with JDK 8. I referrred to other SO thread to start with.


When I built my application and executed Junit tests using JDK 8, tests failed with error:

testRunRule(com.company.app.RuleTest)  Time elapsed: 0.073 sec  <<< ERROR! java.lang.RuntimeException: java.lang.RuntimeException: wrong class format     at org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader.<init>(ClassFileReader.java:372)     at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.createNameEnvironmentAnswer(EclipseJavaCompiler.java:287)     at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:258) 

As mentioned in SO thread, I found reference to this bugfix ticket DROOLS-329.


Based on approaches mentioned in this bugfix ticket, I tried to use JANINO compiler:

Added following maven dependency:

<dependency>     <groupId>org.codehaus.janino</groupId>     <artifactId>janino</artifactId>     <version>2.5.16</version> </dependency> 

I added following VM argument (I was running tests from eclipse, so in eclipse launch configuration in JRE VM argument added the argument):

-Ddrools.dialect.java.compiler=JANINO 

I could still see the wrong class format error. So I modified my code to load DRL files as:

final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); final Properties props = new Properties(); props.setProperty("drools.dialect.java.compiler", "JANINO"); final KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(props, null); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config); kbuilder.add(resource, ResourceType.DRL); knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages()); 

It didn't help. I could still see the wrong class format error.


I followed another apprroach mentioned in this external link. I updated added/updated maven dependencies as:

<dependency>     <groupId>org.drools</groupId>     <artifactId>drools-core</artifactId>     <version>5.4.0.Final</version>     <exclusions>       <exclusion>           <groupId>org.mvel</groupId>           <artifactId>mvel2</artifactId>       </exclusion>    </exclusions> </dependency> <dependency>     <groupId>org.drools</groupId>     <artifactId>drools-decisiontables</artifactId>     <version>5.4.0.Final</version>     <exclusions>       <exclusion>           <groupId>org.eclipse.jdt.core.compiler</groupId>           <artifactId>ecj</artifactId>       </exclusion>       <exclusion>           <groupId>org.mvel</groupId>           <artifactId>mvel2</artifactId>       </exclusion>    </exclusions> </dependency> <dependency>     <groupId>org.mvel</groupId>     <artifactId>mvel2</artifactId>     <version>2.1.9.Final</version> </dependency> <dependency>    <groupId>org.eclipse.jdt.core.compiler</groupId>    <artifactId>ecj</artifactId>    <version>4.6.1</version> </dependency> 

mvel2 patch is built using: https://github.com/mkornipati/mvel/tree/2.1.9.Final.Patch

With this wrong class format error is gone. But my tests are now failing with following error:

testRunRule(com.company.app.RuleTest))  Time elapsed: 4.684 sec  <<< ERROR! java.lang.RuntimeException: org.drools.rule.InvalidRulePackage: Rule Compilation error : [Rule name='ruleCheck']     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (2:80) : Only a type can be imported. java.util.Map resolves to a package     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (2:101) : Only a type can be imported. java.util.HashMap resolves to a package     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (6:299) : org.drools.spi.KnowledgeHelper cannot be resolved to a type     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (6:339) : org.drools.template.parser.Row cannot be resolved to a type     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (6:373) : org.drools.FactHandle cannot be resolved to a type     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (6:411) : org.drools.template.parser.DefaultGenerator cannot be resolved to a type     org/drools/template/parser/Rule_ruleCheck_8eb4621227714a36b7b84c8b764527e4.java (6:487) : org.drools.runtime.rule.RuleContext cannot be resolved to a type     at org.drools.rule.Package.checkValidity(Package.java:445) 

I don't know how to proceed further. Please let me know if you are able to make Drools 5.4 work with JDK 8.

like image 492
user613114 Avatar asked Jan 09 '20 21:01

user613114


People also ask

Why your JUnit 5 Tests won’t run?

Your JUnit 5 tests wont run: Apparently there is some conflict between the JUnit Surefire provider and the JUnit support in the Surefire 2.22.0 plugin release. I ran into this little ‘ feature ‘ while developing my Testing Spring Boot – Beginner to Guru course. The solution is simple, use Maven Release 3.6.0.

Does JUnit 5 support the @ignore annotation in JUnit 4?

The junit-jupiter-migration support module provides support for the @Ignore annotation in JUnit 4 which is equivalent to Jupiter’s @Disabled annotation. However, this is still in the experimental phase. JUnit 5 vs JUnit 4 Comparison In Action We take a simple example on how to run the tests written in JUnit 4 and JUnit 5 together in a project.

What's new in the JUnit 5 Maven release?

This release includes version 2.22.0 of the Maven Surefire Plugin (unit test runner), and 2.22.0 of the Maven Failsafe (integration test runner) plugin. The 2.22.0 releases include support for JUnit. Prior to these releases, to run Junit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin.

What is the latest version of JUnit?

JUnit 5 is the latest version of JUnit, which is primarily composed of three different sub-components – JUnit Platform, JUnit Jupiter, and JUnit Vintage. Before getting to know about these components, one essential requirement about JUnit 5 is that runtime JUnit 5 requires Java version 8 (or any other higher versions).


1 Answers

I used Drools 7.7.0 with Java 8 and worked great for me. I am not sure what is the oldest Drools version that is compatible with Java 8. That said, Drools 5.4.0 was released in 2011. Java 8 was released in 2014, so you can assume it won't be compatible. Java 7 was released in July 2011. Depending on when 5.4.0 was released it might or might not be. Based on that, I expect Drools 5.4.0 to be compatible with Java 6 or earlier. If you want to use Java 8 with Drools, I don't see how you can without upgrading. I think the earliest compatible version is 6.1.0 download.jboss.org/drools/release.

In summary: Either downgrade your project to Java 6 to use Drools 5.4.0 or Upgrade Drools to use 6.1.0. Better yet, upgrade to latest Drools and then figure out what is the latest Java version it supports.

like image 103
hfontanez Avatar answered Sep 23 '22 07:09

hfontanez