Class AbstractCompilersProvider

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

@API(since="0.0.1", status=STABLE) public abstract class AbstractCompilersProvider extends Object implements ArgumentsProvider
Base for defining a compiler-supplying arguments-provider for JUnit Jupiter parameterised test support.

An example annotation would look like the following:


 @ArgumentsSource(MyCompilersProvider.class)
 @ParameterizedTest(name = "for {0}")
 @Retention(RetentionPolicy.RUNTIME)
 @Target({
     ElementType.ANNOTATION_TYPE,
     ElementType.METHOD,
     ElementType.TYPE,
 })
 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(boolean modules) {
     return 11;  // Support Java 11 as the minimum.
   }

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

   @Override
   public void accept(MyCompilerTest annotation) {
     super.configure(
         annotation.minVersion(),
         annotation.maxVersion(),
         true,
         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, boolean modules, Class<? extends JctCompilerConfigurer<?>>[] configurerClasses, VersionStrategy versionStrategy)
      Configure this provider with parameters from annotations.
      Parameters:
      min - the inclusive minimum compiler version to use.
      max - the inclusive maximum compiler version to use.
      modules - whether the compiler version must support modules.
      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(boolean modules)
      Get the minimum supported compiler version.
      Parameters:
      modules - whether to require module support or not.
      Returns:
      the minimum supported compiler version.
    • maxSupportedVersion

      protected abstract int maxSupportedVersion(boolean modules)
      Get the maximum supported compiler version.
      Parameters:
      modules - whether to require module support or not.
      Returns:
      the minimum supported compiler version.