Skip to content

Commit

Permalink
refactoring test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lavanya Singh committed Feb 9, 2023
1 parent ba32071 commit 2c16819
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 99 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -90,7 +90,7 @@ public void testUnionAnnotations() {
assertMethodParamWithTypeParameterHasAnnotation(
visitorBuilder.get(), "safeLong", "safeLongVisitor", "Long", Safe.class);
assertMethodParamWithTypeParameterHasAnnotation(
visitorBuilder.get(), "unknown", "unknownVisitor", "Long", Safe.class);
visitorBuilder.get(), "unknown", "unknownVisitor", "String", Safe.class);

Optional<Class<?>> stageVisitorBuilder = Arrays.stream(ExternalLongUnionExample.class.getDeclaredClasses())
.filter(subclass -> subclass.getName().contains("SafeLongStageVisitorBuilder"))
Expand All @@ -105,45 +105,28 @@ public void testUnionAnnotations() {
.findAny();
assertThat(unknownStageVisitorBuilder).isPresent();
assertMethodParamWithTypeParameterHasAnnotation(
unknownStageVisitorBuilder.get(), "unknown", "unknownVisitor", "Long", Safe.class);
}

private void assertFieldTypeParamHasAnnotation(
Class<?> parentClass, String fieldName, String typeName, Class<? extends Annotation> annotation) {
AnnotatedType type = Arrays.stream(parentClass.getDeclaredFields())
.filter(field -> field.getName().contains(fieldName))
.findAny()
.get()
.getAnnotatedType();
assertThat(((AnnotatedParameterizedType) type).getAnnotatedActualTypeArguments())
.filteredOn(t -> t.getType().getTypeName().contains(typeName))
.hasSize(1)
.allMatch(t -> t.isAnnotationPresent(annotation));
unknownStageVisitorBuilder.get(), "unknown", "unknownVisitor", "String", Safe.class);
}

private void assertMethodHasAnnotation(
Class<?> parentClass, String methodName, Class<? extends Annotation> annotation) {
Method[] methods = parentClass.getMethods();
assertThat(methods)
.filteredOn(method -> method.getName().equals(methodName))
.hasSize(1)
Stream<Method> desiredMethods = getMatchingMethods(parentClass, methodName);
assertThat(desiredMethods)
.withFailMessage(String.format(
"Expected %s:%s to have annotation %s",
parentClass.getName(), methodName, annotation.getName()))
.allMatch(method -> method.isAnnotationPresent(annotation));
}

private void assertMethodParamHasAnnotation(
Class<?> parentClass, String methodName, String parameterName, Class<? extends Annotation> annotation) {
Method[] methods = parentClass.getMethods();
Optional<Method> desiredMethod = Arrays.stream(methods)
.filter(method -> method.getName().equals(methodName))
.findAny();
assertThat(desiredMethod).isPresent();
assertThat(desiredMethod)
.map(method -> Arrays.stream(desiredMethod.get().getParameters())
.filter(parameter -> parameter.getName().contains(parameterName))
.findAny())
.isPresent()
.get()
.satisfies(parameter -> parameter.get().isAnnotationPresent(annotation));
Stream<Method> desiredMethods = getMatchingMethods(parentClass, methodName);
assertThat(desiredMethods)
.withFailMessage(String.format(
"Expected %s:%s parameter %s to have annotation %s",
parentClass.getName(), methodName, parameterName, annotation.getName()))
.map(method -> getMatchingParameter(method, parameterName))
.allMatch(parameter -> parameter.isAnnotationPresent(annotation));
}

private void assertMethodParamWithTypeParameterHasAnnotation(
Expand All @@ -152,20 +135,53 @@ private void assertMethodParamWithTypeParameterHasAnnotation(
String parameterName,
String typeParameter,
Class<? extends Annotation> annotation) {
Method[] methods = parentClass.getMethods();
List<Method> desiredMethods = Arrays.stream(methods)
.filter(method -> method.getName().equals(methodName))
.collect(Collectors.toList());
assertThat(desiredMethods).isNotEmpty();
Stream<AnnotatedType> annotatedTypes = desiredMethods.stream()
.map(method -> Arrays.stream(method.getParameters())
.filter(parameter -> parameter.getName().contains(parameterName))
.findAny()
.get()
.getAnnotatedType());
assertThat(annotatedTypes).allMatch(annotatedType -> Arrays.stream(
((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments())
.filter(t -> t.getType().getTypeName().contains(typeParameter))
.allMatch(t -> t.isAnnotationPresent(annotation)));
Stream<Method> desiredMethods = getMatchingMethods(parentClass, methodName);
Stream<AnnotatedType> annotatedTypes = desiredMethods.map(
method -> getMatchingParameter(method, parameterName).getAnnotatedType());
assertThat(annotatedTypes)
.withFailMessage(String.format(
"Expected %s:%s parameter %s of type %s to have annotation %s",
parentClass.getName(), methodName, parameterName, typeParameter, annotation.getName()))
.map(annotatedType -> getAnnotatedTypeParameter(annotatedType, typeParameter))
.allMatch(t -> t.isAnnotationPresent(annotation));
}

private void assertFieldTypeParamHasAnnotation(
Class<?> parentClass, String fieldName, String typeName, Class<? extends Annotation> annotation) {
Stream<Field> desiredFields = Arrays.stream(parentClass.getDeclaredFields())
.filter(field -> field.getName().contains(fieldName));
Stream<AnnotatedType> annotatedTypeParameters =
desiredFields.map(Field::getAnnotatedType).map(type -> getAnnotatedTypeParameter(type, typeName));
assertThat(annotatedTypeParameters)
.withFailMessage(String.format(
"Expected %s:%s of type %s to have annotation %s",
parentClass.getName(), fieldName, typeName, annotation.getName()))
.allMatch(t -> t.isAnnotationPresent(annotation));
}

private Stream<Method> getMatchingMethods(Class<?> parentClass, String methodName) {
return Arrays.stream(parentClass.getMethods())
.filter(method -> method.getName().equals(methodName));
}

private Parameter getMatchingParameter(Method method, String parameterName) {
Optional<Parameter> parameterIfExists = Arrays.stream(method.getParameters())
.filter(parameter -> parameter.getName().contains(parameterName))
.findAny();
assertThat(parameterIfExists)
.withFailMessage(String.format("Expected to find parameter %s on method %s", parameterName, method))
.isPresent();
return parameterIfExists.get();
}

private AnnotatedType getAnnotatedTypeParameter(AnnotatedType parameterizedType, String parameter) {
Optional<AnnotatedType> typeParameter = Arrays.stream(
((AnnotatedParameterizedType) parameterizedType).getAnnotatedActualTypeArguments())
.filter(t -> t.getType().getTypeName().contains(parameter))
.findAny();
assertThat(typeParameter)
.withFailMessage("Expected type parameter %s on type %s", typeParameter, parameterizedType)
.isPresent();
return typeParameter.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void testMapSafeKey() {
.types(object)
.types(SAFE_ALIAS)
.build();
// there's an ABI break between 4.28.0 -> 4.36.0
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(object)).isEmpty();
Expand Down

0 comments on commit 2c16819

Please sign in to comment.