View Javadoc

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