001
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.List;
026
027
030 public class TestContextHandler {
031
032 public TestContextHandler(Class<?> clazz) {
033 _testContext = new TestContext(clazz);
034
035 registerExecutionListeners(getExecutionTestListeners(clazz));
036 }
037
038 public void registerExecutionListeners(
039 ExecutionTestListener ... executionTestListeners) {
040
041 for (ExecutionTestListener executionTestListener :
042 executionTestListeners) {
043
044 _executionTestListeners.add(executionTestListener);
045 }
046 }
047
048 public void runAfterTestClasses() {
049 for (ExecutionTestListener executionTestListener :
050 _executionTestListeners) {
051
052 executionTestListener.runAfterClass(_testContext);
053 }
054 }
055
056 public void runAfterTestMethod(Object instance, Method method) {
057 for (ExecutionTestListener executionTestListener :
058 _executionTestListeners) {
059
060 executionTestListener.runAfterTest(_testContext);
061 }
062 }
063
064 public void runBeforeTestClasses() {
065 for (ExecutionTestListener executionTestListener :
066 _executionTestListeners) {
067
068 executionTestListener.runBeforeClass(_testContext);
069 }
070 }
071
072 public void runBeforeTestMethod(Object instance, Method method) {
073 _testContext.setInstance(instance);
074 _testContext.setMethod(method);
075
076 for (ExecutionTestListener executionTestListener :
077 _executionTestListeners) {
078
079 executionTestListener.runBeforeTest(_testContext);
080 }
081 }
082
083 protected ExecutionTestListener[] getExecutionTestListeners(
084 Class<?> clazz) {
085
086 Class<?> declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
087 ExecutionTestListeners.class, clazz);
088
089 List<Class<? extends ExecutionTestListener>>
090 executionTestListenerClasses =
091 new ArrayList<Class<? extends ExecutionTestListener>>();
092
093 while (declaringClass != null) {
094 ExecutionTestListeners executionTestListeners =
095 declaringClass.getAnnotation(ExecutionTestListeners.class);
096
097 executionTestListenerClasses.addAll(
098 ListUtil.toList(executionTestListeners.listeners()));
099
100 declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
101 ExecutionTestListeners.class, declaringClass.getSuperclass());
102 }
103
104 ExecutionTestListener[] executionTestListeners =
105 new ExecutionTestListener[executionTestListenerClasses.size()];
106
107 for (int i = 0; i < executionTestListeners.length; i++) {
108 Class<? extends ExecutionTestListener> executionTestListenerClass =
109 null;
110
111 try {
112 executionTestListenerClass = executionTestListenerClasses.get(
113 i);
114
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 }