0

I have eclipse juno and jboss AS 7.1 final running on my machine and configured it through it (eclipse juno). I hava a simple stateless bean and its interface (in fact is exactly the same example from the book Enterprise Java Beans 3.1 6th Edition). Here is the code:


package beans;

public class CalculatorBeanBase implements CalculatorCommonBusiness{

    public int add(int[] opers){

        int sum = 0;
        for(int c:opers){
            sum+=c;
        }
        return sum;
    }

}

package beans;

public interface CalculatorCommonBusiness {

    public int add(int[] opers);

}

package beans;

public interface CalculatorRemoteBusiness extends CalculatorCommonBusiness {}

package beans;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
@LocalBean
public class SimpleCalculatorBean extends CalculatorBeanBase {}

I am trying to do the following JUnit testing, unfortunately the example mentions something like "using a standard jndi.properties file that we assume to be presente on the classpath" (here is my first problem, I have no idea what this means, I in fact did some research but I cant quite understand how it works, confuses me, and ultimately cant seem to make it work).

package beans;

import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.TestCase;
import org.junit.BeforeClass;
import org.junit.Test;

public class CalculatorIntegrationTestCase {

    private static Context namingContext;
    private static SimpleCalculatorBean cal;
    private static final String JNDI_NAME_CALC = "java:global/jndi.properties/SimpleCalculatorBean";

    @BeforeClass
    public static void obtainProxyReferences() throws Throwable {
        namingContext = new InitialContext();
        cal = (SimpleCalculatorBean)namingContext.lookup(JNDI_NAME_CALC);
    }

    private void assertAdditionSucceeds(SimpleCalculatorBean cal){
        final int[] oper = {2,3,4};
        final int expectedSum = 9;
        final int actualSum = cal.add(oper);

        TestCase.assertEquals("Assert Failed!!. The actual value is not the expected",expectedSum, actualSum);
    }

    @Test
    public void testAdditionUsingBusinessReference() throws Throwable {
        this.assertAdditionSucceeds(cal);
    }
}

I need to know how that jndi. properties file works and how I have to set it up. (I read about this last issue, and I got the folloing .txt file and changed its extension to .jar, I dont know if that was right):

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jpn://localhost:1099 
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Finally I put that .jar file into the classpath (Don´t know if I did it right: rigth-click on project, build path, libraries, add external jars).

I did all the above stuff but I keep getting the following:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at beans.CalculatorIntegrationTestCase.obtainProxyReferences(CalculatorIntegrationTestCase.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    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)

Of course I published the Project (through eclipse though, project>run>run on server). Help would be appreciated

Ermal
  • 441
  • 5
  • 19
Jhonathan Ibanez
  • 89
  • 1
  • 2
  • 10

2 Answers2

0

AS7 has completely new implementation of remote ejb/jndi which means you cannot use jnp anymore stuff anymore.

take a look at https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

and https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project

There you can find instructions how to do it now.

Tomaz Cerar
  • 5,761
  • 25
  • 32
  • So basically I must change this: import javax.naming.Context; import javax.naming.InitialContext; import junit.framework.TestCase; import org.junit.BeforeClass; import org.junit.Test; to this: java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory java.naming.provider.url=remote://localhost:4447 ?? by the way thank you for your answer – Jhonathan Ibanez Dec 17 '12 at 03:14
  • @theV0ID No, it didn´t. I tried for a long time a long time ago, now I am in some other business, sorry I could not help. I think that using a properties file instead of the final static variable could do the work, of course we still need the correct properties to be used, however, I am not quite sure wich those would be. Let me know whatever you achieve though. – Jhonathan Ibanez Sep 11 '13 at 13:58
0

Check naming provider url in jndi.properties file. It should be jnp://localhost:1099

BDL
  • 21,052
  • 22
  • 49
  • 55
Beenu
  • 1