Skip to content

Commit 0292b1d

Browse files
committed
Translate tests to Java
1 parent 1f672c2 commit 0292b1d

File tree

8 files changed

+464
-190
lines changed

8 files changed

+464
-190
lines changed

build.gradle.kts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
plugins {
2-
id("groovy")
32
id("org.gradlex.internal.plugin-publish-conventions") version "0.6"
43
}
54

65
group = "org.gradlex"
76
version = "1.0.1"
87

98
java {
10-
sourceCompatibility = JavaVersion.VERSION_1_8
11-
targetCompatibility = JavaVersion.VERSION_1_8
9+
toolchain.languageVersion = JavaLanguageVersion.of(21)
1210
}
1311

14-
dependencies {
15-
testImplementation("org.spockframework:spock-core:2.1-groovy-3.0")
16-
testImplementation("org.gradle.exemplar:samples-check:1.0.3")
17-
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
12+
tasks.compileJava {
13+
options.release = 8
1814
}
1915

2016
pluginPublishConventions {
@@ -31,7 +27,14 @@ pluginPublishConventions {
3127
}
3228
}
3329

34-
tasks.test {
35-
useJUnitPlatform()
36-
maxParallelForks = 4
30+
testing.suites.named<JvmTestSuite>("test") {
31+
useJUnitJupiter()
32+
dependencies {
33+
implementation("org.assertj:assertj-core:3.27.3")
34+
}
35+
targets.configureEach {
36+
testTask {
37+
maxParallelForks = 4
38+
}
39+
}
3740
}

src/test/groovy/org/gradlex/javamodule/packaging/test/JavaModulePackagingTest.groovy

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/test/groovy/org/gradlex/javamodule/packaging/test/fixture/GradleBuild.groovy

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright the GradleX team.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradlex.javamodule.packaging.test;
18+
19+
import org.assertj.core.api.Assertions;
20+
import org.gradlex.javamodule.packaging.test.fixture.GradleBuild;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
import java.util.Locale;
26+
import java.util.stream.Stream;
27+
28+
import static org.gradle.testkit.runner.TaskOutcome.FAILED;
29+
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
30+
import static org.gradlex.javamodule.packaging.test.fixture.GradleBuild.hostOs;
31+
import static org.gradlex.javamodule.packaging.test.fixture.GradleBuild.runsOnLinux;
32+
import static org.gradlex.javamodule.packaging.test.fixture.GradleBuild.runsOnMacos;
33+
import static org.gradlex.javamodule.packaging.test.fixture.GradleBuild.runsOnWindows;
34+
35+
class JavaModulePackagingTest {
36+
37+
GradleBuild build = new GradleBuild();
38+
39+
private static Stream<Arguments> testTargets() {
40+
return Stream.of(
41+
Arguments.of("windows", "windows", runsOnWindows()),
42+
Arguments.of("macos", "macos", runsOnMacos()),
43+
Arguments.of("ubuntu", "linux", runsOnLinux()));
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("testTargets")
48+
void can_use_plugin(String label, String os, boolean success) {
49+
var taskToRun = ":app:assemble" + capitalize(label);
50+
var taskToCheck = ":app:jpackage" + capitalize(label);
51+
var macosArch = System.getProperty("os.arch").contains("aarch") ? "aarch64" : "x86-64";
52+
build.appBuildFile.appendText("""
53+
version = "1.0"
54+
javaModulePackaging {
55+
target("macos") {
56+
operatingSystem = "macos"
57+
architecture = "%s"
58+
}
59+
target("ubuntu") {
60+
operatingSystem = "linux"
61+
architecture = "x86-64"
62+
}
63+
target("windows") {
64+
operatingSystem = "windows"
65+
architecture = "x86-64"
66+
}
67+
}
68+
""".formatted(macosArch));
69+
build.appModuleInfoFile.writeText("""
70+
module org.example.app {
71+
}
72+
""");
73+
74+
var buildResult = success ? build.build(taskToRun) : build.fail(taskToRun);
75+
var taskResult = buildResult.task(taskToCheck);
76+
77+
Assertions.assertThat(taskResult).isNotNull();
78+
Assertions.assertThat(taskResult.getOutcome()).isEqualTo(success ? SUCCESS : FAILED);
79+
if (!success) {
80+
Assertions.assertThat(buildResult.getOutput()).contains(
81+
"> Running on %s; cannot build for %s".formatted(hostOs(), os));
82+
}
83+
}
84+
85+
private static String capitalize(String str) {
86+
return str.substring(0, 1).toUpperCase(Locale.ROOT) + str.substring(1);
87+
}
88+
89+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright the GradleX team.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradlex.javamodule.packaging.test.fixture;
18+
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.util.Comparator;
23+
import java.util.stream.Stream;
24+
25+
public class Directory {
26+
27+
private final Path directory;
28+
29+
Directory(Path directory) {
30+
this.directory = directory;
31+
Io.unchecked(() -> Files.createDirectories(directory));
32+
}
33+
34+
public WritableFile file(String path) {
35+
Path file = directory.resolve(path);
36+
Io.unchecked(() -> Files.createDirectories(file.getParent()));
37+
return new WritableFile(file);
38+
}
39+
40+
public Directory dir(String path) {
41+
Path dir = directory.resolve(path);
42+
Io.unchecked(() -> Files.createDirectories(dir));
43+
return new Directory(dir);
44+
}
45+
46+
public void delete() {
47+
try (Stream<Path> walk = Files.walk(directory)) {
48+
walk.sorted(Comparator.reverseOrder()).forEach(p -> Io.unchecked(p, Files::delete));
49+
} catch (IOException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
54+
public Path getAsPath() {
55+
return directory;
56+
}
57+
58+
public String canonicalPath() {
59+
return Io.unchecked(() -> getAsPath().toFile().getCanonicalPath());
60+
}
61+
}

0 commit comments

Comments
 (0)