View Javadoc

1   /*
2    * $Id: AbstractDerbyTestCase.java 22551 2011-07-25 06:32:00Z mike.schilling $
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.AbstractServiceAndFlowTestCase;
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.sql.SQLException;
20  import java.util.List;
21  
22  import org.apache.commons.dbutils.QueryRunner;
23  import org.apache.commons.dbutils.handlers.ArrayListHandler;
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  
27  public abstract class AbstractDerbyTestCase extends AbstractServiceAndFlowTestCase
28  {
29      
30      private static String connectionString;
31  
32      public AbstractDerbyTestCase(AbstractServiceAndFlowTestCase.ConfigVariant variant, String configResources)
33      {
34          super(variant, configResources);
35      }
36  
37      @BeforeClass
38      public static void startDatabase() throws Exception
39      {
40          String dbName = MuleDerbyTestUtils.loadDatabaseName("derby.properties", "database.name");
41  
42          MuleDerbyTestUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
43          connectionString = "jdbc:derby:" + dbName;
44      }
45  
46      @AfterClass
47      public static void stopDatabase() throws SQLException
48      {
49          MuleDerbyTestUtils.stopDatabase();
50      }
51  
52      protected void doSetUp() throws Exception
53      {
54          super.doSetUp();
55          emptyTable();
56      }
57  
58      /**
59       * Subclasses must implement this method to either delete the table if it doesn't yet
60       * exist or delete all records from it.
61       */
62      protected abstract void emptyTable() throws Exception;
63  
64      protected Connection getConnection() throws Exception
65      {
66          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
67          return DriverManager.getConnection(connectionString);
68      }
69  
70      protected List execSqlQuery(String sql) throws Exception
71      {
72          Connection con = null;
73          try
74          {
75              con = getConnection();
76              return (List)new QueryRunner().query(con, sql, new ArrayListHandler());
77          }
78          finally
79          {
80              JdbcUtils.close(con);
81          }
82      }
83  
84      protected int execSqlUpdate(String sql) throws Exception
85      {
86          Connection con = null;
87          try
88          {
89              con = getConnection();
90              return new QueryRunner().update(con, sql);
91          }
92          finally
93          {
94              JdbcUtils.close(con);
95          }
96      }
97      
98  }
99  
100