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