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.mstat;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.cp.*;
27  import org.apache.juneau.rest.stats.*;
28  import org.junit.jupiter.api.*;
29  
30  class MethodExecStore_Test extends TestBase {
31  
32  	//------------------------------------------------------------------------------------------------------------------
33  	// Builder tests.
34  	//------------------------------------------------------------------------------------------------------------------
35  
36  	@Test void a01_builder_default() {
37  		assertInstanceOf(MethodExecStore.class, MethodExecStore.create().build());
38  	}
39  
40  	public static class A1 extends MethodExecStore{
41  		protected A1(Builder builder) {
42  			super(builder);
43  		}
44  	}
45  
46  	@Test void a02_builder_implClass() {
47  		assertInstanceOf(A1.class, MethodExecStore.create().type(A1.class).build());
48  	}
49  
50  	public static class A4 extends MethodExecStore {
51  		public A4(MethodExecStore.Builder b) throws Exception {
52  			super(b);
53  			throw new RuntimeException("foobar");
54  		}
55  	}
56  
57  	@Test void a04_builder_implClass_bad() {
58  		assertThrowsWithMessage(Exception.class, "foobar", ()->MethodExecStore.create().type(A4.class).build());
59  	}
60  
61  	public static class A5a {}
62  
63  	public static class A5b extends MethodExecStore {
64  		public A5b(MethodExecStore.Builder b, A5a x) throws Exception {
65  			super(b);
66  			if (x == null)
67  				throw new RuntimeException("Bad");
68  		}
69  	}
70  
71  	public static class A5c extends MethodExecStore {
72  		public A5c(MethodExecStore.Builder b, Optional<A5a> x) throws Exception {
73  			super(b);
74  			if (x == null)
75  				throw new RuntimeException("Bad");
76  		}
77  	}
78  
79  	@Test void a05_builder_beanFactory() {
80  		var bs = BeanStore.create().build();
81  
82  		assertThrowsWithMessage(Exception.class, "Public constructor found but could not find prerequisites: A5a", ()->MethodExecStore.create(bs).type(A5b.class).build());
83  		assertInstanceOf(A5c.class, MethodExecStore.create(bs).type(A5c.class).build());
84  
85  		bs.addBean(A5a.class, new A5a());
86  		assertInstanceOf(A5b.class, MethodExecStore.create(bs).type(A5b.class).build());
87  		assertInstanceOf(A5c.class, MethodExecStore.create(bs).type(A5c.class).build());
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 {  // NOSONAR - Must be public.
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 {  // NOSONAR - Must be public.
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 	// Store tests.
137 	//------------------------------------------------------------------------------------------------------------------
138 
139 	@Test public void b01_store_getStats() throws Exception {  // NOSONAR - Must be public.
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 	// MethodExecStats tests.
153 	//------------------------------------------------------------------------------------------------------------------
154 
155 	@Test public void c01_stats_basic() throws Exception {  // NOSONAR - Must be public.
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 }