Search code examples
javalambdaglassfish

Glassfish embedded v4.1.1 and lambda


I have a junit test (integration test) with an embedded glassfish server. My test failed if I use lambda in my stateless ejb. Without lambda expression my test works fine.

Is there any fix or snapshot version of embedded glassfish server? I have not found any info about snapshot version of glassfish-embedded-all :(

Used dependency:

<dependency>
    <groupId>org.glassfish.main.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>4.1.1</version>
    <scope>test</scope>
</dependency>

If I uncomment the line (1) or (2) then the reference of my example bean is null in my junit test (lookup failed exception appears) :(

Bean/Ejb:

@Stateless
@LocalBean
public class ExampleBean {

    @PersistenceContext
    private EntityManager em;

    public Person get(Long id) {
        Optional<Person> p = Optional.ofNullable(em.find(Person.class, id));

        List<Person> persons = new ArrayList<>();
        persons.add(p.get());

        // (1) properties.forEach(person -> System.out.println(person));
        // (2) String name = person.map(Person::getName).orElse(null);
        //System.out.println(name");
        return p;
    }
}

Test class:

public class ExampleBeanTest {
    @EJB
    private static ExampleBean examplebean;
    private static Context context;
    private static EJBContainer container;

    @BeforeClass
    public static void init() {
        Map<String, Object> props = new HashMap<String,Object>();
        props.put(EJBContainer.MODULES, new File[]{new File("target/test-classes"), new File("target/classes")});

        try {
            container = EJBContainer.createEJBContainer(props);
            context = container.getContext();
            examplebean = (ExampleBean) context.lookup("java:global/classes/ExampleBean");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public static void tearDown() throws Exception {
        if (container != null) {
            container.close();
        }
    }

    @Test
    public void testGet() throws NamingException {
        assertNotNull(examplebean);
        Person p = examplebean.get(1);
        assertNotNull(p);
        assertEquals("text", p.getName());
    }
}

Is there any embedded EE container for juint test + java 8 which is able to execute the java lambda expressions?

EDIT1: I have just created another test with lambda and classic iteration. Bean with classic iteration runs fine but with forEach i got a "Lookup failed" exception. What is going on here?

public Person get(Long id) {
    Optional<Person> person = Optional.ofNullable(em.find(Person.class, id));
    List<Person> persons = new ArrayList<>();
    persons.add(person.get());

    long l = persons.stream().count();
    System.out.println("length: " + l); // <- ok, result: 1

    for (Person p : persons) {  // <- ok, one result: "text"
        String name = p.getName();
        System.out.println("name: " + name);
    }

    // if i keep the following line in my code i get a lookup failed exception
    // if i comment these lines my code works fine
    persons.forEach(pp -> {
        System.out.println(pp.getName());
    });

return person.get();
}

Exeption:

Feb 05, 2016 10:36:17 PM org.glassfish.ejb.embedded.EJBContainerProviderImpl createEJBContainer
SEVERE: EJB6005:No EJB modules found
javax.naming.NamingException: Lookup failed for 'java:global/classes/ExampleBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: classes]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at a.b.ExampleBeanTest.init(ExampleBeanTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
Caused by: javax.naming.NameNotFoundException: classes
    at com.sun.enterprise.naming.impl.TransientContext.resolveContext(TransientContext.java:299)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:207)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:114)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:478)
    ... 18 more

EDIT2: I can see an NCLS-DEPLOYMENT-00009 warning message in the maven log file. Only one related forum topic have found: Glassfish deploy error “Archive type of MyProject.jar was not recognized”. Here remove lambda expressions was suggested.

maven log

Feb 06, 2016 8:49:37 AM org.glassfish.deployment.common.GenericAnnotationDetector scanArchive
WARNING: NCLS-DEPLOYMENT-00009
Feb 06, 2016 8:49:37 AM org.glassfish.ejb.embedded.EJBContainerProviderImpl createEJBContainer
SEVERE: EJB6005:No EJB modules found
javax.naming.NamingException: Lookup failed for 'java:global/classes/ExampleBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: classes]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at a.b.ExampleBeanTest.init(ExampleBeanTest.java:39)
...
Caused by: javax.naming.NameNotFoundException: classes
...

Is it a BUG?

Is there any working, embedded EE container to execute JUnit/Integration tests with Java8 + EJB/Bean + Lambda??

Where can I report glassfish-embedded-all module related bug? I only found glassfish server related jira.


Solution

  • The lambda bug is fixed a couple of days ago: https://github.com/payara/Payara/issues/647