diff options
author | Jens Nyman <jnyman@google.com> | 2024-01-17 10:10:47 +0000 |
---|---|---|
committer | Jens Nyman <jnyman@google.com> | 2024-01-17 10:10:47 +0000 |
commit | 8a3e8127e0acd1fe2a71e6727e3797227b70bfde (patch) | |
tree | 0ab1c4139d4931e59577091d6f58f8b840a92aff | |
parent | 4d6e752965b84ca66df9c0bbf56928b6e91ba8e2 (diff) | |
download | TestParameterInjector-8a3e8127e0acd1fe2a71e6727e3797227b70bfde.tar.gz |
TestParameterValuesProvider.Context: Replace otherAnnotations() by a utility method for getting an annotation of a specific type
https://github.com/google/TestParameterInjector/issues/44
2 files changed, 80 insertions, 2 deletions
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 a0078a0..e4ad748 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 @@ -14,6 +14,9 @@ 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.auto.value.AutoValue; import com.google.common.base.Joiner; import com.google.common.collect.FluentIterable; @@ -76,7 +79,7 @@ public abstract class TestParameterValuesProvider * * then this list will contain a single element: @CustomAnnotation(123). */ - public abstract ImmutableList<Annotation> otherAnnotations(); + abstract ImmutableList<Annotation> otherAnnotations(); /** * The class that contains the test that is currently being run. @@ -94,6 +97,42 @@ public abstract class TestParameterValuesProvider return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass); } + /** + * Returns the only annotation with the given type on the field or parameter that was annotated + * with @TestParameter. + * + * <p>For example, if the test code is as follows: + * + * <pre> + * {@literal @}Test + * public void myTest_success( + * {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) { + * ... + * } + * </pre> + * + * then {@code context.getOtherAnnotation(CustomAnnotation.class).value()} will equal 123. + * + * @throws NoSuchElementException if this there is no annotation with the given type + * @throws IllegalArgumentException if there are multiple annotations with the given type + * @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 final <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()); + } + + // TODO: b/317524353 - Add support for repeated annotations + @Override public final String toString() { return String.format( 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 46810cf..4e0ee50 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 @@ -14,6 +14,9 @@ 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.auto.value.AutoValue; import com.google.common.base.Joiner; import com.google.common.collect.FluentIterable; @@ -76,7 +79,7 @@ public abstract class TestParameterValuesProvider * * then this list will contain a single element: @CustomAnnotation(123). */ - public abstract ImmutableList<Annotation> otherAnnotations(); + abstract ImmutableList<Annotation> otherAnnotations(); /** * The class that contains the test that is currently being run. @@ -94,6 +97,42 @@ public abstract class TestParameterValuesProvider return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass); } + /** + * Returns the only annotation with the given type on the field or parameter that was annotated + * with @TestParameter. + * + * <p>For example, if the test code is as follows: + * + * <pre> + * {@literal @}Test + * public void myTest_success( + * {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) { + * ... + * } + * </pre> + * + * then {@code context.getOtherAnnotation(CustomAnnotation.class).value()} will equal 123. + * + * @throws NoSuchElementException if this there is no annotation with the given type + * @throws IllegalArgumentException if there are multiple annotations with the given type + * @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 final <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()); + } + + // TODO: b/317524353 - Add support for repeated annotations + @Override public final String toString() { return String.format( |