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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.SearchEngineUtil;
020    import com.liferay.portal.kernel.servlet.ServletContextPool;
021    import com.liferay.portal.kernel.test.BaseTestRule;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.service.ServiceTestUtil;
024    import com.liferay.portal.servlet.MainServlet;
025    import com.liferay.portal.test.mock.AutoDeployMockServletContext;
026    import com.liferay.portal.util.test.TestPropsValues;
027    
028    import javax.servlet.ServletException;
029    
030    import org.junit.runner.Description;
031    
032    import org.springframework.core.io.FileSystemResourceLoader;
033    import org.springframework.mock.web.MockServletConfig;
034    import org.springframework.mock.web.MockServletContext;
035    
036    /**
037     * @author Miguel Pastor
038     * @author Shuyang Zhou
039     */
040    public class MainServletTestRule extends BaseTestRule<Object, Object> {
041    
042            public static final MainServletTestRule INSTANCE =
043                    new MainServletTestRule();
044    
045            protected MainServletTestRule() {
046            }
047    
048            @Override
049            protected void afterClass(Description description, Object object) {
050                    ServiceTestUtil.destroyServices();
051    
052                    try {
053                            SearchEngineUtil.removeCompany(TestPropsValues.getCompanyId());
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058            }
059    
060            @Override
061            protected Object beforeClass(Description description) {
062                    ServiceTestUtil.initServices();
063    
064                    ServiceTestUtil.initPermissions();
065    
066                    if (_mainServlet == null) {
067                            MockServletContext mockServletContext =
068                                    new AutoDeployMockServletContext(
069                                            new FileSystemResourceLoader());
070    
071                            ServletContextPool.put(StringPool.BLANK, mockServletContext);
072    
073                            MockServletConfig mockServletConfig = new MockServletConfig(
074                                    mockServletContext);
075    
076                            _mainServlet = new MainServlet();
077    
078                            try {
079                                    _mainServlet.init(mockServletConfig);
080                            }
081                            catch (ServletException se) {
082                                    throw new RuntimeException(
083                                            "The main servlet could not be initialized");
084                            }
085                    }
086    
087                    return null;
088            }
089    
090            private static final Log _log = LogFactoryUtil.getLog(
091                    MainServletTestRule.class);
092    
093            private static MainServlet _mainServlet;
094    
095    }