View Javadoc

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