Skip to content

Commit 8a3e812

Browse files
committed
TestParameterValuesProvider.Context: Replace otherAnnotations() by a utility method for getting an annotation of a specific type
#44
1 parent 4d6e752 commit 8a3e812

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package com.google.testing.junit.testparameterinjector;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.collect.Iterables.getOnlyElement;
19+
1720
import com.google.auto.value.AutoValue;
1821
import com.google.common.base.Joiner;
1922
import com.google.common.collect.FluentIterable;
@@ -76,7 +79,7 @@ public abstract static class Context {
7679
*
7780
* then this list will contain a single element: @CustomAnnotation(123).
7881
*/
79-
public abstract ImmutableList<Annotation> otherAnnotations();
82+
abstract ImmutableList<Annotation> otherAnnotations();
8083

8184
/**
8285
* The class that contains the test that is currently being run.
@@ -94,6 +97,42 @@ static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testC
9497
return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass);
9598
}
9699

100+
/**
101+
* Returns the only annotation with the given type on the field or parameter that was annotated
102+
* with @TestParameter.
103+
*
104+
* <p>For example, if the test code is as follows:
105+
*
106+
* <pre>
107+
* {@literal @}Test
108+
* public void myTest_success(
109+
* {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) {
110+
* ...
111+
* }
112+
* </pre>
113+
*
114+
* then {@code context.getOtherAnnotation(CustomAnnotation.class).value()} will equal 123.
115+
*
116+
* @throws NoSuchElementException if this there is no annotation with the given type
117+
* @throws IllegalArgumentException if there are multiple annotations with the given type
118+
* @throws IllegalArgumentException if the argument it TestParameter.class because it is already
119+
* handled by the TestParameterInjector framework.
120+
*/
121+
@SuppressWarnings("unchecked") // Safe because of the filter operation
122+
public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
123+
checkArgument(
124+
!TestParameter.class.equals(annotationType),
125+
"Getting the @TestParameter annotating the field or parameter is not allowed because"
126+
+ " it is already handled by the TestParameterInjector framework.");
127+
return (A)
128+
getOnlyElement(
129+
FluentIterable.from(otherAnnotations())
130+
.filter(annotation -> annotation.annotationType().equals(annotationType))
131+
.toList());
132+
}
133+
134+
// TODO: b/317524353 - Add support for repeated annotations
135+
97136
@Override
98137
public final String toString() {
99138
return String.format(

junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package com.google.testing.junit.testparameterinjector.junit5;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.collect.Iterables.getOnlyElement;
19+
1720
import com.google.auto.value.AutoValue;
1821
import com.google.common.base.Joiner;
1922
import com.google.common.collect.FluentIterable;
@@ -76,7 +79,7 @@ public abstract static class Context {
7679
*
7780
* then this list will contain a single element: @CustomAnnotation(123).
7881
*/
79-
public abstract ImmutableList<Annotation> otherAnnotations();
82+
abstract ImmutableList<Annotation> otherAnnotations();
8083

8184
/**
8285
* The class that contains the test that is currently being run.
@@ -94,6 +97,42 @@ static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testC
9497
return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass);
9598
}
9699

100+
/**
101+
* Returns the only annotation with the given type on the field or parameter that was annotated
102+
* with @TestParameter.
103+
*
104+
* <p>For example, if the test code is as follows:
105+
*
106+
* <pre>
107+
* {@literal @}Test
108+
* public void myTest_success(
109+
* {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) {
110+
* ...
111+
* }
112+
* </pre>
113+
*
114+
* then {@code context.getOtherAnnotation(CustomAnnotation.class).value()} will equal 123.
115+
*
116+
* @throws NoSuchElementException if this there is no annotation with the given type
117+
* @throws IllegalArgumentException if there are multiple annotations with the given type
118+
* @throws IllegalArgumentException if the argument it TestParameter.class because it is already
119+
* handled by the TestParameterInjector framework.
120+
*/
121+
@SuppressWarnings("unchecked") // Safe because of the filter operation
122+
public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
123+
checkArgument(
124+
!TestParameter.class.equals(annotationType),
125+
"Getting the @TestParameter annotating the field or parameter is not allowed because"
126+
+ " it is already handled by the TestParameterInjector framework.");
127+
return (A)
128+
getOnlyElement(
129+
FluentIterable.from(otherAnnotations())
130+
.filter(annotation -> annotation.annotationType().equals(annotationType))
131+
.toList());
132+
}
133+
134+
// TODO: b/317524353 - Add support for repeated annotations
135+
97136
@Override
98137
public final String toString() {
99138
return String.format(

0 commit comments

Comments
 (0)