Search in sources :

Example 11 with EJBContainer

use of javax.ejb.embeddable.EJBContainer in project tomee by apache.

the class GuessHowManyMBeanTest method play.

@Test
public void play() throws Exception {
    Properties properties = new Properties();
    properties.setProperty(LocalMBeanServer.OPENEJB_JMX_ACTIVE, Boolean.TRUE.toString());
    EJBContainer container = EJBContainer.createEJBContainer(properties);
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    ObjectName objectName = new ObjectName(OBJECT_NAME);
    assertEquals(0, server.getAttribute(objectName, "value"));
    server.setAttribute(objectName, new Attribute("value", 3));
    assertEquals(3, server.getAttribute(objectName, "value"));
    assertEquals("winner", server.invoke(objectName, "tryValue", new Object[] { 3 }, null));
    assertEquals("not the correct value, please have another try", server.invoke(objectName, "tryValue", new Object[] { 2 }, null));
    container.close();
}
Also used : Attribute(javax.management.Attribute) EJBContainer(javax.ejb.embeddable.EJBContainer) Properties(java.util.Properties) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 12 with EJBContainer

use of javax.ejb.embeddable.EJBContainer in project tomee by apache.

the class CalculatorTest method test.

@Test
public void test() throws Exception {
    ExecutionChannel.getInstance().addObserver(this);
    final EJBContainer container = EJBContainer.createEJBContainer();
    {
        final CalculatorBean calculator = (CalculatorBean) getContext().lookup(JNDI);
        Assert.assertEquals(10, calculator.add(4, 6));
        //the bean is constructed only when it is used for the first time
        Assert.assertEquals("postConstruct", this.received.remove(0));
        Assert.assertEquals("add", this.received.remove(0));
        Assert.assertEquals(-2, calculator.subtract(4, 6));
        Assert.assertEquals("subtract", this.received.remove(0));
        Assert.assertEquals(24, calculator.multiply(4, 6));
        Assert.assertEquals("multiply", this.received.remove(0));
        Assert.assertEquals(2, calculator.divide(12, 6));
        Assert.assertEquals("divide", this.received.remove(0));
        Assert.assertEquals(4, calculator.remainder(46, 6));
        Assert.assertEquals("remainder", this.received.remove(0));
    }
    {
        final CalculatorBean calculator = (CalculatorBean) getContext().lookup(JNDI);
        Assert.assertEquals(10, calculator.add(4, 6));
        Assert.assertEquals("add", this.received.remove(0));
    }
    container.close();
    Assert.assertEquals("preDestroy", this.received.remove(0));
    Assert.assertTrue(this.received.isEmpty());
}
Also used : EJBContainer(javax.ejb.embeddable.EJBContainer) Test(org.junit.Test)

Example 13 with EJBContainer

use of javax.ejb.embeddable.EJBContainer in project tomee by apache.

the class CalculatorTest method call.

@Test
public void call() throws MalformedURLException {
    final EJBContainer container = EJBContainer.createEJBContainer(new Properties() {

        {
            setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
            // random port to avoid issue on CI, default is 4204
            setProperty("httpejbd.port", "0");
        }
    });
    // get back the random port
    final int port = Integer.parseInt(SystemInstance.get().getProperty("httpejbd.port"));
    // normal call
    final Service service = Service.create(new URL("http://127.0.0.1:" + port + "/webservice-ws-with-resources-config/CalculatorBean?wsdl"), new QName("http://security.ws.superbiz.org/", "CalculatorBeanService"));
    final Calculator calculator = service.getPort(Calculator.class);
    ClientProxy.getClient(calculator).getOutInterceptors().add(new WSS4JOutInterceptor(new HashMap<String, Object>() {

        {
            put("action", "UsernameToken");
            put("user", "openejb");
            put("passwordType", "PasswordText");
            put("passwordCallbackRef", new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
                    pc.setPassword("tomee");
                }
            });
        }
    }));
    assertEquals(5, calculator.add(2, 3));
    // bad auth
    final Calculator calculator2 = service.getPort(Calculator.class);
    ClientProxy.getClient(calculator2).getOutInterceptors().add(new WSS4JOutInterceptor(new HashMap<String, Object>() {

        {
            put("action", "UsernameToken");
            put("user", "openejb");
            put("passwordType", "PasswordText");
            put("passwordCallbackRef", new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    final WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
                    pc.setPassword("wrong");
                }
            });
        }
    }));
    try {
        assertEquals(5, calculator2.add(2, 3));
    } catch (SOAPFaultException sfe) {
        assertThat(sfe.getMessage(), CoreMatchers.containsString("A security error was encountered when verifying the message"));
    }
    container.close();
    // valid it passed because all was fine and not because the server config was not here
    assertTrue(PasswordCallbackHandler.wasCalled());
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Callback(javax.security.auth.callback.Callback) EJBContainer(javax.ejb.embeddable.EJBContainer) WSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) Test(org.junit.Test)

Example 14 with EJBContainer

use of javax.ejb.embeddable.EJBContainer in project tomee by apache.

the class DataSourceCipheredExampleTest method accessDatasourceWithMyImplementation.

@Test
public void accessDatasourceWithMyImplementation() throws Exception {
    // define the datasource
    Properties properties = new Properties();
    properties.setProperty("ProtectedDatasource", "new://Resource?type=DataSource");
    properties.setProperty("ProtectedDatasource.JdbcDriver", "org.hsqldb.jdbcDriver");
    properties.setProperty("ProtectedDatasource.JdbcUrl", "jdbc:hsqldb:mem:protected");
    properties.setProperty("ProtectedDatasource.UserName", USER);
    properties.setProperty("ProtectedDatasource.Password", "3MdniFr3v3NLLuoY");
    properties.setProperty("ProtectedDatasource.PasswordCipher", "reverse");
    properties.setProperty("ProtectedDatasource.JtaManaged", "true");
    // start the context and makes junit test injections
    EJBContainer container = EJBContainer.createEJBContainer(properties);
    Context context = container.getContext();
    context.bind("inject", this);
    // test the datasource
    assertNotNull(dataSource);
    assertNotNull(dataSource.getConnection());
    // closing the context
    container.close();
}
Also used : Context(javax.naming.Context) EJBContainer(javax.ejb.embeddable.EJBContainer) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

EJBContainer (javax.ejb.embeddable.EJBContainer)14 Test (org.junit.Test)11 Properties (java.util.Properties)10 Context (javax.naming.Context)5 IOException (java.io.IOException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 EjbJar (org.apache.openejb.jee.EjbJar)2 File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Attribute (javax.management.Attribute)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 InitialContext (javax.naming.InitialContext)1 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 QName (javax.xml.namespace.QName)1 Service (javax.xml.ws.Service)1