Skip to content

Commit 856ff77

Browse files
committed
Deprecate modules in JavacCompilerTest annotation, add examples to junit package
1 parent 70233af commit 856ff77

File tree

6 files changed

+284
-10
lines changed

6 files changed

+284
-10
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/junit/JavacCompilerTest.java

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
import org.junit.jupiter.params.provider.ArgumentsSource;
3434

3535
/**
36-
* Annotation that can be applied to a {@link ParameterizedTest} to enable passing in a range of
37-
* {@link JavacJctCompilerImpl} instances with specific configured versions as the first
38-
* parameter.
36+
* Annotation that can be applied to a JUnit parameterized test to invoke that test case across
37+
* multiple compilers, each configured to a specific version in a range of Java language versions.
3938
*
4039
* <p>This will also add the {@code "java-compiler-testing-test"} tag and {@code "javac-test"}
4140
* tags to your test method, meaning you can instruct your IDE or build system to optionally only
@@ -45,6 +44,35 @@
4544
* <p>If your build is running in a GraalVM Native Image, then this test will not execute, as
4645
* the <em>Java Compiler Testing</em> API is not yet tested within Native Images.
4746
*
47+
* <p>For example, to run a simple test on Java 11 through 17 (inclusive):
48+
*
49+
* <pre><code>
50+
* class SomeTest {
51+
* {@literal @JavacCompilerTest(minVersion = 11, maxVersion = 17)}
52+
* void canCompileHelloWorld(JctCompiler&lt;?, ?&gt;> compiler) {
53+
* // Given
54+
* try (var workspace = Workspaces.newWorkspace()) {
55+
* workspace
56+
* .createFile("org", "example", "HelloWorld.java")
57+
* .withContents("""
58+
* package org.example;
59+
*
60+
* public class HelloWorld {
61+
* public static void main(String[] args) {
62+
* System.out.println("Hello, World!");
63+
* }
64+
* }
65+
* """);
66+
*
67+
* var compilation = compiler.compile(workspace);
68+
*
69+
* assertThat(compilation)
70+
* .isSuccessfulWithoutWarnings();
71+
* }
72+
* }
73+
* }
74+
* </code></pre>
75+
*
4876
* @author Ashley Scopes
4977
* @since 0.0.1
5078
*/
@@ -66,19 +94,35 @@
6694
/**
6795
* Minimum version to use (inclusive).
6896
*
97+
* <p>By default, it will use the lowest possible version supported by the compiler. This
98+
* varies between versions of the JDK that are in use.
99+
*
100+
* <p>If the version is lower than the minimum supported version, then the minimum supported
101+
* version of the compiler will be used instead. This enables writing tests that will work on
102+
* a range of JDKs during builds without needing to duplicate the test to satisfy different
103+
* JDK supported version ranges.
104+
*
69105
* @return the minimum version.
70106
*/
71107
int minVersion() default Integer.MIN_VALUE;
72108

73109
/**
74110
* Maximum version to use (inclusive).
75111
*
112+
* <p>By default, it will use the highest possible version supported by the compiler. This
113+
* varies between versions of the JDK that are in use.
114+
*
115+
* <p>If the version is higher than the maximum supported version, then the maximum supported
116+
* version of the compiler will be used instead. This enables writing tests that will work on
117+
* a range of JDKs during builds without needing to duplicate the test to satisfy different
118+
* JDK supported version ranges.
119+
*
76120
* @return the maximum version.
77121
*/
78122
int maxVersion() default Integer.MAX_VALUE;
79123

