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