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