camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r673587 - in /activemq/camel/trunk/components/camel-jdbc: ./ src/main/java/org/apache/camel/component/jdbc/ src/test/java/org/apache/camel/component/jdbc/ src/test/resources/
Date Thu, 03 Jul 2008 05:20:31 GMT
Author: davsclaus
Date: Wed Jul  2 22:20:31 2008
New Revision: 673587

URL: http://svn.apache.org/viewvc?rev=673587&view=rev
Log:
Polished the JDBC component to use spring-jdbc for unit testing instead of our own datasource
code etc. And log.warn when error closing JDBC resources as the norm is.

Removed:
    activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/TestDataSource.java
Modified:
    activemq/camel/trunk/components/camel-jdbc/pom.xml
    activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
    activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
    activemq/camel/trunk/components/camel-jdbc/src/test/resources/log4j.properties

Modified: activemq/camel/trunk/components/camel-jdbc/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/pom.xml?rev=673587&r1=673586&r2=673587&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/pom.xml (original)
+++ activemq/camel/trunk/components/camel-jdbc/pom.xml Wed Jul  2 22:20:31 2008
@@ -16,66 +16,72 @@
 	limitations under the License.
 -->
 <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
-	<modelVersion>4.0.0</modelVersion>
-	<parent>
-		<groupId>org.apache.camel</groupId>
-		<artifactId>camel-parent</artifactId>
-		<version>1.4-SNAPSHOT</version>
-	</parent>
-	<artifactId>camel-jdbc</artifactId>
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>camel-parent</artifactId>
+        <version>1.4-SNAPSHOT</version>
+    </parent>
+    <artifactId>camel-jdbc</artifactId>
     <packaging>bundle</packaging>
-	<name>Camel :: JDBC</name>
-	<description>Camel JDBC support</description>
+    <name>Camel :: JDBC</name>
+    <description>Camel JDBC support</description>
 
-  <properties>
-	<camel.osgi.export.pkg>org.apache.camel.component.jdbc.*</camel.osgi.export.pkg>
-  </properties>
+    <properties>
+        <camel.osgi.export.pkg>org.apache.camel.component.jdbc.*</camel.osgi.export.pkg>
+    </properties>
 
-	<dependencies>
-		<dependency>
-			<groupId>org.apache.camel</groupId>
-			<artifactId>camel-core</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.apache.camel</groupId>
-			<artifactId>camel-core</artifactId>
-			<type>test-jar</type>
-			<scope>test</scope>
-		</dependency>
-		<!-- test dependencies -->
-		<dependency>
-			<groupId>junit</groupId>
-			<artifactId>junit</artifactId>
-			<scope>test</scope>
-		</dependency>
-                <!-- to allow Spring annotations (jmx) to be tested -->
-                <dependency>
-                        <groupId>org.springframework</groupId>
-                        <artifactId>spring-context</artifactId>
-                        <version>${spring-version}</version>
-                        <optional>true</optional>
-                        <scope>test</scope>
-                </dependency>
-                <dependency>
-                        <groupId>org.springframework</groupId>
-                        <artifactId>spring-aop</artifactId>
-                        <version>${spring-version}</version>
-                        <optional>true</optional>
-                        <scope>test</scope>
-                </dependency>
+    <dependencies>
 
