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