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.tck.functional;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.transaction.Transaction;
11  import org.mule.api.transaction.TransactionException;
12  import org.mule.config.i18n.MessageFactory;
13  
14  /**
15   * This service is useful for unit tests involving transactionality because it
16   * will roll back the current transaction upon message arrival.  
17   */
18  public class TransactionalFunctionalTestComponent extends FunctionalTestComponent
19  {
20      private boolean expectTransaction = true;
21      private boolean rollback = true;
22  
23      /** {@inheritDoc} */
24      public Object onCall(MuleEventContext context) throws Exception
25      {
26          Object replyMessage = super.onCall(context);
27  
28          if (expectTransaction)
29          {
30              // Verify transaction has begun.
31              Transaction currentTx = context.getCurrentTransaction();
32              if (currentTx == null || !currentTx.isBegun())
33              {    
34                  context.setStopFurtherProcessing(true);
35                  throw new TransactionException(MessageFactory.createStaticMessage("Trying to roll back transaction but no transaction is underway."));
36              }            
37  
38              if (rollback)
39              {
40                  // Mark the transaction for rollback.
41                  logger.info("@@@@ Rolling back transaction @@@@");
42                  currentTx.setRollbackOnly();
43              }        
44          }
45          
46          return replyMessage;
47      }
48  
49      public boolean isRollback()
50      {
51          return rollback;
52      }
53  
54      public void setRollback(boolean rollback)
55      {
56          this.rollback = rollback;
57      }
58  
59      public boolean isExpectTransaction()
60      {
61          return expectTransaction;
62      }
63  
64      public void setExpectTransaction(boolean expectTransaction)
65      {
66          this.expectTransaction = expectTransaction;
67      }
68  }
69