View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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) { /* no-op */ }
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  }