1 /* 2 * Copyright (C) 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 import static com.google.android.mobly.snippet.bundled.utils.Utils.invokeByReflection; 18 19 import com.google.android.mobly.snippet.bundled.utils.Utils; 20 import com.google.common.truth.Truth; 21 import java.io.IOException; 22 import java.util.Collections; 23 import java.util.List; 24 import org.junit.Assert; 25 import org.junit.Test; 26 27 /** Tests for {@link com.google.android.mobly.snippet.bundled.utils.Utils} */ 28 public class UtilsTest { 29 public static final class ReflectionTest_HostClass { returnSame(List<String> arg)30 public Object returnSame(List<String> arg) { 31 return arg; 32 } 33 returnSame(int arg)34 public Object returnSame(int arg) { 35 return arg; 36 } 37 multiArgCall(Object arg1, Object arg2, boolean returnArg1)38 public Object multiArgCall(Object arg1, Object arg2, boolean returnArg1) { 39 if (returnArg1) { 40 return arg1; 41 } 42 return arg2; 43 } 44 returnTrue()45 public boolean returnTrue() { 46 return true; 47 } 48 throwsException()49 public void throwsException() throws IOException { 50 throw new IOException("Example exception"); 51 } 52 } 53 54 @Test testInvokeByReflection_Obj()55 public void testInvokeByReflection_Obj() throws Throwable { 56 List<?> sampleList = Collections.singletonList("sampleList"); 57 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 58 Object ret = invokeByReflection(hostClass, "returnSame", sampleList); 59 Truth.assertThat(ret).isSameAs(sampleList); 60 } 61 62 @Test testInvokeByReflection_Null()63 public void testInvokeByReflection_Null() throws Throwable { 64 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 65 Object ret = invokeByReflection(hostClass, "returnSame", (Object) null); 66 Truth.assertThat(ret).isNull(); 67 } 68 69 @Test testInvokeByReflection_NoArg()70 public void testInvokeByReflection_NoArg() throws Throwable { 71 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 72 boolean ret = (boolean) invokeByReflection(hostClass, "returnTrue"); 73 Truth.assertThat(ret).isTrue(); 74 } 75 76 @Test testInvokeByReflection_Primitive()77 public void testInvokeByReflection_Primitive() throws Throwable { 78 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 79 Object ret = invokeByReflection(hostClass, "returnSame", 5); 80 Truth.assertThat(ret).isEqualTo(5); 81 } 82 83 @Test testInvokeByReflection_MultiArg()84 public void testInvokeByReflection_MultiArg() throws Throwable { 85 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 86 Object arg1 = new Object(); 87 Object arg2 = new Object(); 88 Object ret = 89 invokeByReflection(hostClass, "multiArgCall", arg1, arg2, true /* returnArg1 */); 90 Truth.assertThat(ret).isSameAs(arg1); 91 ret = 92 Utils.invokeByReflection( 93 hostClass, "multiArgCall", arg1, arg2, false /* returnArg1 */); 94 Truth.assertThat(ret).isSameAs(arg2); 95 } 96 97 @Test testInvokeByReflection_NoMatch()98 public void testInvokeByReflection_NoMatch() throws Throwable { 99 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 100 Truth.assertThat(List.class.isAssignableFrom(Object.class)).isFalse(); 101 try { 102 invokeByReflection(hostClass, "returnSame", new Object()); 103 Assert.fail(); 104 } catch (NoSuchMethodException e) { 105 Truth.assertThat(e.getMessage()) 106 .contains("UtilsTest$ReflectionTest_HostClass#returnSame(Object)"); 107 } 108 } 109 110 @Test testInvokeByReflection_UnwrapException()111 public void testInvokeByReflection_UnwrapException() throws Throwable { 112 ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); 113 try { 114 invokeByReflection(hostClass, "throwsException"); 115 Assert.fail(); 116 } catch (IOException e) { 117 Truth.assertThat(e.getMessage()).isEqualTo("Example exception"); 118 } 119 } 120 } 121