This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.14.x by this push:
     new 9f0a082650bf CAMEL-22927: improving SqlServiceLocationHelper to that 
it gets only the required properties (#21142)
9f0a082650bf is described below

commit 9f0a082650bfeaa0fc5789087bf518e7814bd99f
Author: Luis Sergio Faria Carneiro <[email protected]>
AuthorDate: Thu Jan 29 10:21:28 2026 -0300

    CAMEL-22927: improving SqlServiceLocationHelper to that it gets only the 
required properties (#21142)
---
 .../component/sql/SqlServiceLocationHelper.java    |  21 +--
 .../sql/SqlServiceLocationHelperTest.java          | 180 +++++++++++++++++++++
 2 files changed, 187 insertions(+), 14 deletions(-)

diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlServiceLocationHelper.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlServiceLocationHelper.java
index 7080156b9504..787fdd108ebb 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlServiceLocationHelper.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlServiceLocationHelper.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.component.sql;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import javax.sql.DataSource;
 
 import org.apache.camel.spi.BeanIntrospection;
@@ -34,17 +31,15 @@ public class SqlServiceLocationHelper {
             return ads.getUrl();
         }
 
-        Map<String, Object> props = new HashMap<>();
-        bi.getProperties(ds, props, null, false);
-        Object url = props.get("url");
+        Object url = bi.getOrElseProperty(ds, "url", null, false);
         if (url == null) {
-            url = props.get("jdbcUrl");
+            url = bi.getOrElseProperty(ds, "jdbcUrl", null, false);
         }
         if (url != null) {
             return url.toString();
         } else {
             // nested which can be wrapped in connection pooling
-            DataSource ncf = (DataSource) props.get("dataSource");
+            DataSource ncf = (DataSource) bi.getOrElseProperty(ds, 
"dataSource", null, false);
             if (ncf != null) {
                 return getJDBCURLFromDataSource(bi, ncf);
             }
@@ -61,20 +56,18 @@ public class SqlServiceLocationHelper {
             return ads.getUsername();
         }
 
-        Map<String, Object> props = new HashMap<>();
-        bi.getProperties(ds, props, null, false);
-        Object user = props.get("user");
+        Object user = bi.getOrElseProperty(ds, "user", null, false);
         if (user == null) {
-            user = props.get("username");
+            user = bi.getOrElseProperty(ds, "username", null, false);
         }
         if (user == null) {
-            user = props.get("userName");
+            user = bi.getOrElseProperty(ds, "userName", null, false);
         }
         if (user != null) {
             return user.toString();
         } else {
             // nested which can be wrapped in connection pooling
-            DataSource ncf = (DataSource) props.get("dataSource");
+            DataSource ncf = (DataSource) bi.getOrElseProperty(ds, 
"dataSource", null, false);
             if (ncf != null) {
                 return getUsernameFromConnectionFactory(bi, ncf);
             }
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlServiceLocationHelperTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlServiceLocationHelperTest.java
new file mode 100644
index 000000000000..eef0910e7383
--- /dev/null
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlServiceLocationHelperTest.java
@@ -0,0 +1,180 @@
+/*
+ * 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.sql;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.logging.Logger;
+
+import javax.sql.DataSource;
+
+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 SqlServiceLocationHelperTest {
+
+    public static class FakeDataSource implements DataSource {
+
+        private int invalidCalls;
+
+        public int getInvalidCalls() {
+            return invalidCalls;
+        }
+
+        @Override
+        public <T> T unwrap(Class<T> iface) throws SQLException {
+            invalidCalls++;
+            return null;
+        }
+
+        @Override
+        public boolean isWrapperFor(Class<?> iface) throws SQLException {
+            invalidCalls++;
+            return false;
+        }
+
+        @Override
+        public Logger getParentLogger() throws SQLFeatureNotSupportedException 
{
+            invalidCalls++;
+            return null;
+        }
+
+        @Override
+        public void setLoginTimeout(int seconds) throws SQLException {
+            invalidCalls++;
+        }
+
+        @Override
+        public void setLogWriter(PrintWriter out) throws SQLException {
+            invalidCalls++;
+        }
+
+        @Override
+        public int getLoginTimeout() throws SQLException {
+            invalidCalls++;
+            return 0;
+        }
+
+        @Override
+        public PrintWriter getLogWriter() throws SQLException {
+            invalidCalls++;
+            return null;
+        }
+
+        @Override
+        public Connection getConnection(String username, String password) 
throws SQLException {
+            invalidCalls++;
+            return null;
+        }
+
+        @Override
+        public Connection getConnection() throws SQLException {
+            invalidCalls++;
+            return null;
+        }
+    }
+
+    public static class TestDataSource extends FakeDataSource {
+
+        public String getUrl() {
+            return url;
+        }
+
+        public void setUrl(String url) {
+            this.url = url;
+        }
+
+        public String getJdbcUrl() {
+            return jdbcUrl;
+        }
+
+        public void setJdbcUrl(String jdbcUrl) {
+            this.jdbcUrl = jdbcUrl;
+        }
+
+        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;
+        }
+
+        private String url;
+        private String jdbcUrl;
+        private String user;
+        private String username;
+
+        public TestDataSource(String url, String jdbcUrl, String user, String 
username) {
+            super();
+            this.url = url;
+            this.jdbcUrl = jdbcUrl;
+            this.user = user;
+            this.username = username;
+        }
+
+    }
+
+    @Test
+    public void testGetJDBCURLFromDataSource() {
+        BeanIntrospection bi = new DefaultBeanIntrospection();
+
+        String expectedUrl = "jdbc://expected-url";
+
+        TestDataSource testDs = new TestDataSource(expectedUrl, null, null, 
null);
+        String url = SqlServiceLocationHelper.getJDBCURLFromDataSource(bi, 
testDs);
+        assertEquals(expectedUrl, url);
+        assertEquals(0, testDs.getInvalidCalls());
+
+        testDs = new TestDataSource(null, expectedUrl, null, null);
+        url = SqlServiceLocationHelper.getJDBCURLFromDataSource(bi, testDs);
+        assertEquals(expectedUrl, url);
+        assertEquals(0, testDs.getInvalidCalls());
+
+    }
+
+    @Test
+    public void testGetUsernameFromConnectionFactory() {
+        BeanIntrospection bi = new DefaultBeanIntrospection();
+
+        String expectedUsername = "johndoe";
+
+        TestDataSource testDs = new TestDataSource(null, null, 
expectedUsername, null);
+        String username = 
SqlServiceLocationHelper.getUsernameFromConnectionFactory(bi, testDs);
+        assertEquals(expectedUsername, username);
+        assertEquals(0, testDs.getInvalidCalls());
+
+        testDs = new TestDataSource(null, null, null, expectedUsername);
+        username = 
SqlServiceLocationHelper.getUsernameFromConnectionFactory(bi, testDs);
+        assertEquals(expectedUsername, username);
+        assertEquals(0, testDs.getInvalidCalls());
+    }
+
+}

Reply via email to