001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.test; 017 018 import org.kuali.rice.core.api.lifecycle.Lifecycle; 019 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 020 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 021 import org.springframework.transaction.PlatformTransactionManager; 022 import org.springframework.transaction.TransactionStatus; 023 import org.springframework.transaction.support.DefaultTransactionDefinition; 024 025 /** 026 * A lifecycle for testing with database transactional rollback. 027 * @author Kuali Rice Team (rice.collab@kuali.org) 028 */ 029 public class TransactionalLifecycle implements Lifecycle { 030 031 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger 032 .getLogger(TransactionalLifecycle.class); 033 034 /** 035 * Name of the transaction manager to pull from the GlobalResourceLoader. 036 * This will most likely be a Spring bean name. 037 */ 038 public static final String DEFAULT_TRANSACTION_MANAGER_NAME = "transactionManager"; 039 040 private String transactionManagerName; 041 private PlatformTransactionManager transactionManager; 042 043 public TransactionalLifecycle(String transactionManagerName) { 044 this.transactionManagerName = transactionManagerName; 045 } 046 047 public TransactionalLifecycle() { 048 this(DEFAULT_TRANSACTION_MANAGER_NAME); 049 } 050 051 public void setTransactionManager(PlatformTransactionManager transactionManager) { 052 this.transactionManager = transactionManager; 053 } 054 055 /** 056 * Returns the PlatformTransactionManager configured on this lifecycle. 057 * If none defined, pulls the transaction manager out of the GlobalResourceLoader 058 * @return the transaction manager in the GlobalResourceLoader 059 */ 060 private PlatformTransactionManager getTransactionManager() { 061 if (transactionManager == null) { 062 transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName); 063 } 064 return transactionManager; 065 } 066 067 private boolean started; 068 private TransactionStatus TRANSACTION_STATUS; 069 070 public boolean isStarted() { 071 return started; 072 } 073 074 public void start() throws Exception { 075 LOG.info("Starting a transaction from TransactionalLifecycle..."); 076 DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); 077 defaultTransactionDefinition.setTimeout(3600); 078 TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition); 079 started = true; 080 } 081 082 public void stop() throws Exception { 083 LOG.info("...rolling back transaction from TransactionalLifecycle."); 084 getTransactionManager().rollback(TRANSACTION_STATUS); 085 started = false; 086 } 087 088 }