Coverage Report - org.kuali.rice.test.TransactionalLifecycle
 
Classes in this File Line Coverage Branch Coverage Complexity
TransactionalLifecycle
0%
0/22
0%
0/2
1.143
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
         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  0
     public TransactionalLifecycle(String transactionManagerName) {
 44  0
             this.transactionManagerName = transactionManagerName;
 45  0
     }
 46  
     
 47  
     public TransactionalLifecycle() {
 48  0
             this(DEFAULT_TRANSACTION_MANAGER_NAME);
 49  0
     }
 50  
     
 51  
     public void setTransactionManager(PlatformTransactionManager transactionManager) {
 52  0
             this.transactionManager = transactionManager;
 53  0
     }
 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  0
             if (transactionManager == null) {
 62  0
                     transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName);
 63  
             }
 64  0
             return transactionManager;
 65  
     }
 66  
 
 67  
     private boolean started;
 68  
     private TransactionStatus TRANSACTION_STATUS;
 69  
 
 70  
     public boolean isStarted() {
 71  0
         return started;
 72  
     }
 73  
 
 74  
     public void start() throws Exception {
 75  0
             LOG.info("Starting a transaction from TransactionalLifecycle...");
 76  0
         DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
 77  0
         defaultTransactionDefinition.setTimeout(3600);
 78  0
         TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition);
 79  0
         started = true;
 80  0
     }
 81  
 
 82  
     public void stop() throws Exception {
 83  0
             LOG.info("...rolling back transaction from TransactionalLifecycle.");
 84  0
         getTransactionManager().rollback(TRANSACTION_STATUS);
 85  0
         started = false;
 86  0
     }
 87  
 
 88  
 }