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   import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
10  import org.hamcrest.core.IsNull;
11  import org.junit.Before;
12  import org.junit.Test;
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.functional.EventCallback;
17  import org.mule.tck.functional.FunctionalTestComponent;
18  import org.mule.util.concurrent.Latch;
19  
20  import static org.junit.Assert.assertThat;
21  
22  
23  public class XaTransactionJdbcJmsTestCase extends AbstractDerbyTestCase
24  {
25  
26      public static final int TIMEOUT = 3000;
27  
28      public XaTransactionJdbcJmsTestCase()
29      {
30          setStartContext(false);
31      }
32  
33      @Before
34      public void setUp() throws Exception
35      {
36          super.doSetUp();
37      }
38  
39      @Override
40      protected void emptyTable() throws Exception
41      {
42          try
43          {
44              execSqlUpdate("DELETE FROM TEST");
45          }
46          catch (Exception e)
47          {
48              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))");
49          }
50      }
51  
52      @Override
53      protected String getConfigResources()
54      {
55          return "org/mule/test/integration/transaction/xa-transaction-jms-jdbc.xml";
56      }
57  
58      @Test
59      public void testTransactionFromJdbcToJms() throws Exception
60      {
61          final Latch latch = new Latch();
62          MuleClient muleClient = new MuleClient(muleContext);
63          muleContext.start();
64          FunctionalTestComponent ftc = getFunctionalTestComponent("JdbcToJms");
65          ftc.setEventCallback(new EventCallback()
66          {
67              public void eventReceived(MuleEventContext context, Object component) throws Exception
68              {
69                  latch.release();
70              }
71          });
72          execSqlUpdate("INSERT INTO TEST(TYPE, DATA) VALUES (1, 'Test1')");
73          latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
74          MuleMessage muleMessage = muleClient.request("myQueueNotXa", TIMEOUT);
75          assertThat("a message should be commit to jms",muleMessage, IsNull.<Object>notNullValue());
76          assertThat("no exception should happen",muleMessage.getExceptionPayload(), IsNull.<Object>nullValue());
77      }
78  }