Class AbstractCompilersProvider

java.lang.Object
io.github.ascopes.jct.junit.AbstractCompilersProvider
All Implemented Interfaces:
ArgumentsProvider
Direct Known Subclasses:
JavacCompilersProvider

public abstract class AbstractCompilersProvider extends Object implements ArgumentsProvider
Base for defining a compiler-supplying arguments-provider for JUnit Jupiter parameterised test support.

Each implementation is expected to provide:

An example annotation would look like the following:


 @ArgumentsSource(MyCompilersProvider.class)
 @ParameterizedTest(name = "for {0}")
 @Retention(RetentionPolicy.RUNTIME)
 @Tag("java-compiler-testing-test")
 @Target({
     ElementType.ANNOTATION_TYPE,
     ElementType.METHOD,
 })
 @TestTemplate
 public @interface MyCompilerTest {
     int minVersion() default Integer.MIN_VALUE;
     int maxVersion() default Integer.MAX_VALUE;
     Class<? extends JctSimpleCompilerConfigurer>[] configurers() default {};
     VersionStrategy versionStrategy() default VersionStrategy.RELEASE;
 }
 

...with the JUnit5 annotation provider being implemented as:


 public final class MyCompilersProvider
     extends AbstractCompilersProvider
     implements AnnotationConsumer<MyCompilerTest> {

   @Override
   protected JctCompiler initializeNewCompiler() {
     return new MyCompilerImpl();
   }

   @Override
   protected int minSupportedVersion() {
     return 11;  // Support Java 11 as the minimum.
   }

   @Override
   protected int maxSupportedVersion() {
     return 19;  // Support Java 19 as the maximum.
   }

   @Override
   public void accept(MyCompilerTest annotation) {
     super.configure(
         annotation.minVersion(),
         annotation.maxVersion(),
         annotation.configurers(),
         annotation.versionStrategy(),
     );
   }
 }
 

This would enable you to define your test cases like so:


 @MyCompilerTest(minVersion=13, maxVersion=17)
 void testSomething(JctCompiler compiler) {
   ...
 }

 @MyCompilerTest(configurers=WerrorConfigurer.class)
 void testSomethingElse(JctCompiler compiler) {
   ...
 }

 static class WerrorConfigurer implements JctCompilerConfigurer {
   @Override
   public void configure(JctCompiler compiler) {
     compiler.failOnErrors(true);
   }
 }
 

Note that if you are running your tests within a JPMS module, you will need to ensure that you declare your module to be open to io.github.ascopes.jct, otherwise this component will be unable to discover the constructor to initialise your configurer correctly, and may raise an exception as a result.

Since:
0.0.1
Author:
Ashley Scopes
  • Constructor Details

  • Method Details

    • provideArguments

      public Stream<? extends Arguments> provideArguments(ExtensionContext context)
      Specified by:
      provideArguments in interface ArgumentsProvider
    • configure

      protected final void configure(int min, int max, Class<? extends JctCompilerConfigurer<?>>[] configurerClasses, VersionStrategy versionStrategy)
      Configure this provider with parameters from annotations.

      This is expected to be called from an implementation of AnnotationConsumer.

      The minimum compiler version will be set to the min parameter, or minSupportedVersion(), whichever is greater. This means annotations can pass Integer.MIN_VALUE as a default value safely.

      The maximum compiler version will be set to the max parameter, or maxSupportedVersion(), whichever is smaller. This means annotations can pass Integer.MAX_VALUE as a default value safely.

      If implementations do not support specifying custom compiler configurers, then an empty array must be passed for the configurerClasses parameter.

      If implementations do not support changing the version strategy, then it is suggested to pass VersionStrategy.RELEASE as the value for the versionStrategy parameter.

      Parameters:
      min - the inclusive minimum compiler version to use.
      max - the inclusive maximum compiler version to use.
      configurerClasses - the configurer classes to apply to each compiler.
      versionStrategy - the version strategy to use.
    • initializeNewCompiler

      protected abstract JctCompiler initializeNewCompiler()
      Initialise a new compiler.
      Returns:
      the compiler object.
    • minSupportedVersion

      protected abstract int minSupportedVersion()
      Get the minimum supported compiler version.
      Returns:
      the minimum supported compiler version.
      Since:
      1.0.0
    • maxSupportedVersion

      protected abstract int maxSupportedVersion()
      Get the maximum supported compiler version.
      Returns:
      the minimum supported compiler version.
      Since:
      1.0.0