001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Stephane Lacoin 011 */ 012package org.nuxeo.ecm.core.test; 013 014import org.apache.log4j.Logger; 015import org.nuxeo.ecm.core.repository.RepositoryFactory; 016import org.nuxeo.ecm.core.storage.sql.ra.PoolingRepositoryFactory; 017import org.nuxeo.ecm.core.test.annotations.Granularity; 018import org.nuxeo.ecm.core.test.annotations.RepositoryConfig; 019import org.nuxeo.ecm.core.test.annotations.TransactionalConfig; 020import org.nuxeo.runtime.test.runner.ContainerFeature; 021import org.nuxeo.runtime.test.runner.FeaturesRunner; 022import org.nuxeo.runtime.transaction.TransactionHelper; 023 024@RepositoryConfig(cleanup = Granularity.METHOD, repositoryFactoryClass = PoolingRepositoryFactory.class) 025public class TransactionalFeature extends ContainerFeature { 026 027 protected TransactionalConfig config; 028 029 protected String autoactivationValue; 030 031 protected boolean nsOwner; 032 033 protected boolean txStarted; 034 035 protected Class<? extends RepositoryFactory> defaultFactory; 036 037 @Override 038 public void initialize(FeaturesRunner runner) throws Exception { 039 config = runner.getConfig(TransactionalConfig.class); 040 } 041 042 @Override 043 public void beforeSetup(FeaturesRunner runner) throws Exception { 044 if (config.autoStart() == false) { 045 return; 046 } 047 txStarted = TransactionHelper.startTransaction(); 048 } 049 050 @Override 051 public void afterTeardown(FeaturesRunner runner) throws Exception { 052 if (txStarted == false) { 053 if (TransactionHelper.isTransactionActive()) { 054 try { 055 TransactionHelper.setTransactionRollbackOnly(); 056 TransactionHelper.commitOrRollbackTransaction(); 057 } finally { 058 Logger.getLogger(TransactionalFeature.class).warn( 059 "Committing a transaction for your, please do it yourself"); 060 } 061 } 062 return; 063 } 064 TransactionHelper.commitOrRollbackTransaction(); 065 } 066 067}