-<!--
-		<dependency>
-			<groupId>org.apache.derby</groupId>
-			<artifactId>derby</artifactId>
-			<scope>test</scope>
-		</dependency>
--->
-    <dependency>
-      <groupId>hsqldb</groupId>
-      <artifactId>hsqldb</artifactId>
-      <scope>test</scope>
-    </dependency>
-	</dependencies>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+
+        <!-- test dependencies -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-jdbc</artifactId>
+            <version>${spring-version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>hsqldb</groupId>
+            <artifactId>hsqldb</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <!-- to allow Spring annotations (jmx) to be tested -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>${spring-version}</version>
+            <optional>true</optional>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aop</artifactId>
+            <version>${spring-version}</version>
+            <optional>true</optional>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+    
 </project>

Modified: activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java?rev=673587&r1=673586&r2=673587&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
(original)
+++ activemq/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
Wed Jul  2 22:20:31 2008
@@ -23,6 +23,7 @@
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import javax.sql.DataSource;
 
@@ -30,12 +31,17 @@
 import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * @version $Revision$
  */
 public class JdbcProducer extends DefaultProducer<DefaultExchange> {
 
+    private static final transient Log LOG = LogFactory.getLog(JdbcProducer.class);
     private DataSource source;
 
     /** The maximum size for reading a result set <code>readSize</code> */
@@ -56,23 +62,30 @@
         String sql = exchange.getIn().getBody(String.class);
         Connection conn = null;
         Statement stmt = null;
+        ResultSet rs = null;
         try {
             conn = source.getConnection();
             stmt = conn.createStatement();
             if (stmt.execute(sql)) {
-                ResultSet rs = stmt.getResultSet();
+                rs = stmt.getResultSet();
                 setResultSet(exchange, rs);
-                rs.close();
             } else {
                 int updateCount = stmt.getUpdateCount();
                 exchange.getOut().setHeader("jdbc.updateCount", updateCount);
             }
         } finally {
-            if (stmt != null) {
-                stmt.close();
-            }
-            if (conn != null) {
-                conn.close();
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+                if (stmt != null) {
+                    stmt.close();
+                }
+                if (conn != null) {
+                    conn.close();
+                }
+            } catch (SQLException e) {
+                LOG.warn("Error closing JDBC resource: " + e, e);
             }
         }
     }
@@ -87,12 +100,13 @@
 
     public void setResultSet(Exchange exchange, ResultSet rs) throws SQLException {
         ResultSetMetaData meta = rs.getMetaData();
+
         HashMap<String, Object> props = new HashMap<String, Object>();
         IntrospectionSupport.getProperties(meta, props, "jdbc.");
         exchange.getOut().setHeaders(props);
-        //
+
         int count = meta.getColumnCount();
-        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String,
Object>>();
+        List<HashMap<String, Object>> data = new ArrayList<HashMap<String,
Object>>();
         int rowNumber = 0;
         while (rs.next() && rowNumber < readSize) {
             HashMap<String, Object> row = new HashMap<String, Object>();

Modified: activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java?rev=673587&r1=673586&r2=673587&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
(original)
+++ activemq/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
Wed Jul  2 22:20:31 2008
@@ -16,85 +16,94 @@
  */
 package org.apache.camel.component.jdbc;
 
-import java.sql.Connection;
-import java.sql.DriverManager;
 import java.util.ArrayList;
 import java.util.HashMap;
-
 import javax.sql.DataSource;
 
-import junit.framework.TestCase;
-
-import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
-import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.util.jndi.JndiContext;
+import org.apache.camel.impl.JndiRegistry;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
 /**
  * @version $Revision$
  */
-public class JdbcRouteTest extends TestCase {
-    protected String driverClass = "org.hsqldb.jdbcDriver";
-    protected String url = "jdbc:hsqldb:mem:camel_jdbc";
-    protected String user = "sa";
-    protected String password = "";
-    protected Connection connection;
+public class JdbcRouteTest extends ContextTestSupport {
+    private String driverClass = "org.hsqldb.jdbcDriver";
+    private String url = "jdbc:hsqldb:mem:camel_jdbc";
+    private String user = "sa";
+    private String password = "";
+    private DataSource ds;
+    private JdbcTemplate jdbc;
 
     public void testPojoRoutes() throws Exception {
-        // START SNIPPET: register
-        DataSource ds = new TestDataSource(url, user, password);
-        JndiContext context = new JndiContext();
-        context.bind("testdb", ds);
-
-        CamelContext camelContext = new DefaultCamelContext(context);
-        // END SNIPPET: register
-
-        // START SNIPPET: route
-        // lets add simple route
-        camelContext.addRoutes(new RouteBuilder() {
-            public void configure() {
-                from("direct:hello").to("jdbc:testdb?readSize=100");
-            }
-        });
-        // END SNIPPET: route
-
-        camelContext.start();
-
         // START SNIPPET: invoke
-        Endpoint<Exchange> endpoint = camelContext.getEndpoint("direct:hello");
-        ProducerTemplate<Exchange> template = camelContext.createProducerTemplate();
+        // first we create our exchange using the endpoint
+        Endpoint endpoint = context.getEndpoint("direct:hello");
         Exchange exchange = endpoint.createExchange();
-        exchange.getIn().setBody("select * from customer");
+        // then we set the SQL on the in body
+        exchange.getIn().setBody("select * from customer order by ID");
+
+        // now we send the exchange to the endpoint, and receives the response from Camel
         Exchange out = template.send(endpoint, exchange);
+
+        // assertions of the response
         assertNotNull(out);
         assertNotNull(out.getOut());
         ArrayList<HashMap<String, Object>> data = out.getOut().getBody(ArrayList.class);
-        assertNotNull("out body could not be converted to an ArrayList - was: " + out.getOut().getBody(),
data);
+        assertNotNull("out body could not be converted to an ArrayList - was: "
+            + out.getOut().getBody(), data);
         assertEquals(2, data.size());
+        HashMap<String, Object> row = data.get(0);
+        assertEquals("cust1", row.get("ID"));
+        assertEquals("jstrachan", row.get("NAME"));
+        row = data.get(1);
+        assertEquals("cust2", row.get("ID"));
+        assertEquals("nsandhu", row.get("NAME"));
         // END SNIPPET: invoke
+    }
+
+
+    protected JndiRegistry createRegistry() throws Exception {
+        // START SNIPPET: register
+        JndiRegistry reg = super.createRegistry();
+        reg.bind("testdb", ds);
+        return reg;
+        // END SNIPPET: register
+    }
 
-        camelContext.stop();
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            // START SNIPPET: route
+            // lets add simple route
+            public void configure() throws Exception {
+                from("direct:hello").to("jdbc:testdb?readSize=100");
+            }
+            // END SNIPPET: route
+        };
     }
 
-    /**
-     *
-     */
     protected void setUp() throws Exception {
+        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, user, password);
+        dataSource.setDriverClassName(driverClass);
+        ds = dataSource;
+
+        JdbcTemplate jdbc = new JdbcTemplate(ds);
+        // START SNIPPET: setup
+        jdbc.execute("create table customer (id varchar(15), name varchar(10))");
+        jdbc.execute("insert into customer values('cust1','jstrachan')");
+        jdbc.execute("insert into customer values('cust2','nsandhu')");
+        // END SNIPPET: setup
         super.setUp();
-        Class.forName(driverClass);
-        connection = DriverManager.getConnection(url, user, password);
-        connection.createStatement().execute("create table customer (id varchar(15), name
varchar(10))");
-        connection.createStatement().executeUpdate("insert into customer values('cust1','jstrachan')");
-        connection.createStatement().executeUpdate("insert into customer values('cust2','nsandhu')");
     }
 
     protected void tearDown() throws Exception {
-        if (connection != null) {
-            connection.createStatement().execute("drop table customer");
-            connection.close();
-        }
+        super.tearDown();
+        JdbcTemplate jdbc = new JdbcTemplate(ds);
+        jdbc.execute("drop table customer");
     }
+
 }

Modified: activemq/camel/trunk/components/camel-jdbc/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jdbc/src/test/resources/log4j.properties?rev=673587&r1=673586&r2=673587&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jdbc/src/test/resources/log4j.properties (original)
+++ activemq/camel/trunk/components/camel-jdbc/src/test/resources/log4j.properties Wed Jul
 2 22:20:31 2008
@@ -18,7 +18,7 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
 #log4j.logger.org.apache.activemq=DEBUG
 #log4j.logger.org.apache.camel=DEBUG
@@ -28,3 +28,9 @@
 log4j.appender.out.layout=org.apache.log4j.PatternLayout
 log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
 #log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+# File appender
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d %-5p %c{1} - %m %n
+log4j.appender.file.file=target/camel-jdbc-test.log



Mime
View raw message