[jboss-dev-forums] [Design of EJB 3.0] - Re: Which is the best place to write a test case for ejb3-te
jaikiran
do-not-reply at jboss.com
Fri Aug 8 05:11:45 EDT 2008
While testing the SessionContainer.invoke, i observed one other issue which was resulting in a NullPointerException:
| @Stateless
| @Local (MySLSBLocal.class)
| public class MySLSBean implements MySLSBLocal
| {
|
| public void printObject(Object obj)
| {
| // do nothing
|
| }
|
| }
Test case:
@Test
| public void testBeanMethodInvocationForNullParams() throws Throwable
| {
| // create the SLSB container
| this.sessionContainer = Utils.createSlsb(MySLSBean.class);
|
| // bind to JNDI
| Ejb3RegistrarLocator.locateRegistrar().bind(sessionContainer.getName(), sessionContainer);
|
| Context ctx = new InitialContext();
| // lookup the local bean
| MySLSBLocal local = (MySLSBLocal) ctx.lookup(getDefaultBusinessLocalJndiName(sessionContainer)
| );
|
| assertNotNull("Local bean is null",local);
|
| // pass null to the method
| local.printObject(null);
|
| }
|
|
This throws NPE:
| testBeanMethodInvocationForNullParams(org.jboss.ejb3.test.proxy.common.container.unit.SessionContainerTestCase) Time elapsed: 0.031 sec <<< ERROR!
| java.lang.NullPointerException
| at org.jboss.ejb3.test.proxy.common.container.SessionContainer.invoke(SessionContainer.java:182)
| at org.jboss.ejb3.proxy.handler.session.SessionSpecProxyInvocationHandlerBase.invoke(SessionSpecProxyInvocationHandlerBase.java:120)
| at $Proxy37.printObject(Unknown Source)
| at org.jboss.ejb3.test.proxy.common.container.unit.SessionContainerTestCase.testBeanMethodInvocationForNullParams(SessionContainerTestCase.java:149)
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
| at java.lang.reflect.Method.invoke(Method.java:585)
| at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
| at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
| at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
| at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
| at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
| at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
| at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
| at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
| at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
| at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
| at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
| at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
| at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
| at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
| at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:165)
| at org.apache.maven.surefire.Surefire.run(Surefire.java:107)
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
| at java.lang.reflect.Method.invoke(Method.java:585)
| at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:289)
| at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:993)
As part of fixing this and the earlier issue, i am changing the implementation of SessionContainer.invoke to do away with using Java Reflection APIs and instead use the toMethod() API of the SerializableMethod:
|
| Index: SessionContainer.java
| ===================================================================
| --- SessionContainer.java (revision 76706)
| +++ SessionContainer.java (working copy)
| @@ -25,9 +25,7 @@
| import java.lang.reflect.InvocationHandler;
| import java.lang.reflect.Method;
| import java.lang.reflect.Proxy;
| -import java.util.ArrayList;
| import java.util.HashSet;
| -import java.util.List;
| import java.util.Map;
| import java.util.Set;
|
| @@ -168,26 +166,9 @@
| */
| public Object invoke(Object proxy, SerializableMethod method, Object[] args) throws Throwable
| {
| - // Initialize
| - Class<?>[] argTypes = new Class<?>[]
| - {};
|
| - // Get the types from the arguments, if present
| - if (args != null)
| - {
| - List<Class<?>> types = new ArrayList<Class<?>>();
| - for (Object arg : args)
| - {
| - types.add(arg.getClass());
| - }
| - argTypes = types.toArray(new Class<?>[]
| - {});
| - }
| + Method m = method.toMethod(this.getClassLoader());
|
| - // Obtain the method for invocation
| - Method m = this.getClassLoader().loadClass(method.getDeclaringClassName()).getDeclaredMethod(method.getName(),
| - argTypes);
| -
| // Invoke on the bean
| return invokeBean(proxy, m, args);
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4169522#4169522
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4169522
More information about the jboss-dev-forums
mailing list