package org.ow2.easybeans.mavenplugin.examples.helloworld;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* Test class for HelloWorld EJB.
* @author Julien Blais
*/
public class HelloWorldTest {
/**
* JNDI name of the hello bean.
*/
private static final String JNDI_NAME = "org.ow2.easybeans.mavenplugin.examples.helloworld.HelloBean"
+ "_" + HelloInterface.class.getName() + "@Remote";
/**
* EJB to test.
*/
private static HelloInterface hr = getEJB();
/**
* sayHello method test from HelloWorld-ejb.
*/
@Test
public void testHello() {
System.out.println("Test calling helloWorld method...\n");
String res = hr.sayHello("World");
assertTrue(res.equals("Hello World !"));
System.out.println("#################################################");
System.out.println(" " + res);
System.out.println("#################################################\n");
}
/**
* Get Initial Context.
* @return smartFactory initialised context
* @throws NamingException naming exception
*/
private static Context getInitialContext() throws NamingException {
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.ow2.easybeans.component.smartclient.spi.SmartContextFactory");
env.put(Context.PROVIDER_URL, "smart://localhost:22503");
return new InitialContext(env);
}
/**
* Get the HelloWorld EJB interface/.
* @return Interface of EJB
*/
private static HelloInterface getEJB() {
HelloInterface hello = null;
try {
System.setProperty("java.rmi.server.useCodebaseOnly", "true");
Context initialContext = getInitialContext();
hello = (HelloInterface) initialContext.lookup(JNDI_NAME);
} catch (NamingException ex) {
Logger.getLogger(HelloWorldTest.class.getName()).log(Level.SEVERE, "Unable to found HelloWorld EJB.", ex);
}
return hello;
}
}