Author: davsclaus
Date: Tue Nov 11 23:35:48 2008
New Revision: 713290
URL: http://svn.apache.org/viewvc?rev=713290&view=rev
Log:
CAMEL-1063: Added dataSourceRef option to lookup DS in registry
Added:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
(contents, props changed)
- copied, changed from r713277,
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java
Modified:
activemq/camel/trunk/components/camel-sql/pom.xml
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
Modified: activemq/camel/trunk/components/camel-sql/pom.xml
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-sql/pom.xml?rev=713290&r1=713289&r2=713290&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-sql/pom.xml (original)
+++ activemq/camel/trunk/components/camel-sql/pom.xml Tue Nov 11 23:35:48 2008
@@ -67,6 +67,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Modified:
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java?rev=713290&r1=713289&r2=713290&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
(original)
+++
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java
Tue Nov 11 23:35:48 2008
@@ -28,7 +28,6 @@
* @version $Revision:520964 $
*/
public class SqlComponent extends DefaultComponent {
-
private DataSource dataSource;
public SqlComponent() {
@@ -39,12 +38,20 @@
}
@Override
- protected Endpoint createEndpoint(String uri, String remaining, Map
parameters)
- throws Exception {
+ protected Endpoint createEndpoint(String uri, String remaining, Map
parameters) throws Exception {
+ String dataSourceRef = getAndRemoveParameter(parameters,
"dataSourceRef", String.class);
+ if (dataSourceRef != null) {
+ dataSource = getCamelContext().getRegistry().lookup(dataSourceRef,
DataSource.class);
+ if (dataSource == null) {
+ throw new IllegalArgumentException("DataSource " +
dataSourceRef + " not found in registry");
+ }
+ }
+
return new SqlEndpoint(uri, remaining.replaceAll("#", "?"), this,
dataSource, parameters);
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
+
}
Modified:
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java?rev=713290&r1=713289&r2=713290&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
(original)
+++
activemq/camel/trunk/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlEndpoint.java
Tue Nov 11 23:35:48 2008
@@ -32,13 +32,9 @@
* SQL Endpoint. Endpoint URI should contain valid SQL statement, but instead
of
* question marks (that are parameter placeholders), sharp signs should be
used.
* This is because in camel question mark has other meaning.
- *
- * @author romkal
*/
public class SqlEndpoint extends DefaultEndpoint {
-
private JdbcTemplate jdbcTemplate;
-
private String query;
public SqlEndpoint(String uri, String query, Component component,
DataSource dataSource, Map parameters) throws Exception {
@@ -55,7 +51,7 @@
}
public Consumer createConsumer(Processor processor) throws Exception {
- throw new UnsupportedOperationException("Not yet implemented");
+ throw new UnsupportedOperationException("Not implemented");
}
public Producer createProducer() throws Exception {
Copied:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
(from r713277,
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java)
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java?p2=activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java&p1=activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java&r1=713277&r2=713290&rev=713290&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java
(original)
+++
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
Tue Nov 11 23:35:48 2008
@@ -16,30 +16,34 @@
*/
package org.apache.camel.component.sql;
-import java.util.ArrayList;
+import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
-import javax.sql.DataSource;
-
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
-import org.springframework.dao.EmptyResultDataAccessException;
+import org.apache.camel.impl.JndiRegistry;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
/**
* @version $Revision$
*/
-public class SqlRouteTest extends ContextTestSupport {
+public class SqlDataSourceRefTest extends ContextTestSupport {
protected String driverClass = "org.hsqldb.jdbcDriver";
protected String url = "jdbc:hsqldb:mem:camel_jdbc";
protected String user = "sa";
protected String password = "";
- private DataSource ds;
private JdbcTemplate jdbcTemplate;
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("jdbc/myDataSource", createDataSource());
+ return jndi;
+ }
+
public void testSimpleBody() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
@@ -50,80 +54,11 @@
assertEquals("Linux", row.get("PROJECT"));
}
- public void testListBody() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
- List<Object> body = new ArrayList<Object>();
- body.add("ASF");
- body.add("Camel");
- template.sendBody("direct:list", body);
- mock.assertIsSatisfied();
- List received = assertIsInstanceOf(List.class,
mock.getReceivedExchanges().get(0).getIn().getBody());
- Map row = assertIsInstanceOf(Map.class, received.get(0));
- assertEquals(1, row.get("ID"));
- }
-
- public void testBadNumberOfParameter() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
- template.sendBody("direct:list", "ASF");
- mock.assertIsSatisfied();
- List received = assertIsInstanceOf(List.class,
mock.getReceivedExchanges().get(0).getIn().getBody());
- assertEquals(0, received.size());
- }
-
- public void testListResult() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
- List<Object> body = new ArrayList<Object>();
- body.add("ASF");
- template.sendBody("direct:simple", body);
- mock.assertIsSatisfied();
- List received = assertIsInstanceOf(List.class,
mock.getReceivedExchanges().get(0).getIn().getBody());
- assertEquals(2, received.size());
- Map row1 = assertIsInstanceOf(Map.class, received.get(0));
- assertEquals("Camel", row1.get("PROJECT"));
- Map row2 = assertIsInstanceOf(Map.class, received.get(1));
- assertEquals("AMQ", row2.get("PROJECT"));
- }
-
- public void testListLimitedResult() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
- List<Object> body = new ArrayList<Object>();
- body.add("ASF");
- template.sendBody("direct:simpleLimited", body);
- mock.assertIsSatisfied();
- List received = assertIsInstanceOf(List.class,
mock.getReceivedExchanges().get(0).getIn().getBody());
- assertEquals(1, received.size());
- Map row1 = assertIsInstanceOf(Map.class, received.get(0));
- assertEquals("Camel", row1.get("PROJECT"));
- }
-
- public void testInsert() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
-
- template.sendBody("direct:insert", new Object[] {10, "test", "test"});
- mock.assertIsSatisfied();
- try {
- String projectName = (String)jdbcTemplate
- .queryForObject("select project from projects where id = 10",
String.class);
- assertEquals("test", projectName);
- } catch (EmptyResultDataAccessException e) {
- fail("no row inserted");
- }
-
- Integer actualUpdateCount =
mock.getExchanges().get(0).getIn().getHeader(SqlProducer.UPDATE_COUNT,
-
Integer.class);
- assertEquals((Integer)1, actualUpdateCount);
- }
-
protected void setUp() throws Exception {
Class.forName(driverClass);
super.setUp();
- jdbcTemplate = new JdbcTemplate(ds);
+ jdbcTemplate = new JdbcTemplate(createDataSource());
jdbcTemplate.execute("create table projects (id integer primary key,"
+ "project varchar(10), license varchar(5))");
jdbcTemplate.execute("insert into projects values (1, 'Camel',
'ASF')");
@@ -133,32 +68,25 @@
protected void tearDown() throws Exception {
super.tearDown();
- JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
+ JdbcTemplate jdbcTemplate = new JdbcTemplate(createDataSource());
jdbcTemplate.execute("drop table projects");
}
+ private DataSource createDataSource() {
+ return new SingleConnectionDataSource(url, user, password, true);
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() {
- ds = new SingleConnectionDataSource(url, user, password, true);
-
- getContext().getComponent("sql",
SqlComponent.class).setDataSource(ds);
-
- from("direct:simple").to("sql:select * from projects where
license = # order by id")
+ // START SNIPPET: e1
+ from("direct:simple")
+ .to("sql:select * from projects where license = # order by
id?dataSourceRef=jdbc/myDataSource")
.to("mock:result");
-
- from("direct:list")
- .to("sql:select * from projects where license = # and
project = # order by id")
- .to("mock:result");
-
- from("direct:simpleLimited")
- .to("sql:select * from projects where license = # order by
id?template.maxRows=1")
- .to("mock:result");
-
- from("direct:insert").to("sql:insert into projects values (#,
#, #)").to("mock:result");
+ // END SNIPPET: e1
}
};
}
-}
+}
\ No newline at end of file
Propchange:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
activemq/camel/trunk/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
------------------------------------------------------------------------------
svn:mergeinfo =