diff options
author | Jens Nyman <jnyman@google.com> | 2024-02-22 20:14:03 +0000 |
---|---|---|
committer | Jens Nyman <jnyman@google.com> | 2024-02-22 20:14:03 +0000 |
commit | 42fab13ba4f87b8556f9aca032f849e6c13b070e (patch) | |
tree | 6e7231dd13d09094a21c88b18cfba7fb8e09401f | |
parent | 9c1183e5d495f833f7352400255236a5b8a042b9 (diff) | |
download | TestParameterInjector-42fab13ba4f87b8556f9aca032f849e6c13b070e.tar.gz |
Refactor: Create a common GenericParameterContext type that can serve as basis for the context classes of @TestParameter and @TestParameters, as wel as being used ineternally
11 files changed, 208 insertions, 118 deletions
diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/GenericParameterContext.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/GenericParameterContext.java new file mode 100644 index 0000000..f2a8c73 --- /dev/null +++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/GenericParameterContext.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 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 static com.google.common.collect.Iterables.getOnlyElement; + +import com.google.common.base.Joiner; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Ordering; +import java.lang.annotation.Annotation; +import java.util.NoSuchElementException; + +/** A value class that contains extra information about the context of a field or parameter. */ +final class GenericParameterContext { + + private final ImmutableList<Annotation> annotationsOnParameter; + private final Class<?> testClass; + + GenericParameterContext(ImmutableList<Annotation> annotationsOnParameter, Class<?> testClass) { + this.annotationsOnParameter = annotationsOnParameter; + this.testClass = testClass; + } + + /** + * Returns the only annotation with the given type on the field or parameter. + * + * @throws NoSuchElementException if this there is no annotation with the given type + * @throws IllegalArgumentException if there are multiple annotations with the given type + */ + @SuppressWarnings("unchecked") // Safe because of the filter operation + <A extends Annotation> A getAnnotation(Class<A> annotationType) { + return (A) + getOnlyElement( + FluentIterable.from(annotationsOnParameter) + .filter(annotation -> annotation.annotationType().equals(annotationType)) + .toList()); + } + + // TODO: b/317524353 - Add support for repeated annotations + + /** The class that contains the test that is currently being run. */ + Class<?> testClass() { + return testClass; + } + + /** A list of all annotations on the field or parameter. */ + ImmutableList<Annotation> annotationsOnParameter() { + return annotationsOnParameter; + } + + @Override + public String toString() { + return String.format( + "context(annotationsOnParameter=[%s],testClass=%s)", + FluentIterable.from( + ImmutableList.sortedCopyOf( + Ordering.natural().onResultOf(Annotation::toString), annotationsOnParameter)) + .transform( + annotation -> String.format("@%s", annotation.annotationType().getSimpleName())) + .join(Joiner.on(',')), + testClass().getSimpleName()); + } +} diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java index 8e78e7c..d193ec6 100644 --- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java +++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java @@ -21,7 +21,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.primitives.Primitives; import com.google.testing.junit.testparameterinjector.TestParameter.InternalImplementationOfThisParameter; @@ -148,7 +147,7 @@ public @interface TestParameter { class DefaultTestParameterValuesProvider implements TestParameterValuesProvider { @Override public List<Object> provideValues() { - return ImmutableList.of(); + return com.google.common.collect.ImmutableList.of(); } } @@ -157,9 +156,8 @@ public @interface TestParameter { @Override public List<Object> provideValues( Annotation uncastAnnotation, - ImmutableList<Annotation> otherAnnotations, Optional<Class<?>> maybeParameterClass, - Class<?> testClass) { + GenericParameterContext context) { TestParameter annotation = (TestParameter) uncastAnnotation; Class<?> parameterClass = getValueType(annotation.annotationType(), maybeParameterClass); @@ -177,8 +175,7 @@ public @interface TestParameter { .transform(v -> parseStringValue(v, parameterClass)) .toArray(Object.class)); } else if (valuesProviderIsSet) { - return getValuesFromProvider( - annotation.valuesProvider(), new Context(otherAnnotations, testClass)); + return getValuesFromProvider(annotation.valuesProvider(), new Context(context)); } else { if (Enum.class.isAssignableFrom(parameterClass)) { return Arrays.asList((Object[]) parameterClass.asSubclass(Enum.class).getEnumConstants()); diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterAnnotationMethodProcessor.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterAnnotationMethodProcessor.java index d186e84..a4bcb74 100644 --- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterAnnotationMethodProcessor.java +++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterAnnotationMethodProcessor.java @@ -192,9 +192,8 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso .newInstance() .provideValues( annotation, - annotationWithMetadata.otherAnnotations(), annotationWithMetadata.paramClass(), - annotationWithMetadata.testClass())) + annotationWithMetadata.context())) .transform( value -> (value instanceof TestParameterValue) @@ -252,15 +251,6 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso abstract Annotation annotation(); /** - * A list of all other annotations on the field or parameter that was annotated with {@code - * annotation}. - * - * <p>In case the annotation is annotating a method, constructor or class, {@code - * parameterClass} is an empty list. - */ - abstract ImmutableList<Annotation> otherAnnotations(); - - /** * The class of the parameter or field that is being annotated. In case the annotation is * annotating a method, constructor or class, {@code paramClass} is an absent optional. */ @@ -272,8 +262,13 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso */ abstract Optional<String> paramName(); - /** The class that contains the test that is currently being run. */ - abstract Class<?> testClass(); + /** + * A value class that contains extra information about the context of this parameter. + * + * <p>In case the annotation is annotating a method, constructor or class (deprecated + * functionality), the annotations in the context will be empty. + */ + abstract GenericParameterContext context(); public static AnnotationWithMetadata withMetadata( Annotation annotation, @@ -283,12 +278,9 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ FluentIterable.from(allAnnotations) - .filter(a -> !a.equals(annotation)) - .toList(), Optional.of(paramClass), Optional.of(paramName), - testClass); + new GenericParameterContext(ImmutableList.copyOf(allAnnotations), testClass)); } public static AnnotationWithMetadata withMetadata( @@ -298,22 +290,18 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ FluentIterable.from(allAnnotations) - .filter(a -> !a.equals(annotation)) - .toList(), Optional.of(paramClass), Optional.absent(), - testClass); + new GenericParameterContext(ImmutableList.copyOf(allAnnotations), testClass)); } public static AnnotationWithMetadata withoutMetadata( Annotation annotation, Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ ImmutableList.of(), /* paramClass= */ Optional.absent(), /* paramName= */ Optional.absent(), - testClass); + new GenericParameterContext(/* annotationsOnParameter= */ ImmutableList.of(), testClass)); } // Prevent anyone relying on equals() and hashCode() so that it remains possible to add fields diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValueProvider.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValueProvider.java index 08cc173..38c3356 100644 --- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValueProvider.java +++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValueProvider.java @@ -15,7 +15,6 @@ package com.google.testing.junit.testparameterinjector; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; import java.lang.annotation.Annotation; import java.util.List; @@ -75,10 +74,7 @@ interface TestParameterValueProvider { */ @Deprecated default List<Object> provideValues( - Annotation annotation, - ImmutableList<Annotation> otherAnnotations, - Optional<Class<?>> parameterClass, - Class<?> testClass) { + Annotation annotation, Optional<Class<?>> parameterClass, GenericParameterContext context) { return provideValues(annotation, parameterClass); } diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java index 129d514..a8fd19d 100644 --- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java +++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java @@ -15,11 +15,8 @@ package com.google.testing.junit.testparameterinjector; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.collect.Iterables.getOnlyElement; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Joiner; -import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import java.lang.annotation.Annotation; import java.util.List; @@ -69,12 +66,10 @@ public abstract class TestParameterValuesProvider */ public static final class Context { - private final ImmutableList<Annotation> otherAnnotations; - private final Class<?> testClass; + private final GenericParameterContext delegate; - Context(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) { - this.otherAnnotations = otherAnnotations; - this.testClass = testClass; + Context(GenericParameterContext delegate) { + this.delegate = delegate; } /** @@ -98,17 +93,12 @@ public abstract class TestParameterValuesProvider * @throws IllegalArgumentException if the argument it TestParameter.class because it is already * handled by the TestParameterInjector framework. */ - @SuppressWarnings("unchecked") // Safe because of the filter operation public <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) { checkArgument( !TestParameter.class.equals(annotationType), "Getting the @TestParameter annotating the field or parameter is not allowed because" + " it is already handled by the TestParameterInjector framework."); - return (A) - getOnlyElement( - FluentIterable.from(otherAnnotations()) - .filter(annotation -> annotation.annotationType().equals(annotationType)) - .toList()); + return delegate.getAnnotation(annotationType); } // TODO: b/317524353 - Add support for repeated annotations @@ -124,24 +114,18 @@ public abstract class TestParameterValuesProvider * </pre> */ public Class<?> testClass() { - return testClass; + return delegate.testClass(); } - /** - * A list of all other annotations on the field or parameter that was annotated - * with @TestParameter. - */ + /** A list of all annotations on the field or parameter. */ @VisibleForTesting - ImmutableList<Annotation> otherAnnotations() { - return otherAnnotations; + ImmutableList<Annotation> annotationsOnParameter() { + return delegate.annotationsOnParameter(); } @Override public String toString() { - return String.format( - "Context(otherAnnotations=[%s],testClass=%s)", - FluentIterable.from(otherAnnotations()).join(Joiner.on(',')), - testClass().getSimpleName()); + return delegate.toString(); } } } diff --git a/junit4/src/test/java/com/google/testing/junit/testparameterinjector/TestParameterTest.java b/junit4/src/test/java/com/google/testing/junit/testparameterinjector/TestParameterTest.java index d5d3c96..7c915ea 100644 --- a/junit4/src/test/java/com/google/testing/junit/testparameterinjector/TestParameterTest.java +++ b/junit4/src/test/java/com/google/testing/junit/testparameterinjector/TestParameterTest.java @@ -19,9 +19,12 @@ import static com.google.common.collect.Lists.newArrayList; import static java.lang.annotation.RetentionPolicy.RUNTIME; import com.google.common.base.CharMatcher; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.testing.junit.testparameterinjector.SharedTestUtilitiesJUnit4.SuccessfulTestCaseBase; import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider.Context; +import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.util.Arrays; import java.util.Collection; @@ -243,4 +246,9 @@ public class TestParameterTest { } }); } + + private static ImmutableList<Class<? extends Annotation>> annotationTypes( + Iterable<Annotation> annotations) { + return FluentIterable.from(annotations).transform(Annotation::annotationType).toList(); + } } diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/GenericParameterContext.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/GenericParameterContext.java new file mode 100644 index 0000000..15ac68b --- /dev/null +++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/GenericParameterContext.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 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.collect.Iterables.getOnlyElement; + +import com.google.common.base.Joiner; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Ordering; +import java.lang.annotation.Annotation; +import java.util.NoSuchElementException; + +/** A value class that contains extra information about the context of a field or parameter. */ +final class GenericParameterContext { + + private final ImmutableList<Annotation> annotationsOnParameter; + private final Class<?> testClass; + + GenericParameterContext(ImmutableList<Annotation> annotationsOnParameter, Class<?> testClass) { + this.annotationsOnParameter = annotationsOnParameter; + this.testClass = testClass; + } + + /** + * Returns the only annotation with the given type on the field or parameter. + * + * @throws NoSuchElementException if this there is no annotation with the given type + * @throws IllegalArgumentException if there are multiple annotations with the given type + */ + @SuppressWarnings("unchecked") // Safe because of the filter operation + <A extends Annotation> A getAnnotation(Class<A> annotationType) { + return (A) + getOnlyElement( + FluentIterable.from(annotationsOnParameter) + .filter(annotation -> annotation.annotationType().equals(annotationType)) + .toList()); + } + + // TODO: b/317524353 - Add support for repeated annotations + + /** The class that contains the test that is currently being run. */ + Class<?> testClass() { + return testClass; + } + + /** A list of all annotations on the field or parameter. */ + ImmutableList<Annotation> annotationsOnParameter() { + return annotationsOnParameter; + } + + @Override + public String toString() { + return String.format( + "context(annotationsOnParameter=[%s],testClass=%s)", + FluentIterable.from( + ImmutableList.sortedCopyOf( + Ordering.natural().onResultOf(Annotation::toString), annotationsOnParameter)) + .transform( + annotation -> String.format("@%s", annotation.annotationType().getSimpleName())) + .join(Joiner.on(',')), + testClass().getSimpleName()); + } +} diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java index 9798c2f..c26c8ed 100644 --- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java +++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java @@ -21,7 +21,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.primitives.Primitives; import com.google.testing.junit.testparameterinjector.junit5.TestParameter.InternalImplementationOfThisParameter; @@ -148,7 +147,7 @@ public @interface TestParameter { class DefaultTestParameterValuesProvider implements TestParameterValuesProvider { @Override public List<Object> provideValues() { - return ImmutableList.of(); + return com.google.common.collect.ImmutableList.of(); } } @@ -157,9 +156,8 @@ public @interface TestParameter { @Override public List<Object> provideValues( Annotation uncastAnnotation, - ImmutableList<Annotation> otherAnnotations, Optional<Class<?>> maybeParameterClass, - Class<?> testClass) { + GenericParameterContext context) { TestParameter annotation = (TestParameter) uncastAnnotation; Class<?> parameterClass = getValueType(annotation.annotationType(), maybeParameterClass); @@ -177,8 +175,7 @@ public @interface TestParameter { .transform(v -> parseStringValue(v, parameterClass)) .toArray(Object.class)); } else if (valuesProviderIsSet) { - return getValuesFromProvider( - annotation.valuesProvider(), new Context(otherAnnotations, testClass)); + return getValuesFromProvider(annotation.valuesProvider(), new Context(context)); } else { if (Enum.class.isAssignableFrom(parameterClass)) { return Arrays.asList((Object[]) parameterClass.asSubclass(Enum.class).getEnumConstants()); diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterAnnotationMethodProcessor.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterAnnotationMethodProcessor.java index 41d9a7b..3024ffb 100644 --- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterAnnotationMethodProcessor.java +++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterAnnotationMethodProcessor.java @@ -192,9 +192,8 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso .newInstance() .provideValues( annotation, - annotationWithMetadata.otherAnnotations(), annotationWithMetadata.paramClass(), - annotationWithMetadata.testClass())) + annotationWithMetadata.context())) .transform( value -> (value instanceof TestParameterValue) @@ -252,15 +251,6 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso abstract Annotation annotation(); /** - * A list of all other annotations on the field or parameter that was annotated with {@code - * annotation}. - * - * <p>In case the annotation is annotating a method, constructor or class, {@code - * parameterClass} is an empty list. - */ - abstract ImmutableList<Annotation> otherAnnotations(); - - /** * The class of the parameter or field that is being annotated. In case the annotation is * annotating a method, constructor or class, {@code paramClass} is an absent optional. */ @@ -272,8 +262,13 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso */ abstract Optional<String> paramName(); - /** The class that contains the test that is currently being run. */ - abstract Class<?> testClass(); + /** + * A value class that contains extra information about the context of this parameter. + * + * <p>In case the annotation is annotating a method, constructor or class (deprecated + * functionality), the annotations in the context will be empty. + */ + abstract GenericParameterContext context(); public static AnnotationWithMetadata withMetadata( Annotation annotation, @@ -283,12 +278,9 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ FluentIterable.from(allAnnotations) - .filter(a -> !a.equals(annotation)) - .toList(), Optional.of(paramClass), Optional.of(paramName), - testClass); + new GenericParameterContext(ImmutableList.copyOf(allAnnotations), testClass)); } public static AnnotationWithMetadata withMetadata( @@ -298,22 +290,18 @@ final class TestParameterAnnotationMethodProcessor implements TestMethodProcesso Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ FluentIterable.from(allAnnotations) - .filter(a -> !a.equals(annotation)) - .toList(), Optional.of(paramClass), Optional.absent(), - testClass); + new GenericParameterContext(ImmutableList.copyOf(allAnnotations), testClass)); } public static AnnotationWithMetadata withoutMetadata( Annotation annotation, Class<?> testClass) { return new AutoValue_TestParameterAnnotationMethodProcessor_AnnotationWithMetadata( annotation, - /* otherAnnotations= */ ImmutableList.of(), /* paramClass= */ Optional.absent(), /* paramName= */ Optional.absent(), - testClass); + new GenericParameterContext(/* annotationsOnParameter= */ ImmutableList.of(), testClass)); } // Prevent anyone relying on equals() and hashCode() so that it remains possible to add fields diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValueProvider.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValueProvider.java index a3ad576..9cc9f88 100644 --- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValueProvider.java +++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValueProvider.java @@ -15,7 +15,6 @@ package com.google.testing.junit.testparameterinjector.junit5; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; import java.lang.annotation.Annotation; import java.util.List; @@ -75,10 +74,7 @@ interface TestParameterValueProvider { */ @Deprecated default List<Object> provideValues( - Annotation annotation, - ImmutableList<Annotation> otherAnnotations, - Optional<Class<?>> parameterClass, - Class<?> testClass) { + Annotation annotation, Optional<Class<?>> parameterClass, GenericParameterContext context) { return provideValues(annotation, parameterClass); } diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java index 51169b8..603f24d 100644 --- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java +++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java @@ -15,11 +15,8 @@ package com.google.testing.junit.testparameterinjector.junit5; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.collect.Iterables.getOnlyElement; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Joiner; -import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import java.lang.annotation.Annotation; import java.util.List; @@ -69,12 +66,10 @@ public abstract class TestParameterValuesProvider */ public static final class Context { - private final ImmutableList<Annotation> otherAnnotations; - private final Class<?> testClass; + private final GenericParameterContext delegate; - Context(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) { - this.otherAnnotations = otherAnnotations; - this.testClass = testClass; + Context(GenericParameterContext delegate) { + this.delegate = delegate; } /** @@ -98,17 +93,12 @@ public abstract class TestParameterValuesProvider * @throws IllegalArgumentException if the argument it TestParameter.class because it is already * handled by the TestParameterInjector framework. */ - @SuppressWarnings("unchecked") // Safe because of the filter operation public <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) { checkArgument( !TestParameter.class.equals(annotationType), "Getting the @TestParameter annotating the field or parameter is not allowed because" + " it is already handled by the TestParameterInjector framework."); - return (A) - getOnlyElement( - FluentIterable.from(otherAnnotations()) - .filter(annotation -> annotation.annotationType().equals(annotationType)) - .toList()); + return delegate.getAnnotation(annotationType); } // TODO: b/317524353 - Add support for repeated annotations @@ -124,24 +114,18 @@ public abstract class TestParameterValuesProvider * </pre> */ public Class<?> testClass() { - return testClass; + return delegate.testClass(); } - /** - * A list of all other annotations on the field or parameter that was annotated - * with @TestParameter. - */ + /** A list of all annotations on the field or parameter. */ @VisibleForTesting - ImmutableList<Annotation> otherAnnotations() { - return otherAnnotations; + ImmutableList<Annotation> annotationsOnParameter() { + return delegate.annotationsOnParameter(); } @Override public String toString() { - return String.format( - "Context(otherAnnotations=[%s],testClass=%s)", - FluentIterable.from(otherAnnotations()).join(Joiner.on(',')), - testClass().getSimpleName()); + return delegate.toString(); } } } |