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;
016    
017    import com.liferay.portal.kernel.test.AggregateTestRule;
018    import com.liferay.portal.kernel.test.BaseTestRule;
019    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.test.jdbc.ResetDatabaseUtilDataSource;
023    import com.liferay.portal.test.log.LogAssertionTestRule;
024    import com.liferay.portal.test.randomizerbumpers.UniqueStringRandomizerBumper;
025    import com.liferay.portal.test.rule.DeleteAfterTestRunTestRule;
026    import com.liferay.portal.util.InitUtil;
027    import com.liferay.portal.util.PropsUtil;
028    
029    import java.util.List;
030    
031    import org.junit.rules.TestRule;
032    import org.junit.runner.Description;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class LiferayIntegrationTestRule extends AggregateTestRule {
038    
039            public LiferayIntegrationTestRule() {
040                    super(
041                            LogAssertionTestRule.INSTANCE, _clearThreadLocalTestRule,
042                            _uniqueStringRandomizerBumperTestRule,
043                            new DeleteAfterTestRunTestRule());
044    
045                    System.setProperty("catalina.base", ".");
046    
047                    ResetDatabaseUtilDataSource.initialize();
048    
049                    List<String> configLocations = ListUtil.fromArray(
050                            PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
051    
052                    InitUtil.initWithSpring(configLocations, true);
053    
054                    if (System.getProperty("external-properties") == null) {
055                            System.setProperty("external-properties", "portal-test.properties");
056                    }
057            }
058    
059            private static final TestRule _clearThreadLocalTestRule =
060                    new BaseTestRule<Object, Object>() {
061    
062                            @Override
063                            protected void afterClass(Description description, Object object) {
064                                    CentralizedThreadLocal.clearShortLivedThreadLocals();
065                            }
066    
067                    };
068    
069            private static final TestRule _uniqueStringRandomizerBumperTestRule =
070                    new BaseTestRule<Object, Object>() {
071    
072                            @Override
073                            protected Object beforeClass(Description description) {
074                                    UniqueStringRandomizerBumper.reset();
075    
076                                    return null;
077                            }
078    
079                    };
080    
081    }