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.

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