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.kernel.workflow.test;
016    
017    import com.liferay.portal.kernel.util.ProxyUtil;
018    import com.liferay.portal.kernel.workflow.WorkflowHandler;
019    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
020    
021    import java.lang.reflect.InvocationHandler;
022    import java.lang.reflect.Method;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.concurrent.atomic.AtomicInteger;
027    
028    /**
029     * @author Adolfo P??rez
030     */
031    public class WorkflowHandlerInvocationCounter<T> implements AutoCloseable {
032    
033            public WorkflowHandlerInvocationCounter(String className) {
034                    WorkflowHandler<T> workflowHandler =
035                            WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
036    
037                    _counts = new HashMap<>();
038    
039                    WorkflowHandler<T> delegateWorkflowHandler =
040                            _createInvocationCounterWorkflowHandler(workflowHandler);
041    
042                    _workflowHandlerReplacer = new WorkflowHandlerReplacer<>(
043                            className, delegateWorkflowHandler);
044            }
045    
046            @Override
047            public void close() throws Exception {
048                    _workflowHandlerReplacer.close();
049            }
050    
051            public int getCount(String methodName, Class<?>... parameterTypes)
052                    throws Exception {
053    
054                    Method method = WorkflowHandler.class.getMethod(
055                            methodName, parameterTypes);
056    
057                    AtomicInteger count = _counts.get(method);
058    
059                    if (count == null) {
060                            return 0;
061                    }
062    
063                    return count.get();
064            }
065    
066            private WorkflowHandler<T> _createInvocationCounterWorkflowHandler(
067                    final WorkflowHandler<T> workflowHandler) {
068    
069                    Thread currentThread = Thread.currentThread();
070    
071                    ClassLoader classLoader = currentThread.getContextClassLoader();
072    
073                    return (WorkflowHandler<T>)ProxyUtil.newProxyInstance(
074                            classLoader, new Class<?>[] {WorkflowHandler.class},
075                            new InvocationHandler() {
076    
077                                    @Override
078                                    public Object invoke(Object proxy, Method method, Object[] args)
079                                            throws Throwable {
080    
081                                            AtomicInteger count = _counts.get(method);
082    
083                                            if (count == null) {
084                                                    count = new AtomicInteger();
085    
086                                                    _counts.put(method, count);
087                                            }
088    
089                                            count.incrementAndGet();
090    
091                                            return method.invoke(workflowHandler, args);
092                                    }
093    
094                            }
095                    );
096            }
097    
098            private final Map<Method, AtomicInteger> _counts;
099            private final WorkflowHandlerReplacer<T> _workflowHandlerReplacer;
100    
101    }