View Javadoc

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