-
Notifications
You must be signed in to change notification settings - Fork 55
/
JSFTestUtil.java
567 lines (488 loc) · 21.3 KB
/
JSFTestUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Copyright (c) 2022, 2024 Contributors to Eclipse Foundation.
* Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* $Id$
*/
package com.sun.ts.tests.jsf.common.util;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import jakarta.el.ExpressionFactory;
import jakarta.el.ELManager;
import jakarta.faces.FacesException;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.event.PhaseId;
import jakarta.servlet.ServletContext;
public final class JSFTestUtil {
public static final boolean DEBUG = true;
public static final String NL = System.getProperty("line.separator", "\n");
public static final String PASS = "Test PASSED";
public static final String FAIL = "Test FAILED";
public static final String NPE_MESS = FAIL + " Unexpected Exception Thrown!" + NL +
"Expected: NullPointerException" + NL +
"Received: ";
public static final String FE_MESS = FAIL + " Unexpected Exception Thrown!" + NL +
"Expected: FacesException" + NL +
"Received: ";
public static final String ISE_MESS = FAIL + " Unexpected Exception Thrown!" + NL +
"Expected: IllegalStateException" + NL +
"Received: ";
public static final String APE_MESS = FAIL + " Unexpected Exception Thrown!" + NL +
"Expected: AbortProcessingException" + NL +
"Received: ";
public static final String APP_NULL_MSG = FAIL + " :Unable to obtain " + "Application instance.";
public static final String RESHANDLER_NULL_MSG = FAIL + NL + "Unable to obtain ResourceHandler instance.";
private JSFTestUtil() {
throw new IllegalStateException();
}
/**
* Writes the provided message to System.out when <tt>debug</tt> is set.
*
* @param message - the message to write to System.out
*/
public static void debug(String message) {
if (DEBUG) {
System.out.println(message);
}
}
/**
* <p>
* Compares the String values in an Enumeration against the provided String array of values in a case sensitive manner.
* </p>
*
* <p>
* This method will return <code>false</code> under the following circumstances:
* <ul>
* <li>The number of elements in the {@link Iterator} and the number of elements in the Array are not equal (only when
* the <code>enforceSizes</code> parameter is <code>true</code></li>
* <li>If the input {@link Iterator} or Array are null</li>
* <li>If duplicates are found in the {@link Iterator} and <code>allowDuplicates</code> is false</li>
* </ul>
*
* <p>
* If none of the above occur, then <code>true<code> will be returned.
* </p>
*
* @param i - Iterator to validate
* @param values - the values expected to be found in the Enumeration
* @param enforceSizes - ensures that the number of elements in the Enumeration matches the number of elements in the
* array of values
* @param allowDuplicates - If true, the method will true if duplicate elements are found in the Enumeration, if false,
* then false will be return if duplicate elements have been found.
*
* @return true if all the expected values are found, otherwise false.
*/
public static boolean checkIterator(Iterator<?> i, String[] values, boolean enforceSizes, boolean allowDuplicates) {
List<Object> foundValues = null;
if (i == null || !i.hasNext() || values == null) {
return false;
}
if (!allowDuplicates) {
foundValues = new ArrayList<Object>();
}
boolean valuesFound = true;
Arrays.sort(values);
int count = 0;
while (i.hasNext()) {
Object val;
try {
val = i.next();
count++;
if (!allowDuplicates) {
if (foundValues.contains(val)) {
debug("[JSFTestUtil] Duplicate values found in " + "Iterator when duplicates are not allowed."
+ "Values found in the Iterator: " + getAsString(i));
valuesFound = false;
break;
}
foundValues.add(val);
}
} catch (NoSuchElementException nsee) {
debug("[JSFTestUtil] There were less elements in the " + "Iterator than expected");
valuesFound = false;
break;
}
debug("[JSFTestUtil] Looking for '" + val + "' in values: " + getAsString(values));
if ((Arrays.binarySearch(values, val) < 0) && (enforceSizes)) {
debug("[JSFTestUtil] Value '" + val + "' not found.");
valuesFound = false;
}
}
if (enforceSizes) {
if (i.hasNext()) {
// more elements than should have been.
debug("[JSFTestUtil] There were more elements in the Iterator " + "than expected.");
valuesFound = false;
}
if (count != values.length) {
debug("[JSFTestUtil] There number of elements in the Iterator " + "did not match number of expected values."
+ "Expected number of Values=" + values.length + ", Actual number of Iterator elements=" + count);
valuesFound = false;
}
}
return valuesFound;
}
/**
* <p>
* Returns the elements of the provided {@link Iterator} as a String in the following format: <code>[n1,n2,n...]</code>.
* </p>
*
* @param i - an Iterator
*
* @return - a printable version of the contents of the Iterator
*/
public static String getAsString(Iterator<?> i) {
return getAsString(getAsArray(i));
}
/**
* <p>
* Returns the elements contained in the String array in the following format: <code>[n1,n2,n...]</code>.
* </p>
*
* @param sArray - an array of Objects
* @return - a String based off the values in the array
*/
public static String getAsString(Object[] sArray) {
if (sArray == null) {
return null;
}
StringBuffer buf = new StringBuffer();
buf.append("[");
for (int i = 0; i < sArray.length; i++) {
buf.append(sArray[i].toString());
if ((i + 1) != sArray.length) {
buf.append(",");
}
}
buf.append("]");
return buf.toString();
}
/**
* <p>
* Returns a String representation of the {@link Map} provided.
* </p>
*
* @param map input map
* @return String representation of the Map
*/
public static String getAsString(Map<String, Object> map) {
StringBuffer sb = new StringBuffer(32);
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
sb.append("Map Entries\n----------------\n");
for (Map.Entry<String, Object> entry : entrySet) {
sb.append(entry.getKey()).append(", ").append(entry.getValue());
sb.append('\n');
}
sb.append('\n');
return sb.toString();
}
/**
* <p>
* Returns the elements within the provided {@link Iterator} as an Array.
* </p>
*
* @param i - an Iterator
* @return - the elements of the Iterator as an array of Objects
*/
public static Object[] getAsArray(Iterator<?> i) {
List<Object> list = new ArrayList<Object>();
while (i.hasNext()) {
list.add(i.next());
}
return list.toArray(new Object[list.size()]);
}
/**
* <p>
* Returns the elements of the passed {@link Iterator} as an array of Strings.
* </p>
*
* @param i an Iterator
* @return the contents of the Iterator as an array of Strings
*/
public static String[] getAsStringArray(Iterator<String> i) {
List<String> list = new ArrayList<String>();
while (i.hasNext()) {
list.add(i.next());
}
return list.toArray(new String[list.size()]);
}
/**
* <p>
* Returns the elements of the {@link Enumeration} as an array of S trings.
* </p>
*
* @param e an Enumeration
* @return the contexts of the Enumeration as an array of Strings
*/
public static String[] getAsStringArray(Enumeration<?> e) {
List<Object> list = new ArrayList<Object>();
while (e.hasMoreElements()) {
list.add(e.nextElement());
}
return list.toArray(new String[list.size()]);
}
/**
* <p>
* Attempts to load the specified <code>className</code> using the current Thread's context class loader.
* </p>
*
* @param className fully qualified class name
* @return the loaded Class or <code>null</code> if no Class could be found
*/
public static Class<?> loadClass(String className) {
Class<?> c = null;
try {
c = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (Throwable t) {
debug("[JSFTestUtil] Exception occurred trying to get class: " + t.toString());
t.printStackTrace();
}
return c;
}
/**
* <p>
* Returns the ordinal value of a {@link FacesMessage.Severity} Object as a String value.
* </p>
* <ul>
* <li>FacesMessage.SEVERITY_INFO returns SEVERITY_INFO</li>
* <li>FacesMessage.SEVERITY_ERROR returns SEVERITY_ERROR</li>
* <li>FacesMessage.SEVERITY_FATAL returns SEVERITY_FATAL</li>
* <li>FacesMessage.SEVERITY_WARN returns SEVERITY_WARN</li>
* </ul>
*
* @param severity the ordinal value of a FacesMessage.Severity Object
* @return The String equivalent of the passed value.
*/
public static String getSeverityAsString(int severity) {
if (severity == FacesMessage.SEVERITY_INFO.getOrdinal())
return "SEVERITY_INFO";
else if (severity == FacesMessage.SEVERITY_ERROR.getOrdinal())
return "SEVERITY_ERROR";
else if (severity == FacesMessage.SEVERITY_FATAL.getOrdinal())
return "SEVERITY_FATAL";
else if (severity == FacesMessage.SEVERITY_WARN.getOrdinal())
return "SEVERITY_WARN";
else
return "UNKNOWN_SEVERITY";
}
/**
* <p>
* Return a <code>String</code> representation of the <code>PhaseId</code> ordinal.
* </p>
*/
public static String getPhaseIdAsString(PhaseId phaseId) {
int ordinal = phaseId.getOrdinal();
if (ordinal == PhaseId.ANY_PHASE.getOrdinal())
return "ANY_PHASE";
else if (ordinal == PhaseId.APPLY_REQUEST_VALUES.getOrdinal())
return "APPLY_REQUEST_VALUES";
else if (ordinal == PhaseId.INVOKE_APPLICATION.getOrdinal())
return "INVOKE_APPLICATION";
else if (ordinal == PhaseId.PROCESS_VALIDATIONS.getOrdinal())
return "PROCESS_VALIDATIONS";
else if (ordinal == PhaseId.RENDER_RESPONSE.getOrdinal())
return "RENDER_RESPONSE";
else if (ordinal == PhaseId.RESTORE_VIEW.getOrdinal())
return "RESTORE_VIEW";
else if (ordinal == PhaseId.UPDATE_MODEL_VALUES.getOrdinal())
return "UPDATE_MODEL_VALUES";
else
return "UNKNOWN_PHASE";
}
/**
* <p>
* Convenience method to obtain an EL <code>ExpressionFactory</code>.
* </p>
*/
public static ExpressionFactory getExpressionFactory(ServletContext ctx) {
return ELManager.getExpressionFactory();
} // END getExpressionFactory
/**
* Checks to see if a NullPointerException is thrown from a given method(methName) from a given Class(className). use
* this method for any none Abstract classes you wish to test.
*
* @param className - The Class that has the method under test.
* @param methName - The method that you want to test.
* @param argTypes - The type value of the arguments for the method under test.
* @param params - The parameters you are feeding into the method under test.
* @param pw - The PrintWrite that you want the results written to.
*/
public static void checkForNPE(String className, String methName, Class<?>[] argTypes, Object[] params, PrintWriter pw) {
try {
JSFTestUtil.checkForNPE(Class.forName(className), methName, argTypes, params, pw);
} catch (ClassNotFoundException cnfe) {
pw.println(JSFTestUtil.FAIL + JSFTestUtil.NL);
cnfe.printStackTrace();
}
} // End checkForNPE
/**
* Checks to see if a NullPointerException is thrown from a given method(methName) from a given Class. use this method
* for any Abstract classes you wish to test. You can pass in the instantiated class that you want to test.
*
* @param Class - The class you want to test.
* @param methName - The method that you want to test.
* @param argTypes - The type value of the arguments for the method under test.
* @param params - The parameters you are feeding into the method under test.
* @param pw - The PrintWrite that you want the results written to.
*/
public static void checkForNPE(Class<?> clazz, String methName, Class<?>[] argTypes, Object[] params, PrintWriter pw) {
try {
JSFTestUtil.checkForNPE(clazz.newInstance(), methName, argTypes, params, pw);
} catch (Exception e) {
pw.println(JSFTestUtil.FAIL + JSFTestUtil.NL);
e.printStackTrace();
}
} // End checkForNPE
/**
* Checks to see if a NullPointerException is thrown from a given method(methName) from a given Class. use this method
* for any Abstract classes you wish to test. You can pass in the instantiated class that you want to test.
*
* @param object - The Object you want to test.
* @param methName - The method that you want to test.
* @param argTypes - The type value of the arguments for the method under test.
* @param params - The parameters you are feeding into the method under test.
* @param pw - The PrintWrite that you want the results written to.
*/
public static void checkForNPE(Object object, String methName, Class<?>[] argTypes, Object[] params, PrintWriter pw) {
String className = object.getClass().getName();
Method[] methods = null;
try {
methods = object.getClass().getMethods();
Method execMeth = object.getClass().getMethod(methName, argTypes);
execMeth.invoke(object, params);
pw.println(JSFTestUtil.FAIL + " No Exception thrown!" + JSFTestUtil.NL + "Expected a NullPointerException " + "to be thrown "
+ JSFTestUtil.NL + "when testing: " + className + "." + methName);
} catch (InvocationTargetException ite) {
if (ite.getCause() instanceof NullPointerException) {
pw.println(JSFTestUtil.PASS);
} else {
pw.println(JSFTestUtil.NPE_MESS + ite.getCause().getClass().getSimpleName() + JSFTestUtil.NL + "When testing: " + className
+ "." + methName);
ite.getCause().printStackTrace();
}
} catch (NoSuchMethodException nsme) {
pw.println(JSFTestUtil.NPE_MESS + nsme.getClass().getSimpleName() + JSFTestUtil.NL);
JSFTestUtil.printMethods(className, methods, params, pw);
} catch (Throwable t) {
pw.println(
JSFTestUtil.NPE_MESS + t.getClass().getSimpleName() + JSFTestUtil.NL + "When testing: " + className + "." + methName);
t.printStackTrace();
}
} // End checkForNPE
/**
* Checks to see if a FacesException is thrown from a given method.
*
* @param object - The Object you want to test.
* @param methName - The method that you want to test.
* @param argTypes - The type value of the arguments for the method under test.
* @param params - The parameters you are feeding into the method under test.
* @param pw - The PrintWrite that you want the results written to.
*/
public static void checkForFE(Object object, String methName, Class<?>[] argTypes, Object[] params, PrintWriter pw) {
String className = object.getClass().getName();
Method[] methods = null;
try {
methods = object.getClass().getMethods();
Method execMeth = object.getClass().getMethod(methName, argTypes);
execMeth.invoke(object, params);
pw.println(JSFTestUtil.FAIL + " No Exception thrown!" + JSFTestUtil.NL + "Expected a FacesException " + "to be thrown "
+ JSFTestUtil.NL + "when testing: " + className + "." + methName);
} catch (InvocationTargetException ite) {
if (ite.getCause() instanceof FacesException) {
pw.println(JSFTestUtil.PASS);
} else {
pw.println(JSFTestUtil.FE_MESS + ite.getCause().getClass().getSimpleName() + JSFTestUtil.NL + "When testing: " + className
+ "." + methName);
ite.getCause().printStackTrace();
}
} catch (NoSuchMethodException nsme) {
pw.println(JSFTestUtil.FE_MESS + nsme.getClass().getSimpleName() + JSFTestUtil.NL);
JSFTestUtil.printMethods(className, methods, params, pw);
} catch (Throwable t) {
pw.println(JSFTestUtil.FE_MESS + t.getClass().getSimpleName() + JSFTestUtil.NL + "When testing: " + className + "." + methName);
t.printStackTrace();
}
} // End checkForFE
/**
* Checks to see if a IllegalStateException is thrown from a given method.
*
* @param object - The Object you want to test.
* @param methName - The method that you want to test.
* @param argTypes - The type value of the arguments for the method under test.
* @param params - The parameters you are feeding into the method under test.
* @param pw - The PrintWrite that you want the results written to.
*/
public static void checkForISE(Object object, String methName, Class<?>[] argTypes, Object[] params, PrintWriter pw) {
String className = object.getClass().getName();
Method[] methods = null;
try {
methods = object.getClass().getMethods();
Method execMeth = object.getClass().getMethod(methName, argTypes);
execMeth.invoke(object, params);
pw.println(JSFTestUtil.FAIL +
" No Exception thrown!" + JSFTestUtil.NL +
"Expected a IllegalStateException " + "to be thrown " + JSFTestUtil.NL +
"When testing: " + className + "." + methName);
} catch (InvocationTargetException ite) {
if (ite.getCause() instanceof IllegalStateException) {
pw.println(JSFTestUtil.PASS);
} else {
pw.println(JSFTestUtil.ISE_MESS +
ite.getCause().getClass().getSimpleName() + JSFTestUtil.NL +
"When testing: " + className + "." + methName);
ite.getCause().printStackTrace();
}
} catch (NoSuchMethodException nsme) {
pw.println(JSFTestUtil.ISE_MESS +
nsme.getClass().getSimpleName() + JSFTestUtil.NL +
"When testing: " + className + "." + methName);
JSFTestUtil.printMethods(className, methods, params, pw);
} catch (Throwable t) {
pw.println(JSFTestUtil.ISE_MESS +
t.getClass().getSimpleName() + JSFTestUtil.NL +
"When testing: " + className + "." + methName);
t.printStackTrace();
}
} // End checkForFE
// ------------------------------------------------------ private methods
private static void printMethods(String className, Method[] methods, Object[] params, PrintWriter pw) {
pw.println("Existing methods in the " + className + " Object Are: " + JSFTestUtil.NL);
Class<?>[] parms = null;
for (int i = 0; i < methods.length; i++) {
parms = methods[i].getParameterTypes();
pw.print("Method: " + methods[i].getName() + "(");
int plength = parms.length;
for (int ii = 0; ii < plength; ii++) {
pw.print(parms[ii].getSimpleName());
if ((ii + 1) != plength) {
pw.print(", ");
}
}
parms = null;
pw.print(")");
pw.print(JSFTestUtil.NL);
}
}
}