1 package auxtestlib;
2
3 import java.lang.reflect.Field;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Comparator;
7 import java.util.List;
8 import java.util.Properties;
9
10 import org.apache.commons.lang.ArrayUtils;
11 import org.junit.After;
12 import org.junit.Assert;
13 import org.junit.Before;
14
15
16
17
18
19
20
21
22
23 public class DefaultTCase extends Assert {
24
25
26
27 private static final long WAIT_FOR_TRUE_SLEEP_MS = 10;
28
29
30
31
32 private static final long WAIT_FOR_TRUE_DEFAULT_TIMEOUT_MS = 10_000;
33
34
35
36
37 private List<Field> m_helpers;
38
39
40
41
42 private Properties m_system_properties;
43
44
45
46
47
48
49
50 @Before
51 public void pre_set_up() throws Exception {
52
53
54
55 assert m_helpers == null;
56
57
58
59
60 TestPropertiesDefinition.load_global_properties();
61
62
63
64
65
66 m_system_properties = (Properties) System.getProperties().clone();
67
68
69
70
71 assertEquals(0, AbstractTestHelper.getTotalHelperCount());
72
73
74
75
76 m_helpers = new ArrayList<>();
77
78 for (Class<?> cls = getClass(); cls != null;
79 cls = cls.getSuperclass()) {
80 Field[] fields = cls.getDeclaredFields();
81 Arrays.sort(fields, new Comparator<Field>() {
82 @Override
83 public int compare(Field o1, Field o2) {
84 return o1.getName().compareTo(o2.getName());
85 }
86 });
87 for (Field f : fields) {
88 if (f.getAnnotation(TestHelper.class) != null) {
89 if (!f.isAccessible()) {
90 f.setAccessible(true);
91 }
92
93 Class<?> ftype = f.getType();
94 if (!AbstractTestHelper.class.isAssignableFrom(ftype)) {
95 throw new TestCaseConfigurationException("Field '"
96 + f.toString() + "' " + "of type '"
97 + cls.getCanonicalName() + "' has type '"
98 + ftype.getCanonicalName()
99 + "' which is not a subclass of "
100 + "AbstractTestHelper.");
101 }
102
103 m_helpers.add(f);
104 f.set(this, f.getType().newInstance());
105 }
106 }
107 }
108 }
109
110
111
112
113
114 @After
115 public void post_tear_down() throws Exception {
116
117
118
119 assert m_helpers != null;
120
121
122
123
124 Field[] fr = new Field[m_helpers.size()];
125 ArrayUtils.reverse(m_helpers.toArray(fr));
126 for (Field f : fr) {
127 AbstractTestHelper ath = (AbstractTestHelper) f.get(this);
128 ath.tearDown();
129 }
130
131 m_helpers = null;
132
133
134
135
136 assertEquals(0, AbstractTestHelper.getTotalHelperCount());
137
138
139
140
141 System.setProperties(m_system_properties);
142 }
143
144
145
146
147
148
149
150
151
152 protected <E extends Enum<E>> void cover_enumeration(Class<E> cls)
153 throws Exception {
154 E[] values = cls.getEnumConstants();
155 for (E e : values) {
156 e.toString();
157 Enum.valueOf(cls, e.name());
158 cls.getMethod("valueOf", String.class).invoke(null, e.name());
159 }
160
161 cls.getMethod("values").invoke(null);
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176 protected void wait_for_true(BooleanEvaluation eval, long timeout_ms)
177 throws Exception {
178 if (eval == null) {
179 throw new IllegalArgumentException("eval == null");
180 }
181
182 if (timeout_ms <= 0) {
183 throw new IllegalArgumentException("timeout_ms <= 0");
184 }
185
186 long end = System.currentTimeMillis() + timeout_ms;
187 do {
188 if (eval.evaluate()) {
189 return;
190 }
191
192 Thread.sleep(WAIT_FOR_TRUE_SLEEP_MS);
193 } while (System.currentTimeMillis() < end);
194
195 fail();
196 }
197
198
199
200
201
202
203
204
205 protected void wait_for_true(BooleanEvaluation eval) throws Exception {
206 wait_for_true(eval, WAIT_FOR_TRUE_DEFAULT_TIMEOUT_MS);
207 }
208
209
210
211
212
213
214
215
216
217
218
219 public static <FIELD_T> FIELD_T internal_get(Object object,
220 Class<FIELD_T> field_type, String field_name) throws Exception {
221 if (object == null) {
222 throw new IllegalArgumentException("object == null");
223 }
224
225 if (field_type == null) {
226 throw new IllegalArgumentException("field_type == null");
227 }
228
229 if (field_name == null) {
230 throw new IllegalArgumentException("field_name == null");
231 }
232
233 Class<?> class_type = object.getClass();
234
235 Field f = class_type.getDeclaredField(field_name);
236 if (f == null) {
237 throw new Exception("No field '" + field_name + "' found in class '"
238 + class_type.getName() + "'.");
239 }
240
241 f.setAccessible(true);
242 if (!field_type.isAssignableFrom(f.getType())) {
243 throw new Exception("Field '" + field_name + "' in class '"
244 + class_type.getName() + "' is not assignable to type '"
245 + field_type.getName() + "'.");
246 }
247
248 return field_type.cast(f.get(object));
249 }
250
251
252
253
254
255
256
257
258
259 public static void internal_set(Object object, String field_name,
260 Object value) throws Exception {
261 if (object == null) {
262 throw new IllegalArgumentException("object == null");
263 }
264
265 if (field_name == null) {
266 throw new IllegalArgumentException("field_name == null");
267 }
268
269 Class<?> class_type = object.getClass();
270
271 Field f = class_type.getDeclaredField(field_name);
272 if (f == null) {
273 throw new Exception("No field '" + field_name + "' found in class '"
274 + class_type.getName() + "'.");
275 }
276
277 f.setAccessible(true);
278 if (value != null) {
279 if (!f.getType().isAssignableFrom(value.getClass())) {
280 throw new Exception("Field '" + field_name + "' in class '"
281 + class_type.getName() + "' cannot receive value of "
282 + "type '"+ value.getClass().getName() + "'.");
283 }
284
285 }
286
287 f.set(object, value);
288 }
289 }