Skip to content

Commit 2efb033

Browse files
author
Alexey Semenyuk
committed
8350601: Miscellaneous updates to jpackage test lib
Reviewed-by: almatvee
1 parent d4fdc79 commit 2efb033

File tree

5 files changed

+226
-53
lines changed

5 files changed

+226
-53
lines changed

test/jdk/tools/jpackage/helpers-test/jdk/jpackage/test/TKitTest.java

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,19 +26,24 @@
2626
import static jdk.jpackage.internal.util.function.ThrowingSupplier.toSupplier;
2727

2828
import java.lang.reflect.Method;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
2931
import java.util.ArrayList;
3032
import java.util.Arrays;
3133
import java.util.Collection;
3234
import java.util.List;
3335
import java.util.Objects;
36+
import java.util.Optional;
37+
import java.util.function.Consumer;
3438
import java.util.stream.Stream;
39+
import jdk.jpackage.internal.util.function.ThrowingFunction;
3540
import jdk.jpackage.internal.util.function.ThrowingRunnable;
3641
import jdk.jpackage.test.Annotations.ParameterSupplier;
3742
import jdk.jpackage.test.Annotations.Test;
3843

3944
public class TKitTest extends JUnitAdapter {
4045

41-
public static Collection<Object[]> assertTestsData() {
46+
public static Collection<Object[]> test() {
4247
List<MethodCallConfig> data = new ArrayList<>();
4348

4449
var assertFunc = MethodCallConfig.build("assertTrue", boolean.class, String.class);
@@ -57,6 +62,12 @@ public static Collection<Object[]> assertTestsData() {
5762
data.addAll(List.of(assertFunc.args(7, 7).pass().expectLog("assertEquals(7)").createForMessage("Owl")));
5863
data.addAll(List.of(assertFunc.args(7, 10).fail().expectLog("Expected [7]. Actual [10]").createForMessage("Owl")));
5964

65+
assertFunc = MethodCallConfig.build("assertEquals", boolean.class, boolean.class, String.class);
66+
data.addAll(List.of(assertFunc.args(true, true).pass().expectLog("assertEquals(true)").createForMessage("Emu")));
67+
data.addAll(List.of(assertFunc.args(false, false).pass().expectLog("assertEquals(false)").createForMessage("Emu")));
68+
data.addAll(List.of(assertFunc.args(true, false).fail().expectLog("Expected [true]. Actual [false]").createForMessage("Emu")));
69+
data.addAll(List.of(assertFunc.args(false, true).fail().expectLog("Expected [false]. Actual [true]").createForMessage("Emu")));
70+
6071
assertFunc = MethodCallConfig.build("assertNotEquals", String.class, String.class, String.class);
6172
data.addAll(List.of(assertFunc.args("a", "b").pass().expectLog("assertNotEquals(a, b)").createForMessage("Tit")));
6273
data.addAll(List.of(assertFunc.args("a", "a").fail().expectLog("Unexpected [a] value").createForMessage("Tit")));
@@ -65,6 +76,12 @@ public static Collection<Object[]> assertTestsData() {
6576
data.addAll(List.of(assertFunc.args(7, 10).pass().expectLog("assertNotEquals(7, 10)").createForMessage("Duck")));
6677
data.addAll(List.of(assertFunc.args(7, 7).fail().expectLog("Unexpected [7] value").createForMessage("Duck")));
6778

79+
assertFunc = MethodCallConfig.build("assertNotEquals", boolean.class, boolean.class, String.class);
80+
data.addAll(List.of(assertFunc.args(true, false).pass().expectLog("assertNotEquals(true, false)").createForMessage("Sparrow")));
81+
data.addAll(List.of(assertFunc.args(false, true).pass().expectLog("assertNotEquals(false, true)").createForMessage("Sparrow")));
82+
data.addAll(List.of(assertFunc.args(true, true).fail().expectLog("Unexpected [true] value").createForMessage("Sparrow")));
83+
data.addAll(List.of(assertFunc.args(false, false).fail().expectLog("Unexpected [false] value").createForMessage("Sparrow")));
84+
6885
assertFunc = MethodCallConfig.build("assertNull", Object.class, String.class);
6986
data.addAll(List.of(assertFunc.args((Object) null).pass().expectLog("assertNull()").createForMessage("Ibis")));
7087
data.addAll(List.of(assertFunc.args("v").fail().expectLog("Unexpected not null value [v]").createForMessage("Ibis")));
@@ -186,13 +203,136 @@ Builder withAutoExpectLogPrefix(boolean v) {
186203
}
187204

188205
@Test
189-
@ParameterSupplier("assertTestsData")
206+
@ParameterSupplier
190207
public void test(MethodCallConfig methodCall) {
191208
runAssertWithExpectedLogOutput(() -> {
192209
methodCall.method.invoke(null, methodCall.args);
193210
}, methodCall.expectFail, methodCall.expectLog);
194211
}
195212

213+
@Test
214+
@ParameterSupplier("testCreateTempPath")
215+
public void testCreateTempFile(CreateTempTestSpec testSpec) throws Throwable {
216+
testSpec.test(TKit::createTempFile, TKit::assertFileExists);
217+
}
218+
219+
@Test
220+
@ParameterSupplier("testCreateTempPath")
221+
public void testCreateTempDirectory(CreateTempTestSpec testSpec) throws Throwable {
222+
testSpec.test(TKit::createTempDirectory, TKit::assertDirectoryEmpty);
223+
}
224+
225+
record CreateTempTestSpec(String role, Path expectedPath, List<Path> existingFiles,
226+
Class<? extends Exception> expectedExceptionClass) {
227+
228+
CreateTempTestSpec {
229+
Objects.requireNonNull(existingFiles);
230+
if ((expectedExceptionClass == null) == (expectedPath == null)) {
231+
throw new IllegalArgumentException("Only one of `expectedPath` and `expectedExceptionClass` should be set");
232+
}
233+
}
234+
235+
void test(ThrowingFunction<String, Path> createTempPath, Consumer<Path> assertTempPathExists) throws Throwable {
236+
for (var existingFile : existingFiles) {
237+
existingFile = TKit.workDir().resolve(existingFile);
238+
239+
Files.createDirectories(existingFile.getParent());
240+
Files.createFile(existingFile);
241+
}
242+
243+
if (expectedExceptionClass != null) {
244+
try {
245+
createTempPath.apply(role);
246+
TKit.assertUnexpected("Exception expected");
247+
} catch (Exception ex) {
248+
TKit.assertTrue(expectedExceptionClass.isInstance(ex),
249+
String.format("Check exception [%s] is instance of %s", ex, expectedExceptionClass));
250+
}
251+
} else {
252+
final var tempPath = createTempPath.apply(role);
253+
254+
assertTempPathExists.accept(tempPath);
255+
TKit.assertTrue(tempPath.startsWith(TKit.workDir()), "Check temp path created in the work directory");
256+
257+
final var relativeTempPath = TKit.workDir().relativize(tempPath);
258+
TKit.assertTrue(expectedPath.equals(relativeTempPath),
259+
String.format("Check [%s]=[%s]", expectedPath, relativeTempPath));
260+
}
261+
}
262+
263+
@Override
264+
public String toString() {
265+
final var sb = new StringBuilder();
266+
sb.append("role=").append(role);
267+
if (expectedPath != null) {
268+
sb.append("; expected=").append(expectedPath);
269+
}
270+
if (!existingFiles.isEmpty()) {
271+
sb.append("; exits=").append(existingFiles);
272+
}
273+
if (expectedExceptionClass != null) {
274+
sb.append("; exception=").append(expectedExceptionClass);
275+
}
276+
return sb.toString();
277+
}
278+
279+
static Builder role(String role) {
280+
return new Builder(role);
281+
}
282+
283+
final static class Builder {
284+
285+
private Builder(String role) {
286+
this.role = role;
287+
}
288+
289+
CreateTempTestSpec create() {
290+
return new CreateTempTestSpec(role, expectedPath, existingFiles, expectedExceptionClass);
291+
}
292+
293+
Builder expectedPath(String v) {
294+
expectedPath = Optional.of(v).map(Path::of).orElse(null);
295+
return this;
296+
}
297+
298+
Builder existingFiles(String ...v) {
299+
existingFiles.addAll(Stream.of(v).map(Path::of).toList());
300+
return this;
301+
}
302+
303+
Builder expectedExceptionClass(Class<? extends Exception> v) {
304+
expectedExceptionClass = v;
305+
return this;
306+
}
307+
308+
private final String role;
309+
private Path expectedPath;
310+
private final List<Path> existingFiles = new ArrayList<>();
311+
private Class<? extends Exception> expectedExceptionClass;
312+
}
313+
}
314+
315+
public static Collection<Object[]> testCreateTempPath() {
316+
return Stream.of(
317+
CreateTempTestSpec.role("foo").expectedPath("foo"),
318+
CreateTempTestSpec.role("foo.b").expectedPath("foo.b"),
319+
CreateTempTestSpec.role("foo").expectedPath("foo-0").existingFiles("foo"),
320+
CreateTempTestSpec.role("foo.b").expectedPath("foo-0.b").existingFiles("foo.b"),
321+
CreateTempTestSpec.role("foo..b").expectedPath("foo.-0.b").existingFiles("foo..b"),
322+
CreateTempTestSpec.role("a.b.c.d").expectedPath("a.b.c-0.d").existingFiles("a.b.c.d"),
323+
CreateTempTestSpec.role("foo").expectedPath("foo-1").existingFiles("foo", "foo-0"),
324+
CreateTempTestSpec.role("foo/bar/buz").expectedPath("foo/bar/buz"),
325+
CreateTempTestSpec.role("foo/bar/buz").expectedPath("foo/bar/buz-0").existingFiles("foo/bar/buz"),
326+
CreateTempTestSpec.role("foo/bar/buz.tmp").expectedPath("foo/bar/buz-0.tmp").existingFiles("foo/bar/buz.tmp"),
327+
CreateTempTestSpec.role("foo/bar").expectedPath("foo/bar-0").existingFiles("foo/bar/buz"),
328+
CreateTempTestSpec.role(Path.of("").toAbsolutePath().toString()).expectedExceptionClass(IllegalArgumentException.class),
329+
CreateTempTestSpec.role(null).expectedExceptionClass(NullPointerException.class),
330+
CreateTempTestSpec.role("").expectedExceptionClass(IllegalArgumentException.class)
331+
).map(CreateTempTestSpec.Builder::create).map(testSpec -> {
332+
return new Object[] { testSpec };
333+
}).toList();
334+
}
335+
196336
private static void runAssertWithExpectedLogOutput(ThrowingRunnable action,
197337
boolean expectFail, String... expectLogStrings) {
198338
runWithExpectedLogOutput(() -> {

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/AdditionalLauncher.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,10 @@ static PropertyFile getAdditionalLauncherProperties(
186186
return Optional.of(shell[0]).get();
187187
}
188188

189-
private void initialize(JPackageCommand cmd) {
190-
Path propsFile = TKit.workDir().resolve(name + ".properties");
191-
if (Files.exists(propsFile)) {
192-
// File with the given name exists, pick another name that
193-
// will not reference existing file.
194-
try {
195-
propsFile = TKit.createTempFile(propsFile);
196-
TKit.deleteIfExists(propsFile);
197-
} catch (IOException ex) {
198-
rethrowUnchecked(ex);
199-
}
200-
}
189+
private void initialize(JPackageCommand cmd) throws IOException {
190+
final Path propsFile = TKit.createTempFile(name + ".properties");
201191

202-
cmd.addArguments("--add-launcher", String.format("%s=%s", name,
203-
propsFile));
192+
cmd.addArguments("--add-launcher", String.format("%s=%s", name, propsFile));
204193

205194
List<Map.Entry<String, String>> properties = new ArrayList<>();
206195
if (defaultArguments != null) {

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,7 @@ public JPackageCommand setDefaultInputOutput() {
252252

253253
public JPackageCommand setInputToEmptyDirectory() {
254254
if (Files.exists(inputDir())) {
255-
try {
256-
setArgumentValue("--input", TKit.createTempDirectory("input"));
257-
} catch (IOException ex) {
258-
throw new RuntimeException(ex);
259-
}
255+
setArgumentValue("--input", TKit.createTempDirectory("input"));
260256
}
261257
return this;
262258
}
@@ -841,6 +837,11 @@ public static enum AppLayoutAssert {
841837
TKit.assertFileExists(cmd.appLauncherCfgPath(null));
842838
}
843839
}),
840+
MAIN_JAR_FILE(cmd -> {
841+
Optional.ofNullable(cmd.getArgumentValue("--main-jar", () -> null)).ifPresent(mainJar -> {
842+
TKit.assertFileExists(cmd.appLayout().appDirectory().resolve(mainJar));
843+
});
844+
}),
844845
RUNTIME_DIRECTORY(cmd -> {
845846
TKit.assertDirectoryExists(cmd.appRuntimeDirectory());
846847
if (TKit.isOSX()) {

0 commit comments

Comments
 (0)