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    import com.liferay.portal.test.rule.MainServletTestRule;
031    
032    import javax.servlet.ServletException;
033    
034    import org.junit.Assert;
035    import org.junit.runner.Description;
036    import org.junit.runner.RunWith;
037    import org.junit.runner.Runner;
038    
039    import org.springframework.core.io.FileSystemResourceLoader;
040    import org.springframework.mock.web.MockServletConfig;
041    import org.springframework.mock.web.MockServletContext;
042    
043    /**
044     * @author Shuyang Zhou
045     */
046    public class MainServletTestCallback extends BaseTestCallback<Object, Object> {
047    
048            public static final MainServletTestCallback INSTANCE =
049                    new MainServletTestCallback();
050    
051            public static MainServlet getMainServlet() {
052                    return _mainServlet;
053            }
054    
055            @Override
056            public void afterClass(Description description, Object object) {
057                    try {
058                            SearchEngineUtil.removeCompany(TestPropsValues.getCompanyId());
059                    }
060                    catch (Exception e) {
061                            _log.error(e, e);
062                    }
063            }
064    
065            @Override
066            public Object beforeClass(Description description) {
067                    if (isArquillianTest(description)) {
068                            Assert.fail(
069                                    description.getTestClass() + " is an Arquillian test and " +
070                                            "should not use " + MainServletTestRule.class);
071                    }
072    
073                    if (_mainServlet == null) {
074                            final MockServletContext mockServletContext =
075                                    new AutoDeployMockServletContext(
076                                            new FileSystemResourceLoader());
077    
078                            PortalLifecycleUtil.register(
079                                    new PortalLifecycle() {
080    
081                                            @Override
082                                            public void portalInit() {
083                                                    ModuleFrameworkUtilAdapter.registerContext(
084                                                            mockServletContext);
085                                            }
086    
087                                            @Override
088                                            public void portalDestroy() {
089                                            }
090    
091                                    });
092    
093                            ServletContextPool.put(StringPool.BLANK, mockServletContext);
094    
095                            MockServletConfig mockServletConfig = new MockServletConfig(
096                                    mockServletContext);
097    
098                            _mainServlet = new MainServlet();
099    
100                            try {
101                                    _mainServlet.init(mockServletConfig);
102                            }
103                            catch (ServletException se) {
104                                    throw new RuntimeException(
105                                            "The main servlet could not be initialized");
106                            }
107    
108                            ServiceTestUtil.initStaticServices();
109                    }
110    
111                    ServiceTestUtil.initServices();
112    
113                    ServiceTestUtil.initPermissions();
114    
115                    return null;
116            }
117    
118            protected MainServletTestCallback() {
119            }
120    
121            protected boolean isArquillianTest(Description description) {
122                    RunWith runWith = description.getAnnotation(RunWith.class);
123    
124                    if (runWith == null) {
125                            return false;
126                    }
127    
128                    Class<? extends Runner> runnerClass = runWith.value();
129    
130                    String runnerClassName = runnerClass.getName();
131    
132                    if (runnerClassName.equals(
133                                    "com.liferay.arquillian.extension.junit.bridge.junit." +
134                                            "Arquillian")) {
135    
136                            return true;
137                    }
138    
139                    return false;
140            }
141    
142            private static final Log _log = LogFactoryUtil.getLog(
143                    MainServletTestCallback.class);
144    
145            private static MainServlet _mainServlet;
146    
147    }