blob: a3606ed414f3c1e0a147034bb44134f15cbf7deb [file] [log] [blame]
Yuta HIGUCHIbaa766f2014-07-18 13:48:58 -07001package net.onrc.onos.core.util;
2
3import static org.junit.Assert.*;
4
5import java.io.PrintWriter;
6import java.io.StringWriter;
7import java.lang.reflect.Field;
8import java.lang.reflect.InvocationTargetException;
9import java.lang.reflect.Method;
10
11
12/**
13 * Utils for testing.
14 */
15public final class TestUtils {
16
17 /**
18 * Sets the field, bypassing scope restriction.
19 *
20 * @param subject Object where the field belongs
21 * @param fieldName name of the field to set
22 * @param value value to set to the field.
23 * @param <T> subject type
24 * @param <U> value type
25 */
26 public static <T, U> void setField(T subject, String fieldName, U value) {
27 @SuppressWarnings("unchecked")
28 Class<T> clazz = (Class<T>) subject.getClass();
29 try {
30 Field field = clazz.getDeclaredField(fieldName);
31 field.setAccessible(true);
32 field.set(subject, value);
33 } catch (NoSuchFieldException | SecurityException |
34 IllegalArgumentException | IllegalAccessException e) {
35 StringWriter sw = new StringWriter();
36 e.printStackTrace(new PrintWriter(sw));
37 fail(sw.toString());
38 }
39 }
40
41 /**
42 * Gets the field, bypassing scope restriction.
43 *
44 * @param subject Object where the field belongs
45 * @param fieldName name of the field to get
46 * @return value of the field.
47 * @param <T> subject type
48 * @param <U> field value type
49 */
50 public static <T, U> U getField(T subject, String fieldName) {
51 try {
52 @SuppressWarnings("unchecked")
53 Class<T> clazz = (Class<T>) subject.getClass();
54 Field field = clazz.getDeclaredField(fieldName);
55 field.setAccessible(true);
56
57 @SuppressWarnings("unchecked")
58 U result = (U) field.get(subject);
59 return result;
60 } catch (NoSuchFieldException | SecurityException |
61 IllegalArgumentException | IllegalAccessException e) {
62
63 StringWriter sw = new StringWriter();
64 e.printStackTrace(new PrintWriter(sw));
65 fail(sw.toString());
66 return null;
67 }
68 }
69
70 /**
71 * Calls the method, bypassing scope restriction.
72 *
73 * @param subject Object where the method belongs
74 * @param methodName name of the method to call
75 * @param paramTypes formal parameter type array
76 * @param args arguments
77 * @return return value or null if void
78 * @param <T> subject type
79 * @param <U> return value type
80 */
81 public static <T, U> U callMethod(T subject, String methodName, Class<?>[] paramTypes, Object...args) {
82
83 try {
84 @SuppressWarnings("unchecked")
85 Class<T> clazz = (Class<T>) subject.getClass();
86 Method method = clazz.getDeclaredMethod(methodName, paramTypes);
87 method.setAccessible(true);
88
89 @SuppressWarnings("unchecked")
90 U result = (U) method.invoke(subject, args);
91 return result;
92 } catch (NoSuchMethodException | SecurityException |
93 IllegalAccessException | IllegalArgumentException |
94 InvocationTargetException e) {
95
96 StringWriter sw = new StringWriter();
97 e.printStackTrace(new PrintWriter(sw));
98 fail(sw.toString());
99 return null;
100 }
101 }
102
103 /**
104 * Calls the method, bypassing scope restriction.
105 *
106 * @param subject Object where the method belongs
107 * @param methodName name of the method to call
108 * @param paramType formal parameter type
109 * @param arg argument
110 * @return return value or null if void
111 * @param <T> subject type
112 * @param <U> return value type
113 */
114 public static <T, U> U callMethod(T subject, String methodName, Class<?> paramType, Object arg) {
115 return callMethod(subject, methodName, new Class<?>[]{paramType}, arg);
116 }
117
118 /**
119 * Avoid instantiation.
120 */
121 private TestUtils() {}
122}