View Javadoc

1   /*
2    * $Id: InboundMessageLossTransactionsTestCase.java 22552 2011-07-25 07:18:19Z claude.mamo $
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.transport.jdbc.reliability;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import org.mule.tck.probe.Probe;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  
20  import org.apache.commons.dbutils.handlers.ArrayHandler;
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  
24  
25  /**
26   * Verify that no inbound messages are lost when exceptions occur.  
27   * The message must either make it all the way to the SEDA queue (in the case of 
28   * an asynchronous inbound endpoint), or be restored/rolled back at the source.
29   * 
30   * In the case of JDBC, this will cause the ACK query to not be executed and therefore 
31   * the source data will still be present the next time the database is polled.
32   */
33  public class InboundMessageLossTransactionsTestCase extends InboundMessageLossTestCase
34  {
35      public InboundMessageLossTransactionsTestCase(ConfigVariant variant, String configResources)
36      {
37          super(variant, configResources);     
38      }
39    
40      @Parameters
41      public static Collection<Object[]> parameters()
42      {
43          return Arrays.asList(new Object[][]{
44              {ConfigVariant.SERVICE, "reliability/jdbc-connector.xml, reliability/inbound-message-loss-transactions.xml"},            
45          });
46      }      
47      
48      @Override
49      @Test
50      public void testComponentException() throws Exception
51      {
52          assertEquals(1, qr.update(jdbcConnector.getConnection(), 
53              "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (4, '" + TEST_MESSAGE + "', NULL, NULL)"));
54  
55          prober.check(new Probe()
56          {
57              @Override
58              public boolean isSatisfied()
59              {
60                  try
61                  {
62                      Object[] queryResult = (Object[]) qr.query(jdbcConnector.getConnection(), 
63                          "SELECT DATA FROM TEST WHERE TYPE = 4 AND ACK IS NULL", new ArrayHandler());
64                      // Although a component exception occurs after the SEDA queue, the use of transactions 
65                      // bypasses the SEDA queue, so message should get redelivered.
66                      return (queryResult != null && queryResult.length == 1);
67                  }
68                  catch (Exception e)
69                  {
70                      throw new RuntimeException(e);
71                  }
72              }
73  
74              @Override
75              public String describeFailure()
76              {
77                  return "Row should not be acknowledged (marked read)";
78              }
79          });
80      }    
81  }