1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.test;
17
18 import java.lang.annotation.Annotation;
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import junit.framework.Assert;
26
27 import org.kuali.rice.core.api.config.property.Config;
28 import org.kuali.rice.core.api.config.property.ConfigContext;
29 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
30 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
31 import org.mortbay.jetty.webapp.WebAppClassLoader;
32
33 public class TestUtilities {
34
35 private static Thread exceptionThreader;
36
37
38
39
40
41
42
43
44
45 public static void waitForExceptionRouting() {
46 waitForExceptionRouting(5*60*1000);
47 }
48
49 public static void waitForExceptionRouting(long milliseconds) {
50 try {
51 Thread thread = getExceptionThreader();
52 if (thread == null) {
53 throw new IllegalStateException("No exception thread was established, likely message is not being processed for exception routing.");
54 }
55 thread.join(milliseconds);
56 } catch (InterruptedException e) {
57 Assert.fail("This thread was interuppted while waiting for exception routing.");
58 }
59 if (getExceptionThreader().isAlive()) {
60 Assert.fail("Document was not put into exception routing within the specified amount of time " + milliseconds);
61 }
62 }
63
64 public static Thread getExceptionThreader() {
65 return exceptionThreader;
66 }
67
68 public static void setExceptionThreader(Thread exceptionThreader) {
69 TestUtilities.exceptionThreader = exceptionThreader;
70 }
71
72 protected List<Class> annotationsPresent(Class clazz, Class[] annotationClasses) {
73 List<Class> annotationsPresent = new ArrayList<Class>();
74 for (Class c: annotationClasses) {
75 if (clazz.isAnnotationPresent(c)) {
76 annotationsPresent.add(c);
77 }
78 }
79 return annotationsPresent;
80 }
81
82 protected static boolean contains(Class[] list, Class target) {
83 for (Class c: list) {
84 if (c.getName().equals(target.getName())) {
85 return true;
86 }
87 }
88 return false;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public static List<Class> getHierarchyClassesToHandle(Class testClass, Class[] annotationClasses, Set<String> classesHandled) throws Exception {
112 List<Class> classesThatNeedHandling = new ArrayList<Class>();
113
114 List<Class> classesToCheck = new ArrayList<Class>();
115
116 {
117 Class clazz = testClass;
118 superClassLoop: while (!clazz.getName().equals(Object.class.getName())) {
119 for (Annotation annotation : clazz.getDeclaredAnnotations()) {
120
121 if (!contains(annotationClasses, annotation.annotationType())) {
122 continue;
123 }
124
125
126 classesToCheck.add(0, clazz);
127
128
129 if (annotationOverridesSuperClass(annotation)) {
130
131 break superClassLoop;
132 }
133
134
135 break;
136 }
137 clazz = clazz.getSuperclass();
138 }
139 }
140
141 for (Class clazz: classesToCheck) {
142 if (!classesHandled.contains(clazz.getName())) {
143 classesThatNeedHandling.add(clazz);
144 }
145 }
146 return classesThatNeedHandling;
147 }
148
149
150
151
152
153
154
155
156
157 protected static boolean annotationOverridesSuperClass(Annotation annotation) throws Exception {
158 boolean overrides = true;
159 Method m = null;;
160 try {
161 m = annotation.getClass().getMethod("overrideSuperClasses", null);
162 } catch (NoSuchMethodException nsme) {
163
164 }
165 if (m != null) {
166 Object result = m.invoke(annotation, (Object[]) null);
167 if (result instanceof Boolean) {
168 overrides = (Boolean) result;
169 } else {
170 throw new RuntimeException("Annotation 'overrideSuperClasses' did not return Boolean value");
171 }
172 }
173 return overrides;
174 }
175
176
177
178
179
180 public static void addWebappsToContext() {
181 for (Map.Entry<ClassLoader, Config> configEntry : ConfigContext.getConfigs()) {
182 if (configEntry.getKey() instanceof WebAppClassLoader) {
183 ResourceLoader rl = GlobalResourceLoader.getResourceLoader(configEntry.getKey());
184 if (rl == null) {
185 Assert.fail("didn't find resource loader for workflow test harness web app");
186 }
187 GlobalResourceLoader.addResourceLoader(rl);
188 ConfigContext.overrideConfig(Thread.currentThread().getContextClassLoader(), configEntry.getValue());
189 }
190 }
191 }
192 }