All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tapirtest.junit5.annotations.UnitTestProcessor Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package io.tapirtest.junit5.annotations;

import de.bmiag.tapir.annotationprocessing.annotation.AnnotationProcessor;
import de.bmiag.tapir.core.annotation.useextension.ExtensionService;
import de.bmiag.tapir.coreassertion.CoreAssertions;
import de.bmiag.tapir.xunit.annotations.UnitTest;
import org.eclipse.xtend.lib.macro.AbstractClassProcessor;
import org.eclipse.xtend.lib.macro.TransformationContext;
import org.eclipse.xtend.lib.macro.declaration.AnnotationReference;
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration;
import org.eclipse.xtend.lib.macro.declaration.Type;
import org.eclipse.xtend.lib.macro.declaration.TypeReference;
import org.eclipse.xtend.lib.macro.declaration.Visibility;
import org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@AnnotationProcessor(UnitTest.class)
@Order((-9000))
@SuppressWarnings("all")
public class UnitTestProcessor extends AbstractClassProcessor {
  @Override
  public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    this.addSpringBootTestAnnotation(annotatedClass, context);
    this.addExtendWithAnnotation(annotatedClass, context);
    this.registerCoreAssertionsExtension(annotatedClass, context);
  }
  
  protected void addExtendWithAnnotation(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final Type extendWithType = context.findTypeGlobally(ExtendWith.class);
    final AnnotationReference extendWithAnnotation = annotatedClass.findAnnotation(extendWithType);
    if ((extendWithAnnotation == null)) {
      final Procedure1 _function = (AnnotationReferenceBuildContext it) -> {
        it.setClassValue("value", context.newTypeReference(SpringExtension.class));
      };
      annotatedClass.addAnnotation(context.newAnnotationReference(extendWithType, _function));
    }
  }
  
  protected void addSpringBootTestAnnotation(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final Type springBootTestType = context.findTypeGlobally(SpringBootTest.class);
    final AnnotationReference springBootTestAnnotation = annotatedClass.findAnnotation(springBootTestType);
    if ((springBootTestAnnotation == null)) {
      final AnnotationReference unitTestAnnotation = annotatedClass.findAnnotation(context.findTypeGlobally(UnitTest.class));
      final String[] properties = unitTestAnnotation.getStringArrayValue("value");
      final Procedure1 _function = (AnnotationReferenceBuildContext it) -> {
        it.setStringValue("properties", properties);
      };
      annotatedClass.addAnnotation(context.newAnnotationReference(springBootTestType, _function));
    }
  }
  
  protected void registerCoreAssertionsExtension(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final TypeReference coreAssertionsTypeRef = context.newTypeReference(CoreAssertions.class);
    final ExtensionService extensionService = new ExtensionService();
    extensionService.registerExtension(annotatedClass, new TypeReference[] { coreAssertionsTypeRef }, Visibility.PRIVATE, context);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy