1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.mstat;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.util.*;
23
24 import org.apache.juneau.*;
25 import org.apache.juneau.cp.*;
26 import org.apache.juneau.rest.stats.*;
27 import org.junit.jupiter.api.*;
28
29 class MethodExecStore_Test extends TestBase {
30
31
32
33
34
35 @Test void a01_builder_default() {
36 assertInstanceOf(MethodExecStore.class, MethodExecStore.create().build());
37 }
38
39 public static class A1 extends MethodExecStore{
40 protected A1(Builder builder) {
41 super(builder);
42 }
43 }
44
45 @Test void a02_builder_implClass() {
46 assertInstanceOf(A1.class, MethodExecStore.create().type(A1.class).build());
47 }
48
49 public static class A4 extends MethodExecStore {
50 public A4(MethodExecStore.Builder b) throws Exception {
51 super(b);
52 throw new RuntimeException("foobar");
53 }
54 }
55
56 @Test void a04_builder_implClass_bad() {
57 assertThrowsWithMessage(Exception.class, "foobar", ()->MethodExecStore.create().type(A4.class).build());
58 }
59
60 public static class A5a {}
61
62 public static class A5b extends MethodExecStore {
63 public A5b(MethodExecStore.Builder b, A5a x) throws Exception {
64 super(b);
65 if (x == null)
66 throw new RuntimeException("Bad");
67 }
68 }
69
70 public static class A5c extends MethodExecStore {
71 public A5c(MethodExecStore.Builder b, Optional<A5a> x) throws Exception {
72 super(b);
73 if (x == null)
74 throw new RuntimeException("Bad");
75 }
76 }
77
78 @Test void a05_builder_beanFactory() {
79 var bs = BeanStore.create().build();
80
81 assertThrowsWithMessage(Exception.class, "Public constructor found but could not find prerequisites: A5a", ()->MethodExecStore.create(bs).type(A5b.class).build());
82 assertInstanceOf(A5c.class, MethodExecStore.create(bs).type(A5c.class).build());
83
84 bs.addBean(A5a.class, new A5a());
85 assertInstanceOf(A5b.class, MethodExecStore.create(bs).type(A5b.class).build());
86 assertInstanceOf(A5c.class, MethodExecStore.create(bs).type(A5c.class).build());
87 }
88
89
90 public static class A6a {}
91
92 public static class A6b extends MethodExecStats {
93 public A6b(MethodExecStats.Builder b, A6a x) throws Exception {
94 super(b);
95 if (x == null)
96 throw new RuntimeException("Bad");
97 }
98 }
99
100 public static class A6c extends MethodExecStats {
101 public A6c(MethodExecStats.Builder b, Optional<A6a> x) throws Exception {
102 super(b);
103 if (x == null)
104 throw new RuntimeException("Bad");
105 }
106 }
107
108 @Test public void a06_builder_statsImplClass() throws Exception {
109 var bs = BeanStore.create().build();
110 var m = MethodExecStore_Test.class.getMethod("a06_builder_statsImplClass");
111
112 assertThrowsWithMessage(Exception.class, "Public constructor found but could not find prerequisites: A6a", ()->MethodExecStore.create(bs).statsImplClass(A6b.class).build().getStats(m));
113 assertInstanceOf(A6c.class, MethodExecStore.create(bs).statsImplClass(A6c.class).build().getStats(m));
114
115 bs.addBean(A6a.class, new A6a());
116 assertInstanceOf(A6b.class, MethodExecStore.create(bs).statsImplClass(A6b.class).build().getStats(m));
117 assertInstanceOf(A6c.class, MethodExecStore.create(bs).statsImplClass(A6c.class).build().getStats(m));
118 }
119
120 @Test public void a07_builder_thrownStore() throws Exception {
121 var m = MethodExecStore_Test.class.getMethod("a07_builder_thrownStore");
122 var s = ThrownStore.create().build();
123
124 var store = MethodExecStore.create().thrownStore(s).build();
125 store.getStats(m).error(new Throwable());
126 assertSize(1, s.getStats());
127 assertSame(s, store.getThrownStore());
128
129 var s2 = ThrownStore.create().build();
130 var bs = BeanStore.create().build().addBean(ThrownStore.class, s2);
131 store = MethodExecStore.create(bs).build();
132 assertSame(s2, store.getThrownStore());
133 }
134
135
136
137
138
139 @Test public void b01_store_getStats() throws Exception {
140 var m = MethodExecStore_Test.class.getMethod("b01_store_getStats");
141 var s = ThrownStore.create().build();
142
143 var store = MethodExecStore.create().thrownStore(s).build();
144 store.getStats(m).error(new Throwable());
145
146 assertSize(1, store.getStats(m).getThrownStore().getStats());
147 assertSize(1, store.getStats(m).getThrownStore().getStats());
148 assertSize(1, store.getStats());
149 }
150
151
152
153
154
155 @Test public void c01_stats_basic() throws Exception {
156 var m = MethodExecStore_Test.class.getMethod("c01_stats_basic");
157 var s = ThrownStore.create().build();
158
159 var store = MethodExecStore.create().thrownStore(s).build();
160 var stats = store.getStats(m);
161
162 assertNotEquals(0L, stats.getGuid());
163 assertSame(m, stats.getMethod());
164
165 assertEquals(0, stats.getRuns());
166 assertEquals(0, stats.getRunning());
167 assertEquals(0, stats.getErrors());
168 assertEquals(0, stats.getMinTime());
169 assertEquals(0, stats.getMaxTime());
170 assertEquals(0, stats.getAvgTime());
171 assertEquals(0L, stats.getTotalTime());
172
173 stats.started().finished(100*1000000).started().finished(200*1000000).started().error(new Throwable());
174
175 assertEquals(3, stats.getRuns());
176 assertEquals(1, stats.getRunning());
177 assertEquals(1, stats.getErrors());
178 assertEquals(100, stats.getMinTime());
179 assertEquals(200, stats.getMaxTime());
180 assertEquals(150, stats.getAvgTime());
181 assertEquals(300L, stats.getTotalTime());
182
183 assertContains("300", stats);
184 }
185 }