blob: 8dafad5be7c3543ed8253bb165862a178dabbd55 [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;
Ray Milkey0f42e832014-07-21 10:49:23 -07007import java.lang.reflect.Constructor;
Yuta HIGUCHIbaa766f2014-07-18 13:48:58 -07008import java.lang.reflect.Field;
9import java.lang.reflect.InvocationTargetException;
10import java.lang.reflect.Method;
11
12
13/**
14 * Utils for testing.
15 */
16public final class TestUtils {
17
18 /**
19 * Sets the field, bypassing scope restriction.
20 *
21 * @param subject Object where the field belongs
22 * @param fieldName name of the field to set
23 * @param value value to set to the field.
24 * @param <T> subject type
25 * @param <U> value type
26 */
27 public static <T, U> void setField(T subject, String fieldName, U value) {
28 @SuppressWarnings("unchecked")
29 Class<T> clazz = (Class<T>) subject.getClass();
30 try {
31 Field field = clazz.getDeclaredField(fieldName);
32 field.setAccessible(true);
33 field.set(subject, value);
34 } catch (NoSuchFieldException | SecurityException |
35 IllegalArgumentException | IllegalAccessException e) {
36 StringWriter sw = new StringWriter();
37 e.printStackTrace(new PrintWriter(sw));
38 fail(sw.toString());
39 }
40 }
41
42 /**
43 * Gets the field, bypassing scope restriction.
44 *
45 * @param subject Object where the field belongs
46 * @param fieldName name of the field to get
47 * @return value of the field.
48 * @param <T> subject type
49 * @param <U> field value type
50 */
51 public static <T, U> U getField(T subject, String fieldName) {
52 try {
53 @SuppressWarnings("unchecked")
54 Class<T> clazz = (Class<T>) subject.getClass();
55 Field field = clazz.getDeclaredField(fieldName);
56 field.setAccessible(true);
57
58 @SuppressWarnings("unchecked")
59 U result = (U) field.get(subject);
60 return result;
61 } catch (NoSuchFieldException | SecurityException |
62 IllegalArgumentException | IllegalAccessException e) {
63
64 StringWriter sw = new StringWriter();
65 e.printStackTrace(new PrintWriter(sw));
66 fail(sw.toString());
67 return null;
68 }
69 }
70
71 /**
72 * Calls the method, bypassing scope restriction.
73 *
74 * @param subject Object where the method belongs
75 * @param methodName name of the method to call
76 * @param paramTypes formal parameter type array
77 * @param args arguments
78 * @return return value or null if void
79 * @param <T> subject type
80 * @param <U> return value type
81 */
82 public static <T, U> U callMethod(T subject, String methodName, Class<?>[] paramTypes, Object...args) {
83
84 try {
85 @SuppressWarnings("unchecked")
86 Class<T> clazz = (Class<T>) subject.getClass();
Yuta HIGUCHI8f182192014-08-02 18:47:42 -070087 final Method method;
88 if (paramTypes == null || paramTypes.length == 0) {
89 method = clazz.getDeclaredMethod(methodName);
90 } else {
91 method = clazz.getDeclaredMethod(methodName, paramTypes);
92 }
Yuta HIGUCHIbaa766f2014-07-18 13:48:58 -070093 method.setAccessible(true);
94
95 @SuppressWarnings("unchecked")
96 U result = (U) method.invoke(subject, args);
97 return result;
98 } catch (NoSuchMethodException | SecurityException |
99 IllegalAccessException | IllegalArgumentException |
100 InvocationTargetException e) {
101
102 StringWriter sw = new StringWriter();
103 e.printStackTrace(new PrintWriter(sw));
104 fail(sw.toString());
105 return null;
106 }
107 }
108
109 /**
110 * Calls the method, bypassing scope restriction.
111 *
112 * @param subject Object where the method belongs
113 * @param methodName name of the method to call
114 * @param paramType formal parameter type
115 * @param arg argument
116 * @return return value or null if void
117 * @param <T> subject type
118 * @param <U> return value type
119 */
120 public static <T, U> U callMethod(T subject, String methodName, Class<?> paramType, Object arg) {
121 return callMethod(subject, methodName, new Class<?>[]{paramType}, arg);
122 }
123
124 /**
Ray Milkey0f42e832014-07-21 10:49:23 -0700125 * Triggers an allocation of an object of type <T> and forces a call to
126 * the private constructor.
127 *
128 * @param constructor Constructor to call
129 * @param <T> type of the object to create
130 * @return created object of type <T>
131 */
132 public static <T> T callConstructor(Constructor<T> constructor) {
133 try {
134 constructor.setAccessible(true);
135 return constructor.newInstance();
136 } catch (InstantiationException | IllegalAccessException |
137 InvocationTargetException error) {
138 return null;
139 }
140 }
141
142 /**
Yuta HIGUCHIbaa766f2014-07-18 13:48:58 -0700143 * Avoid instantiation.
144 */
145 private TestUtils() {}
146}