80124
/**
81-
* Get an array of compiler configurers to apply in-order before starting the test.
125+
* Get an array of compiler configurer classes to apply in-order before starting the test.
82126
*
83127
* <p>Each configurer must have a public no-args constructor, and their package must be
84128
* open to this module if JPMS modules are in-use, for example:
@@ -92,6 +136,26 @@
92136
* }
93137
* </code></pre>
94138
*
139+
* An example of usage:
140+
*
141+
* <pre><code>
142+
* public class WerrorConfigurer implements JctCompilerConfigurer&lt;RuntimeException&gt; {
143+
* {@literal @Override}
144+
* public void configure(JctCompiler&lt;?, ?&gt; compiler) {
145+
* compiler.failOnWarnings(true);
146+
* }
147+
* }
148+
*
149+
* // ...
150+
*
151+
* class SomeTest {
152+
* {@literal @JavacCompilerTest(configurers = WerrorConfigurer.class)}
153+
* void someTest(JctCompiler&lt;?, ?&gt; compiler) {
154+
* // ...
155+
* }
156+
* }
157+
* </code></pre>
158+
*
95159
* @return an array of classes to run to configure the compiler. These run in the given order.
96160
*/
97161
Class<? extends JctCompilerConfigurer<?>>[] configurers() default {};
@@ -103,7 +167,9 @@
103167
* modules.
104168
*
105169
* @return {@code true} if we need to support modules, or {@code false} if we do not.
170+
* @deprecated this will be removed in a future release, since Java 8 is reaching end-of-life.
106171
*/
172+
@Deprecated(forRemoval = true, since = "0.0.2")
107173
boolean modules() default false;
108174

109175
/**

java-compiler-testing/src/main/java/io/github/ascopes/jct/junit/JavacCompilersProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected int maxSupportedVersion(boolean modules) {
5959
}
6060

6161
@Override
62+
@SuppressWarnings("removal")
6263
public void accept(JavacCompilerTest javacCompilers) {
6364
// Super is needed here to prevent IntelliJ getting confused.
6465
super.configure(

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/DirectoryBuilder.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.github.ascopes.jct.workspaces;
1717

1818
import java.io.File;
19+
import java.nio.file.FileSystem;
1920
import java.nio.file.Path;
2021
import org.apiguardian.api.API;
2122
import org.apiguardian.api.API.Status;
@@ -32,27 +33,40 @@ public interface DirectoryBuilder {
3233
/**
3334
* Copy the contents of the directory at the given path recursively into this directory.
3435
*
35-
* <p>This uses the default file system.
36+
* <p>Symbolic links will not be followed.
37+
*
38+
* <p>This uses the default file system. If you want to use a different {@link FileSystem}
39+
* as your source, then use {@link #copyContentsFrom(Path)} instead.
3640
*
3741
* <p>Examples:
3842
*
3943
* <pre><code>
40-
* // Using platform-specific separators.
41-
* directoryBuilder.copyContentsFrom("foo/bar/baz");
42-
*
4344
* // Letting JCT infer the correct path separators to use (recommended).
4445
* directoryBuilder.copyContentsFrom("foo", "bar", "baz");
46+
*
47+
* // Using POSIX platform-specific separators (may cause issues if your tests run on Windows)
48+
* directoryBuilder.copyContentsFrom("foo/bar/baz");
49+
*
50+
* // Using Windows platform-specific separators (may cause issues if your tests run on POSIX)
51+
* directoryBuilder.copyContentsFrom("foo\\bar\\baz");
4552
* </code></pre>
4653
*
4754
* @param first the first part of the path to the directory to copy from.
4855
* @param rest any additional parts of the path.
4956
* @return the root managed directory for further configuration.
57+
* @see #copyContentsFrom(Path)
5058
*/
5159
ManagedDirectory copyContentsFrom(String first, String... rest);
5260

5361
/**
5462
* Copy the contents of the directory at the given path recursively into this directory.
5563
*
64+
* <p>Symbolic links will not be followed.
65+
*
66+
* <pre><code>
67+
* directory.copyContentsFrom(new File("code/examples"));
68+
* </code></pre>
69+
*
5670
* @param dir the directory to copy the contents from.
5771
* @return the root managed directory for further configuration.
5872
*/
@@ -61,6 +75,12 @@ public interface DirectoryBuilder {
6175
/**
6276
* Copy the contents of the directory at the given path recursively into this directory.
6377
*
78+
* <p>Symbolic links will not be followed.
79+
*
80+
* <pre><code>
81+
* directory.copyContentsFrom(Path.of("code", "examples"));
82+
* </code></pre>
83+
*
6484
* @param rootDir the directory to copy the contents from.
6585
* @return the root managed directory for further configuration.
6686
*/
@@ -69,6 +89,10 @@ public interface DirectoryBuilder {
6989
/**
7090
* Create an empty directory.
7191
*
92+
* <pre><code>
93+
* directory.thatIsEmpty();
94+
* </code></pre>
95+
*
7296
* @return the root managed directory for further configuration.
7397
*/
7498
ManagedDirectory thatIsEmpty();

0 commit comments

Comments
 (0)