1   /*
2    * $Id: JdbcMessageDispatcherTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.jdbc;
12  
13  import org.mule.impl.ImmutableMuleEndpoint;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.endpoint.UMOImmutableEndpoint;
16  
17  import com.mockobjects.dynamic.Mock;
18  
19  import java.sql.Connection;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  
23  import javax.sql.DataSource;
24  
25  import org.apache.commons.dbutils.QueryRunner;
26  import org.apache.commons.dbutils.ResultSetHandler;
27  
28  public class JdbcMessageDispatcherTestCase extends AbstractMuleTestCase
29  {
30  
31      public void testCustomResultSetHandlerIsNotIgnored() throws Exception
32      {
33          UMOImmutableEndpoint ep = new ImmutableMuleEndpoint("jdbc://select * from test", false);
34          JdbcConnector connector = (JdbcConnector)ep.getConnector();
35          connector.setQueryRunner(TestQueryRunner.class.getName());
36          connector.setResultSetHandler(TestResultSetHandler.class.getName());
37          connector.setDataSource(getDataSource());
38          connector.initialise();
39          ep.receive(0);
40      }
41  
42      protected DataSource getDataSource()
43      {
44          Mock mockDataSource = new Mock(DataSource.class);
45          Mock mockConnection = new Mock(Connection.class);
46  
47          mockDataSource.expectAndReturn("getConnection", mockConnection.proxy());
48  
49          mockConnection.expectAndReturn("getAutoCommit", false);
50          mockConnection.expect("commit");
51          mockConnection.expect("close");
52  
53          return (DataSource)mockDataSource.proxy();
54      }
55  
56      public static final class TestQueryRunner extends QueryRunner
57      {
58  
59          public Object query(Connection connection,
60                              String string,
61                              Object[] objects,
62                              ResultSetHandler resultSetHandler) throws SQLException
63          {
64              assertTrue("Custom result set handler has been ignored.",
65                  resultSetHandler instanceof TestResultSetHandler);
66              return new Object();
67          }
68      }
69  
70      public static final class TestResultSetHandler implements ResultSetHandler
71      {
72  
73          public Object handle(ResultSet resultSet) throws SQLException
74          {
75              return new Object();
76          }
77      }
78  }