adds test to pool endpoint (cherry picked from commit 4e4c3dbd04c3e6d6cf688c34404e469c78606cf3)
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/a13b295e Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/a13b295e Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/a13b295e Branch: refs/heads/tomee-1.7.x Commit: a13b295ea46adc23705abb7b5bb115fd7d336017 Parents: c539266 Author: Otavio Santana <[email protected]> Authored: Mon Dec 18 11:01:33 2017 -0300 Committer: Thiago Veronezi <[email protected]> Committed: Wed Jan 17 12:59:13 2018 -0500 ---------------------------------------------------------------------- .../core/mdb/PoolEndpointHandlerTest.java | 161 +++++++++++++++++++ 1 file changed, 161 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/a13b295e/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/PoolEndpointHandlerTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/PoolEndpointHandlerTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/PoolEndpointHandlerTest.java new file mode 100644 index 0000000..ea3b012 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/PoolEndpointHandlerTest.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.core.mdb; + +import org.apache.activemq.ActiveMQXAConnectionFactory; +import org.apache.openejb.activemq.AMQXASupportTest; +import org.apache.openejb.jee.MessageDrivenBean; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.testing.Configuration; +import org.apache.openejb.testing.Module; +import org.apache.openejb.testng.PropertiesBuilder; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.testng.Assert; + +import javax.annotation.Resource; +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.XAConnectionFactory; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.testng.Assert.*; + +@RunWith(ApplicationComposer.class) +public class PoolEndpointHandlerTest { + + + private static final String TEXT = "foo"; + + + @Configuration + public Properties config() { + return new PropertiesBuilder() + + .p("amq", "new://Resource?type=ActiveMQResourceAdapter") + .p("amq.DataSource", "") + .p("amq.BrokerXmlConfig", "broker:(vm://localhost)") + + .p("target", "new://Resource?type=Queue") + + .p("mdbs", "new://Container?type=MESSAGE") + .p("mdbs.ResourceAdapter", "amq") + .p("mdbs.pool", "true") + + .p("cf", "new://Resource?type=" + ConnectionFactory.class.getName()) + .p("cf.ResourceAdapter", "amq") + + .p("xaCf", "new://Resource?class-name=" + ActiveMQXAConnectionFactory.class.getName()) + .p("xaCf.BrokerURL", "vm://localhost") + .p("mdb.activation.ignore", "testString") + .p("mdb.activation.ignore2", "testString") + .p("openejb.provider.default", "org.apache.openejb.actpropfalse") // service-jar.xml with FailOnUnknowActivationSpec = false + .build(); + } + + @Module + public MessageDrivenBean jar() { + return new MessageDrivenBean(Listener.class); + } + + @Resource(name = "target") + private Queue destination; + + @Resource(name = "xaCf") + private XAConnectionFactory xacf; + + @Resource(name = "cf") + private ConnectionFactory cf; + + @Before + public void resetLatch() { + Listener.reset(); + } + + @Test + public void shouldSendMessage() throws Exception { + assertNotNull(cf); + + for (int i = 0; i < 1_000; i++) { + final Connection connection = cf.createConnection(); + try { + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + final MessageProducer producer = session.createProducer(destination); + producer.send(session.createTextMessage(TEXT)); + assertTrue(Listener.sync()); + } finally { + connection.close(); + } + } + Assert.assertTrue(Listener.COUNTER.get() < 10); + + } + + @MessageDriven(activationConfig = { + @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "target") + }) + public static class Listener implements MessageListener { + public static CountDownLatch latch; + public static boolean ok = false; + + static final AtomicLong COUNTER = new AtomicLong(); + + public Listener() { + COUNTER.incrementAndGet(); + } + + @Override + public void onMessage(final Message message) { + try { + try { + ok = TextMessage.class.isInstance(message) && TEXT.equals(TextMessage.class.cast(message).getText()); + } catch (final JMSException e) { + // no-op + } + } finally { + latch.countDown(); + } + } + + public static void reset() { + latch = new CountDownLatch(1); + ok = false; + } + + public static boolean sync() throws InterruptedException { + latch.await(1, TimeUnit.MINUTES); + return ok; + } + } + +} \ No newline at end of file
