Skip to content

Commit e312198

Browse files
committed
TestParameterValuesProvider.Context: Convert from AutoValue to regular class.
Reason: To avoid clients from relying on equals/hashCode being consistent with the contents. This allows future expansion. #44
1 parent 8a3e812 commit e312198

File tree

4 files changed

+66
-76
lines changed

4 files changed

+66
-76
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public List<Object> provideValues(
170170
.toArray(Object.class));
171171
} else if (valuesProviderIsSet) {
172172
return getValuesFromProvider(
173-
annotation.valuesProvider(), Context.create(otherAnnotations, testClass));
173+
annotation.valuesProvider(), new Context(otherAnnotations, testClass));
174174
} else {
175175
if (Enum.class.isAssignableFrom(parameterClass)) {
176176
return Arrays.asList((Object[]) parameterClass.asSubclass(Enum.class).getEnumConstants());

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

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.collect.Iterables.getOnlyElement;
1919

20-
import com.google.auto.value.AutoValue;
20+
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.base.Joiner;
2222
import com.google.common.collect.FluentIterable;
2323
import com.google.common.collect.ImmutableList;
@@ -61,40 +61,14 @@ public final TestParameterValue value(@Nullable Object wrappedValue) {
6161
* An immutable value class that contains extra information about the context of the parameter for
6262
* which values are being provided.
6363
*/
64-
@AutoValue
65-
public abstract static class Context {
66-
/**
67-
* A list of all other annotations on the field or parameter that was annotated
68-
* with @TestParameter.
69-
*
70-
* <p>For example, if the test code is as follows:
71-
*
72-
* <pre>
73-
* {@literal @}Test
74-
* public void myTest_success(
75-
* {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) {
76-
* ...
77-
* }
78-
* </pre>
79-
*
80-
* then this list will contain a single element: @CustomAnnotation(123).
81-
*/
82-
abstract ImmutableList<Annotation> otherAnnotations();
64+
public static final class Context {
8365

84-
/**
85-
* The class that contains the test that is currently being run.
86-
*
87-
* <p>Having this can be useful when sharing providers between tests that have the same base
88-
* class. In those cases, an abstract method can be called as follows:
89-
*
90-
* <pre>
91-
* ((MyBaseClass) context.testClass().newInstance()).myAbstractMethod()
92-
* </pre>
93-
*/
94-
public abstract Class<?> testClass();
66+
private final ImmutableList<Annotation> otherAnnotations;
67+
private final Class<?> testClass;
9568

96-
static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) {
97-
return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass);
69+
Context(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) {
70+
this.otherAnnotations = otherAnnotations;
71+
this.testClass = testClass;
9872
}
9973

10074
/**
@@ -119,7 +93,7 @@ static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testC
11993
* handled by the TestParameterInjector framework.
12094
*/
12195
@SuppressWarnings("unchecked") // Safe because of the filter operation
122-
public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
96+
public <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
12397
checkArgument(
12498
!TestParameter.class.equals(annotationType),
12599
"Getting the @TestParameter annotating the field or parameter is not allowed because"
@@ -133,14 +107,35 @@ public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType
133107

134108
// TODO: b/317524353 - Add support for repeated annotations
135109

110+
/**
111+
* The class that contains the test that is currently being run.
112+
*
113+
* <p>Having this can be useful when sharing providers between tests that have the same base
114+
* class. In those cases, an abstract method can be called as follows:
115+
*
116+
* <pre>
117+
* ((MyBaseClass) context.testClass().newInstance()).myAbstractMethod()
118+
* </pre>
119+
*/
120+
public Class<?> testClass() {
121+
return testClass;
122+
}
123+
124+
/**
125+
* A list of all other annotations on the field or parameter that was annotated
126+
* with @TestParameter.
127+
*/
128+
@VisibleForTesting
129+
ImmutableList<Annotation> otherAnnotations() {
130+
return otherAnnotations;
131+
}
132+
136133
@Override
137-
public final String toString() {
134+
public String toString() {
138135
return String.format(
139136
"Context(otherAnnotations=[%s],testClass=%s)",
140137
FluentIterable.from(otherAnnotations()).join(Joiner.on(',')),
141138
testClass().getSimpleName());
142139
}
143-
144-
Context() {} // Prevent implementations outside of this package
145140
}
146141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public List<Object> provideValues(
170170
.toArray(Object.class));
171171
} else if (valuesProviderIsSet) {
172172
return getValuesFromProvider(
173-
annotation.valuesProvider(), Context.create(otherAnnotations, testClass));
173+
annotation.valuesProvider(), new Context(otherAnnotations, testClass));
174174
} else {
175175
if (Enum.class.isAssignableFrom(parameterClass)) {
176176
return Arrays.asList((Object[]) parameterClass.asSubclass(Enum.class).getEnumConstants());

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

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.collect.Iterables.getOnlyElement;
1919

20-
import com.google.auto.value.AutoValue;
20+
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.base.Joiner;
2222
import com.google.common.collect.FluentIterable;
2323
import com.google.common.collect.ImmutableList;
@@ -61,40 +61,14 @@ public final TestParameterValue value(@Nullable Object wrappedValue) {
6161
* An immutable value class that contains extra information about the context of the parameter for
6262
* which values are being provided.
6363
*/
64-
@AutoValue
65-
public abstract static class Context {
66-
/**
67-
* A list of all other annotations on the field or parameter that was annotated
68-
* with @TestParameter.
69-
*
70-
* <p>For example, if the test code is as follows:
71-
*
72-
* <pre>
73-
* {@literal @}Test
74-
* public void myTest_success(
75-
* {@literal @}CustomAnnotation(123) {@literal @}TestParameter(valuesProvider=MyProvider.class) Foo foo) {
76-
* ...
77-
* }
78-
* </pre>
79-
*
80-
* then this list will contain a single element: @CustomAnnotation(123).
81-
*/
82-
abstract ImmutableList<Annotation> otherAnnotations();
64+
public static final class Context {
8365

84-
/**
85-
* The class that contains the test that is currently being run.
86-
*
87-
* <p>Having this can be useful when sharing providers between tests that have the same base
88-
* class. In those cases, an abstract method can be called as follows:
89-
*
90-
* <pre>
91-
* ((MyBaseClass) context.testClass().newInstance()).myAbstractMethod()
92-
* </pre>
93-
*/
94-
public abstract Class<?> testClass();
66+
private final ImmutableList<Annotation> otherAnnotations;
67+
private final Class<?> testClass;
9568

96-
static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) {
97-
return new AutoValue_TestParameterValuesProvider_Context(otherAnnotations, testClass);
69+
Context(ImmutableList<Annotation> otherAnnotations, Class<?> testClass) {
70+
this.otherAnnotations = otherAnnotations;
71+
this.testClass = testClass;
9872
}
9973

10074
/**
@@ -119,7 +93,7 @@ static Context create(ImmutableList<Annotation> otherAnnotations, Class<?> testC
11993
* handled by the TestParameterInjector framework.
12094
*/
12195
@SuppressWarnings("unchecked") // Safe because of the filter operation
122-
public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
96+
public <A extends Annotation> A getOtherAnnotation(Class<A> annotationType) {
12397
checkArgument(
12498
!TestParameter.class.equals(annotationType),
12599
"Getting the @TestParameter annotating the field or parameter is not allowed because"
@@ -133,14 +107,35 @@ public final <A extends Annotation> A getOtherAnnotation(Class<A> annotationType
133107

134108
// TODO: b/317524353 - Add support for repeated annotations
135109

110+
/**
111+
* The class that contains the test that is currently being run.
112+
*
113+
* <p>Having this can be useful when sharing providers between tests that have the same base
114+
* class. In those cases, an abstract method can be called as follows:
115+
*
116+
* <pre>
117+
* ((MyBaseClass) context.testClass().newInstance()).myAbstractMethod()
118+
* </pre>
119+
*/
120+
public Class<?> testClass() {
121+
return testClass;
122+
}
123+
124+
/**
125+
* A list of all other annotations on the field or parameter that was annotated
126+
* with @TestParameter.
127+
*/
128+
@VisibleForTesting
129+
ImmutableList<Annotation> otherAnnotations() {
130+
return otherAnnotations;
131+
}
132+
136133
@Override
137-
public final String toString() {
134+
public String toString() {
138135
return String.format(
139136
"Context(otherAnnotations=[%s],testClass=%s)",
140137
FluentIterable.from(otherAnnotations()).join(Joiner.on(',')),
141138
testClass().getSimpleName());
142139
}
143-
144-
Context() {} // Prevent implementations outside of this package
145140
}
146141
}

0 commit comments

Comments
 (0)