Glassfish嵌入了v4.1.1和lambda

时间:2016-02-05 18:04:05

标签: java lambda glassfish

我有一个嵌入式glassfish服务器的junit测试(集成测试)。如果我在无状态ejb中使用lambda,我的测试失败了。没有lambda表达式,我的测试工作正常。

是否有嵌入式glassfish服务器的修复程序或快照版本?我还没有找到关于glassfish-embedded-all的快照版本的任何信息:(

使用的依赖:

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

如果我取消注释第(1)行或第(2)行,那么我的例子bean的引用在我的junit测试中为空(出现查找失败的异常):(

豆/ 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;
    }
}

测试类:

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());
    }
}

是否有用于juint test + java 8的嵌入式EE容器能够执行java lambda表达式?

EDIT1: 我刚刚用lambda和经典迭代创建了另一个测试。经典迭代的Bean运行正常,但是对于每个我有一个“查找失败”异常。这是怎么回事?

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: 我可以在maven日志文件中看到NCLS-DEPLOYMENT-00009警告消息。只找到一个相关的论坛主题:Glassfish deploy error “Archive type of MyProject.jar was not recognized”。建议删除lambda表达式。

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
...

是BUG吗?

是否有任何可用的嵌入式EE容器用Java8 + EJB / Bean + Lambda执行JUnit / Integration测试?

我在哪里可以报告与glassfish嵌入的所有模块相关的错误?我只发现了玻璃鱼服务器相关的jira。

1 个答案:

答案 0 :(得分:0)

几天前修复了lambda bug: https://github.com/payara/Payara/issues/647