blob: 06ccedbee2955cef2d704b316a6d427424b8217b [file] [log] [blame]
Yuta HIGUCHIbaa766f2014-07-18 13:48:58 -07001package net.onrc.onos.core.util;
2
3import static org.junit.Assert.*;
4import static net.onrc.onos.core.util.TestUtils.*;
5
6import org.junit.Before;
7import org.junit.Test;
8
9/**
10 * Test and usage examples for TestUtils.
11 */
12public class TestUtilsTest {
13
14 /**
15 * Test data.
16 */
17 private static final class TestClass {
18
19 @SuppressWarnings("unused")
20 private int privateField = 42;
21
22 @SuppressWarnings("unused")
23 protected int protectedField = 2501; // CHECKSTYLE IGNORE THIS LINE
24
25 /**
26 * Protected method with multiple argument.
27 *
28 * @param x simply returns
29 * @param y not used
30 * @return x
31 */
32 @SuppressWarnings("unused")
33 private int privateMethod(Number x, Long y) {
34 return x.intValue();
35 }
36
37 /**
38 * Protected method with no argument.
39 *
40 * @return int
41 */
42 @SuppressWarnings("unused")
43 protected int protectedMethod() {
44 return 42;
45 }
46
47 /**
48 * Method returning array.
49 *
50 * @param ary random array
51 * @return ary
52 */
53 @SuppressWarnings("unused")
54 private int[] arrayReturnMethod(int[] ary) {
55 return ary;
56 }
57
58 /**
59 * Method without return value.
60 *
61 * @param s ignored
62 */
63 @SuppressWarnings("unused")
64 private void voidMethod(String s) {
65 System.out.println(s);
66 }
67 }
68
69 private TestClass test;
70
71 /**
72 * setUp.
73 */
74 @Before
75 public void setUp() {
76 test = new TestClass();
77 }
78
79 /**
80 * Example to access private field.
81 */
82 @Test
83 public void testSetGetPrivateField() {
84
85 assertEquals(42, getField(test, "privateField"));
86 setField(test, "privateField", 0xDEAD);
87 assertEquals(0xDEAD, getField(test, "privateField"));
88 }
89
90 /**
91 * Example to access protected field.
92 */
93 @Test
94 public void testSetGetProtectedField() {
95
96 assertEquals(2501, getField(test, "protectedField"));
97 setField(test, "protectedField", 0xBEEF);
98 assertEquals(0xBEEF, getField(test, "protectedField"));
99 }
100
101 /**
102 * Example to call private method and multiple parameters.
103 * <p/>
104 * It also illustrates that paramTypes must match declared type,
105 * not the runtime types of arguments.
106 */
107 @Test
108 public void testCallPrivateMethod() {
109
110 int result = callMethod(test, "privateMethod",
111 new Class<?>[] {Number.class, Long.class},
112 Long.valueOf(42), Long.valueOf(32));
113 assertEquals(42, result);
114 }
115
116 /**
117 * Example to call protected method and no parameters.
118 */
119 @Test
120 public void testCallProtectedMethod() {
121
122 int result = callMethod(test, "protectedMethod",
123 new Class<?>[] {});
124 assertEquals(42, result);
125 }
126
127 /**
128 * Example to call method returning array.
129 * <p/>
130 * Note: It is not required to receive as Object.
131 * Following is just verifying it is not Boxed arrays.
132 */
133 @Test
134 public void testCallArrayReturnMethod() {
135
136 int[] array = {1, 2, 3};
137 Object aryResult = callMethod(test, "arrayReturnMethod",
138 new Class<?>[] {int[].class}, array);
139 assertEquals(int[].class, aryResult.getClass());
140 assertArrayEquals(array, (int[]) aryResult);
141 }
142
143
144 /**
145 * Example to call void returning method.
146 * <p/>
147 * Note: Return value will be null for void methods.
148 */
149 @Test
150 public void testCallVoidReturnMethod() {
151
152 Object voidResult = callMethod(test, "voidMethod",
153 String.class, "foobar");
154 assertNull(voidResult);
155 }
156}