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.callback.CompanyProviderTestCallback;
021    import com.liferay.portal.kernel.test.rule.callback.DeleteAfterTestRunTestCallback;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.ServerDetector;
026    import com.liferay.portal.kernel.util.SystemProperties;
027    import com.liferay.portal.test.rule.callback.CITimeoutTestCallback;
028    import com.liferay.portal.test.rule.callback.ClearThreadLocalTestCallback;
029    import com.liferay.portal.test.rule.callback.LogAssertionTestCallback;
030    import com.liferay.portal.test.rule.callback.MainServletTestCallback;
031    import com.liferay.portal.test.rule.callback.SybaseDumpTransactionLogTestCallback;
032    import com.liferay.portal.test.rule.callback.UniqueStringRandomizerBumperTestCallback;
033    import com.liferay.portal.util.InitUtil;
034    import com.liferay.portal.util.PropsUtil;
035    import com.liferay.util.log4j.Log4JUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Collections;
039    import java.util.List;
040    
041    import org.junit.rules.TestRule;
042    import org.junit.runner.Description;
043    import org.junit.runners.model.Statement;
044    
045    /**
046     * @author Shuyang Zhou
047     */
048    public class LiferayIntegrationTestRule extends AggregateTestRule {
049    
050            public LiferayIntegrationTestRule() {
051                    super(false, _getTestRules());
052            }
053    
054            private static TestRule[] _getTestRules() {
055                    List<TestRule> testRules = new ArrayList<>();
056    
057                    if (System.getenv("JENKINS_HOME") != null) {
058                            testRules.add(_ciTimeoutTestRule);
059                    }
060    
061                    testRules.add(LogAssertionTestRule.INSTANCE);
062                    testRules.add(_springInitializationTestRule);
063                    testRules.add(_sybaseDumpTransactionLogTestRule);
064                    testRules.add(_clearThreadLocalTestRule);
065                    testRules.add(_uniqueStringRandomizerBumperTestRule);
066                    testRules.add(_mainServletTestRule);
067                    testRules.add(_companyProviderTestRule);
068                    testRules.add(_deleteAfterTestRunTestRule);
069    
070                    return testRules.toArray(new TestRule[testRules.size()]);
071            }
072    
073            private static final TestRule _ciTimeoutTestRule = new BaseTestRule<>(
074                    CITimeoutTestCallback.INSTANCE);
075            private static final TestRule _clearThreadLocalTestRule =
076                    new BaseTestRule<>(ClearThreadLocalTestCallback.INSTANCE);
077            private static final TestRule _companyProviderTestRule = new BaseTestRule<>(
078                    CompanyProviderTestCallback.INSTANCE);
079            private static final TestRule _deleteAfterTestRunTestRule =
080                    new BaseTestRule<>(DeleteAfterTestRunTestCallback.INSTANCE);
081            private static final TestRule _mainServletTestRule = new BaseTestRule<>(
082                    MainServletTestCallback.INSTANCE);
083    
084            private static final TestRule _springInitializationTestRule =
085                    new TestRule() {
086    
087                            @Override
088                            public Statement apply(
089                                    Statement statement, Description description) {
090    
091                                    return new StatementWrapper(statement) {
092    
093                                            @Override
094                                            public void evaluate() throws Throwable {
095                                                    if (!InitUtil.isInitialized()) {
096                                                            ServerDetector.init(ServerDetector.TOMCAT_ID);
097    
098                                                            List<String> configLocations = ListUtil.fromArray(
099                                                                    PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
100    
101                                                            boolean configureLog4j = false;
102    
103                                                            if (GetterUtil.getBoolean(
104                                                                            SystemProperties.get(
105                                                                                    "log4j.configure.on.startup"),
106                                                                            true)) {
107    
108                                                                    SystemProperties.set(
109                                                                            "log4j.configure.on.startup", "false");
110    
111                                                                    configureLog4j = true;
112                                                            }
113    
114                                                            InitUtil.initWithSpring(configLocations, true);
115    
116                                                            if (configureLog4j) {
117                                                                    Log4JUtil.configureLog4J(
118                                                                            InitUtil.class.getClassLoader());
119    
120                                                                    LogAssertionTestCallback.startAssert(
121                                                                            Collections.<ExpectedLogs>emptyList());
122                                                            }
123    
124                                                            if (System.getProperty("external-properties") ==
125                                                                            null) {
126    
127                                                                    System.setProperty(
128                                                                            "external-properties",
129                                                                            "portal-test.properties");
130                                                            }
131                                                    }
132    
133                                                    statement.evaluate();
134                                            }
135    
136                                    };
137                            }
138    
139                    };
140    
141            private static final TestRule _sybaseDumpTransactionLogTestRule =
142                    new BaseTestRule<>(SybaseDumpTransactionLogTestCallback.INSTANCE);
143            private static final TestRule _uniqueStringRandomizerBumperTestRule =
144                    new BaseTestRule<>(UniqueStringRandomizerBumperTestCallback.INSTANCE);
145    
146    }