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.DeleteAfterTestRunTestRule;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.ServerDetector;
023    import com.liferay.portal.test.rule.callback.ClearThreadLocalTestCallback;
024    import com.liferay.portal.test.rule.callback.UniqueStringRandomizerBumperTestCallback;
025    import com.liferay.portal.util.InitUtil;
026    import com.liferay.portal.util.PropsUtil;
027    
028    import java.util.List;
029    
030    import org.junit.rules.TestRule;
031    import org.junit.runner.Description;
032    import org.junit.runners.model.Statement;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class LiferayIntegrationTestRule extends AggregateTestRule {
038    
039            public LiferayIntegrationTestRule() {
040                    super(
041                            false, CITimeoutTestRule.INSTANCE, LogAssertionTestRule.INSTANCE,
042                            SybaseDumpTransactionLogTestRule.INSTANCE,
043                            _clearThreadLocalTestRule, _uniqueStringRandomizerBumperTestRule,
044                            new DeleteAfterTestRunTestRule());
045            }
046    
047            @Override
048            public Statement apply(Statement statement, Description description) {
049                    if (!InitUtil.isInitialized()) {
050                            ServerDetector.init(ServerDetector.TOMCAT_ID);
051    
052                            List<String> configLocations = ListUtil.fromArray(
053                                    PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
054    
055                            InitUtil.initWithSpring(configLocations, true);
056    
057                            if (System.getProperty("external-properties") == null) {
058                                    System.setProperty(
059                                            "external-properties", "portal-test.properties");
060                            }
061                    }
062    
063                    return super.apply(statement, description);
064            }
065    
066            private static final TestRule _clearThreadLocalTestRule =
067                    new BaseTestRule<>(ClearThreadLocalTestCallback.INSTANCE);
068            private static final TestRule _uniqueStringRandomizerBumperTestRule =
069                    new BaseTestRule<>(UniqueStringRandomizerBumperTestCallback.INSTANCE);
070    
071    }