1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.utils;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.lang.reflect.*;
23
24 import org.apache.juneau.*;
25 import org.apache.juneau.rest.stats.*;
26 import org.junit.jupiter.api.*;
27
28 class MethodInvokerTest extends TestBase {
29
30 private MethodExecStore store = MethodExecStore
31 .create()
32 .thrownStore(
33 ThrownStore.create().ignoreClasses(MethodInvokerTest.class).build()
34 )
35 .build();
36
37 public static class A {
38 public int foo() { return 0; }
39 public int bar() { throw new RuntimeException("bar"); }
40 public void baz(int x) { }
41 }
42
43 private MethodInvoker create(Method m) {
44 return new MethodInvoker(m, store.getStats(m));
45 }
46
47 @Test void a01_basic() throws Exception {
48 var m = A.class.getMethod("foo");
49
50 var mi = create(m);
51
52 var a = new A();
53 mi.invoke(a);
54 mi.invoke(a);
55 mi.invoke(a);
56
57 assertBean(mi.getStats(), "runs,errors", "3,0");
58 }
59
60 @Test void a02_exception() throws Exception {
61 var m = A.class.getMethod("bar");
62
63 var mi = create(m);
64
65 var a = new A();
66 assertThrows(Exception.class, ()->mi.invoke(a));
67 assertThrows(Exception.class, ()->mi.invoke(a));
68 assertThrows(Exception.class, ()->mi.invoke(a));
69
70 assertBean(mi.getStats(), "runs,errors", "3,3");
71 }
72
73 @Test void a03_illegalArgument() throws Exception {
74 var m = A.class.getMethod("baz", int.class);
75
76 var mi = create(m);
77
78 var a = new A();
79 assertThrows(Exception.class, ()->mi.invoke(a, "x"));
80 assertThrows(Exception.class, ()->mi.invoke(a));
81 assertThrows(Exception.class, ()->mi.invoke(a, 1, "x"));
82
83 assertBean(mi.getStats(), "runs,errors", "3,3");
84 }
85
86 @Test void a04_otherMethods() throws Exception {
87 var m = A.class.getMethod("foo");
88 var mi = create(m);
89
90 assertEquals(m, mi.inner().inner());
91 assertEquals("A", mi.getDeclaringClass().getSimpleName());
92 assertEquals(A.class.getName() + ".foo()", mi.getFullName());
93 }
94 }