Hi Edson,
thank you very much for testing this! Since I could not discern any
differences between your approach and mine, I decided to resolve the
issue manually by inserting a work-around in my test routine which
sets the context class loader for the current thread before calling
the method under test.
if (Thread.currentThread().getContextClassLoader() == null) {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
}
My guess is that the custom JUnit 4 class runner
(SpringJUnit4ClassRunner) somehow is responsible for the faulty
behaviour.
Thanks again
Marcus
On Mon, May 19, 2008 at 3:09 PM, Edson Tirelli <tirelli(a)post.com> wrote:
All,
I see that the code in that spot is weak and could be improved. Although,
using drools 4.0.7, I successfully created a junit 4 test that I paste
bellow. Also, the whole drools code is tested with junit 3 and we have no
problems. For instance, look at our integration tests here:
http://anonsvn.labs.jboss.com/labs/jbossrules/branches/4.0.x/drools-compi...
The code I used to create the junit 4 test is based on the default
example create by the drools plugin + the snippet you showed bellow:
@Test
public void testRuleBase() {
try {
//load up the rulebase
RuleBase ruleBase = readRule();
StatefulSession workingMemory = ruleBase.newStatefulSession();
//go !
Message message = new Message();
message.setMessage( "Hello World" );
message.setStatus( Message.HELLO );
workingMemory.insert( message );
workingMemory.fireAllRules();
workingMemory.dispose();
} catch (Throwable t) {
t.printStackTrace();
Assert.fail("Something is wrong: "+t.getMessage());
}
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private RuleBase readRule() throws Exception {
//read in the source
Reader source = new InputStreamReader(
DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );
PackageBuilderConfiguration conf = new
PackageBuilderConfiguration();
JavaDialectConfiguration javaConf = (JavaDialectConfiguration)
conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaDialectConfiguration.ECLIPSE);
javaConf.setJavaLanguageLevel("1.5");
PackageBuilder builder = new PackageBuilder( conf );
builder.addPackageFromDrl( source );
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage() );
return ruleBase;
}
Hope it helps,
Edson
2008/5/19 Marcus Ilgner <marcus.ilgner(a)gmail.com>:
>
> Hi list,
>
> I'm reviving this thread because I happen to have the same problem.
>
> On Sun, Mar 23, 2008 at 7:34 PM, Mike Dean <mdean77(a)comcast.net> wrote:
> >
> > Thanks (months later!). I have cut down my code example below to show
> > this
> > issue more clearly:
> >
> >
> > John J. Franey wrote:
> >>
> >> NullPointerException on line 146 means classLoader is null. That is
> >> odd.
> >> I have no idea why the code that gets classLoader won't work in
> >> eclipse's
> >> junit runtime, but does when run in your eclipse plugin.
>
> It seems indeed like during JUnit tests, the context classloader of
> the thread gets not set (null).
>
> The code leading up to the problem is as follows:
> PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
> JavaDialectConfiguration javaConf = (JavaDialectConfiguration)
> conf.getDialectConfiguration("java");
> javaConf.setCompiler(JavaDialectConfiguration.ECLIPSE);
> javaConf.setJavaLanguageLevel("1.5");
> RuleBase ruleBase = RuleBaseFactory.newRuleBase();
>
> The resulting stacktrace is:
> java.lang.NullPointerException
> at
> org.drools.RuleBaseConfiguration.instantiateClass(RuleBaseConfiguration.java:569)
> at
>
org.drools.RuleBaseConfiguration.determineConsequenceExceptionHandler(RuleBaseConfiguration.java:561)
> at
> org.drools.RuleBaseConfiguration.init(RuleBaseConfiguration.java:220)
> at
> org.drools.RuleBaseConfiguration.<init>(RuleBaseConfiguration.java:133)
> at
> org.drools.common.AbstractRuleBase.<init>(AbstractRuleBase.java:147)
> at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:124)
> at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:101)
> at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:57)
> at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:38)
>
> Has anyone set up Drools and JUnit successfully? Maybe we forgot to
> set up the environment properly?
>
> Best regards
> Marcus