View Javadoc

1   /*
2    * $Id: AbstractJdbcFunctionalTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.jdbc.functional;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.tck.FunctionalTestCase;
15  import org.mule.tck.util.MuleDerbyTestUtils;
16  import org.mule.transport.jdbc.JdbcConnector;
17  import org.mule.transport.jdbc.JdbcUtils;
18  
19  import java.sql.Connection;
20  import java.util.List;
21  
22  import org.apache.commons.dbutils.QueryRunner;
23  import org.apache.commons.dbutils.handlers.ArrayListHandler;
24  
25  public abstract class AbstractJdbcFunctionalTestCase extends FunctionalTestCase
26  {
27      protected static final String[] TEST_VALUES = {"Test", "The Moon", "Terra"};
28  
29      protected JdbcConnector jdbcConnector;
30  
31      private boolean populateTestData = true;
32  
33      protected String getConfigResources()
34      {
35          return "jdbc-connector.xml";
36      }
37  
38      @Override
39      protected void doSetUp() throws Exception
40      {
41          super.doSetUp();
42  
43          jdbcConnector = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector");
44  
45          try
46          {
47              deleteTable();
48          }
49          catch (Exception e)
50          {
51              createTable();
52          }
53  
54          if (populateTestData)
55          {
56              populateTable();
57          }
58      }
59  
60      @Override
61      protected void doTearDown() throws Exception
62      {
63          deleteTable();
64  
65          super.doTearDown();
66      }
67  
68      protected void createTable() throws Exception
69      {
70          QueryRunner qr = jdbcConnector.getQueryRunner();
71          qr.update(jdbcConnector.getConnection(), "CREATE TABLE TEST(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0)  NOT NULL PRIMARY KEY,TYPE INTEGER,DATA VARCHAR(255),ACK TIMESTAMP,RESULT VARCHAR(255))");
72          logger.debug("Table created");
73      }
74      
75      protected void deleteTable() throws Exception
76      {
77          QueryRunner qr = jdbcConnector.getQueryRunner();
78          int updated = qr.update(jdbcConnector.getConnection(), "DELETE FROM TEST");
79          logger.debug(updated + " rows deleted");
80      }
81      
82      protected void populateTable() throws Exception
83      {
84          QueryRunner qr = jdbcConnector.getQueryRunner();
85          int updated;
86          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (1, '" + TEST_VALUES[0] + "')");
87          logger.debug(updated + " rows updated");
88          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (2, '" + TEST_VALUES[1] + "')");
89          logger.debug(updated + " rows updated");
90          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (3, '" + TEST_VALUES[2] + "')");
91          logger.debug(updated + " rows updated");
92      }
93      
94      @Override
95      protected void suitePreSetUp() throws Exception
96      {
97          MuleDerbyTestUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
98          super.suitePreSetUp();
99      }
100 
101     /*
102      * org.apache.commons.dbutils.ResultSetHandler (called by QueryRunner which is
103      * called by JdbcMessageReceiver) allows either null or a List of 0 rows to be
104      * returned so we check for both.
105      */
106     protected static void assertResultSetEmpty(MuleMessage message)
107     {
108         assertNotNull(message);
109         Object payload = message.getPayload();
110         assertTrue(payload instanceof java.util.List);
111         List list = (List)payload;
112         assertTrue(list.isEmpty());
113     }
114 
115     protected static void assertResultSetNotEmpty(MuleMessage message)
116     {
117         assertNotNull(message);
118         Object payload = message.getPayload();
119         assertTrue(payload instanceof java.util.List);
120         List list = (List)payload;
121         assertFalse(list.isEmpty());
122     }
123 
124     public boolean isPopulateTestData()
125     {
126         return populateTestData;
127     }
128 
129     public void setPopulateTestData(boolean populateTestData)
130     {
131         this.populateTestData = populateTestData;
132     }
133     
134     protected List execSqlQuery(String sql) throws Exception
135     {
136         Connection con = null;
137         try
138         {
139             con = jdbcConnector.getConnection();
140             return (List)new QueryRunner().query(con, sql, new ArrayListHandler());
141         }
142         finally
143         {
144             JdbcUtils.close(con);
145         }
146     }
147 
148     protected int execSqlUpdate(String sql) throws Exception
149     {
150         Connection con = null;
151         try
152         {
153             con = jdbcConnector.getConnection();
154             return new QueryRunner().update(con, sql);
155         }
156         finally
157         {
158             JdbcUtils.close(con);
159         }
160     }
161     
162 }
163 
164