GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 42557c...66f82e )
by Glythcing
04:18
created

beforeTestExecution(ExtensionContext)   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
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.benchmark;
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
23
import java.lang.reflect.Method;
24
import java.util.concurrent.TimeUnit;
25
26
import static io.github.glytching.junit.extension.util.ExtensionUtil.getStore;
27
28
/**
29
 * The benchmark extension publishes elapsed time to the execution listener. By default, this
30
 * produces output like so:
31
 *
32
 * <pre>
33
 * timestamp = 2018-08-30T16:28:47.701, Elapsed time in MICROSECONDS for canBenchmark = 13213
34
 * </pre>
35
 *
36
 * Your own implementation of the {@code EngineExecutionListener} could adopt a different template
37
 * for the event string or it could collect and aggregate results for all tests in a test case or it
38
 * could write results to somewhere other than the console etc.
39
 *
40
 * <p>By default, elapsed times are reported in {@link TimeUnit#MILLISECONDS} but you can use {@link
41
 * org.junit.jupiter.api.extension.RegisterExtension} to choose a different {@link TimeUnit}.
42
 *
43
 * <p>Usage example:
44
 *
45
 * <pre>
46
 * &#064;ExtendWith(BenchmarkExtension.class)
47
 * public class MyTest {
48
 *
49
 *  &#064;Test
50
 *  public void aTest() {
51
 *      // ...
52
 *  }
53
 * }
54
 * </pre>
55
 *
56
 * <pre>
57
 * public class MyTest {
58
 *
59
 *  // report elapsed times in a non default time unit
60
 *  &#064;@RegisterExtension
61
 *  static BenchmarkExtension benchmarkExtension = new BenchmarkExtension(TimeUnit.MICROSECONDS);
62
 *
63
 *  &#064;Test
64
 *  public void aTest() {
65
 *      // ...
66
 *  }
67
 * }
68
 * </pre>
69
 *
70
 * @since 2.4.0
71
 */
72
public class BenchmarkExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
73
74
  public static final String REPORT_EVENT_FORMAT = "Elapsed time in %s for %s";
75
76
  private final TimeUnit timeUnit;
77
78
  /** Constructs an instance of this class which will report using the default time unit. */
79
  @SuppressWarnings("unused")
80
  public BenchmarkExtension() {
81
    this(TimeUnit.MILLISECONDS);
82
  }
83
84
  /**
85
   * Constructs an instance of this class which will report using the given {@code timeUnit}.
86
   *
87
   * @param timeUnit the time unit in which benchmarks will be reported
88
   */
89
  public BenchmarkExtension(TimeUnit timeUnit) {
90
    this.timeUnit = timeUnit;
91
  }
92
93
  /**
94
   * Store a {@link StopWatch} instance against the test method name for use in {@link
95
   * #afterTestExecution(ExtensionContext)}.
96
   *
97
   * @param extensionContext the <em>context</em> in which the current test or container is being
98
   *     executed
99
   * @throws Exception
100
   */
101
  @Override
102
  public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
103
    // put a StopWatch in the context for the current test invocation
104
    getStore(extensionContext, this.getClass())
105
        .put(extensionContext.getRequiredTestMethod(), new StopWatch());
106
  }
107
108
  /**
109
   * Gather the elapsed time, using {@link StopWatch} stored by {@link
110
   * #beforeTestExecution(ExtensionContext)}.
111
   *
112
   * @param extensionContext the <em>context</em> in which the current test or container is being
113
   *     executed
114
   * @throws Exception
115
   */
116
  @Override
117
  public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
118
    Method testMethod = extensionContext.getRequiredTestMethod();
119
120
    // get the StopWatch from the context for the current test invocation and report on it
121
    long duration =
122
        getStore(extensionContext, this.getClass())
123
            .get(testMethod, StopWatch.class)
124
            .duration(timeUnit);
125
126
    extensionContext.publishReportEntry(
127
        String.format(REPORT_EVENT_FORMAT, timeUnit.name(), testMethod.getName()),
128
        Long.toString(duration));
129
  }
130
}
131