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                    try {
054                            SearchEngineUtil.removeCompany(TestPropsValues.getCompanyId());
055                    }
056                    catch (Exception e) {
057                            _log.error(e, e);
058                    }
059            }
060    
061            @Override
062            public Object doBeforeClass(Description description) {
063                    if (_mainServlet == null) {
064                            final MockServletContext mockServletContext =
065                                    new AutoDeployMockServletContext(
066                                            new FileSystemResourceLoader());
067    
068                            PortalLifecycleUtil.register(
069                                    new PortalLifecycle() {
070    
071                                            @Override
072                                            public void portalInit() {
073                                                    ModuleFrameworkUtilAdapter.registerContext(
074                                                            mockServletContext);
075                                            }
076    
077                                            @Override
078                                            public void portalDestroy() {
079                                            }
080    
081                                    });
082    
083                            ServletContextPool.put(StringPool.BLANK, mockServletContext);
084    
085                            MockServletConfig mockServletConfig = new MockServletConfig(
086                                    mockServletContext);
087    
088                            _mainServlet = new MainServlet();
089    
090                            try {
091                                    _mainServlet.init(mockServletConfig);
092                            }
093                            catch (ServletException se) {
094                                    throw new RuntimeException(
095                                            "The main servlet could not be initialized");
096                            }
097    
098                            ServiceTestUtil.initStaticServices();
099                    }
100    
101                    ServiceTestUtil.initServices();
102    
103                    ServiceTestUtil.initPermissions();
104    
105                    return null;
106            }
107    
108            protected MainServletTestCallback() {
109            }
110    
111            private static final Log _log = LogFactoryUtil.getLog(
112                    MainServletTestCallback.class);
113    
114            private static MainServlet _mainServlet;
115    
116    }