Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* int minVersion() default Integer.MIN_VALUE;
* int maxVersion() default Integer.MAX_VALUE;
* Class<? extends JctSimpleCompilerConfigurer>[] configurers() default {};
* VersionStrategy versionStrategy() default VersionStrategy.RELEASE;
* }
* </code></pre>
*
Expand All @@ -62,18 +63,18 @@
* implements AnnotationConsumer&lt;MyCompilerTest&gt; {
*
* {@literal @Override}
* protected JctCompiler&lt;?, ?&gt; compilerForVersion(int release) {
* return new MyCompilerImpl().release(release);
* protected JctCompiler&lt;?, ?&gt; initializeNewCompiler() {
* return new MyCompilerImpl();
* }
*
* {@literal @Override}
* protected int minSupportedVersion(boolean modules) {
* return 11;
* return 11; // Support Java 11 as the minimum.
* }
*
* {@literal @Override}
* protected int maxSupportedVersion(boolean modules) {
* return 19;
* return 19; // Support Java 19 as the maximum.
* }
*
* {@literal @Override}
Expand All @@ -82,7 +83,8 @@
* annotation.minVersion(),
* annotation.maxVersion(),
* true,
* annotation.configurers()
* annotation.configurers(),
* annotation.versionStrategy(),
* );
* }
* }
Expand Down Expand Up @@ -121,6 +123,7 @@ public abstract class AbstractCompilersProvider implements ArgumentsProvider {
private int minVersion;
private int maxVersion;
private Class<? extends JctCompilerConfigurer<?>>[] configurerClasses;
private VersionStrategy versionStrategy;

/**
* Initialise this provider.
Expand All @@ -129,13 +132,18 @@ protected AbstractCompilersProvider() {
minVersion = 0;
maxVersion = Integer.MAX_VALUE;
configurerClasses = emptyArray();
versionStrategy = VersionStrategy.RELEASE;
}

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return IntStream
.rangeClosed(minVersion, maxVersion)
.mapToObj(this::compilerForVersion)
.mapToObj(version -> {
var compiler = initializeNewCompiler();
versionStrategy.configureCompiler(compiler, version);
return compiler;
})
.peek(this::applyConfigurers)
.map(Arguments::of);
}
Expand All @@ -147,12 +155,14 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
* @param max the inclusive maximum compiler version to use.
* @param modules whether the compiler version must support modules.
* @param configurerClasses the configurer classes to apply to each compiler.
* @param versionStrategy the version strategy to use.
*/
protected final void configure(
int min,
int max,
boolean modules,
Class<? extends JctCompilerConfigurer<?>>[] configurerClasses
Class<? extends JctCompilerConfigurer<?>>[] configurerClasses,
VersionStrategy versionStrategy
) {
min = Math.max(min, minSupportedVersion(modules));
max = Math.min(max, maxSupportedVersion(modules));
Expand All @@ -171,15 +181,15 @@ protected final void configure(
maxVersion = max;

this.configurerClasses = requireNonNullValues(configurerClasses, "configurerClasses");
this.versionStrategy = requireNonNull(versionStrategy, "versionStrategy");
}

/**
* Initialise a new compiler on the given release.
* Initialise a new compiler.
*
* @param release the release version to use.
* @return the compiler.
* @return the compiler object.
*/
protected abstract JctCompiler<?, ?> compilerForVersion(int release);
protected abstract JctCompiler<?, ?> initializeNewCompiler();

/**
* Get the minimum supported compiler version.
Expand All @@ -201,7 +211,7 @@ private void applyConfigurers(JctCompiler<?, ?> compiler) {
var classes = requireNonNull(configurerClasses);

for (var configurerClass : classes) {
var configurer = initialiseConfigurer(configurerClass);
var configurer = initializeConfigurer(configurerClass);

try {
configurer.configure(compiler);
Expand All @@ -218,7 +228,7 @@ private void applyConfigurers(JctCompiler<?, ?> compiler) {
}
}

private JctCompilerConfigurer<?> initialiseConfigurer(
private JctCompilerConfigurer<?> initializeConfigurer(
Class<? extends JctCompilerConfigurer<?>> configurerClass
) {
Constructor<? extends JctCompilerConfigurer<?>> constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@
*/
boolean modules() default false;

/**
* The version strategy to use.
*
* <p>This determines whether the version number being iterated across specifies the
* release, source, target, or source and target versions.
*
* <p>The default is to specify the release.
*
* @return the version strategy to use.
*/
VersionStrategy versionStrategy() default VersionStrategy.RELEASE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public JavacCompilersProvider() {
}

@Override
protected JctCompiler<?, ?> compilerForVersion(int release) {
return new JavacJctCompilerImpl("javac release " + release).release(release);
protected JctCompiler<?, ?> initializeNewCompiler() {
return new JavacJctCompilerImpl();
}

@Override
Expand All @@ -62,7 +62,8 @@ public void accept(JavacCompilerTest javacCompilers) {
javacCompilers.minVersion(),
javacCompilers.maxVersion(),
javacCompilers.modules(),
javacCompilers.configurers()
javacCompilers.configurers(),
javacCompilers.versionStrategy()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2022 - 2023, the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.ascopes.jct.junit;

import io.github.ascopes.jct.compilers.JctCompiler;
import java.util.function.BiConsumer;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* Strategy for setting a version on a JUnit compiler annotation.
*
* @author Ashley Scopes
* @since 0.0.1 (0.0.1-M7)
*/
@API(since = "0.0.1", status = Status.STABLE)
@Immutable
@ThreadSafe
public enum VersionStrategy {

/**
* Set the {@link JctCompiler#release release}.
*/
RELEASE(
JctCompiler::release,
(compiler, version) -> compiler
.name(compiler.getName() + " (release = Java " + version + ")")
),

/**
* Set the {@link JctCompiler#source} source}.
*/
SOURCE(
JctCompiler::source,
(compiler, version) -> compiler
.name(compiler.getName() + " (source = Java " + version + ")")
),

/**
* Set the {@link JctCompiler#target} target}.
*/
TARGET(
JctCompiler::target,
(compiler, version) -> compiler
.name(compiler.getName() + " (target = Java " + version + ")")
),

/**
* Set the {@link JctCompiler#source source} and {@link JctCompiler#target target}.
*/
SOURCE_AND_TARGET(
(compiler, version) -> compiler
.source(version)
.target(version),
(compiler, version) -> compiler
.name(compiler.getName() + " (source and target = Java " + version + ")")
);

private final BiConsumer<JctCompiler<?, ?>, Integer> versionSetter;
private final BiConsumer<JctCompiler<?, ?>, Integer> descriptionFormatter;

VersionStrategy(
BiConsumer<JctCompiler<?, ?>, Integer> versionSetter,
BiConsumer<JctCompiler<?, ?>, Integer> descriptionFormatter
) {
this.versionSetter = versionSetter;
this.descriptionFormatter = descriptionFormatter;
}

/**
* Set the given version on the compiler, according to the strategy in use.
*
* @param compiler the compiler to configure.
* @param version the version to set.
*/
public void configureCompiler(JctCompiler<?, ?> compiler, int version) {
versionSetter.accept(compiler, version);
descriptionFormatter.accept(compiler, version);
}
}
Loading