diff options
author | Alexander Dorokhine <adorokhine@google.com> | 2017-05-02 19:34:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-02 19:34:59 -0700 |
commit | 796a97541ca681478b707c0b9e535e0d201496ac (patch) | |
tree | 9329f582913f12c356a2f694a469bff1162a95b3 /src/test | |
parent | fec0d88ea2860ef5b595d6f04481cec963350a1d (diff) | |
download | mobly-bundled-snippets-796a97541ca681478b707c0b9e535e0d201496ac.tar.gz |
Simplify API required to call methods by reflection. (#45)
* Implement a cleaner way to call methods by reflection.
* Port all callers to the new reflection API.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/UtilsTest.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/test/java/UtilsTest.java b/src/test/java/UtilsTest.java new file mode 100644 index 0000000..b9a70d2 --- /dev/null +++ b/src/test/java/UtilsTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +import static com.google.android.mobly.snippet.bundled.utils.Utils.invokeByReflection; + +import com.google.android.mobly.snippet.bundled.utils.Utils; +import com.google.common.truth.Truth; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +/** Tests for {@link com.google.android.mobly.snippet.bundled.utils.Utils} */ +public class UtilsTest { + public static final class ReflectionTest_HostClass { + public Object returnSame(List<String> arg) { + return arg; + } + + public Object returnSame(int arg) { + return arg; + } + + public Object multiArgCall(Object arg1, Object arg2, boolean returnArg1) { + if (returnArg1) { + return arg1; + } + return arg2; + } + + public boolean returnTrue() { + return true; + } + + public void throwsException() throws IOException { + throw new IOException("Example exception"); + } + } + + @Test + public void testInvokeByReflection_Obj() throws Throwable { + List<?> sampleList = Collections.singletonList("sampleList"); + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + Object ret = invokeByReflection(hostClass, "returnSame", sampleList); + Truth.assertThat(ret).isSameAs(sampleList); + } + + @Test + public void testInvokeByReflection_Null() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + Object ret = invokeByReflection(hostClass, "returnSame", (Object) null); + Truth.assertThat(ret).isNull(); + } + + @Test + public void testInvokeByReflection_NoArg() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + boolean ret = (boolean) invokeByReflection(hostClass, "returnTrue"); + Truth.assertThat(ret).isTrue(); + } + + @Test + public void testInvokeByReflection_Primitive() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + Object ret = invokeByReflection(hostClass, "returnSame", 5); + Truth.assertThat(ret).isEqualTo(5); + } + + @Test + public void testInvokeByReflection_MultiArg() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + Object arg1 = new Object(); + Object arg2 = new Object(); + Object ret = + invokeByReflection(hostClass, "multiArgCall", arg1, arg2, true /* returnArg1 */); + Truth.assertThat(ret).isSameAs(arg1); + ret = + Utils.invokeByReflection( + hostClass, "multiArgCall", arg1, arg2, false /* returnArg1 */); + Truth.assertThat(ret).isSameAs(arg2); + } + + @Test + public void testInvokeByReflection_NoMatch() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + Truth.assertThat(List.class.isAssignableFrom(Object.class)).isFalse(); + try { + invokeByReflection(hostClass, "returnSame", new Object()); + Assert.fail(); + } catch (NoSuchMethodException e) { + Truth.assertThat(e.getMessage()) + .contains("UtilsTest$ReflectionTest_HostClass#returnSame(Object)"); + } + } + + @Test + public void testInvokeByReflection_UnwrapException() throws Throwable { + ReflectionTest_HostClass hostClass = new ReflectionTest_HostClass(); + try { + invokeByReflection(hostClass, "throwsException"); + Assert.fail(); + } catch (IOException e) { + Truth.assertThat(e.getMessage()).isEqualTo("Example exception"); + } + } +} |