View Javadoc

1   /*
2    * $Id: AbstractDerbyTestCase.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.test.integration.transaction;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.tck.util.MuleDerbyTestUtils;
15  import org.mule.transport.jdbc.JdbcUtils;
16  
17  import java.sql.Connection;
18  import java.sql.DriverManager;
19  import java.util.List;
20  
21  import org.apache.commons.dbutils.QueryRunner;
22  import org.apache.commons.dbutils.handlers.ArrayListHandler;
23  
24  public abstract class AbstractDerbyTestCase extends FunctionalTestCase
25  {
26      
27      private static String connectionString;
28  
29      protected void suitePreSetUp() throws Exception
30      {
31          String dbName = MuleDerbyTestUtils.loadDatabaseName("derby.properties", "database.name");
32      
33          MuleDerbyTestUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
34          connectionString = "jdbc:derby:" + dbName;
35      
36          super.suitePreSetUp();
37      }
38  
39      protected void doSetUp() throws Exception
40      {
41          super.doSetUp();
42          emptyTable();
43      }
44  
45      /**
46       * Subclasses must implement this method to either delete the table if it doesn't yet
47       * exist or delete all records from it.
48       */
49      protected abstract void emptyTable() throws Exception;
50  
51      protected Connection getConnection() throws Exception
52      {
53          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
54          return DriverManager.getConnection(connectionString);
55      }
56  
57      protected List execSqlQuery(String sql) throws Exception
58      {
59          Connection con = null;
60          try
61          {
62              con = getConnection();
63              return (List)new QueryRunner().query(con, sql, new ArrayListHandler());
64          }
65          finally
66          {
67              JdbcUtils.close(con);
68          }
69      }
70  
71      protected int execSqlUpdate(String sql) throws Exception
72      {
73          Connection con = null;
74          try
75          {
76              con = getConnection();
77              return new QueryRunner().update(con, sql);
78          }
79          finally
80          {
81              JdbcUtils.close(con);
82          }
83      }
84      
85  }
86  
87