Repository: camel
Updated Branches:
  refs/heads/master aa7780182 -> 0548f87de


CAMEL-10618: allow to use spring-boot DataSource


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ec3f9e73
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ec3f9e73
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ec3f9e73

Branch: refs/heads/master
Commit: ec3f9e73e866bf735ec7c8cdb3bcd10b9f47c30b
Parents: aa77801
Author: Nicola Ferraro <ni.ferr...@gmail.com>
Authored: Mon Feb 13 17:15:02 2017 +0100
Committer: Nicola Ferraro <ni.ferr...@gmail.com>
Committed: Mon Feb 13 17:15:02 2017 +0100

----------------------------------------------------------------------
 .../camel/component/sql/SqlComponent.java       | 10 ++++
 .../camel-sql-starter/pom.xml                   |  6 +++
 .../sql/DataSourceAutoConfigurationTest.java    | 48 +++++++++++++++++++
 .../component/sql/support/DummyJDBCDriver.java  | 50 ++++++++++++++++++++
 4 files changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ec3f9e73/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
index 1a1e28c..d26511a 100755
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.sql;
 
 import java.util.Map;
+import java.util.Set;
 import javax.sql.DataSource;
 
 import org.apache.camel.CamelContext;
@@ -71,6 +72,15 @@ public class SqlComponent extends UriEndpointComponent {
             target = dataSource;
         }
         if (target == null) {
+            // check if the registry contains a single instance of DataSource
+            Set<DataSource> dataSources = 
getCamelContext().getRegistry().findByType(DataSource.class);
+            if (dataSources.size() > 1) {
+                throw new IllegalArgumentException("Multiple DataSources found 
in the registry and no explicit configuration provided");
+            } else if (dataSources.size() == 1) {
+                target = dataSources.stream().findFirst().orElse(null);
+            }
+        }
+        if (target == null) {
             throw new IllegalArgumentException("DataSource must be 
configured");
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/ec3f9e73/platforms/spring-boot/components-starter/camel-sql-starter/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-sql-starter/pom.xml 
b/platforms/spring-boot/components-starter/camel-sql-starter/pom.xml
index a094f10..85b82af 100644
--- a/platforms/spring-boot/components-starter/camel-sql-starter/pom.xml
+++ b/platforms/spring-boot/components-starter/camel-sql-starter/pom.xml
@@ -45,6 +45,12 @@
       </exclusions>
       <!--END OF GENERATED CODE-->
     </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-jdbc</artifactId>
+      <version>${spring-boot-version}</version>
+      <scope>test</scope>
+    </dependency>
     <!--START OF GENERATED CODE-->
     <dependency>
       <groupId>org.apache.camel</groupId>

http://git-wip-us.apache.org/repos/asf/camel/blob/ec3f9e73/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/DataSourceAutoConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/DataSourceAutoConfigurationTest.java
 
b/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/DataSourceAutoConfigurationTest.java
new file mode 100644
index 0000000..0528e04
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/DataSourceAutoConfigurationTest.java
@@ -0,0 +1,48 @@
+package org.apache.camel.component.sql;
+
+import javax.sql.DataSource;
+
+import org.apache.camel.CamelContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(SpringRunner.class)
+@SpringBootApplication
+@DirtiesContext
+@ContextConfiguration(classes = DataSourceAutoConfigurationTest.class)
+@SpringBootTest(properties = {
+        "spring.datasource.url=jdbc:dummy://localhost/test",
+        "spring.datasource.username=dbuser",
+        "spring.datasource.password=dbpass",
+        
"spring.datasource.driver-class-name=org.apache.camel.component.sql.support.DummyJDBCDriver"
+})
+public class DataSourceAutoConfigurationTest {
+
+    @Autowired
+    private DataSource datasource;
+
+    @Autowired
+    private CamelContext context;
+
+    @Test
+    public void testInjectionWorks() {
+        assertNotNull(datasource);
+    }
+
+    @Test
+    public void testCamelUsesTheConfiguredDatasource() throws Exception {
+        SqlComponent component = (SqlComponent) context.getComponent("sql");
+        SqlEndpoint endpoint = (SqlEndpoint) 
component.createEndpoint("sql:select * from table where id=#");
+        assertEquals(datasource, endpoint.getJdbcTemplate().getDataSource());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ec3f9e73/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/support/DummyJDBCDriver.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/support/DummyJDBCDriver.java
 
b/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/support/DummyJDBCDriver.java
new file mode 100644
index 0000000..7615014
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-sql-starter/src/test/java/org/apache/camel/component/sql/support/DummyJDBCDriver.java
@@ -0,0 +1,50 @@
+package org.apache.camel.component.sql.support;
+
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverPropertyInfo;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+/**
+ * Dummy JDBCDriver to be used in tests.
+ */
+public class DummyJDBCDriver implements Driver {
+
+    @Override
+    public Connection connect(String s, Properties properties) throws 
SQLException {
+        return null;
+    }
+
+    @Override
+    public boolean acceptsURL(String s) throws SQLException {
+        return false;
+    }
+
+    @Override
+    public DriverPropertyInfo[] getPropertyInfo(String s, Properties 
properties) throws SQLException {
+        return new DriverPropertyInfo[0];
+    }
+
+    @Override
+    public int getMajorVersion() {
+        return 0;
+    }
+
+    @Override
+    public int getMinorVersion() {
+        return 0;
+    }
+
+    @Override
+    public boolean jdbcCompliant() {
+        return false;
+    }
+
+    @Override
+    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+        return null;
+    }
+}

Reply via email to