blob: 39712ba8571dc75abf4dc1bc62e82e0ed48cf72b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070016package org.onlab.junit;
Jonathan Hart20d8e512014-10-16 11:05:52 -070017
18import static org.junit.Assert.assertArrayEquals;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNull;
21
22import org.junit.Before;
23import org.junit.Test;
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070024import org.onlab.junit.TestUtils.TestUtilsException;
Jonathan Hart20d8e512014-10-16 11:05:52 -070025
26/**
27 * Test and usage examples for TestUtils.
28 */
29public class TestUtilsTest {
30
31 /**
32 * Test data.
33 */
34 private static final class TestClass {
35
36 @SuppressWarnings("unused")
37 private int privateField = 42;
38
39 @SuppressWarnings("unused")
40 protected int protectedField = 2501; // CHECKSTYLE IGNORE THIS LINE
41
42 /**
43 * Protected method with multiple argument.
44 *
45 * @param x simply returns
46 * @param y not used
47 * @return x
48 */
49 @SuppressWarnings("unused")
50 private int privateMethod(Number x, Long y) {
51 return x.intValue();
52 }
53
54 /**
55 * Protected method with no argument.
56 *
57 * @return int
58 */
59 @SuppressWarnings("unused")
60 protected int protectedMethod() {
61 return 42;
62 }
63
64 /**
65 * Method returning array.
66 *
67 * @param ary random array
68 * @return ary
69 */
70 @SuppressWarnings("unused")
71 private int[] arrayReturnMethod(int[] ary) {
72 return ary;
73 }
74
75 /**
76 * Method without return value.
77 *
78 * @param s ignored
79 */
80 @SuppressWarnings("unused")
81 private void voidMethod(String s) {
82 System.out.println(s);
83 }
84 }
85
86 private TestClass test;
87
88 /**
89 * Sets up the test fixture.
90 */
91 @Before
92 public void setUp() {
93 test = new TestClass();
94 }
95
96 /**
97 * Example to access private field.
98 *
99 * @throws TestUtilsException TestUtils error
100 */
101 @Test
102 public void testSetGetPrivateField() throws TestUtilsException {
103
104 assertEquals(42, TestUtils.getField(test, "privateField"));
105 TestUtils.setField(test, "privateField", 0xDEAD);
106 assertEquals(0xDEAD, TestUtils.getField(test, "privateField"));
107 }
108
109 /**
110 * Example to access protected field.
111 *
112 * @throws TestUtilsException TestUtils error
113 */
114 @Test
115 public void testSetGetProtectedField() throws TestUtilsException {
116
117 assertEquals(2501, TestUtils.getField(test, "protectedField"));
118 TestUtils.setField(test, "protectedField", 0xBEEF);
119 assertEquals(0xBEEF, TestUtils.getField(test, "protectedField"));
120 }
121
122 /**
123 * Example to call private method and multiple parameters.
124 * <p/>
125 * It also illustrates that paramTypes must match declared type,
126 * not the runtime types of arguments.
127 *
128 * @throws TestUtilsException TestUtils error
129 */
130 @Test
131 public void testCallPrivateMethod() throws TestUtilsException {
132
133 int result = TestUtils.callMethod(test, "privateMethod",
134 new Class<?>[] {Number.class, Long.class},
135 Long.valueOf(42), Long.valueOf(32));
136 assertEquals(42, result);
137 }
138
139 /**
140 * Example to call protected method and no parameters.
141 *
142 * @throws TestUtilsException TestUtils error
143 */
144 @Test
145 public void testCallProtectedMethod() throws TestUtilsException {
146
147 int result = TestUtils.callMethod(test, "protectedMethod",
148 new Class<?>[] {});
149 assertEquals(42, result);
150 }
151
152 /**
153 * Example to call method returning array.
154 * <p/>
155 * Note: It is not required to receive as Object.
156 * Following is just verifying it is not Boxed arrays.
157 *
158 * @throws TestUtilsException TestUtils error
159 */
160 @Test
161 public void testCallArrayReturnMethod() throws TestUtilsException {
162
163 int[] array = {1, 2, 3};
164 Object aryResult = TestUtils.callMethod(test, "arrayReturnMethod",
165 new Class<?>[] {int[].class}, array);
166 assertEquals(int[].class, aryResult.getClass());
167 assertArrayEquals(array, (int[]) aryResult);
168 }
169
170
171 /**
172 * Example to call void returning method.
173 * <p/>
174 * Note: Return value will be null for void methods.
175 *
176 * @throws TestUtilsException TestUtils error
177 */
178 @Test
179 public void testCallVoidReturnMethod() throws TestUtilsException {
180
181 Object voidResult = TestUtils.callMethod(test, "voidMethod",
182 String.class, "foobar");
183 assertNull(voidResult);
184 }
185}