This is an automated email from the ASF dual-hosted git repository.
fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 2eadcaa19b53 Improve JmsServiceLocationHelper
2eadcaa19b53 is described below
commit 2eadcaa19b535bd1bb120f5310def586465d78f6
Author: Croway <[email protected]>
AuthorDate: Thu Jan 29 16:46:46 2026 +0100
Improve JmsServiceLocationHelper
---
.../component/jms/JmsServiceLocationHelper.java | 17 +--
.../jms/JmsServiceLocationHelperTest.java | 162 +++++++++++++++++++++
2 files changed, 168 insertions(+), 11 deletions(-)
diff --git
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsServiceLocationHelper.java
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsServiceLocationHelper.java
index 555d0ced122e..37bf63e35f9f 100644
---
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsServiceLocationHelper.java
+++
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsServiceLocationHelper.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.component.jms;
-import java.util.HashMap;
import java.util.Map;
import jakarta.jms.ConnectionFactory;
@@ -39,14 +38,12 @@ final class JmsServiceLocationHelper {
if (cf == null) {
return null;
}
- Map<String, Object> props = new HashMap<>();
- bi.getProperties(cf, props, null, false);
- Object url = props.get("brokerURL");
+ Object url = bi.getOrElseProperty(cf, "brokerURL", null, false);
if (url != null) {
return url.toString();
} else {
// nested connection factory which can be wrapped in connection
pooling
- ConnectionFactory ncf = (ConnectionFactory)
props.get("connectionFactory");
+ ConnectionFactory ncf = (ConnectionFactory)
bi.getOrElseProperty(cf, "connectionFactory", null, false);
if (ncf != null) {
return getBrokerURLFromConnectionFactory(bi, ncf);
}
@@ -59,20 +56,18 @@ final class JmsServiceLocationHelper {
if (cf == null) {
return null;
}
- Map<String, Object> props = new HashMap<>();
- bi.getProperties(cf, props, null, false);
- Object user = props.get("user");
+ Object user = bi.getOrElseProperty(cf, "user", null, false);
if (user == null) {
- user = props.get("username");
+ user = bi.getOrElseProperty(cf, "username", null, false);
}
if (user == null) {
- user = props.get("userName");
+ user = bi.getOrElseProperty(cf, "userName", null, false);
}
if (user != null) {
return user.toString();
} else {
// nested connection factory which can be wrapped in connection
pooling
- ConnectionFactory ncf = (ConnectionFactory)
props.get("connectionFactory");
+ ConnectionFactory ncf = (ConnectionFactory)
bi.getOrElseProperty(cf, "connectionFactory", null, false);
if (ncf != null) {
return getUsernameFromConnectionFactory(bi, ncf);
}
diff --git
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsServiceLocationHelperTest.java
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsServiceLocationHelperTest.java
new file mode 100644
index 000000000000..6c16ed2dd1a5
--- /dev/null
+++
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsServiceLocationHelperTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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.camel.component.jms;
+
+import jakarta.jms.Connection;
+import jakarta.jms.ConnectionFactory;
+import jakarta.jms.JMSContext;
+import jakarta.jms.JMSException;
+
+import org.apache.camel.impl.engine.DefaultBeanIntrospection;
+import org.apache.camel.spi.BeanIntrospection;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JmsServiceLocationHelperTest {
+
+ public static class FakeConnectionFactory implements ConnectionFactory {
+
+ private int invalidCalls;
+
+ public int getInvalidCalls() {
+ return invalidCalls;
+ }
+
+ @Override
+ public Connection createConnection() throws JMSException {
+ invalidCalls++;
+ return null;
+ }
+
+ @Override
+ public Connection createConnection(String userName, String password)
throws JMSException {
+ invalidCalls++;
+ return null;
+ }
+
+ @Override
+ public JMSContext createContext() {
+ invalidCalls++;
+ return null;
+ }
+
+ @Override
+ public JMSContext createContext(String userName, String password) {
+ invalidCalls++;
+ return null;
+ }
+
+ @Override
+ public JMSContext createContext(String userName, String password, int
sessionMode) {
+ invalidCalls++;
+ return null;
+ }
+
+ @Override
+ public JMSContext createContext(int sessionMode) {
+ invalidCalls++;
+ return null;
+ }
+ }
+
+ public static class TestConnectionFactory extends FakeConnectionFactory {
+
+ private String brokerURL;
+ private String user;
+ private String username;
+ private String userName;
+
+ public TestConnectionFactory(String brokerURL, String user, String
username, String userName) {
+ super();
+ this.brokerURL = brokerURL;
+ this.user = user;
+ this.username = username;
+ this.userName = userName;
+ }
+
+ public String getBrokerURL() {
+ return brokerURL;
+ }
+
+ public void setBrokerURL(String brokerURL) {
+ this.brokerURL = brokerURL;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public void setUser(String user) {
+ this.user = user;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+ }
+
+ @Test
+ public void testGetBrokerURLFromConnectionFactory() {
+ BeanIntrospection bi = new DefaultBeanIntrospection();
+
+ String expectedUrl = "tcp://localhost:61616";
+
+ TestConnectionFactory testCf = new TestConnectionFactory(expectedUrl,
null, null, null);
+ String url =
JmsServiceLocationHelper.getBrokerURLFromConnectionFactory(bi, testCf);
+ assertEquals(expectedUrl, url);
+ assertEquals(0, testCf.getInvalidCalls());
+ }
+
+ @Test
+ public void testGetUsernameFromConnectionFactory() {
+ BeanIntrospection bi = new DefaultBeanIntrospection();
+
+ String expectedUsername = "johndoe";
+
+ // Test with "user" property
+ TestConnectionFactory testCf = new TestConnectionFactory(null,
expectedUsername, null, null);
+ String username =
JmsServiceLocationHelper.getUsernameFromConnectionFactory(bi, testCf);
+ assertEquals(expectedUsername, username);
+ assertEquals(0, testCf.getInvalidCalls());
+
+ // Test with "username" property
+ testCf = new TestConnectionFactory(null, null, expectedUsername, null);
+ username =
JmsServiceLocationHelper.getUsernameFromConnectionFactory(bi, testCf);
+ assertEquals(expectedUsername, username);
+ assertEquals(0, testCf.getInvalidCalls());
+
+ // Test with "userName" property
+ testCf = new TestConnectionFactory(null, null, null, expectedUsername);
+ username =
JmsServiceLocationHelper.getUsernameFromConnectionFactory(bi, testCf);
+ assertEquals(expectedUsername, username);
+ assertEquals(0, testCf.getInvalidCalls());
+ }
+
+}