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   
10  import java.util.List;
11  
12  import org.junit.Test;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertTrue;
16  
17  
18  public class XABridgeJmsJdbcTestCase extends AbstractDerbyTestCase
19  {
20  
21      private static final int NUMBER_OF_MESSAGES = 1;
22      
23      @Override
24      protected String getConfigResources()
25      {
26          return "org/mule/test/integration/transaction/xabridge-jms-jdbc-mule.xml";
27      }
28  
29      @Override
30      protected void emptyTable() throws Exception
31      {
32          try
33          {
34              execSqlUpdate("DELETE FROM TEST");
35          }
36          catch (Exception e)
37          {
38              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))");
39          }
40      }
41  
42      protected void doTestXaBridge(boolean rollback) throws Exception
43      {
44          XABridgeComponent.mayRollback = rollback;
45  
46          List results = execSqlQuery("SELECT * FROM TEST");
47          assertEquals(0, results.size());
48  
49          for (int i = 0; i < NUMBER_OF_MESSAGES; i++)
50          {
51              execSqlUpdate("INSERT INTO TEST(TYPE, DATA) VALUES (1, 'Test " + i + "')");
52          }
53          results = execSqlQuery("SELECT * FROM TEST WHERE TYPE = 1");
54          assertEquals(NUMBER_OF_MESSAGES, results.size());
55  
56          long t0 = System.currentTimeMillis();
57          while (true)
58          {
59              results = execSqlQuery("SELECT * FROM TEST WHERE TYPE = 2");
60              logger.info("Results found: " + results.size());
61              if (results.size() >= NUMBER_OF_MESSAGES)
62              {
63                  break;
64              }
65              System.out.println("Time elapsed: " + (System.currentTimeMillis() - t0));
66              assertTrue(System.currentTimeMillis() - t0 < 40000);
67              Thread.sleep(500);
68          }
69          
70          assertTrue(results.size() >= NUMBER_OF_MESSAGES);
71      }
72  
73      @Test
74      public void testXaBridgeWithoutRollbacks() throws Exception
75      {
76          doTestXaBridge(false);
77      }
78  
79      @Test
80      public void testXaBridgeWithRollbacks() throws Exception
81      {
82          doTestXaBridge(true);
83      }
84  }