001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.transaction.Propagation;
020    import com.liferay.portal.kernel.transaction.TransactionAttribute;
021    import com.liferay.portal.kernel.transaction.TransactionInvokerUtil;
022    
023    import java.util.concurrent.Callable;
024    
025    import org.junit.rules.TestRule;
026    import org.junit.runner.Description;
027    import org.junit.runners.model.Statement;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class TransactionalTestRule implements TestRule {
033    
034            public TransactionalTestRule() {
035                    this(Propagation.SUPPORTS);
036            }
037    
038            public TransactionalTestRule(Propagation propagation) {
039                    TransactionAttribute.Builder builder =
040                            new TransactionAttribute.Builder();
041    
042                    builder.setPropagation(propagation);
043                    builder.setRollbackForClasses(
044                            PortalException.class, SystemException.class);
045    
046                    _transactionAttribute = builder.build();
047            }
048    
049            @Override
050            public Statement apply(final Statement statement, Description description) {
051                    return new Statement() {
052    
053                            @Override
054                            public void evaluate() throws Throwable {
055                                    TransactionInvokerUtil.invoke(
056                                            getTransactionAttribute(), new Callable<Void>() {
057    
058                                            @Override
059                                            public Void call() throws Exception {
060                                                    try {
061                                                            statement.evaluate();
062                                                    }
063                                                    catch (Throwable t) {
064                                                            throw new Exception(t);
065                                                    }
066    
067                                                    return null;
068                                            }
069                                    });
070                            }
071    
072                    };
073            }
074    
075            public TransactionAttribute getTransactionAttribute() {
076                    return _transactionAttribute;
077            }
078    
079            private final TransactionAttribute _transactionAttribute;
080    
081    }