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.exception.PortalException;
018    import com.liferay.portal.kernel.search.SearchEngineHelperUtil;
019    import com.liferay.portal.kernel.servlet.ServletContextPool;
020    import com.liferay.portal.kernel.test.rule.ArquillianUtil;
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<Void, Void> {
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 afterClass(Description description, Void c)
053                    throws PortalException {
054    
055                    if (ArquillianUtil.isArquillianTest(description)) {
056                            return;
057                    }
058    
059                    SearchEngineHelperUtil.removeCompany(TestPropsValues.getCompanyId());
060            }
061    
062            @Override
063            public Void beforeClass(Description description) {
064                    if (ArquillianUtil.isArquillianTest(description)) {
065                            return null;
066                    }
067    
068                    if (_mainServlet == null) {
069                            final MockServletContext mockServletContext =
070                                    new AutoDeployMockServletContext(
071                                            new FileSystemResourceLoader());
072    
073                            PortalLifecycleUtil.register(
074                                    new PortalLifecycle() {
075    
076                                            @Override
077                                            public void portalInit() {
078                                                    ModuleFrameworkUtilAdapter.registerContext(
079                                                            mockServletContext);
080                                            }
081    
082                                            @Override
083                                            public void portalDestroy() {
084                                            }
085    
086                                    });
087    
088                            ServletContextPool.put(StringPool.BLANK, mockServletContext);
089    
090                            MockServletConfig mockServletConfig = new MockServletConfig(
091                                    mockServletContext);
092    
093                            _mainServlet = new MainServlet();
094    
095                            try {
096                                    _mainServlet.init(mockServletConfig);
097                            }
098                            catch (ServletException se) {
099                                    throw new RuntimeException(
100                                            "The main servlet could not be initialized");
101                            }
102    
103                            ServiceTestUtil.initStaticServices();
104                    }
105    
106                    ServiceTestUtil.initServices();
107    
108                    ServiceTestUtil.initPermissions();
109    
110                    return null;
111            }
112    
113            protected MainServletTestCallback() {
114            }
115    
116            private static MainServlet _mainServlet;
117    
118    }