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.concurrent.ThreadPoolExecutor;
018    import com.liferay.portal.kernel.executor.PortalExecutorManager;
019    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
020    import com.liferay.registry.BasicRegistryImpl;
021    import com.liferay.registry.Registry;
022    import com.liferay.registry.RegistryUtil;
023    
024    import org.junit.runner.Description;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class PortalExecutorManagerTestCallback
030            extends BaseTestCallback<Object, Object> {
031    
032            public static final PortalExecutorManagerTestCallback INSTANCE =
033                    new PortalExecutorManagerTestCallback();
034    
035            @Override
036            public void afterClass(Description description, Object o) {
037                    _portalExecutorManager.shutdown(true);
038            }
039    
040            protected class MockPortalExecutorManager implements PortalExecutorManager {
041    
042                    @Override
043                    public ThreadPoolExecutor getPortalExecutor(String name) {
044                            return _threadPoolExecutor;
045                    }
046    
047                    @Override
048                    public ThreadPoolExecutor getPortalExecutor(
049                            String name, boolean createIfAbsent) {
050    
051                            return _threadPoolExecutor;
052                    }
053    
054                    @Override
055                    public ThreadPoolExecutor registerPortalExecutor(
056                            String name, ThreadPoolExecutor threadPoolExecutor) {
057    
058                            return _threadPoolExecutor;
059                    }
060    
061                    @Override
062                    public void shutdown() {
063                            shutdown(false);
064                    }
065    
066                    @Override
067                    public void shutdown(boolean interrupt) {
068                            if (interrupt) {
069                                    _threadPoolExecutor.shutdownNow();
070                            }
071                            else {
072                                    _threadPoolExecutor.shutdown();
073                            }
074                    }
075    
076                    private final ThreadPoolExecutor _threadPoolExecutor =
077                            new ThreadPoolExecutor(0, 1);
078    
079            }
080    
081            private PortalExecutorManagerTestCallback() {
082                    RegistryUtil.setRegistry(new BasicRegistryImpl());
083    
084                    Registry registry = RegistryUtil.getRegistry();
085    
086                    _portalExecutorManager = new MockPortalExecutorManager();
087    
088                    registry.registerService(
089                            PortalExecutorManager.class, _portalExecutorManager);
090            }
091    
092            private final PortalExecutorManager _portalExecutorManager;
093    
094    }