Skip to content

Commit f6dc787

Browse files
committed
Backport fe9284a8c523bc747e107836c85114864efab2a1
1 parent 3bc06ab commit f6dc787

File tree

3 files changed

+69
-49
lines changed

3 files changed

+69
-49
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ private static Stream<MethodCall> toMethodCalls(Object[] ctorArgs, Method method
392392
}
393393

394394
private static Object fromString(String value, Class toType) {
395+
if (toType.isEnum()) {
396+
return Enum.valueOf(toType, value);
397+
}
395398
Function<String, Object> converter = conv.get(toType);
396399
if (converter == null) {
397400
throw new RuntimeException(String.format(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ private static Path getInstallationSubDirectory(JPackageCommand cmd) {
6161
return Path.of(cmd.getArgumentValue("--install-dir", cmd::name));
6262
}
6363

64+
// Tests have problems on windows where path in the temp dir are too long
65+
// for the wix tools. We can't use a tempDir outside the TKit's WorkDir, so
66+
// we minimize both the tempRoot directory name (above) and the tempDir name
67+
// (below) to the extension part (which is necessary to differenciate between
68+
// the multiple PackageTypes that will be run for one JPackageCommand).
69+
// It might be beter if the whole work dir name was shortened from:
70+
// jtreg_open_test_jdk_tools_jpackage_share_jdk_jpackage_tests_BasicTest_java.
71+
public static Path getTempDirectory(JPackageCommand cmd, Path tempRoot) {
72+
String ext = cmd.outputBundle().getFileName().toString();
73+
int i = ext.lastIndexOf(".");
74+
if (i > 0 && i < (ext.length() - 1)) {
75+
ext = ext.substring(i+1);
76+
}
77+
return tempRoot.resolve(ext);
78+
}
79+
6480
private static void runMsiexecWithRetries(Executor misexec) {
6581
Executor.Result result = null;
6682
for (int attempt = 0; attempt < 8; ++attempt) {

test/jdk/tools/jpackage/share/jdk/jpackage/tests/BasicTest.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, 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
@@ -30,7 +30,6 @@
3030
import java.util.ArrayList;
3131
import java.util.function.Function;
3232
import java.util.function.Predicate;
33-
import java.util.function.Supplier;
3433
import java.util.regex.Pattern;
3534
import java.util.stream.Stream;
3635
import jdk.jpackage.test.TKit;
@@ -43,6 +42,8 @@
4342
import jdk.jpackage.test.Annotations.Test;
4443
import jdk.jpackage.test.Annotations.Parameter;
4544

45+
import static jdk.jpackage.test.WindowsHelper.getTempDirectory;
46+
4647
/*
4748
* @test
4849
* @summary jpackage basic testing
@@ -263,64 +264,64 @@ public void testAddModules(String... addModulesArg) {
263264
cmd.executeAndAssertHelloAppImageCreated();
264265
}
265266

267+
public static enum TestTempType {
268+
TEMPDIR_EMPTY,
269+
TEMPDIR_NOT_EMPTY,
270+
TEMPDIR_NOT_EXIST,
271+
}
272+
266273
/**
267274
* Test --temp option. Doesn't make much sense for app image as temporary
268275
* directory is used only on Windows. Test it in packaging mode.
269-
* @throws IOException
270276
*/
271277
@Test
272-
@Parameter("true")
273-
@Parameter("false")
274-
public void testTemp(boolean withExistingTempDir) throws IOException {
278+
@Parameter("TEMPDIR_EMPTY")
279+
@Parameter("TEMPDIR_NOT_EMPTY")
280+
@Parameter("TEMPDIR_NOT_EXIST")
281+
public void testTemp(TestTempType type) throws IOException {
275282
final Path tempRoot = TKit.createTempDirectory("tmp");
276-
// This Test has problems on windows where path in the temp dir are too long
277-
// for the wix tools. We can't use a tempDir outside the TKit's WorkDir, so
278-
// we minimize both the tempRoot directory name (above) and the tempDir name
279-
// (below) to the extension part (which is necessary to differenciate between
280-
// the multiple PackageTypes that will be run for one JPackageCommand).
281-
// It might be beter if the whole work dir name was shortened from:
282-
// jtreg_open_test_jdk_tools_jpackage_share_jdk_jpackage_tests_BasicTest_java.
283-
Function<JPackageCommand, Path> getTempDir = cmd -> {
284-
String ext = cmd.outputBundle().getFileName().toString();
285-
int i = ext.lastIndexOf(".");
286-
if (i > 0 && i < (ext.length() - 1)) {
287-
ext = ext.substring(i+1);
288-
}
289-
return tempRoot.resolve(ext);
290-
};
291283

292-
Supplier<PackageTest> createTest = () -> {
293-
return new PackageTest()
294-
.configureHelloApp()
295-
// Force save of package bundle in test work directory.
296-
.addInitializer(JPackageCommand::setDefaultInputOutput)
297-
.addInitializer(cmd -> {
298-
Path tempDir = getTempDir.apply(cmd);
299-
if (withExistingTempDir) {
300-
Files.createDirectories(tempDir);
301-
} else {
302-
Files.createDirectories(tempDir.getParent());
284+
var pkgTest = new PackageTest()
285+
.configureHelloApp()
286+
// Force save of package bundle in test work directory.
287+
.addInitializer(JPackageCommand::setDefaultInputOutput)
288+
.addInitializer(cmd -> {
289+
Path tempDir = getTempDirectory(cmd, tempRoot);
290+
switch (type) {
291+
case TEMPDIR_EMPTY -> Files.createDirectories(tempDir);
292+
case TEMPDIR_NOT_EXIST -> Files.createDirectories(tempDir.getParent());
293+
case TEMPDIR_NOT_EMPTY -> {
294+
Files.createDirectories(tempDir);
295+
TKit.createTextFile(tempDir.resolve("foo.txt"), List.of(
296+
"Hello Duke!"));
297+
}
303298
}
304299
cmd.addArguments("--temp", tempDir);
300+
}
301+
);
302+
303+
if (TestTempType.TEMPDIR_NOT_EMPTY.equals(type)) {
304+
pkgTest.setExpectedExitCode(1).addBundleVerifier(cmd -> {
305+
// Check jpackage didn't use the supplied directory.
306+
Path tempDir = getTempDirectory(cmd, tempRoot);
307+
String[] tempDirContents = tempDir.toFile().list();
308+
TKit.assertStringListEquals(List.of("foo.txt"), List.of(
309+
tempDirContents), String.format(
310+
"Check the contents of the supplied temporary directory [%s]",
311+
tempDir));
312+
TKit.assertStringListEquals(List.of("Hello Duke!"),
313+
Files.readAllLines(tempDir.resolve(tempDirContents[0])),
314+
"Check the contents of the file in the supplied temporary directory");
305315
});
306-
};
316+
} else {
317+
pkgTest.addBundleVerifier(cmd -> {
318+
// Check jpackage used the supplied directory.
319+
Path tempDir = getTempDirectory(cmd, tempRoot);
320+
TKit.assertDirectoryNotEmpty(tempDir);
321+
});
322+
}
307323

308-
createTest.get()
309-
.addBundleVerifier(cmd -> {
310-
// Check jpackage actually used the supplied directory.
311-
Path tempDir = getTempDir.apply(cmd);
312-
TKit.assertNotEquals(0, tempDir.toFile().list().length,
313-
String.format(
314-
"Check jpackage wrote some data in the supplied temporary directory [%s]",
315-
tempDir));
316-
})
317-
.run(PackageTest.Action.CREATE);
318-
319-
createTest.get()
320-
// Temporary directory should not be empty,
321-
// jpackage should exit with error.
322-
.setExpectedExitCode(1)
323-
.run(PackageTest.Action.CREATE);
324+
pkgTest.run(PackageTest.Action.CREATE);
324325
}
325326

326327
@Test

0 commit comments

Comments
 (0)