1   /*
2    * Copyright 2020 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.testing.junit;
18  
19  import org.junit.jupiter.api.AfterAll;
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.BeforeAll;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.extension.AfterAllCallback;
24  import org.junit.jupiter.api.extension.AfterEachCallback;
25  import org.junit.jupiter.api.extension.BeforeAllCallback;
26  import org.junit.jupiter.api.extension.BeforeEachCallback;
27  import org.junit.jupiter.api.extension.ExtensionContext;
28  
29  /**
30   * A base class for JUnit5 extensions that allows implementations to control whether the callbacks are run
31   * around the entire class, like {@link BeforeAll} or {@link AfterAll}, or around each test method, like
32   * {@link BeforeEach} or {@link AfterEach}. By default, the extension will run around the entire class -
33   * implementations that want to run around each test method should override {@link #runForEachTest}.
34   */
35  public abstract class AbstractAllOrEachExtension
36          implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
37  
38      /**
39       * A method that should be run at the beginning of a test lifecycle. If {@link #runForEachTest()}
40       * returns {@code false}, this is run once before all tests, otherwise it is run before each test
41       * method.
42       */
43      protected abstract void before(ExtensionContext context) throws Exception;
44  
45      /**
46       * A method that should be run at the end of a test lifecycle. If {@link #runForEachTest()}
47       * returns {@code false}, this is run once after all tests, otherwise it is run after each test
48       * method.
49       */
50      protected abstract void after(ExtensionContext context) throws Exception;
51  
52      @Override
53      public final void beforeAll(ExtensionContext context) throws Exception {
54          if (!runForEachTest()) {
55              before(context);
56          }
57      }
58  
59      @Override
60      public final void afterAll(ExtensionContext context) throws Exception {
61          if (!runForEachTest()) {
62              after(context);
63          }
64      }
65  
66      @Override
67      public final void beforeEach(ExtensionContext context) throws Exception {
68          if (runForEachTest()) {
69              before(context);
70          }
71      }
72  
73      @Override
74      public final void afterEach(ExtensionContext context) throws Exception {
75          if (runForEachTest()) {
76              after(context);
77          }
78      }
79  
80      /**
81       * Returns whether this extension should run around each test method instead of the entire test class.
82       * Implementations should override this method to return {@code true} to run around each test method.
83       */
84      protected boolean runForEachTest() {
85          return false;
86      }
87  }