1   /*
2    * $Id: XAJdbcMule1479TestCase.java 10789 2008-02-12 20:04:43Z 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  package org.mule.test.integration.transaction;
11  
12  import org.mule.module.client.MuleClient;
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.transport.jdbc.JdbcUtils;
15  import org.mule.transport.jms.JmsConnector;
16  import org.mule.transport.jms.JmsConstants;
17  import org.mule.transport.jms.activemq.ActiveMQJmsConnector;
18  
19  import java.sql.Connection;
20  import java.sql.DriverManager;
21  import java.util.List;
22  
23  import org.apache.commons.dbutils.QueryRunner;
24  import org.apache.commons.dbutils.handlers.ArrayListHandler;
25  
26  public class XAJdbcMule1479TestCase extends FunctionalTestCase
27  {
28      protected String getConfigResources()
29      {
30          return "org/mule/test/integration/transaction/jdbc-xatransaction-1479.xml";
31      }
32  
33      protected void doPostFunctionalSetUp() throws Exception
34      {
35          emptyTable();
36      }
37  
38      protected void emptyTable() throws Exception
39      {
40          try
41          {
42              execSqlUpdate("DELETE FROM TEST");
43          }
44          catch (Exception e)
45          {
46              execSqlUpdate("CREATE TABLE TEST(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0)  NOT NULL PRIMARY KEY,DATA VARCHAR(255))");
47          }
48      }
49  
50      protected Connection getConnection() throws Exception
51      {
52          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
53          return DriverManager.getConnection("jdbc:derby:muleEmbeddedDB;create=true");
54      }
55  
56      protected List execSqlQuery(String sql) throws Exception
57      {
58          Connection con = null;
59          try
60          {
61              con = getConnection();
62              return (List)new QueryRunner().query(con, sql, new ArrayListHandler());
63          }
64          finally
65          {
66              JdbcUtils.close(con);
67          }
68      }
69  
70      protected int execSqlUpdate(String sql) throws Exception
71      {
72          Connection con = null;
73          try
74          {
75              con = getConnection();
76              return new QueryRunner().update(con, sql);
77          }
78          finally
79          {
80              JdbcUtils.close(con);
81          }
82      }
83  
84  /*
85      public void testJdbcXa() throws Exception
86      {
87  
88          MuleClient client = new MuleClient();
89  
90          client.dispatch("vm://in","test",null);
91  
92          List results = execSqlQuery("SELECT * FROM TEST");
93          assertEquals(0, results.size());
94      }    
95  */
96  
97      public void testJmsXa() throws Exception
98      {
99  
100         MuleClient client = new MuleClient();
101 
102         client.dispatch("vm://in1","test",null);
103 
104         logger.debug("########### receiving message");
105 
106 //        MuleMessage res = client.receive("jms://queue.out", 1000);
107 //        assertNotNull(res);
108 
109         Thread.sleep(5000000);
110 
111         List results = execSqlQuery("SELECT * FROM TEST");
112         assertEquals(1, results.size());
113     }
114 
115     public JmsConnector createConnector() throws Exception
116     {
117         ActiveMQJmsConnector connector = new ActiveMQJmsConnector();
118         connector.setSpecification(JmsConstants.JMS_SPECIFICATION_11);
119         connector.setName("myConnector");
120         connector.getDispatcherThreadingProfile().setDoThreading(false);
121         return connector;
122     }
123     
124 
125 }