-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TOMEE-3902: Allow properties to be injected into any activation prope…
…rties
- Loading branch information
1 parent
e29bfd7
commit 26a079e
Showing
10 changed files
with
624 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
container/openejb-core/src/test/java/org/apache/openejb/core/mdb/ActivationConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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.ra.ActiveMQActivationSpec; | ||
import org.apache.activemq.ra.ActiveMQResourceAdapter; | ||
import org.apache.openejb.AppContext; | ||
import org.apache.openejb.BeanContext; | ||
import org.apache.openejb.ModuleContext; | ||
import org.apache.openejb.core.ivm.naming.IvmContext; | ||
import org.apache.openejb.loader.SystemInstance; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import jakarta.jms.Message; | ||
import jakarta.jms.MessageListener; | ||
import jakarta.resource.spi.ActivationSpec; | ||
import jakarta.resource.spi.ResourceAdapter; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.executable.ExecutableValidator; | ||
import jakarta.validation.metadata.BeanDescriptor; | ||
import java.net.InetAddress; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
public class ActivationConfigTest { | ||
|
||
@Test | ||
public void testShouldResolvePlaceHolder() throws Exception { | ||
final ResourceAdapter ra = new ActiveMQResourceAdapter(); | ||
final MdbContainer container = new MdbContainer("TestMdbContainer", null, ra, MessageListener.class, ActiveMQActivationSpec.class, 10, false); | ||
|
||
|
||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put("clientId", "{appId}#{ejbJarId}#{ejbName}#{hostName}#{uniqueId}"); | ||
properties.put("subscriptionName", "subscription-{appId}#{ejbJarId}#{ejbName}#{hostName}#{uniqueId}"); | ||
properties.put("destination", "MyTopic"); | ||
properties.put("destinationType", "jakarta.jms.Topic"); | ||
|
||
final BeanContext beanContext = getMockBeanContext(properties); | ||
|
||
final ActivationSpec activationSpec = container.createActivationSpec(beanContext); | ||
Assert.assertTrue(activationSpec instanceof ActiveMQActivationSpec); | ||
|
||
ActiveMQActivationSpec spec = (ActiveMQActivationSpec) activationSpec; | ||
|
||
final String clientId = spec.getClientId(); | ||
final String[] parts = clientId.split("#"); | ||
|
||
final String hostname = (InetAddress.getLocalHost()).getHostName(); | ||
|
||
Assert.assertEquals("appId", parts[0]); | ||
Assert.assertEquals("moduleId", parts[1]); | ||
Assert.assertEquals("MyEjb", parts[2]); | ||
Assert.assertEquals(hostname, parts[3]); | ||
|
||
final Pattern pattern = Pattern.compile("ID:.*?-\\d+-\\d+-\\d+:\\d"); | ||
Assert.assertTrue(pattern.matcher(parts[4]).matches()); | ||
} | ||
|
||
private BeanContext getMockBeanContext(final Map<String, String> properties) throws Exception { | ||
final IvmContext context = new IvmContext(); | ||
context.bind("comp/Validator", new NoOpValidator()); | ||
|
||
final AppContext mockAppContext = new AppContext("appId", SystemInstance.get(), this.getClass().getClassLoader(), context, context, false); | ||
final ModuleContext mockModuleContext = new ModuleContext("moduleId", new URI(""), "uniqueId", mockAppContext, context, this.getClass().getClassLoader()); | ||
final BeanContext mockBeanContext = new BeanContext("test", context, mockModuleContext, MyListener.class, MessageListener.class, properties); | ||
mockBeanContext.setEjbName("MyEjb"); | ||
|
||
return mockBeanContext; | ||
} | ||
|
||
public static class MyListener implements MessageListener { | ||
|
||
@Override | ||
public void onMessage(Message message) { | ||
|
||
} | ||
} | ||
|
||
public static class NoOpValidator implements Validator { | ||
@Override | ||
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public BeanDescriptor getConstraintsForClass(Class<?> clazz) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T> T unwrap(Class<T> type) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ExecutableValidator forExecutables() { | ||
return null; | ||
} | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...jb-core/src/test/java/org/apache/openejb/core/mdb/MdbContainerClientIdActivationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* 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.broker.BrokerService; | ||
import org.apache.activemq.broker.TransportConnector; | ||
import org.apache.activemq.broker.region.Destination; | ||
import org.apache.activemq.broker.region.Subscription; | ||
import org.apache.activemq.command.ActiveMQTopic; | ||
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.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import jakarta.annotation.Resource; | ||
import jakarta.ejb.ActivationConfigProperty; | ||
import jakarta.ejb.MessageDriven; | ||
import jakarta.jms.*; | ||
import javax.management.MBeanServer; | ||
import javax.management.ObjectName; | ||
import java.lang.management.ManagementFactory; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.regex.Pattern; | ||
|
||
@RunWith(ApplicationComposer.class) | ||
public class MdbContainerClientIdActivationTest { | ||
|
||
private static BrokerService broker; | ||
|
||
@Resource(name = "target") | ||
private Topic destination; | ||
|
||
@Resource(name = "cf") | ||
private ConnectionFactory cf; | ||
|
||
@Configuration | ||
public Properties config() throws Exception{ | ||
final TransportConnector tc = broker.getTransportConnectors().iterator().next(); | ||
final int port = tc.getConnectUri().getPort(); | ||
|
||
return new PropertiesBuilder() | ||
|
||
.p("amq", "new://Resource?type=ActiveMQResourceAdapter") | ||
|
||
.p("amq.DataSource", "") | ||
.p("amq.BrokerXmlConfig", "") //connect to an external broker | ||
.p("amq.ServerUrl", "tcp://localhost:" + port) | ||
|
||
.p("target", "new://Resource?type=Topic") | ||
|
||
.p("mdbs", "new://Container?type=MESSAGE") | ||
.p("mdbs.ResourceAdapter", "amq") | ||
.p("mdbs.pool", "false") | ||
.p("cf", "new://Resource?type=" + ConnectionFactory.class.getName()) | ||
.p("cf.ResourceAdapter", "amq") | ||
|
||
.p("mdb.activation.clientId", "{ejbName}-{uniqueId}") | ||
|
||
.build(); | ||
} | ||
|
||
@Module | ||
public MessageDrivenBean jar() { | ||
return new MessageDrivenBean(Listener.class); | ||
} | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws Exception { | ||
broker = new BrokerService(); | ||
broker.setPersistent(false); | ||
broker.setUseJmx(true); | ||
broker.addConnector("tcp://localhost:0"); // pick a random available port | ||
|
||
broker.start(); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() throws Exception { | ||
broker.stop(); | ||
} | ||
|
||
@Test | ||
public void shouldHaveAUniqueClientID() throws Exception { | ||
final Connection connection = cf.createConnection(); | ||
connection.start(); | ||
|
||
final Session session = connection.createSession(); | ||
final MessageProducer producer = session.createProducer(this.destination); | ||
final TextMessage msg = session.createTextMessage("Hello"); | ||
producer.send(msg); | ||
producer.close(); | ||
session.close(); | ||
connection.close(); | ||
|
||
Listener.latch.await(); | ||
|
||
|
||
final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); | ||
final Set<ObjectName> objectNames = platformMBeanServer.queryNames(new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Topic,destinationName=target,endpoint=Consumer,*"), null); | ||
|
||
ObjectName match = null; | ||
|
||
for (final ObjectName objectName : objectNames) { | ||
if (objectName.getKeyProperty("clientId").startsWith("testMDB")) { | ||
match = objectName; | ||
break; | ||
} | ||
} | ||
|
||
Assert.assertNotNull(match); | ||
|
||
final String clientId = match.getKeyProperty("clientId"); | ||
final String uniquePart = clientId.substring(8); | ||
|
||
Assert.assertNotNull(clientId); | ||
Assert.assertNotNull(uniquePart); | ||
|
||
final Pattern pattern = Pattern.compile("ID_.*?-\\d+-\\d+-\\d+_\\d"); | ||
Assert.assertTrue(pattern.matcher(uniquePart).matches()); | ||
} | ||
|
||
@MessageDriven(name="testMDB", activationConfig = { | ||
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "jakarta.jms.Topic"), | ||
@ActivationConfigProperty(propertyName = "destination", propertyValue = "target") | ||
}) | ||
public static class Listener implements MessageListener { | ||
public static CountDownLatch latch = new CountDownLatch(1); | ||
|
||
@Override | ||
public void onMessage(final Message message) { | ||
latch.countDown(); | ||
} | ||
|
||
|
||
} | ||
|
||
} |
Oops, something went wrong.