aboutsummaryrefslogtreecommitdiff
path: root/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterInjectorExtension.java
blob: 6d6aa511fd63738d4806043be82d01b60459688e (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * 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.junit5;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;

import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableList;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

/** Implements the TestParameterInjector logic for JUnit5 (Jupiter). */
class TestParameterInjectorExtension implements TestTemplateInvocationContextProvider {

  private static final TestMethodProcessorList testMethodProcessors =
      TestMethodProcessorList.createNewParameterizedProcessors();

  @Override
  public boolean supportsTestTemplate(ExtensionContext context) {
    return true;
  }

  @Override
  public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
      ExtensionContext extensionContext) {
    validateTestMethodAndConstructor(
        extensionContext.getRequiredTestMethod(), extensionContext.getRequiredTestClass());
    List<TestInfo> testInfos =
        testMethodProcessors.calculateTestInfos(
            extensionContext.getRequiredTestMethod(), extensionContext.getRequiredTestClass());

    return testInfos.stream().map(CustomInvocationContext::of);
  }

  private void validateTestMethodAndConstructor(Method testMethod, Class<?> testClass) {
    checkState(
        testClass.getDeclaredConstructors().length == 1,
        "Only a single constructor is allowed, but found %s in %s",
        testClass.getDeclaredConstructors().length,
        testClass.getSimpleName());
    Constructor<?> constructor =
        getOnlyElement(ImmutableList.copyOf(testClass.getDeclaredConstructors()));

    testMethodProcessors.validateConstructor(constructor).assertValid();

    testMethodProcessors.validateTestMethod(testMethod, testClass).assertValid();

    checkState(
        testMethod.getAnnotation(TestParameterInjectorTest.class) != null,
        "Each test method handled by this extension should be annotated with"
            + " @TestParameterInjectorTest");
  }

  @AutoValue
  abstract static class CustomInvocationContext implements TestTemplateInvocationContext {

    abstract TestInfo testInfo();

    @Memoized
    List<Object> getConstructorParameters() {
      Constructor<?> constructor =
          getOnlyElement(ImmutableList.copyOf(testInfo().getTestClass().getDeclaredConstructors()));

      return testMethodProcessors.getConstructorParameters(constructor, testInfo());
    }

    @Memoized
    List<Object> getTestMethodParameters() {
      return testMethodProcessors.getTestMethodParameters(testInfo());
    }

    static CustomInvocationContext of(TestInfo testInfo) {
      return new AutoValue_TestParameterInjectorExtension_CustomInvocationContext(testInfo);
    }

    @Override
    public String getDisplayName(int invocationIndex) {
      return testInfo().getName();
    }

    @Override
    public List<Extension> getAdditionalExtensions() {
      return ImmutableList.of(new CustomAdditionalExtension());
    }

    class CustomAdditionalExtension implements ParameterResolver, TestInstancePostProcessor {

      @Override
      public boolean supportsParameter(
          ParameterContext parameterContext, ExtensionContext extensionContext) {
        if (parameterContext.getDeclaringExecutable() instanceof Constructor) {
          return true;
        } else {
          return parameterContext
              .getDeclaringExecutable()
              .isAnnotationPresent(TestParameterInjectorTest.class);
        }
      }

      @Override
      public Object resolveParameter(
          ParameterContext parameterContext, ExtensionContext extensionContext) {
        if (parameterContext.getDeclaringExecutable() instanceof Constructor) {
          return getConstructorParameters().get(parameterContext.getIndex());
        } else {
          return getTestMethodParameters().get(parameterContext.getIndex());
        }
      }

      @Override
      public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext)
          throws Exception {
        testMethodProcessors.postProcessTestInstance(testInstance, testInfo());
      }
    }
  }
}