Search in sources :

Example 1 with SampleEjb

use of org.glassfish.distributions.test.ejb.SampleEjb in project Payara by payara.

the class EmbeddedTest method testEjb.

@Test
public void testEjb() throws GlassFishException {
    Deployer deployer = glassfish.getDeployer();
    URL source = SampleEjb.class.getClassLoader().getResource("org/glassfish/distributions/test/ejb/SampleEjb.class");
    String p = source.getPath().substring(0, source.getPath().length() - "org/glassfish/distributions/test/ejb/SimpleEjb.class".length());
    String appName = deployer.deploy(new File(p).toURI(), "--name=sample");
    Assert.assertNotNull("AppName is null from deployer of type " + deployer.getClass().getName(), appName);
    // ok now let's look up the EJB...
    try {
        InitialContext ic = new InitialContext();
        SampleEjb ejb = (SampleEjb) ic.lookup("java:global/sample/SampleEjb");
        if (ejb != null) {
            try {
                System.out.println(ejb.saySomething());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (NamingException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
    deployer.undeploy(appName);
    System.out.println("Done with EJB");
}
Also used : NamingException(javax.naming.NamingException) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) Deployer(org.glassfish.embeddable.Deployer) URL(java.net.URL) InitialContext(javax.naming.InitialContext) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 2 with SampleEjb

use of org.glassfish.distributions.test.ejb.SampleEjb in project Payara by payara.

the class UnitTest method test.

@Test
public void test() {
    // Calculate test-classes location
    String cname = "org/glassfish/distributions/test/UnitTest.class";
    URL source = UnitTest.class.getClassLoader().getResource(cname);
    String dir = source.getPath().substring(0, source.getPath().length() - cname.length());
    Map<String, File> p = new HashMap<String, File>();
    p.put(EJBContainer.MODULES, new File(dir));
    EJBContainer c = EJBContainer.createEJBContainer(p);
    // ok now let's look up the EJB...
    Context ic = c.getContext();
    try {
        System.out.println("Looking up EJB...");
        SampleEjb ejb = (SampleEjb) ic.lookup("java:global/classes/SampleEjb");
        if (ejb != null) {
            System.out.println("Invoking EJB...");
            System.out.println(ejb.saySomething());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Done calling EJB");
    try {
        System.out.println("Creating another container without closing...");
        EJBContainer c0 = EJBContainer.createEJBContainer();
        if (c0 != null)
            throw new RuntimeException("Create another container");
    } catch (EJBException e) {
        System.out.println("Caught expected: " + e.getMessage());
    }
    c.close();
    System.out.println("Creating container after closing the previous...");
    c = EJBContainer.createEJBContainer(p);
    c.close();
}
Also used : Context(javax.naming.Context) HashMap(java.util.HashMap) EJBContainer(javax.ejb.embeddable.EJBContainer) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) URL(java.net.URL) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Aggregations

File (java.io.File)2 URL (java.net.URL)2 NamingException (javax.naming.NamingException)2 SampleEjb (org.glassfish.distributions.test.ejb.SampleEjb)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 EJBContainer (javax.ejb.embeddable.EJBContainer)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 Deployer (org.glassfish.embeddable.Deployer)1 GlassFishException (org.glassfish.embeddable.GlassFishException)1