Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 16 | package org.onlab.junit; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertArrayEquals; |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 24 | import org.onlab.junit.TestUtils.TestUtilsException; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Test and usage examples for TestUtils. |
| 28 | */ |
| 29 | public 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 | |
Yuta HIGUCHI | f2548cb | 2014-11-03 18:02:25 -0800 | [diff] [blame] | 104 | assertEquals(42, (int) TestUtils.getField(test, "privateField")); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 105 | TestUtils.setField(test, "privateField", 0xDEAD); |
Yuta HIGUCHI | f2548cb | 2014-11-03 18:02:25 -0800 | [diff] [blame] | 106 | assertEquals(0xDEAD, (int) TestUtils.getField(test, "privateField")); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | f2548cb | 2014-11-03 18:02:25 -0800 | [diff] [blame] | 117 | assertEquals(2501, (int) TestUtils.getField(test, "protectedField")); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 118 | TestUtils.setField(test, "protectedField", 0xBEEF); |
Yuta HIGUCHI | f2548cb | 2014-11-03 18:02:25 -0800 | [diff] [blame] | 119 | assertEquals(0xBEEF, (int) TestUtils.getField(test, "protectedField")); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Example to call private method and multiple parameters. |
HIGUCHI Yuta | 67023a2 | 2016-03-28 13:35:44 -0700 | [diff] [blame] | 124 | * <p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 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. |
HIGUCHI Yuta | 67023a2 | 2016-03-28 13:35:44 -0700 | [diff] [blame] | 154 | * <p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 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. |
HIGUCHI Yuta | 67023a2 | 2016-03-28 13:35:44 -0700 | [diff] [blame] | 173 | * <p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 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 | } |