Search in sources :

Example 6 with EJBContainer

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

the class CounterCallbacksTest method test.

@Test
public void test() throws Exception {
    final Map<String, Object> p = new HashMap<String, Object>();
    p.put("MySTATEFUL", "new://Container?type=STATEFUL");
    //How many instances of Stateful beans can our server hold in memory?
    p.put("MySTATEFUL.Capacity", "2");
    //Interval in seconds between checks
    p.put("MySTATEFUL.Frequency", "1");
    //No bulkPassivate - just passivate entities whenever it is needed
    p.put("MySTATEFUL.BulkPassivate", "0");
    final EJBContainer container = EJBContainer.createEJBContainer(p);
    //this is going to track the execution
    ExecutionChannel.getInstance().addObserver(this);
    {
        final Context context = getContext();
        CallbackCounter counterA = (CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
        Assert.assertNotNull(counterA);
        Assert.assertEquals("postConstruct", this.received.remove(0));
        Assert.assertEquals(0, counterA.count());
        Assert.assertEquals("count", this.received.remove(0));
        Assert.assertEquals(1, counterA.increment());
        Assert.assertEquals("increment", this.received.remove(0));
        Assert.assertEquals(0, counterA.reset());
        Assert.assertEquals("reset", this.received.remove(0));
        Assert.assertEquals(1, counterA.increment());
        Assert.assertEquals("increment", this.received.remove(0));
        System.out.println("Waiting 2 seconds...");
        Thread.sleep(2000);
        Assert.assertEquals("preDestroy", this.received.remove(0));
        try {
            counterA.increment();
            Assert.fail("The ejb is not supposed to be there.");
        } catch (javax.ejb.NoSuchEJBException e) {
        //excepted
        }
        context.close();
    }
    {
        final Context context = getContext();
        CallbackCounter counterA = (CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
        Assert.assertEquals("postConstruct", this.received.remove(0));
        Assert.assertEquals(1, counterA.increment());
        Assert.assertEquals("increment", this.received.remove(0));
        ((CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
        Assert.assertEquals("postConstruct", this.received.remove(0));
        Assert.assertEquals("count", this.received.remove(0));
        ((CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
        Assert.assertEquals("postConstruct", this.received.remove(0));
        Assert.assertEquals("count", this.received.remove(0));
        System.out.println("Waiting 2 seconds...");
        Thread.sleep(2000);
        Assert.assertEquals("prePassivate", this.received.remove(0));
        context.close();
    }
    container.close();
    Assert.assertEquals(this.received.toString(), 2, this.received.size());
    Assert.assertEquals("preDestroy", this.received.remove(0));
    Assert.assertEquals("preDestroy", this.received.remove(0));
    Assert.assertTrue(this.received.toString(), this.received.isEmpty());
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) HashMap(java.util.HashMap) EJBContainer(javax.ejb.embeddable.EJBContainer) Test(org.junit.Test)

Example 7 with EJBContainer

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

the class EmbeddedTomEEContainerTest method classpath.

@Test
public void classpath() throws Exception {
    final Properties p = new Properties();
    p.setProperty(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class.getName());
    p.setProperty(DeploymentsResolver.CLASSPATH_INCLUDE, ".*tomee-embedded.*");
    p.setProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT, "-1");
    EJBContainer container = null;
    try {
        container = EJBContainer.createEJBContainer(p);
        assertNotNull(container);
        assertNotNull(container.getContext());
        final ABean bean = ABean.class.cast(container.getContext().lookup("java:global/tomee-embedded/ABean"));
        assertNotNull(bean);
        assertEquals("ok", bean.embedded());
    } finally {
        if (container != null) {
            container.close();
        }
    }
}
Also used : EJBContainer(javax.ejb.embeddable.EJBContainer) Properties(java.util.Properties) Test(org.junit.Test)

Example 8 with EJBContainer

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

the class EmbeddedTomEEContainerTest method containerTest.

@Test
public void containerTest() throws Exception {
    final File war = createWar();
    final Properties p = new Properties();
    p.setProperty(EJBContainer.APP_NAME, "test");
    p.setProperty(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class.getName());
    p.put(EJBContainer.MODULES, war.getAbsolutePath());
    p.setProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT, "-1");
    EJBContainer container = null;
    try {
        container = EJBContainer.createEJBContainer(p);
        assertNotNull(container);
        assertNotNull(container.getContext());
        final URL url = new URL("http://127.0.0.1:" + System.getProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT) + "/test/index.html");
        assertEquals("true", getOk(url, 2));
    } finally {
        if (container != null) {
            container.close();
        }
        try {
            FileUtils.forceDelete(war);
        } catch (final IOException e) {
            FileUtils.deleteQuietly(war);
        }
    }
}
Also used : EJBContainer(javax.ejb.embeddable.EJBContainer) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 9 with EJBContainer

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

the class DataSourceCipheredExampleTest method accessDatasource.

@Test
public void accessDatasource() 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", DATASOURCE_URL);
    properties.setProperty("ProtectedDatasource.UserName", USER);
    properties.setProperty("ProtectedDatasource.Password", "fEroTNXjaL5SOTyRQ92x3DNVS/ksbtgs");
    properties.setProperty("ProtectedDatasource.PasswordCipher", "Static3DES");
    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)

Example 10 with EJBContainer

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

the class MoviesTest method test.

@Test
public void test() throws Exception {
    final Properties p = new Properties();
    p.put("movieDatabase", "new://Resource?type=DataSource");
    p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
    p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
    final EJBContainer container = EJBContainer.createEJBContainer(p);
    final Context context = container.getContext();
    context.bind("inject", this);
    assertTrue(((ReloadableEntityManagerFactory) emf).getManagedClasses().contains(Movie.class.getName()));
    container.close();
}
Also used : Context(javax.naming.Context) EJBContainer(javax.ejb.embeddable.EJBContainer) Properties(java.util.Properties) ReloadableEntityManagerFactory(org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory) 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