You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/11/12 10:25:06 UTC
svn commit: r713329 - in /activemq/camel/branches/camel-1.x: ./
components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
Author: davsclaus
Date: Wed Nov 12 01:25:04 2008
New Revision: 713329
URL: http://svn.apache.org/viewvc?rev=713329&view=rev
Log:
Merged revisions 713295 via svnmerge from
https://svn.apache.org/repos/asf/activemq/camel/trunk
........
r713295 | davsclaus | 2008-11-12 08:50:26 +0100 (on, 12 nov 2008) | 1 line
CAMEL-1063: Added dataSourceRef option to lookup DS in registry
........
Added:
activemq/camel/branches/camel-1.x/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
Modified:
activemq/camel/branches/camel-1.x/ (props changed)
activemq/camel/branches/camel-1.x/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
Propchange: activemq/camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Nov 12 01:25:04 2008
@@ -1 +1 @@
-/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713273,713290,713292,713314
+/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713273,713290,713292,713295,713314
Modified: activemq/camel/branches/camel-1.x/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java?rev=713329&r1=713328&r2=713329&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java Wed Nov 12 01:25:04 2008
@@ -30,11 +30,8 @@
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
public class SqlProducer extends DefaultProducer {
-
public static final String UPDATE_COUNT = "org.apache.camel.sql.update-count";
-
private String query;
-
private JdbcTemplate jdbcTemplate;
public SqlProducer(SqlEndpoint endpoint, String query, JdbcTemplate jdbcTemplate) {
@@ -44,10 +41,7 @@
}
public void process(final Exchange exchange) throws Exception {
-
-
jdbcTemplate.execute(query, new PreparedStatementCallback() {
-
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException,
DataAccessException {
int argNumber = 1;
@@ -56,18 +50,15 @@
}
boolean isResultSet = ps.execute();
if (isResultSet) {
- RowMapperResultSetExtractor mapper = new RowMapperResultSetExtractor(
- new ColumnMapRowMapper());
- List<?> result = (List<?>)mapper.extractData(ps.getResultSet());
+ RowMapperResultSetExtractor mapper = new RowMapperResultSetExtractor(new ColumnMapRowMapper());
+ List<?> result = (List<?>) mapper.extractData(ps.getResultSet());
exchange.getOut().setBody(result);
} else {
exchange.getIn().setHeader(UPDATE_COUNT, ps.getUpdateCount());
}
return null;
}
-
});
-
}
}
Added: activemq/camel/branches/camel-1.x/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java?rev=713329&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java (added)
+++ activemq/camel/branches/camel-1.x/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlDataSourceRefTest.java Wed Nov 12 01:25:04 2008
@@ -0,0 +1,109 @@
+/**
+ * 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 javax.sql.DataSource;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.SingleConnectionDataSource;
+
+/**
+ * @version $Revision:$
+ */
+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 JdbcTemplate jdbcTemplate;
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("jdbc/myDataSource", createDataSource());
+ return jndi;
+ }
+
+ public void testSimpleBody() throws Exception {
+ // START SNIPPET: e3
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ // send the query to direct that will route it to the sql where we will execute the query
+ // and bind the parameters with the data from the body. The body only contains one value
+ // in this case (GPL) but if we should use multi values then the body will be iterated
+ // so we could supply a List<String> instead containing each binding value.
+ template.sendBody("direct:simple", "GPL");
+
+ mock.assertIsSatisfied();
+
+ // the result is a List
+ List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());
+
+ // and each row in the list is a Map
+ Map row = assertIsInstanceOf(Map.class, received.get(0));
+
+ // and we should be able the get the project from the map that should be Linux
+ assertEquals("Linux", row.get("PROJECT"));
+ // END SNIPPET: e3
+ }
+
+ protected void setUp() throws Exception {
+ Class.forName(driverClass);
+ super.setUp();
+
+ jdbcTemplate = new JdbcTemplate(createDataSource());
+ // START SNIPPET: e2
+ // this is the database we create with some initial data for our unit test
+ jdbcTemplate.execute("create table projects (id integer primary key,"
+ + "project varchar(10), license varchar(5))");
+ jdbcTemplate.execute("insert into projects values (1, 'Camel', 'ASF')");
+ jdbcTemplate.execute("insert into projects values (2, 'AMQ', 'ASF')");
+ jdbcTemplate.execute("insert into projects values (3, 'Linux', 'GPL')");
+ // END SNIPPET: e2
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ 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() {
+ // START SNIPPET: e1
+ from("direct:simple")
+ .to("sql:select * from projects where license = # order by id?dataSourceRef=jdbc/myDataSource")
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+
+}
\ No newline at end of file