aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/testing/junit/testparameterinjector/TestMethodProcessor.java
blob: 880327fcafab137d0316bff956bee76bfbd7b2fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2021 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.testing.junit.testparameterinjector;

import com.google.common.base.Optional;
import java.util.List;
import org.junit.runner.Description;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;

/**
 * Interface to change the list of methods used in a test.
 *
 * <p>Note: Implementations of this interface are expected to be immutable, i.e. they no longer
 * change after construction.
 */
interface TestMethodProcessor {

  /** Allows to transform the test information (name and annotations). */
  List<TestInfo> processTest(Class<?> testClass, TestInfo originalTest);

  /**
   * Allows to change the code executed during the test.
   *
   * @param finalTestDescription the final description calculated taking into account this and all
   *     other test processors
   */
  Statement processStatement(Statement originalStatement, Description finalTestDescription);

  /**
   * This method allows to transform the test object used for {@link #processStatement(Statement,
   * Description)}.
   *
   * @param test the value returned by the previous processor, or {@link Optional#absent()} if this
   *     processor is the first.
   * @return {@link Optional#absent()} if the default test instance will be used from instantiating
   *     the test class with the default constructor.
   *     <p>The default implementation should return {@code test}.
   */
  Optional<Object> createTest(TestClass testClass, FrameworkMethod method, Optional<Object> test);

  /**
   * This method allows to transform the statement object used for {@link
   * #processStatement(Statement, Description)}.
   *
   * @param statement the value returned by the previous processor, or {@link Optional#absent()} if
   *     this processor is the first.
   * @return {@link Optional#absent()} if the default statement will be used from invoking the test
   *     method with no parameters.
   *     <p>The default implementation should return {@code statement}.
   */
  Optional<Statement> createStatement(
      TestClass testClass,
      FrameworkMethod method,
      Object testObject,
      Optional<Statement> statement);

  /**
   * Optionally validates the {@code testClass} constructor, and returns whether the validation
   * should continue or stop.
   *
   * @param errorsReturned A mutable list that any validation error should be added to.
   */
  ValidationResult validateConstructor(TestClass testClass, List<Throwable> errorsReturned);

  /**
   * Optionally validates the {@code testClass} methods, and returns whether the validation should
   * continue or stop.
   *
   * @param errorsReturned A mutable list that any validation error should be added to.
   */
  ValidationResult validateTestMethod(
      TestClass testClass, FrameworkMethod testMethod, List<Throwable> errorsReturned);

  /**
   * Whether the constructor or method validation has been handled or not.
   *
   * <p>If the validation is not handled by a processor, it will be handled using the default {@link
   * BlockJUnit4ClassRunner} validator.
   */
  enum ValidationResult {
    NOT_HANDLED,
    HANDLED,
  }
}