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.kernel.test.rule;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.test.rule.BaseTestRule.StatementWrapper;
020    import com.liferay.portal.kernel.transaction.Propagation;
021    import com.liferay.portal.kernel.transaction.TransactionAttribute;
022    import com.liferay.portal.kernel.transaction.TransactionInvokerUtil;
023    import com.liferay.portal.kernel.util.ReflectionUtil;
024    
025    import java.util.concurrent.Callable;
026    
027    import org.junit.rules.TestRule;
028    import org.junit.runner.Description;
029    import org.junit.runners.model.Statement;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class TransactionalTestRule implements TestRule {
035    
036            public static final TransactionalTestRule INSTANCE =
037                    new TransactionalTestRule();
038    
039            public TransactionalTestRule() {
040                    this(Propagation.SUPPORTS);
041            }
042    
043            public TransactionalTestRule(Propagation propagation) {
044                    TransactionAttribute.Builder builder =
045                            new TransactionAttribute.Builder();
046    
047                    builder.setPropagation(propagation);
048                    builder.setRollbackForClasses(
049                            PortalException.class, SystemException.class);
050    
051                    _transactionAttribute = builder.build();
052            }
053    
054            @Override
055            public Statement apply(Statement statement, Description description) {
056                    String methodName = description.getMethodName();
057    
058                    if (methodName == null) {
059                            return statement;
060                    }
061    
062                    return new StatementWrapper(statement) {
063    
064                            @Override
065                            public void evaluate() throws Throwable {
066                                    TransactionInvokerUtil.invoke(
067                                            getTransactionAttribute(),
068                                            new Callable<Void>() {
069    
070                                                    @Override
071                                                    public Void call() throws Exception {
072                                                            try {
073                                                                    statement.evaluate();
074                                                            }
075                                                            catch (Throwable t) {
076                                                                    ReflectionUtil.throwException(t);
077                                                            }
078    
079                                                            return null;
080                                                    }
081    
082                                            });
083                            }
084    
085                    };
086            }
087    
088            public TransactionAttribute getTransactionAttribute() {
089                    return _transactionAttribute;
090            }
091    
092            private final TransactionAttribute _transactionAttribute;
093    
094    }