1   /*
2    * $Id: AbstractJdbcFunctionalTestCase.java 10787 2008-02-12 18:51:50Z dfeist $
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.transport.jdbc.functional;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.tck.FunctionalTestCase;
15  import org.mule.transport.jdbc.JdbcConnector;
16  import org.mule.transport.jdbc.util.MuleDerbyUtils;
17  
18  import java.util.List;
19  
20  import org.apache.commons.dbutils.QueryRunner;
21  
22  public abstract class AbstractJdbcFunctionalTestCase extends FunctionalTestCase
23  {
24      protected static final String[] TEST_VALUES = {"Test", "The Moon", "Terra"};
25  
26      private boolean populateTestData = true;
27      
28      JdbcConnector jdbcConnector = null;
29      
30      protected String getConfigResources()
31      {
32          return "jdbc-connector.xml";
33      }
34  
35      // @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39  
40          jdbcConnector = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector");
41  
42          try
43          {
44              deleteTable();
45          }
46          catch (Exception e)
47          {
48              createTable();
49          }
50  
51          if (populateTestData)
52          {
53              populateTable();
54          }
55      }
56  
57      // @Override
58      protected void doTearDown() throws Exception
59      {
60          deleteTable();
61  
62          super.doTearDown();
63      }
64  
65      protected void createTable() throws Exception
66      {
67          QueryRunner qr = jdbcConnector.getQueryRunner();
68          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))");
69          logger.debug("Table created");
70      }
71      
72      protected void deleteTable() throws Exception
73      {
74          QueryRunner qr = jdbcConnector.getQueryRunner();
75          int updated = qr.update(jdbcConnector.getConnection(), "DELETE FROM TEST");
76          logger.debug(updated + " rows deleted");
77      }
78      
79      protected void populateTable() throws Exception
80      {
81          QueryRunner qr = jdbcConnector.getQueryRunner();
82          int updated;
83          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (1, '" + TEST_VALUES[0] + "')");
84          logger.debug(updated + " rows updated");
85          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (2, '" + TEST_VALUES[1] + "')");
86          logger.debug(updated + " rows updated");
87          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (3, '" + TEST_VALUES[2] + "')");
88          logger.debug(updated + " rows updated");
89      }
90      
91      // @Override
92      protected void suitePreSetUp() throws Exception
93      {
94          MuleDerbyUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
95          super.suitePreSetUp();
96      }
97  
98      /*
99       * org.apache.commons.dbutils.ResultSetHandler (called by QueryRunner which is
100      * called by JdbcMessageReceiver) allows either null or a List of 0 rows to be
101      * returned so we check for both.
102      */
103     protected static void assertResultSetEmpty(MuleMessage message)
104     {
105         assertNotNull(message);
106         Object payload = message.getPayload();
107         assertTrue(payload instanceof java.util.List);
108         List list = (List)payload;
109         assertTrue(list.isEmpty());
110     }
111 
112     protected static void assertResultSetNotEmpty(MuleMessage message)
113     {
114         assertNotNull(message);
115         Object payload = message.getPayload();
116         assertTrue(payload instanceof java.util.List);
117         List list = (List)payload;
118         assertFalse(list.isEmpty());
119     }
120 
121     public boolean isPopulateTestData()
122     {
123         return populateTestData;
124     }
125 
126     public void setPopulateTestData(boolean populateTestData)
127     {
128         this.populateTestData = populateTestData;
129     }
130 }
131 
132