InitialContext problem

1,723 views
Skip to first unread message

LF

unread,
Oct 21, 2014, 4:33:44 AM10/21/14
to cdi-...@googlegroups.com
Hi!

We need to bind some objects to the intial context in a test case. 

Does CDI-unit provide some other way to access the initial context than the way I tried below or 
does anyone know how to solve the problem below?

In the CDI-unit testcase I tried in @BeforeClass:

Context context = new InitialContext();
context.bind(<property>, <object>);

However when running the test case I get the following Exception:

   java.lang.NoClassDefFoundError: com/ibm/ejs/ras/hpel/HpelHelper
at com.ibm.ejs.ras.RasHelper.getThreadId(RasHelper.java:1764)
at com.ibm.ejs.ras.RasEvent6$1.initialValue(RasEvent6.java:101)
at java.lang.ThreadLocal.setInitialValue(Unknown Source)
at java.lang.ThreadLocal.get(Unknown Source)
at com.ibm.ejs.ras.RasEvent6.<init>(RasEvent6.java:292)
at com.ibm.ejs.ras.MessageEvent6.<init>(MessageEvent6.java:205)
at com.ibm.ejs.ras.Tr.fireMessageEvent(Tr.java:1563)
at com.ibm.ejs.ras.Tr.error(Tr.java:731)
at com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable(javaURLContextFactory.java:108)
at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:85)
at javax.naming.spi.NamingManager.getURLObject(Unknown Source)
at javax.naming.spi.NamingManager.getURLContext(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.bind(Unknown Source)
at org.jglue.cdiunit.CdiRunner$2.evaluate(CdiRunner.java:127)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.ibm.ejs.ras.hpel.HpelHelper
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 30 more

Bryn Cooke

unread,
Oct 21, 2014, 4:44:07 AM10/21/14
to cdi-...@googlegroups.com
Hi,
CDI unit does have limited support initial context via CdiUnitContext, but I've not done much work in this area.

I would guess that somehow the IBM initial context implementation is on your classpath and somehow CdiRunner can't override it? If you are in a web app then perhaps you need to change some dependency to provided?

See CdiRunner.methodBlock to see where the initial context is being set up.

Bryn
--
You received this message because you are subscribed to the Google Groups "CDI-Unit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lars-Fredrik Smedberg

unread,
Oct 21, 2014, 4:56:06 AM10/21/14
to cdi-...@googlegroups.com
Hi Bryn

Thanks for the answer, we also tried the way its done in CdiRunner, I will try to cleanup the project and the dependencies to see if that solves the problem.

Regards
Lars-Fredrik
--
Med vänlig hälsning / Best regards

Lars-Fredrik Smedberg

STATEMENT OF CONFIDENTIALITY:
The information contained in this electronic message and any
attachments to this message are intended for the exclusive use of the
address(es) and may contain confidential or privileged information. If
you are not the intended recipient, please notify Lars-Fredrik Smedberg
immediately at itsm...@gmail.com, and destroy all copies of this
message and any attachments.

LF

unread,
Oct 21, 2014, 7:47:51 AM10/21/14
to cdi-...@googlegroups.com
Hi again

When running a clean test case it was working fine.

When debugging the original testcase that did not work I saw that some of the included JARs (either wia a jndi.properties or System.setProperty(...)) set the value of Context.URL_PKG_PREFIXES.
I was not able to clear the system property with key Context.URL_PKG_PREFIXES since some included JAR added to it.

What I did to get it to work was to first add (to get our ObjectFactory first in the list):

System.setProperty(Context.URL_PKG_PREFIXES, "test");

And provided a very simple ObjectFactory in test.java (according to the naming convention for the "java:" schema and URLContextFactory) that uses the InitialContextFactory in CDI-Unit:

package test.java;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.spi.InitialContextFactory;
import javax.naming.spi.ObjectFactory;

public class javaURLContextFactory implements ObjectFactory {

  @Override
  public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {

    String initialContextFactoryClass = (String) environment.get(Context.INITIAL_CONTEXT_FACTORY);
    InitialContextFactory initialContextFactory = (InitialContextFactory) Class.forName(initialContextFactoryClass).newInstance();
    return initialContextFactory.getInitialContext(environment);
  }
}



Maybe this is something you can include in CDI-Unit?

I also looked at the implementation of CdiUnitContext and it would be nice if it also implemented rebind, unbind etc

Regards
LF


To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "CDI-Unit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bryn Cooke

unread,
Oct 21, 2014, 8:42:35 AM10/21/14
to cdi-...@googlegroups.com
Glad you got it working.

A pull request would be grand for these features. Basically I'll accept anything with javadocs and unit tests and as the codebase is mostly stable I can release quickly if requested.

Bryn
To unsubscribe from this group and stop receiving emails from it, send an email to cdi-unit+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages