1   /*
2    * $Id: XABridgeJmsJdbcTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.jdbc.JdbcUtils;
15  import org.mule.transport.jdbc.util.MuleDerbyUtils;
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 class XABridgeJmsJdbcTestCase extends FunctionalTestCase
25  {
26      private static String connectionString;
27      
28      protected String getConfigResources()
29      {
30          return "org/mule/test/integration/transaction/xabridge-jms-jdbc-mule.xml";
31      }
32  
33      protected void suitePreSetUp() throws Exception
34      {
35          String dbName = MuleDerbyUtils.loadDatabaseName("derby.properties", "database.name");
36  
37          MuleDerbyUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
38          connectionString = "jdbc:derby:" + dbName;
39       
40          super.suitePreSetUp();
41      }
42  
43      // @Override
44      protected void doSetUp() throws Exception
45      {
46          super.doSetUp();
47          emptyTable();
48      }
49  
50      protected void emptyTable() throws Exception
51      {
52          try
53          {
54              execSqlUpdate("DELETE FROM TEST");
55          }
56          catch (Exception e)
57          {
58              execSqlUpdate("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))");
59          }
60      }
61  
62      protected Connection getConnection() throws Exception
63      {
64          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
65          return DriverManager.getConnection(connectionString);
66      }
67  
68      protected List execSqlQuery(String sql) throws Exception
69      {
70          Connection con = null;
71          try
72          {
73              con = getConnection();
74              return (List)new QueryRunner().query(con, sql, new ArrayListHandler());
75          }
76          finally
77          {
78              JdbcUtils.close(con);
79          }
80      }
81  
82      protected int execSqlUpdate(String sql) throws Exception
83      {
84          Connection con = null;
85          try
86          {
87              con = getConnection();
88              return new QueryRunner().update(con, sql);
89          }
90          finally
91          {
92              JdbcUtils.close(con);
93          }
94      }
95  
96      protected void doTestXaBridge(boolean rollback) throws Exception
97      {
98          XABridgeComponent.mayRollback = rollback;
99  
100         List results = execSqlQuery("SELECT * FROM TEST");
101         assertEquals(0, results.size());
102 
103         for (int i = 0; i < 10; i++)
104         {
105             execSqlUpdate("INSERT INTO TEST(TYPE, DATA) VALUES (1, 'Test " + i + "')");
106         }
107         results = execSqlQuery("SELECT * FROM TEST WHERE TYPE = 1");
108         assertEquals(10, results.size());
109 
110         long t0 = System.currentTimeMillis();
111         while (true)
112         {
113             results = execSqlQuery("SELECT * FROM TEST WHERE TYPE = 2");
114             logger.info("Results found: " + results.size());
115             if (results.size() >= 10)
116             {
117                 break;
118             }
119             assertTrue(System.currentTimeMillis() - t0 < 20000);
120             Thread.sleep(500);
121         }
122     }
123 
124     public void testXaBridgeWithoutRollbacks() throws Exception
125     {
126         doTestXaBridge(false);
127     }
128 
129     public void testXaBridgeWithRollbacks() throws Exception
130     {
131         doTestXaBridge(true);
132     }
133 }