Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BEAM-790] Validate PipelineOptions Default annotation. #1159

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,9 @@ public Iterable<String> apply(final @Nonnull Method method) {
@Override
public String apply(@Nonnull Annotation annotation) {
return String.format(
"[%s on %s.%s]",
toStringForPrint(annotation),
method.getDeclaringClass().getSimpleName(),
method.getName());
"[%s on %s]",
ReflectHelpers.toStringForPrint(annotation),
ReflectHelpers.CLASS_AND_METHOD_FORMATTER.apply(method));
}
});

Expand Down Expand Up @@ -1152,12 +1151,6 @@ public String apply(@Nonnull Annotation annotation) {
inconsistentlyAnnotatedGetters, annotationPredicates.annotationClass);
}

@VisibleForTesting
static String toStringForPrint(Annotation annotation) {
String toString = annotation.toString();
return toString.substring(toString.lastIndexOf('.') + 1).replace('$', '.');
}

/**
* Validates that setters don't have the given annotation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Queues;
import java.lang.annotation.Annotation;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -186,4 +187,19 @@ public static Iterable<Method> getClosureOfMethodsOnInterface(Class<?> iface) {
}
return builder.build();
}

/**
* Returns a concise string for printing a {@link Annotation}.
*
* @param annotation The {@link Annotation} to print.
* @return a concise string representation that doesn't contain the package name.
*/
public static String toStringForPrint(Annotation annotation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a function object like the others in this class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

String annotationName = annotation.annotationType().getName();
String annotationNameWithoutPackage =
annotationName.substring(annotationName.lastIndexOf('.') + 1).replace('$', '.');
String annotationToString = annotation.toString();
String values = annotationToString.substring(annotationToString.indexOf('('));
return String.format("%s%s", annotationNameWithoutPackage, values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,10 @@ public void testGettersAnnotatedWithInconsistentDefault() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Property [object] is marked with contradictory annotations. Found ["
+ "[Default.Integer(value=1) on GetterWithDefault.getObject], "
+ "[Default.String(value=abc) on GetterWithInconsistentDefaultType.getObject]].");
+ "[Default.Integer(value=1) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithDefault#getObject()], "
+ "[Default.String(value=abc) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithInconsistentDefaultType#getObject()]].");

// When we attempt to convert, we should error at this moment.
options.as(GetterWithInconsistentDefaultType.class);
Expand All @@ -576,8 +578,10 @@ public void testGettersAnnotatedWithInconsistentDefaultValue() throws Exception
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Property [object] is marked with contradictory annotations. Found ["
+ "[Default.Integer(value=1) on GetterWithDefault.getObject], "
+ "[Default.Integer(value=0) on GetterWithInconsistentDefaultValue.getObject]].");
+ "[Default.Integer(value=1) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithDefault#getObject()], "
+ "[Default.Integer(value=0) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithInconsistentDefaultValue#getObject()]].");

// When we attempt to convert, we should error at this moment.
options.as(GetterWithInconsistentDefaultValue.class);
Expand All @@ -591,8 +595,10 @@ public void testGettersAnnotatedWithInconsistentJsonIgnoreValue() throws Excepti
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Property [object] is marked with contradictory annotations. Found ["
+ "[JsonIgnore(value=false) on GetterWithInconsistentJsonIgnoreValue.getObject], "
+ "[JsonIgnore(value=true) on GetterWithJsonIgnore.getObject]].");
+ "[JsonIgnore(value=false) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithInconsistentJsonIgnoreValue#getObject()], "
+ "[JsonIgnore(value=true) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GetterWithJsonIgnore#getObject()]].");

// When we attempt to convert, we should error at this moment.
options.as(GetterWithInconsistentJsonIgnoreValue.class);
Expand All @@ -610,8 +616,10 @@ public void testGettersWithMultipleDefaults() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(
"Property [object] is marked with contradictory annotations. Found ["
+ "[Default.String(value=abc) on GettersWithMultipleDefault.getObject], "
+ "[Default.Integer(value=0) on GettersWithMultipleDefault.getObject]].");
+ "[Default.String(value=abc) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GettersWithMultipleDefault#getObject()], "
+ "[Default.Integer(value=0) on org.apache.beam.sdk.options.PipelineOptionsFactoryTest"
+ "$GettersWithMultipleDefault#getObject()]].");

// When we attempt to create, we should error at this moment.
PipelineOptionsFactory.as(GettersWithMultipleDefault.class);
Expand Down Expand Up @@ -672,19 +680,6 @@ public void testMultipleGettersWithInconsistentDefault() {
options.as(MultipleGettersWithInconsistentDefault.class);
}

@Test
public void testToStringForPrint() throws Exception {
assertEquals(
"Default.Integer(value=1)",
PipelineOptionsFactory.toStringForPrint(
GetterWithDefault.class.getMethod("getObject").getAnnotations()[0]));

assertEquals(
"JsonIgnore(value=true)",
PipelineOptionsFactory.toStringForPrint(
GetterWithJsonIgnore.class.getMethod("getObject").getAnnotations()[0]));
}

@Test
public void testAppNameIsNotOverriddenWhenPassedInViaCommandLine() {
ApplicationNameOptions options = PipelineOptionsFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
import java.util.Map;
import org.apache.beam.sdk.options.Default;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.values.TypeDescriptor;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -123,4 +126,26 @@ public <InputT, OutputT> void testTypeFormatterWithMultipleWildcards() throws Ex
ReflectHelpers.TYPE_SIMPLE_DESCRIPTION.apply(
new TypeDescriptor<Map<? super InputT, ? extends OutputT>>() {}.getType()));
}

private interface Options extends PipelineOptions {
@Default.Integer(1)
Integer getInteger();

@JsonIgnore
Object getObject();
}

@Test
public void testToStringForPrint() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testToStringForPrint -> testAnnotationFormatter

Add a test for a default value with a . and ( and $ in it so that we remember why we have string parsing logic setup the way we do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

assertEquals(
"Default.Integer(value=1)",
ReflectHelpers.toStringForPrint(
Options.class.getMethod("getInteger").getAnnotations()[0]));

assertEquals(
"JsonIgnore(value=true)",
ReflectHelpers.toStringForPrint(
Options.class.getMethod("getObject").getAnnotations()[0]));
}

}