|
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 io.github.glytching.junit.extension.watcher; |
|
18
|
|
|
|
|
19
|
|
|
import org.junit.jupiter.api.extension.AfterTestExecutionCallback; |
|
20
|
|
|
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; |
|
21
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext; |
|
22
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext.Store; |
|
23
|
|
|
|
|
24
|
|
|
import java.lang.reflect.Method; |
|
25
|
|
|
import java.util.logging.Logger; |
|
26
|
|
|
|
|
27
|
|
|
import static io.github.glytching.junit.extension.util.ExtensionUtil.getStore; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The watcher extension logs test execution flow including: |
|
31
|
|
|
* |
|
32
|
|
|
* <ul> |
|
33
|
|
|
* <li>Entry |
|
34
|
|
|
* <li>Exit |
|
35
|
|
|
* <li>Elapsed time in ms |
|
36
|
|
|
* </ul> |
|
37
|
|
|
* |
|
38
|
|
|
* <p>It produces output like so: |
|
39
|
|
|
* |
|
40
|
|
|
* <pre> |
|
41
|
|
|
* INFO: Starting test: [aTest] |
|
42
|
|
|
* INFO: Completed test [aTest] in 21 ms. |
|
43
|
|
|
* </pre> |
|
44
|
|
|
* |
|
45
|
|
|
* <p>Usage example: |
|
46
|
|
|
* |
|
47
|
|
|
* <pre> |
|
48
|
|
|
* @ExtendWith(WatcherExtension.class) |
|
49
|
|
|
* public class MyTest { |
|
50
|
|
|
* |
|
51
|
|
|
* @Test |
|
52
|
|
|
* public void aTest() { |
|
53
|
|
|
* // ... |
|
54
|
|
|
* } |
|
55
|
|
|
* } |
|
56
|
|
|
* </pre> |
|
57
|
|
|
* |
|
58
|
|
|
* @see <a |
|
59
|
|
|
* href="https://github.com/junit-team/junit4/wiki/Rules#testwatchmantestwatcher-rules">JUnit 4 |
|
60
|
|
|
* TestWatchman/TestWatcher Rules</a> |
|
61
|
|
|
* @since 1.0.0 |
|
62
|
|
|
*/ |
|
63
|
|
|
public class WatcherExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback { |
|
64
|
|
|
private final Logger logger; |
|
65
|
|
|
|
|
66
|
|
|
WatcherExtension() { |
|
67
|
|
|
this(Logger.getLogger(WatcherExtension.class.getName())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// erm, facilitates testing |
|
71
|
|
|
WatcherExtension(Logger logger) { |
|
72
|
|
|
this.logger = logger; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Log test method entry and store its start time in the {@link Store} for use in {@link |
|
77
|
|
|
* #afterTestExecution(ExtensionContext)}. |
|
78
|
|
|
* |
|
79
|
|
|
* @param extensionContext the <em>context</em> in which the current test or container is being |
|
80
|
|
|
* executed |
|
81
|
|
|
* @throws Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
@Override |
|
84
|
|
|
public void beforeTestExecution(ExtensionContext extensionContext) throws Exception { |
|
85
|
|
|
Method testMethod = extensionContext.getRequiredTestMethod(); |
|
86
|
|
|
logger.info(String.format("Starting test [%s]", testMethod.getName())); |
|
87
|
|
|
getStore(extensionContext, this.getClass()).put(testMethod, System.currentTimeMillis()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Log test method exit, using the start time stored by {@link |
|
92
|
|
|
* #beforeTestExecution(ExtensionContext)} to calculate a duration. |
|
93
|
|
|
* |
|
94
|
|
|
* @param extensionContext the <em>context</em> in which the current test or container is being |
|
95
|
|
|
* executed |
|
96
|
|
|
* @throws Exception |
|
97
|
|
|
*/ |
|
98
|
|
|
@Override |
|
99
|
|
|
public void afterTestExecution(ExtensionContext extensionContext) throws Exception { |
|
100
|
|
|
Method testMethod = extensionContext.getRequiredTestMethod(); |
|
101
|
|
|
long start = getStore(extensionContext, this.getClass()).remove(testMethod, long.class); |
|
102
|
|
|
long duration = System.currentTimeMillis() - start; |
|
103
|
|
|
|
|
104
|
|
|
logger.info(String.format("Completed test [%s] in %sms", testMethod.getName(), duration)); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|