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.listeners;
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.AbstractExecutionTestListener;
022    import com.liferay.portal.kernel.test.TestContext;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.service.ServiceTestUtil;
025    import com.liferay.portal.servlet.MainServlet;
026    import com.liferay.portal.test.mock.AutoDeployMockServletContext;
027    import com.liferay.portal.test.rule.DeleteAfterTestRunRule;
028    import com.liferay.portal.util.test.TestPropsValues;
029    
030    import javax.servlet.ServletException;
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     */
039    public class MainServletExecutionTestListener
040            extends AbstractExecutionTestListener {
041    
042            @Override
043            public void runAfterClass(TestContext testContext) {
044                    ServiceTestUtil.destroyServices();
045    
046                    try {
047                            SearchEngineUtil.removeCompany(TestPropsValues.getCompanyId());
048                    }
049                    catch (Exception e) {
050                            _log.error(e, e);
051                    }
052            }
053    
054            @Override
055            public void runAfterTest(TestContext testContext) {
056                    DeleteAfterTestRunRule deleteAfterTestRunRule =
057                            new DeleteAfterTestRunRule(testContext.getInstance());
058    
059                    deleteAfterTestRunRule.after(testContext.getClazz());
060            }
061    
062            @Override
063            public void runBeforeClass(TestContext testContext) {
064                    ServiceTestUtil.initServices();
065    
066                    ServiceTestUtil.initPermissions();
067    
068                    if (_mainServlet != null) {
069                            return;
070                    }
071    
072                    MockServletContext mockServletContext =
073                            new AutoDeployMockServletContext(new FileSystemResourceLoader());
074    
075                    ServletContextPool.put(StringPool.BLANK, mockServletContext);
076    
077                    MockServletConfig mockServletConfig = new MockServletConfig(
078                            mockServletContext);
079    
080                    _mainServlet = new MainServlet();
081    
082                    try {
083                            _mainServlet.init(mockServletConfig);
084                    }
085                    catch (ServletException se) {
086                            throw new RuntimeException(
087                                    "The main servlet could not be initialized");
088                    }
089            }
090    
091            private static final Log _log = LogFactoryUtil.getLog(
092                    MainServletExecutionTestListener.class);
093    
094            private static MainServlet _mainServlet;
095    
096    }