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.test;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.ReflectionUtil;
021    
022    import java.lang.reflect.Method;
023    
024    import java.util.ArrayList;
025    import java.util.LinkedHashSet;
026    import java.util.List;
027    import java.util.Set;
028    
029    /**
030     * @author Miguel Pastor
031     */
032    public class TestContextHandler {
033    
034            public TestContextHandler(Class<?> clazz) {
035                    _testContext = new TestContext(clazz);
036    
037                    registerExecutionListeners(getExecutionTestListeners(clazz));
038            }
039    
040            public void registerExecutionListeners(
041                    ExecutionTestListener ... executionTestListeners) {
042    
043                    for (ExecutionTestListener executionTestListener :
044                                    executionTestListeners) {
045    
046                            _executionTestListeners.add(executionTestListener);
047                    }
048            }
049    
050            public void runAfterTestClasses() {
051                    for (ExecutionTestListener executionTestListener :
052                                    _executionTestListeners) {
053    
054                            executionTestListener.runAfterClass(_testContext);
055                    }
056            }
057    
058            public void runAfterTestMethod(Object instance, Method method) {
059                    for (ExecutionTestListener executionTestListener :
060                                    _executionTestListeners) {
061    
062                            executionTestListener.runAfterTest(_testContext);
063                    }
064            }
065    
066            public void runBeforeTestClasses() {
067                    for (ExecutionTestListener executionTestListener :
068                                    _executionTestListeners) {
069    
070                            executionTestListener.runBeforeClass(_testContext);
071                    }
072            }
073    
074            public void runBeforeTestMethod(Object instance, Method method) {
075                    _testContext.setInstance(instance);
076                    _testContext.setMethod(method);
077    
078                    for (ExecutionTestListener executionTestListener :
079                                    _executionTestListeners) {
080    
081                            executionTestListener.runBeforeTest(_testContext);
082                    }
083            }
084    
085            protected ExecutionTestListener[] getExecutionTestListeners(
086                    Class<?> clazz) {
087    
088                    Class<?> declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
089                            ExecutionTestListeners.class, clazz);
090    
091                    Set<Class<? extends ExecutionTestListener>>
092                            executionTestListenerClasses =
093                                    new LinkedHashSet<Class<? extends ExecutionTestListener>>();
094    
095                    while (declaringClass != null) {
096                            ExecutionTestListeners executionTestListeners =
097                                    declaringClass.getAnnotation(ExecutionTestListeners.class);
098    
099                            executionTestListenerClasses.addAll(
100                                    ListUtil.toList(executionTestListeners.listeners()));
101    
102                            declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
103                                    ExecutionTestListeners.class, declaringClass.getSuperclass());
104                    }
105    
106                    int i = 0;
107    
108                    ExecutionTestListener[] executionTestListeners =
109                            new ExecutionTestListener[executionTestListenerClasses.size()];
110    
111                    for (Class<? extends ExecutionTestListener> executionTestListenerClass :
112                                    executionTestListenerClasses) {
113    
114                            try {
115                                    executionTestListeners[i++] =
116                                            executionTestListenerClass.newInstance();
117                            }
118                            catch (Exception e) {
119                                    _log.error(
120                                            "Unable to instantiate " + executionTestListenerClass, e);
121                            }
122                    }
123    
124                    return executionTestListeners;
125            }
126    
127            private static Log _log = LogFactoryUtil.getLog(TestContextHandler.class);
128    
129            private List<ExecutionTestListener> _executionTestListeners =
130                    new ArrayList<ExecutionTestListener>();
131            private TestContext _testContext;
132    
133    }