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