| 14 | import java.lang.reflect.Proxy; |
| 15 | |
| 16 | @RunWith(AndroidJUnit4.class) |
| 17 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 18 | public class UnitTest { |
| 19 | |
| 20 | @Test |
| 21 | public void t00_init() { |
| 22 | Assert.assertTrue(LSPTest.initHooker()); |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | public void t01_staticMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 27 | var staticMethod = LSPTest.class.getDeclaredMethod("staticMethod"); |
| 28 | var staticMethodReplacement = Replacement.class.getDeclaredMethod("staticMethodReplacement", Hooker.MethodCallback.class); |
| 29 | Assert.assertFalse(LSPTest.staticMethod()); |
| 30 | |
| 31 | Hooker hooker = Hooker.hook(staticMethod, staticMethodReplacement, null); |
| 32 | Assert.assertNotNull(hooker); |
| 33 | Assert.assertTrue(LSPTest.staticMethod()); |
| 34 | Assert.assertFalse((boolean) hooker.backup.invoke(null)); |
| 35 | |
| 36 | Assert.assertTrue(hooker.unhook()); |
| 37 | Assert.assertFalse(LSPTest.staticMethod()); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void t02_normalMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 42 | var normalMethod = LSPTest.class.getDeclaredMethod("normalMethod", String.class, int.class, long.class); |
| 43 | var normalMethodReplacement = Replacement.class.getDeclaredMethod("normalMethodReplacement", Hooker.MethodCallback.class); |
| 44 | var a = "test"; |
| 45 | var b = 114514; |
| 46 | var c = 1919810L; |
| 47 | var o = a + b + c; |
| 48 | var r = a + b + c + "replace"; |
| 49 | LSPTest test = new LSPTest(); |
| 50 | Assert.assertEquals(o, test.normalMethod(a, b, c)); |
| 51 | |
| 52 | Hooker hooker = Hooker.hook(normalMethod, normalMethodReplacement, new Replacement()); |
| 53 | Assert.assertNotNull(hooker); |
| 54 | Assert.assertEquals(r, test.normalMethod(a, b, c)); |
| 55 | Assert.assertEquals(o, hooker.backup.invoke(test, a, b, c)); |
| 56 | |
| 57 | Assert.assertTrue(hooker.unhook()); |
| 58 | Assert.assertEquals(o, test.normalMethod(a, b, c)); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void t03_constructor() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { |
| 63 | var constructor = LSPTest.class.getDeclaredConstructor(); |
| 64 | var constructorReplacement = Replacement.class.getDeclaredMethod("constructorReplacement", Hooker.MethodCallback.class); |
| 65 | Assert.assertFalse(new LSPTest().field); |
| 66 | Assert.assertFalse(constructor.newInstance().field); |
| 67 | |
| 68 | Hooker hooker = Hooker.hook(constructor, constructorReplacement, new Replacement()); |
| 69 | Assert.assertNotNull(hooker); |
| 70 | Assert.assertTrue(new LSPTest().field); |
| 71 | Assert.assertTrue(constructor.newInstance().field); |
| 72 | |
| 73 | Assert.assertTrue(hooker.unhook()); |
nothing calls this directly
no outgoing calls
no test coverage detected