Yuta HIGUCHI | baa766f | 2014-07-18 13:48:58 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.io.PrintWriter; |
| 6 | import java.io.StringWriter; |
Ray Milkey | 0f42e83 | 2014-07-21 10:49:23 -0700 | [diff] [blame] | 7 | import java.lang.reflect.Constructor; |
Yuta HIGUCHI | baa766f | 2014-07-18 13:48:58 -0700 | [diff] [blame] | 8 | import java.lang.reflect.Field; |
| 9 | import java.lang.reflect.InvocationTargetException; |
| 10 | import java.lang.reflect.Method; |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * Utils for testing. |
| 15 | */ |
| 16 | public 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(); |
| 87 | Method method = clazz.getDeclaredMethod(methodName, paramTypes); |
| 88 | method.setAccessible(true); |
| 89 | |
| 90 | @SuppressWarnings("unchecked") |
| 91 | U result = (U) method.invoke(subject, args); |
| 92 | return result; |
| 93 | } catch (NoSuchMethodException | SecurityException | |
| 94 | IllegalAccessException | IllegalArgumentException | |
| 95 | InvocationTargetException e) { |
| 96 | |
| 97 | StringWriter sw = new StringWriter(); |
| 98 | e.printStackTrace(new PrintWriter(sw)); |
| 99 | fail(sw.toString()); |
| 100 | return null; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Calls the method, bypassing scope restriction. |
| 106 | * |
| 107 | * @param subject Object where the method belongs |
| 108 | * @param methodName name of the method to call |
| 109 | * @param paramType formal parameter type |
| 110 | * @param arg argument |
| 111 | * @return return value or null if void |
| 112 | * @param <T> subject type |
| 113 | * @param <U> return value type |
| 114 | */ |
| 115 | public static <T, U> U callMethod(T subject, String methodName, Class<?> paramType, Object arg) { |
| 116 | return callMethod(subject, methodName, new Class<?>[]{paramType}, arg); |
| 117 | } |
| 118 | |
| 119 | /** |
Ray Milkey | 0f42e83 | 2014-07-21 10:49:23 -0700 | [diff] [blame] | 120 | * Triggers an allocation of an object of type <T> and forces a call to |
| 121 | * the private constructor. |
| 122 | * |
| 123 | * @param constructor Constructor to call |
| 124 | * @param <T> type of the object to create |
| 125 | * @return created object of type <T> |
| 126 | */ |
| 127 | public static <T> T callConstructor(Constructor<T> constructor) { |
| 128 | try { |
| 129 | constructor.setAccessible(true); |
| 130 | return constructor.newInstance(); |
| 131 | } catch (InstantiationException | IllegalAccessException | |
| 132 | InvocationTargetException error) { |
| 133 | return null; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
Yuta HIGUCHI | baa766f | 2014-07-18 13:48:58 -0700 | [diff] [blame] | 138 | * Avoid instantiation. |
| 139 | */ |
| 140 | private TestUtils() {} |
| 141 | } |