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