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.callback;
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.rule.callback.BaseTestCallback;
022    import com.liferay.portal.kernel.test.util.TestPropsValues;
023    import com.liferay.portal.kernel.util.PortalLifecycle;
024    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
027    import com.liferay.portal.service.test.ServiceTestUtil;
028    import com.liferay.portal.servlet.MainServlet;
029    import com.liferay.portal.test.mock.AutoDeployMockServletContext;
030    
031    import javax.servlet.ServletException;
032    
033    import org.junit.runner.Description;
034    
035    import org.springframework.core.io.FileSystemResourceLoader;
036    import org.springframework.mock.web.MockServletConfig;
037    import org.springframework.mock.web.MockServletContext;
038    
039    /**
040     * @author Shuyang Zhou
041     */
042    public class MainServletTestCallback extends BaseTestCallback<Object, Object> {
043    
044            public static final MainServletTestCallback INSTANCE =
045                    new MainServletTestCallback();
046    
047            public static MainServlet getMainServlet() {
048                    return _mainServlet;
049            }
050    
051            @Override
052            public void doAfterClass(Description description, Object object) {
053                    ServiceTestUtil.destroyServices();
054    
055                    try {
056                            SearchEngineUtil.removeCompany(TestPropsValues.getCompanyId());
057                    }
058                    catch (Exception e) {
059                            _log.error(e, e);
060                    }
061            }
062    
063            @Override
064            public Object doBeforeClass(Description description) {
065                    if (_mainServlet == null) {
066                            final MockServletContext mockServletContext =
067                                    new AutoDeployMockServletContext(
068                                            new FileSystemResourceLoader());
069    
070                            PortalLifecycleUtil.register(
071                                    new PortalLifecycle() {
072    
073                                            @Override
074                                            public void portalInit() {
075                                                    ModuleFrameworkUtilAdapter.registerContext(
076                                                            mockServletContext);
077                                            }
078    
079                                            @Override
080                                            public void portalDestroy() {
081                                            }
082    
083                                    });
084    
085                            ServletContextPool.put(StringPool.BLANK, mockServletContext);
086    
087                            MockServletConfig mockServletConfig = new MockServletConfig(
088                                    mockServletContext);
089    
090                            _mainServlet = new MainServlet();
091    
092                            try {
093                                    _mainServlet.init(mockServletConfig);
094                            }
095                            catch (ServletException se) {
096                                    throw new RuntimeException(
097                                            "The main servlet could not be initialized");
098                            }
099    
100                            ServiceTestUtil.initStaticServices();
101                    }
102    
103                    ServiceTestUtil.initServices();
104    
105                    ServiceTestUtil.initPermissions();
106    
107                    return null;
108            }
109    
110            protected MainServletTestCallback() {
111            }
112    
113            private static final Log _log = LogFactoryUtil.getLog(
114                    MainServletTestCallback.class);
115    
116            private static MainServlet _mainServlet;
117    
118    }