14
14
15
15
package com .google .testing .junit .testparameterinjector .junit5 ;
16
16
17
+ import static com .google .common .base .Preconditions .checkArgument ;
18
+ import static com .google .common .collect .Iterables .getOnlyElement ;
19
+
17
20
import com .google .auto .value .AutoValue ;
18
21
import com .google .common .base .Joiner ;
19
22
import com .google .common .collect .FluentIterable ;
@@ -76,7 +79,7 @@ public abstract static class Context {
76
79
*
77
80
* then this list will contain a single element: @CustomAnnotation(123).
78
81
*/
79
- public abstract ImmutableList <Annotation > otherAnnotations ();
82
+ abstract ImmutableList <Annotation > otherAnnotations ();
80
83
81
84
/**
82
85
* The class that contains the test that is currently being run.
@@ -94,6 +97,42 @@ static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testC
94
97
return new AutoValue_TestParameterValuesProvider_Context (otherAnnotations , testClass );
95
98
}
96
99
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
+
97
136
@ Override
98
137
public final String toString () {
99
138
return String .format (
0 commit comments