View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.test;
17  
18  import org.kuali.rice.core.api.lifecycle.Lifecycle;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.springframework.transaction.PlatformTransactionManager;
22  import org.springframework.transaction.TransactionStatus;
23  import org.springframework.transaction.support.DefaultTransactionDefinition;
24  
25  /**
26   * A lifecycle for testing with database transactional rollback.
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class TransactionalLifecycle implements Lifecycle {
30  	
31  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
32  			.getLogger(TransactionalLifecycle.class);
33  	
34      /**
35       * Name of the transaction manager to pull from the GlobalResourceLoader.
36       * This will most likely be a Spring bean name.
37       */
38      public static final String DEFAULT_TRANSACTION_MANAGER_NAME = "transactionManager";
39  
40      private String transactionManagerName;
41      private PlatformTransactionManager transactionManager;
42  
43      public TransactionalLifecycle(String transactionManagerName) {
44      	this.transactionManagerName = transactionManagerName;
45      }
46      
47      public TransactionalLifecycle() {
48      	this(DEFAULT_TRANSACTION_MANAGER_NAME);
49      }
50      
51      public void setTransactionManager(PlatformTransactionManager transactionManager) {
52      	this.transactionManager = transactionManager;
53      }
54      
55      /**
56       * Returns the PlatformTransactionManager configured on this lifecycle.
57       * If none defined, pulls the transaction manager out of the GlobalResourceLoader
58       * @return the transaction manager in the GlobalResourceLoader
59       */
60      private PlatformTransactionManager getTransactionManager() {
61      	if (transactionManager == null) {
62      		transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName);
63      	}
64      	return transactionManager;
65      }
66  
67      private boolean started;
68      private TransactionStatus TRANSACTION_STATUS;
69  
70      public boolean isStarted() {
71          return started;
72      }
73  
74      public void start() throws Exception {
75      	LOG.info("Starting a transaction from TransactionalLifecycle...");
76          DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
77          defaultTransactionDefinition.setTimeout(3600);
78          TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition);
79          started = true;
80      }
81  
82      public void stop() throws Exception {
83      	LOG.info("...rolling back transaction from TransactionalLifecycle.");
84          getTransactionManager().rollback(TRANSACTION_STATUS);
85          started = false;
86      }
87  
88  }