View Javadoc

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