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.rule;
016    
017    import com.liferay.portal.kernel.test.rule.AggregateTestRule;
018    import com.liferay.portal.kernel.test.rule.BaseTestRule;
019    import com.liferay.portal.kernel.test.rule.BaseTestRule.StatementWrapper;
020    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRunTestRule;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.ServerDetector;
025    import com.liferay.portal.kernel.util.SystemProperties;
026    import com.liferay.portal.test.rule.callback.ClearThreadLocalTestCallback;
027    import com.liferay.portal.test.rule.callback.LogAssertionTestCallback;
028    import com.liferay.portal.test.rule.callback.UniqueStringRandomizerBumperTestCallback;
029    import com.liferay.portal.util.InitUtil;
030    import com.liferay.portal.util.PropsUtil;
031    import com.liferay.util.log4j.Log4JUtil;
032    
033    import java.util.Collections;
034    import java.util.List;
035    
036    import org.junit.rules.TestRule;
037    import org.junit.runner.Description;
038    import org.junit.runners.model.Statement;
039    
040    /**
041     * @author Shuyang Zhou
042     */
043    public class LiferayIntegrationTestRule extends AggregateTestRule {
044    
045            public LiferayIntegrationTestRule() {
046                    super(
047                            false, CITimeoutTestRule.INSTANCE, LogAssertionTestRule.INSTANCE,
048                            _springInitializationTestRule,
049                            SybaseDumpTransactionLogTestRule.INSTANCE,
050                            _clearThreadLocalTestRule, _uniqueStringRandomizerBumperTestRule,
051                            new DeleteAfterTestRunTestRule());
052            }
053    
054            private static final TestRule _clearThreadLocalTestRule =
055                    new BaseTestRule<>(ClearThreadLocalTestCallback.INSTANCE);
056    
057            private static final TestRule _springInitializationTestRule =
058                    new TestRule() {
059    
060                            @Override
061                            public Statement apply(
062                                    Statement statement, Description description) {
063    
064                                    return new StatementWrapper(statement) {
065    
066                                            @Override
067                                            public void evaluate() throws Throwable {
068                                                    if (!InitUtil.isInitialized()) {
069                                                            ServerDetector.init(ServerDetector.TOMCAT_ID);
070    
071                                                            List<String> configLocations = ListUtil.fromArray(
072                                                                    PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
073    
074                                                            boolean configureLog4j = false;
075    
076                                                            if (GetterUtil.getBoolean(
077                                                                            SystemProperties.get(
078                                                                                    "log4j.configure.on.startup"),
079                                                                            true)) {
080    
081                                                                    SystemProperties.set(
082                                                                            "log4j.configure.on.startup", "false");
083    
084                                                                    configureLog4j = true;
085                                                            }
086    
087                                                            InitUtil.initWithSpring(configLocations, true);
088    
089                                                            if (configureLog4j) {
090                                                                    Log4JUtil.configureLog4J(
091                                                                            InitUtil.class.getClassLoader());
092    
093                                                                    LogAssertionTestCallback.startAssert(
094                                                                            Collections.<ExpectedLogs>emptyList());
095                                                            }
096    
097                                                            if (System.getProperty("external-properties") ==
098                                                                            null) {
099    
100                                                                    System.setProperty(
101                                                                            "external-properties",
102                                                                            "portal-test.properties");
103                                                            }
104                                                    }
105    
106                                                    statement.evaluate();
107                                            }
108    
109                                    };
110                            }
111    
112                    };
113    
114            private static final TestRule _uniqueStringRandomizerBumperTestRule =
115                    new BaseTestRule<>(UniqueStringRandomizerBumperTestCallback.INSTANCE);
116    
117    }