8344770: Switch jpackage unit tests to use JUnit5

Reviewed-by: almatvee
This commit is contained in:
Alexey Semenyuk 2024-11-27 12:42:34 +00:00
parent 494806286f
commit 4a22c1fefc
11 changed files with 769 additions and 766 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -31,17 +31,19 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import org.junit.Assert; import java.util.stream.Stream;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Rule; import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.rules.TemporaryFolder; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import org.junit.function.ThrowingRunnable; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class AppImageFileTest { public class AppImageFileTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Test @Test
public void testIdentity() throws IOException { public void testIdentity() throws IOException {
Map<String, Object> params = new LinkedHashMap<>(); Map<String, Object> params = new LinkedHashMap<>();
@ -51,7 +53,7 @@ public class AppImageFileTest {
params.put(Arguments.CLIOptions.DESCRIPTION.getId(), "Duck is the King"); params.put(Arguments.CLIOptions.DESCRIPTION.getId(), "Duck is the King");
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertEquals("Foo", aif.getLauncherName()); assertEquals("Foo", aif.getLauncherName());
} }
@Test @Test
@ -73,60 +75,79 @@ public class AppImageFileTest {
create(params); create(params);
} }
@Test @ParameterizedTest
public void testInavlidXml() throws IOException { @MethodSource
assertInvalid(() -> createFromXml("<foo/>")); public void testInavlidXml(String[] xmlData) throws IOException {
assertInvalid(() -> createFromXml("<jpackage-state/>")); Exception ex = assertThrowsExactly(RuntimeException.class, () -> createFromXml(xmlData));
assertInvalid(() -> createFromXml(JPACKAGE_STATE_OPEN, "</jpackage-state>")); assertTrue(ex.getMessage().contains("generated by another jpackage version or malformed"));
assertInvalid(() -> createFromXml( assertTrue(ex.getMessage().endsWith(".jpackage.xml\""));
JPACKAGE_STATE_OPEN,
"<main-launcher></main-launcher>",
"</jpackage-state>"));
assertInvalid(() -> createFromXml(
JPACKAGE_STATE_OPEN,
"<main-launcher>Foo</main-launcher>",
"<main-class></main-class>",
"</jpackage-state>"));
assertInvalid(() -> createFromXml(
JPACKAGE_STATE_OPEN,
"<launcher>A</launcher>",
"<launcher>B</launcher>",
"</jpackage-state>"));
} }
@Test private static Stream<org.junit.jupiter.params.provider.Arguments> testInavlidXml() {
public void testValidXml() throws IOException { return Stream.of(
Assert.assertEquals("Foo", (createFromXml( makeArguments((Object)new String[] {"<foo/>"}),
JPACKAGE_STATE_OPEN, makeArguments((Object)new String[] {"<jpackage-state/>"}),
"<app-version>1.0</app-version>", makeArguments((Object)new String[] {JPACKAGE_STATE_OPEN, "</jpackage-state>"}),
"<main-launcher>Foo</main-launcher>", makeArguments((Object)new String[] {
"<main-class>main.Class</main-class>", JPACKAGE_STATE_OPEN,
"<signed>false</signed>", "<main-launcher></main-launcher>",
"<app-store>false</app-store>", "</jpackage-state>"
"</jpackage-state>")).getLauncherName()); }),
makeArguments((Object)new String[] {
JPACKAGE_STATE_OPEN,
"<main-launcher>Foo</main-launcher>",
"<main-class></main-class>",
"</jpackage-state>"
}),
makeArguments((Object)new String[] {
JPACKAGE_STATE_OPEN,
"<launcher>A</launcher>",
"<launcher>B</launcher>",
"</jpackage-state>"
})
);
}
Assert.assertEquals("Boo", (createFromXml( @ParameterizedTest
JPACKAGE_STATE_OPEN, @MethodSource
"<app-version>1.0</app-version>", public void testValidXml(String expectedLauncherName, String xmlData[]) throws IOException {
"<main-launcher>Boo</main-launcher>", var file = createFromXml(xmlData);
"<main-launcher>Bar</main-launcher>", assertEquals(expectedLauncherName, file.getLauncherName());
"<main-class>main.Class</main-class>", assertTrue(file.getAddLaunchers().isEmpty());
"<signed>false</signed>", }
"<app-store>false</app-store>",
"</jpackage-state>")).getLauncherName());
var file = createFromXml( private static Stream<org.junit.jupiter.params.provider.Arguments> testValidXml() {
JPACKAGE_STATE_OPEN, return Stream.of(
"<app-version>1.0</app-version>", makeArguments("Foo", List.of(
"<main-launcher>Foo</main-launcher>", JPACKAGE_STATE_OPEN,
"<main-class>main.Class</main-class>", "<app-version>1.0</app-version>",
"<signed>false</signed>", "<main-launcher>Foo</main-launcher>",
"<app-store>false</app-store>", "<main-class>main.Class</main-class>",
"<launcher></launcher>", "<signed>false</signed>",
"</jpackage-state>"); "<app-store>false</app-store>",
Assert.assertEquals("Foo", file.getLauncherName()); "</jpackage-state>").toArray(String[]::new)
),
Assert.assertEquals(0, file.getAddLaunchers().size()); makeArguments("Boo", List.of(
JPACKAGE_STATE_OPEN,
"<app-version>1.0</app-version>",
"<main-launcher>Boo</main-launcher>",
"<main-launcher>Bar</main-launcher>",
"<main-class>main.Class</main-class>",
"<signed>false</signed>",
"<app-store>false</app-store>",
"</jpackage-state>").toArray(String[]::new)
),
makeArguments("duke", List.of(
JPACKAGE_STATE_OPEN,
"<app-version>1.0</app-version>",
"<main-launcher>duke</main-launcher>",
"<main-class>main.Class</main-class>",
"<signed>false</signed>",
"<app-store>false</app-store>",
"<launcher></launcher>",
"</jpackage-state>").toArray(String[]::new)
)
);
} }
@Test @Test
@ -137,7 +158,7 @@ public class AppImageFileTest {
params.put("description", "Duck App Description"); params.put("description", "Duck App Description");
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertEquals("Foo", aif.getLauncherName()); assertEquals("Foo", aif.getLauncherName());
} }
@Test @Test
@ -148,7 +169,7 @@ public class AppImageFileTest {
params.put("description", "Duck App Description"); params.put("description", "Duck App Description");
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertEquals("main.Class", aif.getMainClass()); assertEquals("main.Class", aif.getMainClass());
} }
@Test @Test
@ -160,7 +181,7 @@ public class AppImageFileTest {
params.put("mac-sign", Boolean.TRUE); params.put("mac-sign", Boolean.TRUE);
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertTrue(aif.isSigned()); assertTrue(aif.isSigned());
} }
@Test @Test
@ -172,10 +193,10 @@ public class AppImageFileTest {
params.put("mac-sign", Boolean.FALSE); params.put("mac-sign", Boolean.FALSE);
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertFalse(aif.isSigned()); assertFalse(aif.isSigned());
aif = aif.copyAsSigned(); aif = aif.copyAsSigned();
Assert.assertTrue(aif.isSigned()); assertTrue(aif.isSigned());
} }
@Test @Test
@ -187,7 +208,7 @@ public class AppImageFileTest {
params.put("mac-app-store", Boolean.TRUE); params.put("mac-app-store", Boolean.TRUE);
AppImageFile aif = create(params); AppImageFile aif = create(params);
Assert.assertTrue(aif.isAppStore()); assertTrue(aif.isAppStore());
} }
@Test @Test
@ -210,32 +231,22 @@ public class AppImageFileTest {
AppImageFile aif = create(params); AppImageFile aif = create(params);
List<AppImageFile.LauncherInfo> addLaunchers = aif.getAddLaunchers(); List<AppImageFile.LauncherInfo> addLaunchers = aif.getAddLaunchers();
Assert.assertEquals(2, addLaunchers.size()); assertEquals(2, addLaunchers.size());
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
names.add(addLaunchers.get(0).getName()); names.add(addLaunchers.get(0).getName());
names.add(addLaunchers.get(1).getName()); names.add(addLaunchers.get(1).getName());
Assert.assertTrue(names.contains("Launcher2Name")); assertTrue(names.contains("Launcher2Name"));
Assert.assertTrue(names.contains("Launcher3Name")); assertTrue(names.contains("Launcher3Name"));
} }
private AppImageFile create(Map<String, Object> params) throws IOException { private AppImageFile create(Map<String, Object> params) throws IOException {
AppImageFile.save(tempFolder.getRoot().toPath(), params); AppImageFile.save(tempFolder, params);
return AppImageFile.load(tempFolder.getRoot().toPath()); return AppImageFile.load(tempFolder);
}
private void assertInvalid(ThrowingRunnable action) {
Exception ex = Assert.assertThrows(RuntimeException.class, action);
Assert.assertTrue(ex instanceof RuntimeException);
Assert.assertTrue(ex.getMessage()
.contains("generated by another jpackage version or malformed"));
Assert.assertTrue(ex.getMessage()
.endsWith(".jpackage.xml\""));
} }
private AppImageFile createFromXml(String... xmlData) throws IOException { private AppImageFile createFromXml(String... xmlData) throws IOException {
Path directory = tempFolder.getRoot().toPath(); Path path = AppImageFile.getPathInAppImage(tempFolder);
Path path = AppImageFile.getPathInAppImage(directory);
path.toFile().mkdirs(); path.toFile().mkdirs();
Files.delete(path); Files.delete(path);
@ -246,11 +257,18 @@ public class AppImageFileTest {
Files.write(path, data, StandardOpenOption.CREATE, Files.write(path, data, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING); StandardOpenOption.TRUNCATE_EXISTING);
AppImageFile image = AppImageFile.load(directory); AppImageFile image = AppImageFile.load(tempFolder);
return image; return image;
} }
private final static String JPACKAGE_STATE_OPEN = String.format( static org.junit.jupiter.params.provider.Arguments makeArguments(Object ... args) {
return org.junit.jupiter.params.provider.Arguments.of(args);
}
@TempDir
private Path tempFolder;
private static final String JPACKAGE_STATE_OPEN = String.format(
"<jpackage-state platform=\"%s\" version=\"%s\">", "<jpackage-state platform=\"%s\" version=\"%s\">",
AppImageFile.getPlatform(), AppImageFile.getVersion()); AppImageFile.getPlatform(), AppImageFile.getVersion());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,31 +26,44 @@ package jdk.jpackage.internal;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.Rule; import org.junit.jupiter.api.io.TempDir;
import org.junit.rules.TemporaryFolder; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
public class ApplicationLayoutTest { public class ApplicationLayoutTest {
@Rule private Path newFolder(Path folderName, String ... extraFolderNames) throws IOException {
public final TemporaryFolder tempFolder = new TemporaryFolder(); var path = tempFolder.resolve(folderName);
Files.createDirectories(path);
for (var extraFolderName : extraFolderNames) {
path = path.resolve(extraFolderName);
Files.createDirectories(path);
}
return path;
}
private Path newFile(Path fileName) throws IOException {
var path = tempFolder.resolve(fileName);
Files.createDirectories(path.getParent());
Files.createFile(path);
return path;
}
private void fillLinuxAppImage() throws IOException { private void fillLinuxAppImage() throws IOException {
appImage = tempFolder.newFolder("Foo").toPath(); appImage = newFolder(Path.of("Foo"));
Path base = appImage.getFileName(); Path base = appImage.getFileName();
tempFolder.newFolder(base.toString(), "bin"); newFolder(base, "bin");
tempFolder.newFolder(base.toString(), "lib", "app", "mods"); newFolder(base, "lib", "app", "mods");
tempFolder.newFolder(base.toString(), "lib", "runtime", "bin"); newFolder(base, "lib", "runtime", "bin");
tempFolder.newFile(base.resolve("bin/Foo").toString()); newFile(base.resolve("bin/Foo"));
tempFolder.newFile(base.resolve("lib/app/Foo.cfg").toString()); newFile(base.resolve("lib/app/Foo.cfg"));
tempFolder.newFile(base.resolve("lib/app/hello.jar").toString()); newFile(base.resolve("lib/app/hello.jar"));
tempFolder.newFile(base.resolve("lib/Foo.png").toString()); newFile(base.resolve("lib/Foo.png"));
tempFolder.newFile(base.resolve("lib/libapplauncher.so").toString()); newFile(base.resolve("lib/libapplauncher.so"));
tempFolder.newFile(base.resolve("lib/runtime/bin/java").toString()); newFile(base.resolve("lib/runtime/bin/java"));
} }
@Test @Test
@ -84,5 +97,7 @@ public class ApplicationLayoutTest {
assertTrue(Files.isRegularFile(layout.runtimeDirectory().resolve("bin/java"))); assertTrue(Files.isRegularFile(layout.runtimeDirectory().resolve("bin/java")));
} }
@TempDir
private Path tempFolder;
private Path appImage; private Path appImage;
} }

View File

@ -1,111 +0,0 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jpackage.internal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class CompareDottedVersionTest {
public CompareDottedVersionTest(boolean greedy, String version1,
String version2, int result) {
this.version1 = version1;
this.version2 = version2;
this.expectedResult = result;
if (greedy) {
createTestee = DottedVersion::greedy;
} else {
createTestee = DottedVersion::lazy;
}
}
@Parameters
public static List<Object[]> data() {
List<Object[]> data = new ArrayList<>();
for (var greedy : List.of(true, false)) {
data.addAll(List.of(new Object[][] {
{ greedy, "00.0.0", "0", 0 },
{ greedy, "00.0.0", "0.000", 0 },
{ greedy, "0.035", "0.0035", 0 },
{ greedy, "0.035", "0.0035.0", 0 },
{ greedy, "1", "1", 0 },
{ greedy, "2", "2.0", 0 },
{ greedy, "2.00", "2.0", 0 },
{ greedy, "1.2.3.4", "1.2.3.4.5", -1 },
{ greedy, "1.2.3.4", "1.2.3.4.0.1", -1 },
{ greedy, "34", "33", 1 },
{ greedy, "34.0.78", "34.1.78", -1 }
}));
}
data.addAll(List.of(new Object[][] {
{ false, "", "1", -1 },
{ false, "", "0", 0 },
{ false, "0", "", 0 },
{ false, "1.2.4-R4", "1.2.4-R5", 0 },
{ false, "1.2.4.-R4", "1.2.4.R5", 0 },
{ false, "7+1", "7+4", 0 },
{ false, "2+14", "2-14", 0 },
{ false, "23.4.RC4", "23.3.RC10", 1 },
{ false, "77." + "9".repeat(1000), "77." + "9".repeat(1000 -1) + "8", 1 },
}));
return data;
}
@Test
public void testIt() {
int actualResult = compare(version1, version2);
assertEquals(expectedResult, actualResult);
int actualNegateResult = compare(version2, version1);
assertEquals(actualResult, -1 * actualNegateResult);
}
private int compare(String x, String y) {
int result = DottedVersion.compareComponents(createTestee.apply(x), createTestee.apply(y));
if (result < 0) {
return -1;
}
if (result > 0) {
return 1;
}
return 0;
}
private final String version1;
private final String version2;
private final int expectedResult;
private final Function<String, DottedVersion> createTestee;
}

View File

@ -22,21 +22,15 @@
*/ */
package jdk.jpackage.internal; package jdk.jpackage.internal;
import org.hamcrest.BaseMatcher; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import org.hamcrest.Description; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.rules.ExpectedException; import org.junit.jupiter.params.provider.ValueSource;
import org.junit.rules.TemporaryFolder;
public class DeployParamsTest { public class DeployParamsTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test @Test
public void testValidAppName() throws PackagerException { public void testValidAppName() throws PackagerException {
initParamsAppName(); initParamsAppName();
@ -48,58 +42,13 @@ public class DeployParamsTest {
setAppNameAndValidate("Test - Name !!!"); setAppNameAndValidate("Test - Name !!!");
} }
@Test @ParameterizedTest
public void testInvalidAppName() throws PackagerException { @ValueSource(strings = {"Test\nName", "Test\rName", "TestName\\", "Test \" Name"})
initForInvalidAppNamePackagerException(); public void testInvalidAppName(String appName) throws PackagerException {
initParamsAppName(); initParamsAppName();
setAppNameAndValidate("Test\nName"); var ex = assertThrowsExactly(PackagerException.class, () -> setAppNameAndValidate(appName));
}
@Test assertTrue(ex.getMessage().startsWith("Error: Invalid Application name"));
public void testInvalidAppName2() throws PackagerException {
initForInvalidAppNamePackagerException();
initParamsAppName();
setAppNameAndValidate("Test\rName");
}
@Test
public void testInvalidAppName3() throws PackagerException {
initForInvalidAppNamePackagerException();
initParamsAppName();
setAppNameAndValidate("TestName\\");
}
@Test
public void testInvalidAppName4() throws PackagerException {
initForInvalidAppNamePackagerException();
initParamsAppName();
setAppNameAndValidate("Test \" Name");
}
private void initForInvalidAppNamePackagerException() {
thrown.expect(PackagerException.class);
String msg = "Error: Invalid Application name";
// Unfortunately org.hamcrest.core.StringStartsWith is not available
// with older junit, DIY
// thrown.expectMessage(startsWith("Error: Invalid Application name"));
thrown.expectMessage(new BaseMatcher() {
@Override
@SuppressWarnings("unchecked")
public boolean matches(Object o) {
if (o instanceof String) {
return ((String) o).startsWith(msg);
}
return false;
}
@Override
public void describeTo(Description d) {
d.appendText(msg);
}
});
} }
// Returns deploy params initialized to pass all validation, except for // Returns deploy params initialized to pass all validation, except for

View File

@ -22,174 +22,224 @@
*/ */
package jdk.jpackage.internal; package jdk.jpackage.internal;
import java.util.Collections; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.Rule; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.rules.ExpectedException; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertFalse; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runner.RunWith; import org.junit.jupiter.params.provider.EnumSource;
import org.junit.runners.Parameterized; import org.junit.jupiter.params.provider.MethodSource;
@RunWith(Parameterized.class)
public class DottedVersionTest { public class DottedVersionTest {
public DottedVersionTest(boolean greedy) { public record TestConfig(String input,
this.greedy = greedy; Function<String, DottedVersion> createVersion, String expectedSuffix,
if (greedy) { int expectedComponentCount, String expectedToComponent) {
createTestee = DottedVersion::greedy;
} else { TestConfig(String input, Type type, int expectedComponentCount, String expectedToComponent) {
createTestee = DottedVersion::lazy; this(input, type.createVersion, "", expectedComponentCount, expectedToComponent);
}
TestConfig(String input, Type type, int expectedComponentCount) {
this(input, type.createVersion, "", expectedComponentCount, input);
}
static TestConfig greedy(String input, int expectedComponentCount, String expectedToComponent) {
return new TestConfig(input, Type.GREEDY.createVersion, "", expectedComponentCount, expectedToComponent);
}
static TestConfig greedy(String input, int expectedComponentCount) {
return new TestConfig(input, Type.GREEDY.createVersion, "", expectedComponentCount, input);
}
static TestConfig lazy(String input, String expectedSuffix, int expectedComponentCount, String expectedToComponent) {
return new TestConfig(input, Type.LAZY.createVersion, expectedSuffix, expectedComponentCount, expectedToComponent);
} }
} }
@Parameterized.Parameters @ParameterizedTest
public static List<Object[]> data() { @MethodSource
return List.of(new Object[] { true }, new Object[] { false }); public void testValid(TestConfig cfg) {
var dv = cfg.createVersion.apply(cfg.input());
assertEquals(cfg.expectedSuffix(), dv.getUnprocessedSuffix());
assertEquals(cfg.expectedComponentCount(), dv.getComponents().length);
assertEquals(cfg.expectedToComponent(), dv.toComponentsString());
} }
@Rule private static List<TestConfig> testValid() {
public ExpectedException exceptionRule = ExpectedException.none(); List<TestConfig> data = new ArrayList<>();
for (var type : Type.values()) {
private static class CtorTester { data.addAll(List.of(
new TestConfig("1.0", type, 2),
CtorTester(String input, boolean greedy, String expectedSuffix, new TestConfig("1", type, 1),
int expectedComponentCount, String expectedToComponent) { new TestConfig("2.20034.045", type, 3, "2.20034.45"),
this.input = input; new TestConfig("2.234.0", type, 3),
this.greedy = greedy; new TestConfig("0", type, 1),
this.expectedSuffix = expectedSuffix; new TestConfig("0.1", type, 2),
this.expectedComponentCount = expectedComponentCount; new TestConfig("9".repeat(1000), type, 1),
this.expectedToComponent = expectedToComponent; new TestConfig("00.0.0", type, 3, "0.0.0")
));
} }
CtorTester(String input, boolean greedy, int expectedComponentCount, data.addAll(List.of(
String expectedToComponent) { TestConfig.lazy("1.-1", ".-1", 1, "1"),
this(input, greedy, "", expectedComponentCount, expectedToComponent); TestConfig.lazy("5.", ".", 1, "5"),
} TestConfig.lazy("4.2.", ".", 2, "4.2"),
TestConfig.lazy("3..2", "..2", 1, "3"),
TestConfig.lazy("3......2", "......2", 1, "3"),
TestConfig.lazy("2.a", ".a", 1, "2"),
TestConfig.lazy("a", "a", 0, ""),
TestConfig.lazy("2..a", "..a", 1, "2"),
TestConfig.lazy("0a", "a", 1, "0"),
TestConfig.lazy("120a", "a", 1, "120"),
TestConfig.lazy("120abc", "abc", 1, "120"),
TestConfig.lazy(".", ".", 0, ""),
TestConfig.lazy("....", "....", 0, ""),
TestConfig.lazy(" ", " ", 0, ""),
TestConfig.lazy(" 1", " 1", 0, ""),
TestConfig.lazy("678. 2", ". 2", 1, "678"),
TestConfig.lazy("+1", "+1", 0, ""),
TestConfig.lazy("-1", "-1", 0, ""),
TestConfig.lazy("-0", "-0", 0, ""),
TestConfig.lazy("+0", "+0", 0, "")
));
CtorTester(String input, boolean greedy, int expectedComponentCount) { return data;
this(input, greedy, "", expectedComponentCount, input);
}
static CtorTester greedy(String input, int expectedComponentCount,
String expectedToComponent) {
return new CtorTester(input, true, "", expectedComponentCount, expectedToComponent);
}
static CtorTester greedy(String input, int expectedComponentCount) {
return new CtorTester(input, true, "", expectedComponentCount, input);
}
static CtorTester lazy(String input, String expectedSuffix, int expectedComponentCount,
String expectedToComponent) {
return new CtorTester(input, false, expectedSuffix, expectedComponentCount,
expectedToComponent);
}
void run() {
DottedVersion dv;
if (greedy) {
dv = DottedVersion.greedy(input);
} else {
dv = DottedVersion.lazy(input);
}
assertEquals(expectedSuffix, dv.getUnprocessedSuffix());
assertEquals(expectedComponentCount, dv.getComponents().length);
assertEquals(expectedToComponent, dv.toComponentsString());
}
private final String input;
private final boolean greedy;
private final String expectedSuffix;
private final int expectedComponentCount;
private final String expectedToComponent;
} }
@Test @ParameterizedTest
public void testValid() { @MethodSource
final List<CtorTester> validStrings = List.of( public void testInvalid(String str) {
new CtorTester("1.0", greedy, 2), assertThrowsExactly(IllegalArgumentException.class, () -> new DottedVersion(str));
new CtorTester("1", greedy, 1), }
new CtorTester("2.20034.045", greedy, 3, "2.20034.45"),
new CtorTester("2.234.0", greedy, 3), private static Stream<String> testInvalid() {
new CtorTester("0", greedy, 1), return Stream.of(
new CtorTester("0.1", greedy, 2), "1.-1",
new CtorTester("9".repeat(1000), greedy, 1), "5.",
new CtorTester("00.0.0", greedy, 3, "0.0.0") "4.2.",
"3..2",
"2.a",
"0a",
".",
" ",
" 1",
"1. 2",
"+1",
"-1",
"-0",
"+0"
); );
}
final List<CtorTester> validLazyStrings; @ParameterizedTest
if (greedy) { @EnumSource(Type.class)
validLazyStrings = Collections.emptyList(); public void testNull(Type type) {
} else { assertThrowsExactly(NullPointerException.class, () -> type.createVersion.apply(null));
validLazyStrings = List.of(
CtorTester.lazy("1.-1", ".-1", 1, "1"),
CtorTester.lazy("5.", ".", 1, "5"),
CtorTester.lazy("4.2.", ".", 2, "4.2"),
CtorTester.lazy("3..2", "..2", 1, "3"),
CtorTester.lazy("3......2", "......2", 1, "3"),
CtorTester.lazy("2.a", ".a", 1, "2"),
CtorTester.lazy("a", "a", 0, ""),
CtorTester.lazy("2..a", "..a", 1, "2"),
CtorTester.lazy("0a", "a", 1, "0"),
CtorTester.lazy("120a", "a", 1, "120"),
CtorTester.lazy("120abc", "abc", 1, "120"),
CtorTester.lazy(".", ".", 0, ""),
CtorTester.lazy("....", "....", 0, ""),
CtorTester.lazy(" ", " ", 0, ""),
CtorTester.lazy(" 1", " 1", 0, ""),
CtorTester.lazy("678. 2", ". 2", 1, "678"),
CtorTester.lazy("+1", "+1", 0, ""),
CtorTester.lazy("-1", "-1", 0, ""),
CtorTester.lazy("-0", "-0", 0, ""),
CtorTester.lazy("+0", "+0", 0, "")
);
}
Stream.concat(validStrings.stream(), validLazyStrings.stream()).forEach(CtorTester::run);
} }
@Test @Test
public void testNull() { public void testEmptyGreedy() {
exceptionRule.expect(NullPointerException.class); assertThrowsExactly(IllegalArgumentException.class, () -> DottedVersion.greedy(""), "Version may not be empty string");
createTestee.apply(null);
} }
@Test @Test
public void testEmpty() { public void testEmptyLazy() {
if (greedy) { assertEquals(0, DottedVersion.lazy("").getComponents().length);
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Version may not be empty string");
createTestee.apply("");
} else {
assertEquals(0, createTestee.apply("").getComponents().length);
}
} }
@Test @ParameterizedTest
public void testEquals() { @EnumSource(Type.class)
DottedVersion dv = createTestee.apply("1.0"); public void testEquals(Type type) {
DottedVersion dv = type.createVersion.apply("1.0");
assertFalse(dv.equals(null)); assertFalse(dv.equals(null));
assertFalse(dv.equals(Integer.valueOf(1))); assertFalse(dv.equals(1));
assertFalse(dv.equals(dv.toString()));
for (var ver : List.of("3", "3.4", "3.0.0")) { for (var ver : List.of("3", "3.4", "3.0.0")) {
DottedVersion a = createTestee.apply(ver); DottedVersion a = type.createVersion.apply(ver);
DottedVersion b = createTestee.apply(ver); DottedVersion b = type.createVersion.apply(ver);
assertTrue(a.equals(b)); assertTrue(a.equals(b));
assertTrue(b.equals(a)); assertTrue(b.equals(a));
} }
if (!greedy) {
assertTrue(createTestee.apply("3.6+67").equals(createTestee.apply("3.6+67")));
assertFalse(createTestee.apply("3.6+67").equals(createTestee.apply("3.6+067")));
}
} }
private final boolean greedy; @Test
private final Function<String, DottedVersion> createTestee; public void testEqualsLazy() {
assertTrue(DottedVersion.lazy("3.6+67").equals(DottedVersion.lazy("3.6+67")));
assertFalse(DottedVersion.lazy("3.6+67").equals(DottedVersion.lazy("3.6+067")));
}
private static List<Object[]> testCompare() {
List<Object[]> data = new ArrayList<>();
for (var type : Type.values()) {
data.addAll(List.of(new Object[][] {
{ type, "00.0.0", "0", 0 },
{ type, "00.0.0", "0.000", 0 },
{ type, "0.035", "0.0035", 0 },
{ type, "0.035", "0.0035.0", 0 },
{ type, "1", "1", 0 },
{ type, "2", "2.0", 0 },
{ type, "2.00", "2.0", 0 },
{ type, "1.2.3.4", "1.2.3.4.5", -1 },
{ type, "1.2.3.4", "1.2.3.4.0.1", -1 },
{ type, "34", "33", 1 },
{ type, "34.0.78", "34.1.78", -1 }
}));
}
data.addAll(List.of(new Object[][] {
{ Type.LAZY, "", "1", -1 },
{ Type.LAZY, "", "0", 0 },
{ Type.LAZY, "0", "", 0 },
{ Type.LAZY, "1.2.4-R4", "1.2.4-R5", 0 },
{ Type.LAZY, "1.2.4.-R4", "1.2.4.R5", 0 },
{ Type.LAZY, "7+1", "7+4", 0 },
{ Type.LAZY, "2+14", "2-14", 0 },
{ Type.LAZY, "23.4.RC4", "23.3.RC10", 1 },
{ Type.LAZY, "77." + "9".repeat(1000), "77." + "9".repeat(1000 -1) + "8", 1 },
}));
return data;
}
@ParameterizedTest
@MethodSource
public void testCompare(Type type, String version1, String version2, int expectedResult) {
final int actualResult = compare(type, version1, version2);
assertEquals(expectedResult, actualResult);
final int actualNegateResult = compare(type, version2, version1);
assertEquals(actualResult, -1 * actualNegateResult);
}
private int compare(Type type, String x, String y) {
int result = DottedVersion.compareComponents(type.createVersion.apply(x), type.createVersion.apply(y));
if (result < 0) {
return -1;
}
if (result > 0) {
return 1;
}
return 0;
}
public enum Type {
GREEDY(DottedVersion::greedy),
LAZY(DottedVersion::lazy);
Type(Function<String, DottedVersion> createVersion) {
this.createVersion = createVersion;
}
private final Function<String, DottedVersion> createVersion;
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,30 +23,49 @@
package jdk.jpackage.internal; package jdk.jpackage.internal;
import static org.junit.Assert.assertEquals; import java.util.stream.Stream;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class EnquoterTest { public class EnquoterTest {
@Test @ParameterizedTest
public void testForShellLiterals() { @MethodSource
var enquoter = Enquoter.forShellLiterals(); public void testForShellLiterals(String expected, String input) {
var actual = Enquoter.forShellLiterals().applyTo(input);
assertEquals(null, "''", enquoter.applyTo("")); assertEquals(expected, actual);
assertEquals(null, "'foo'", enquoter.applyTo("foo"));
assertEquals(null, "' foo '", enquoter.applyTo(" foo "));
assertEquals(null, "'foo bar'", enquoter.applyTo("foo bar"));
assertEquals(null, "'foo\\' bar'", enquoter.applyTo("foo' bar"));
} }
@Test @ParameterizedTest
public void testForPropertyValues() { @MethodSource
var enquoter = Enquoter.forPropertyValues(); public void testForPropertyValues(String expected, String input) {
var actual = Enquoter.forPropertyValues().applyTo(input);
assertEquals(expected, actual);
}
assertEquals(null, "", enquoter.applyTo("")); private static Stream<org.junit.jupiter.params.provider.Arguments> testForShellLiterals() {
assertEquals(null, "foo", enquoter.applyTo("foo")); return Stream.of(
assertEquals(null, "\" foo \"", enquoter.applyTo(" foo ")); makeArguments("''", ""),
assertEquals(null, "\"foo bar\"", enquoter.applyTo("foo bar")); makeArguments("'foo'", "foo"),
assertEquals(null, "\"foo' bar\"", enquoter.applyTo("foo' bar")); makeArguments("' foo '", " foo "),
makeArguments("'foo bar'", "foo bar"),
makeArguments("'foo\\' bar'", "foo' bar")
);
}
private static Stream<org.junit.jupiter.params.provider.Arguments> testForPropertyValues() {
return Stream.of(
makeArguments("", ""),
makeArguments("foo", "foo"),
makeArguments("\" foo \"", " foo "),
makeArguments("\"foo bar\"", "foo bar"),
makeArguments("\"foo' bar\"", "foo' bar")
);
}
static org.junit.jupiter.params.provider.Arguments makeArguments(Object ... args) {
return org.junit.jupiter.params.provider.Arguments.of(args);
} }
} }

View File

@ -1,72 +0,0 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jpackage.internal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class InvalidDottedVersionTest {
public InvalidDottedVersionTest(String version) {
this.version = version;
}
@Parameters
public static List<Object[]> data() {
return Stream.of(
"1.-1",
"5.",
"4.2.",
"3..2",
"2.a",
"0a",
".",
" ",
" 1",
"1. 2",
"+1",
"-1",
"-0",
"+0"
).map(version -> new Object[] { version }).collect(Collectors.toList());
}
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void testIt() {
exceptionRule.expect(IllegalArgumentException.class);
new DottedVersion(version);
}
private final String version;
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,60 +23,106 @@
package jdk.jpackage.internal; package jdk.jpackage.internal;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import jdk.internal.util.OperatingSystem; import jdk.internal.util.OperatingSystem;
import static jdk.jpackage.internal.OverridableResource.Source.DefaultResource;
import static jdk.jpackage.internal.OverridableResource.Source.ResourceDir;
import jdk.jpackage.internal.resources.ResourceLocator; import jdk.jpackage.internal.resources.ResourceLocator;
import static org.hamcrest.CoreMatchers.is; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.hamcrest.CoreMatchers.not; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertThat; import org.junit.jupiter.api.Test;
import org.junit.Rule; import org.junit.jupiter.api.io.TempDir;
import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.rules.TemporaryFolder; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
public class OverridableResourceTest { public class OverridableResourceTest {
@Rule private static String[] saveResource(ResourceWriter resourceWriter, Path dstPath) throws IOException {
public final TemporaryFolder tempFolder = new TemporaryFolder(); switch (dstPath.getFileName().toString()) {
case "file" -> {
@Test return resourceWriter.saveToFile(dstPath);
public void testDefault() throws IOException { }
byte[] actualBytes = saveToFile(new OverridableResource(DEFAULT_NAME)); case "dir" -> {
return resourceWriter.saveInDir(dstPath);
try (InputStream is = ResourceLocator.class.getResourceAsStream( }
DEFAULT_NAME)) { default -> {
assertArrayEquals(is.readAllBytes(), actualBytes); throw new IllegalArgumentException();
}
} }
} }
@Test private static String[] saveResource(
public void testDefaultWithSubstitution() throws IOException { OverridableResource resource, Path dstPath, boolean dstFileOverwrite)
OverridableResource resource = new OverridableResource(DEFAULT_NAME); throws IOException {
return saveResource(buildResourceWriter(resource).dstFileOverwrite(dstFileOverwrite), dstPath);
}
List<String> linesBeforeSubstitution = convertToStringList(saveToFile( private static List<Object[]> data() {
resource)); List<Object[]> data = new ArrayList<>();
for (var dstPath : List.of("file", "dir")) {
for (var dstFileOverwrite : List.of(true, false)) {
data.add(new Object[]{Path.of(dstPath), dstFileOverwrite});
}
}
for (var dstPath : List.of("dir/file", "dir/dir")) {
data.add(new Object[]{Path.of(dstPath), false});
}
return data;
}
@ParameterizedTest
@MethodSource("data")
public void testDefault(Path dstPath, boolean dstFileOverwrite,
@TempDir Path tempFolder) throws IOException {
final String[] content = saveResource(
new OverridableResource(DEFAULT_NAME), tempFolder.resolve(
dstPath), dstFileOverwrite);
try (var resource = ResourceLocator.class.getResourceAsStream(DEFAULT_NAME);
var isr = new InputStreamReader(resource, StandardCharsets.ISO_8859_1);
var br = new BufferedReader(isr)) {
assertArrayEquals(br.lines().toArray(String[]::new), content);
}
}
@ParameterizedTest
@MethodSource("data")
public void testDefaultWithSubstitution(Path dstPath, boolean dstFileOverwrite,
@TempDir Path tempFolder) throws IOException {
if (SUBSTITUTION_DATA.size() != 1) { if (SUBSTITUTION_DATA.size() != 1) {
// Test setup issue // Test setup issue
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Substitution map should contain only a single entry"); "Substitution map should contain only a single entry");
} }
OverridableResource resource = new OverridableResource(DEFAULT_NAME);
var linesBeforeSubstitution = List.of(saveResource(resource, tempFolder.resolve(dstPath), dstFileOverwrite));
resource.setSubstitutionData(SUBSTITUTION_DATA); resource.setSubstitutionData(SUBSTITUTION_DATA);
List<String> linesAfterSubstitution = convertToStringList(saveToFile( var linesAfterSubstitution = List.of(saveResource(resource, tempFolder.resolve(dstPath), dstFileOverwrite));
resource));
assertEquals(linesBeforeSubstitution.size(), linesAfterSubstitution.size()); assertEquals(linesBeforeSubstitution.size(), linesAfterSubstitution.size());
@ -103,130 +149,239 @@ public class OverridableResourceTest {
assertTrue(linesMismatch); assertTrue(linesMismatch);
} }
@Test private static Stream<Object[]> dataWithResourceName() {
public void testCustom() throws IOException { return data().stream().flatMap(origArgs -> {
testCustom(DEFAULT_NAME); return Stream.of(ResourceName.values()).map(defaultName -> {
Object[] args = new Object[origArgs.length + 1];
args[0] = defaultName;
System.arraycopy(origArgs, 0, args, 1, origArgs.length);
return args;
});
});
} }
@Test @ParameterizedTest
public void testCustomNoDefault() throws IOException { @MethodSource("dataWithResourceName")
testCustom(null); public void testResourceDir(ResourceName defaultName, Path dstPath,
} boolean dstFileOverwrite, @TempDir Path tempFolder) throws IOException {
private void testCustom(String defaultName) throws IOException {
List<String> expectedResourceData = List.of("A", "B", "C"); List<String> expectedResourceData = List.of("A", "B", "C");
Path customFile = createCustomFile("foo", expectedResourceData); Path customFile = tempFolder.resolve("hello");
Files.write(customFile, expectedResourceData);
List<String> actualResourceData = convertToStringList(saveToFile( final var actualResourceData = saveResource(buildResourceWriter(
new OverridableResource(defaultName) new OverridableResource(defaultName.value)
.setPublicName(customFile.getFileName()) .setPublicName(customFile.getFileName())
.setResourceDir(customFile.getParent()))); .setResourceDir(customFile.getParent())
).dstFileOverwrite(dstFileOverwrite).expectedSource(ResourceDir),
tempFolder.resolve(dstPath));
assertArrayEquals(expectedResourceData.toArray(String[]::new), assertArrayEquals(expectedResourceData.toArray(String[]::new), actualResourceData);
actualResourceData.toArray(String[]::new));
} }
@Test @ParameterizedTest
public void testCustomtWithSubstitution() throws IOException { @MethodSource("dataWithResourceName")
testCustomtWithSubstitution(DEFAULT_NAME); public void testResourceDirWithSubstitution(ResourceName defaultName, Path dstPath,
} boolean dstFileOverwrite, @TempDir Path tempFolder) throws IOException {
@Test
public void testCustomtWithSubstitutionNoDefault() throws IOException {
testCustomtWithSubstitution(null);
}
private void testCustomtWithSubstitution(String defaultName) throws IOException {
final List<String> resourceData = List.of("A", "[BB]", "C", "Foo", "Foo", final List<String> resourceData = List.of("A", "[BB]", "C", "Foo", "Foo",
"GoodbyeHello", "_B"); "GoodbyeHello", "_B");
final Path customFile = createCustomFile("foo", resourceData);
final Map<String, String> substitutionData = new HashMap(Map.of("B", final Path customFile = tempFolder.resolve("hello");
"Bar", "Foo", "B", "_B", "JJ")); Files.write(customFile, resourceData);
final Map<String, String> substitutionData = new HashMap<>(Map.of(
"B", "Bar",
"Foo", "B",
"_B", "JJ"));
substitutionData.put("Hello", null); substitutionData.put("Hello", null);
final List<String> expectedResourceData = List.of("A", "[BarBar]", "C", final List<String> expectedResourceData = List.of("A", "[BarBar]", "C",
"Bar", "Bar", "Goodbye", "JJ"); "Bar", "Bar", "Goodbye", "JJ");
final List<String> actualResourceData = convertToStringList(saveToFile( final var actualResourceData = saveResource(buildResourceWriter(
new OverridableResource(defaultName) new OverridableResource(defaultName.value)
.setPublicName(customFile.getFileName())
.setSubstitutionData(substitutionData) .setSubstitutionData(substitutionData)
.setResourceDir(customFile.getParent()))); .setPublicName(customFile.getFileName())
assertArrayEquals(expectedResourceData.toArray(String[]::new), .setResourceDir(customFile.getParent())
actualResourceData.toArray(String[]::new)); ).dstFileOverwrite(dstFileOverwrite).expectedSource(ResourceDir),
tempFolder.resolve(dstPath));
// Don't call setPublicName() assertArrayEquals(expectedResourceData.toArray(String[]::new), actualResourceData);
final Path dstFile = tempFolder.newFolder().toPath().resolve(customFile.getFileName());
new OverridableResource(defaultName)
.setSubstitutionData(substitutionData)
.setResourceDir(customFile.getParent())
.saveToFile(dstFile);
assertArrayEquals(expectedResourceData.toArray(String[]::new),
convertToStringList(Files.readAllBytes(dstFile)).toArray(
String[]::new));
// Verify setSubstitutionData() stores a copy of passed in data
Map<String, String> substitutionData2 = new HashMap(substitutionData);
var resource = new OverridableResource(defaultName)
.setResourceDir(customFile.getParent());
resource.setSubstitutionData(substitutionData2);
substitutionData2.clear();
Files.delete(dstFile);
resource.saveToFile(dstFile);
assertArrayEquals(expectedResourceData.toArray(String[]::new),
convertToStringList(Files.readAllBytes(dstFile)).toArray(
String[]::new));
} }
@Test // Test it can derive a file in the resource dir from the name of the output file if the public name is not set
public void testNoDefault() throws IOException { @ParameterizedTest
Path dstFolder = tempFolder.newFolder().toPath(); @ValueSource(booleans = {true, false})
Path dstFile = dstFolder.resolve(Path.of("foo", "bar")); public void testPublicNameNotSet(boolean namesMatch, @TempDir Path tempFolder) throws IOException {
final List<String> expectedResourceData = List.of("A", "B", "C");
new OverridableResource(null).saveToFile(dstFile); final Path customFile = tempFolder.resolve("hello");
Files.write(customFile, expectedResourceData);
assertFalse(dstFile.toFile().exists()); final Path outputDir = tempFolder.resolve("output");
}
private final static String DEFAULT_NAME; var resourceWriter = buildResourceWriter(
private final static Map<String, String> SUBSTITUTION_DATA; new OverridableResource(null).setResourceDir(customFile.getParent()));
static {
if (OperatingSystem.isWindows()) { if (namesMatch) {
DEFAULT_NAME = "WinLauncher.template"; final var actualResourceData = resourceWriter
SUBSTITUTION_DATA = Map.of("COMPANY_NAME", "Foo9090345"); .expectedSource(ResourceDir)
} else if (OperatingSystem.isLinux()) { .saveToFile(outputDir.resolve(customFile.getFileName()));
DEFAULT_NAME = "template.control"; assertArrayEquals(expectedResourceData.toArray(String[]::new), actualResourceData);
SUBSTITUTION_DATA = Map.of("APPLICATION_PACKAGE", "Package1967");
} else if (OperatingSystem.isMacOS()) {
DEFAULT_NAME = "Info-lite.plist.template";
SUBSTITUTION_DATA = Map.of("DEPLOY_BUNDLE_IDENTIFIER", "12345");
} else { } else {
throw new IllegalArgumentException("Unknown platform: " + OperatingSystem.current()); final var actualResourceData = resourceWriter
.expectedSource(null)
.saveToFile(outputDir.resolve("another"));
assertNull(actualResourceData);
} }
} }
private byte[] saveToFile(OverridableResource resource) throws IOException { // Test setSubstitutionData() stores a copy of passed in data
Path dstFile = tempFolder.newFile().toPath(); @Test
resource.saveToFile(dstFile); public void testSubstitutionDataCopied(@TempDir Path tempFolder) throws IOException {
assertThat(0, is(not(dstFile.toFile().length()))); final Path customFile = tempFolder.resolve("hello");
Files.write(customFile, List.of("Hello"));
return Files.readAllBytes(dstFile); final Map<String, String> substitutionData = new HashMap<>(Map.of("Hello", "Goodbye"));
var resource = new OverridableResource(null)
.setSubstitutionData(substitutionData)
.setPublicName(customFile.getFileName())
.setResourceDir(customFile.getParent());
final var resourceWriter = buildResourceWriter(resource).expectedSource(ResourceDir);
var contents = resourceWriter.saveToFile(tempFolder.resolve("output"));
assertArrayEquals(new String[] { "Goodbye" }, contents);
substitutionData.put("Hello", "Ciao");
contents = resourceWriter.saveToFile(tempFolder.resolve("output"));
assertArrayEquals(new String[] { "Goodbye" }, contents);
resource.setSubstitutionData(substitutionData);
contents = resourceWriter.saveToFile(tempFolder.resolve("output"));
assertArrayEquals(new String[] { "Ciao" }, contents);
} }
private Path createCustomFile(String publicName, List<String> data) throws @Test
IOException { public void testNoDefault(@TempDir Path tempFolder) throws IOException {
Path resourceFolder = tempFolder.newFolder().toPath(); var resourceWriter = buildResourceWriter(new OverridableResource(null)).expectedSource(null);
Path customFile = resourceFolder.resolve(publicName); assertEquals(null, resourceWriter.saveInDir(tempFolder));
Files.write(customFile, data); var dstDir = tempFolder.resolve("foo");
assertEquals(null, resourceWriter.saveInDir(dstDir));
return customFile; assertFalse(Files.exists(dstDir));
} }
private static List<String> convertToStringList(byte[] data) { enum ResourceName {
return List.of(new String(data, StandardCharsets.UTF_8).split("\\R")); DEFAULT_NAME(OverridableResourceTest.DEFAULT_NAME),
NULL_NAME(null);
ResourceName(String value) {
this.value = value;
}
private final String value;
}
private static final String DEFAULT_NAME;
private static final Map<String, String> SUBSTITUTION_DATA;
static {
switch (OperatingSystem.current()) {
case WINDOWS -> {
DEFAULT_NAME = "WinLauncher.template";
SUBSTITUTION_DATA = Map.of("COMPANY_NAME", "Foo9090345");
}
case LINUX -> {
DEFAULT_NAME = "template.control";
SUBSTITUTION_DATA = Map.of("APPLICATION_PACKAGE", "Package1967");
}
case MACOS -> {
DEFAULT_NAME = "Info-lite.plist.template";
SUBSTITUTION_DATA = Map.of("DEPLOY_BUNDLE_IDENTIFIER", "12345");
}
default -> {
throw new IllegalArgumentException("Unsupported platform: " + OperatingSystem.current());
}
}
}
static class ResourceWriter {
ResourceWriter(OverridableResource resource) {
this.resource = Objects.requireNonNull(resource);
}
ResourceWriter expectedSource(OverridableResource.Source v) {
expectedSource = v;
return this;
}
ResourceWriter dstFileOverwrite(boolean v) {
dstFileOverwrite = v;
return this;
}
String[] saveInDir(Path dstDir) throws IOException {
Path dstFile;
if (expectedSource != null) {
if (!Files.exists(dstDir)) {
Files.createDirectories(dstDir);
}
dstFile = Files.createTempFile(dstDir, null, null);
} else if (!Files.exists(dstDir)) {
dstFile = dstDir.resolve("nonexistant");
} else {
dstFile = Files.createTempFile(dstDir, null, null);
Files.delete(dstFile);
}
return saveToFile(dstFile);
}
String[] saveToFile(Path dstFile) throws IOException {
saveResource(dstFile);
if (expectedSource == null) {
return null;
} else {
return Files.readAllLines(dstFile).toArray(String[]::new);
}
}
private void saveResource(Path dstFile) throws IOException {
if (dstFileOverwrite && !Files.exists(dstFile)) {
Files.writeString(dstFile, "abcABC");
} else if (!dstFileOverwrite && Files.exists(dstFile)) {
Files.delete(dstFile);
}
final byte[] dstFileContent;
if (expectedSource == null && Files.exists(dstFile)) {
dstFileContent = Files.readAllBytes(dstFile);
} else {
dstFileContent = null;
}
var actualSource = resource.saveToFile(dstFile);
assertEquals(expectedSource, actualSource);
if (actualSource != null) {
assertNotEquals(0, Files.size(dstFile));
} else if (dstFileContent == null) {
assertFalse(Files.exists(dstFile));
} else {
var actualDstFileContent = Files.readAllBytes(dstFile);
assertArrayEquals(dstFileContent, actualDstFileContent);
}
}
final OverridableResource resource;
OverridableResource.Source expectedSource = DefaultResource;
boolean dstFileOverwrite;
}
private static ResourceWriter buildResourceWriter(OverridableResource resource) {
return new ResourceWriter(resource);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -32,29 +32,24 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.hamcrest.CoreMatchers.not; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.Assert.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertThat; import org.junit.jupiter.api.Test;
import org.junit.Rule; import org.junit.jupiter.api.io.TempDir;
import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.rules.TemporaryFolder; import org.junit.jupiter.params.provider.MethodSource;
public class PathGroupTest { public class PathGroupTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Test(expected = NullPointerException.class)
public void testNullId() { public void testNullId() {
new PathGroup(Map.of()).getPath(null); assertThrowsExactly(NullPointerException.class, () -> new PathGroup(Map.of()).getPath(null));
} }
@Test @Test
@ -82,8 +77,10 @@ public class PathGroupTest {
@Test @Test
public void testDuplicatedRoots() { public void testDuplicatedRoots() {
final PathGroup pg = new PathGroup(Map.of("main", PATH_FOO, "another", final PathGroup pg = new PathGroup(Map.of(
PATH_FOO, "root", PATH_EMPTY)); "main", PATH_FOO,
"another", PATH_FOO,
"root", PATH_EMPTY));
List<Path> paths = pg.paths(); List<Path> paths = pg.paths();
paths = paths.stream().sorted().toList(); paths = paths.stream().sorted().toList();
@ -100,8 +97,10 @@ public class PathGroupTest {
@Test @Test
public void testRoots() { public void testRoots() {
final PathGroup pg = new PathGroup(Map.of(1, Path.of("foo"), 2, Path.of( final PathGroup pg = new PathGroup(Map.of(
"foo", "bar"), 3, Path.of("foo", "bar", "buz"))); 1, Path.of("foo"),
2, Path.of("foo", "bar"),
3, Path.of("foo", "bar", "buz")));
List<Path> paths = pg.paths(); List<Path> paths = pg.paths();
assertEquals(3, paths.size()); assertEquals(3, paths.size());
@ -116,13 +115,15 @@ public class PathGroupTest {
@Test @Test
public void testResolveAt() { public void testResolveAt() {
final PathGroup pg = new PathGroup(Map.of(0, PATH_FOO, 1, PATH_BAR, 2, final PathGroup pg = new PathGroup(Map.of(
PATH_EMPTY)); 0, PATH_FOO,
1, PATH_BAR,
2, PATH_EMPTY));
final Path aPath = Path.of("a"); final Path aPath = Path.of("a");
final PathGroup pg2 = pg.resolveAt(aPath); final PathGroup pg2 = pg.resolveAt(aPath);
assertThat(pg, not(equalTo(pg2))); assertNotEquals(pg, pg2);
List<Path> paths = pg.paths(); List<Path> paths = pg.paths();
assertEquals(3, paths.size()); assertEquals(3, paths.size());
@ -139,29 +140,31 @@ public class PathGroupTest {
assertEquals(aPath, pg2.roots().get(0)); assertEquals(aPath, pg2.roots().get(0));
} }
@Test enum TransformType { COPY, MOVE, HANDLER };
public void testTransform() throws IOException {
for (var transform : TransformType.values()) { private static Stream<Object[]> testTransform() {
testTransform(false, transform); return Stream.of(TransformType.values()).flatMap(transform -> {
} return Stream.of(true, false).map(withExcludes -> {
return new Object[]{withExcludes,transform};
});
});
} }
@Test @ParameterizedTest
public void testTransformWithExcludes() throws IOException { @MethodSource("testTransform")
for (var transform : TransformType.values()) { public void testTransform(boolean withExcludes, TransformType transform, @TempDir Path tempDir) throws IOException {
testTransform(true, transform);
}
}
enum TransformType { Copy, Move, Handler }; final Path srcDir = tempDir.resolve("src");
Files.createDirectories(srcDir);
private void testTransform(boolean withExcludes, TransformType transform) final Path dstDir = tempDir.resolve("dst");
throws IOException { Files.createDirectories(dstDir);
final PathGroup pg = new PathGroup(Map.of(0, PATH_FOO, 1, PATH_BAR, 2,
PATH_EMPTY, 3, PATH_BAZ));
final Path srcDir = tempFolder.newFolder().toPath(); final PathGroup pg = new PathGroup(Map.of(
final Path dstDir = tempFolder.newFolder().toPath(); 0, PATH_FOO,
1, PATH_BAR,
2, PATH_EMPTY,
3, PATH_BAZ));
Files.createDirectories(srcDir.resolve(PATH_FOO).resolve("a/b/c/d")); Files.createDirectories(srcDir.resolve(PATH_FOO).resolve("a/b/c/d"));
Files.createFile(srcDir.resolve(PATH_FOO).resolve("a/b/c/file1")); Files.createFile(srcDir.resolve(PATH_FOO).resolve("a/b/c/file1"));
@ -181,7 +184,7 @@ public class PathGroupTest {
var srcFilesBeforeTransform = walkFiles(srcDir); var srcFilesBeforeTransform = walkFiles(srcDir);
if (transform == TransformType.Handler) { if (transform == TransformType.HANDLER) {
List<Map.Entry<Path, Path>> copyFile = new ArrayList<>(); List<Map.Entry<Path, Path>> copyFile = new ArrayList<>();
List<Path> createDirectory = new ArrayList<>(); List<Path> createDirectory = new ArrayList<>();
src.transform(dst, new PathGroup.TransformHandler() { src.transform(dst, new PathGroup.TransformHandler() {
@ -231,9 +234,9 @@ public class PathGroupTest {
return; return;
} }
if (transform == TransformType.Copy) { if (transform == TransformType.COPY) {
src.copy(dst); src.copy(dst);
} else if (transform == TransformType.Move) { } else if (transform == TransformType.MOVE) {
src.move(dst); src.move(dst);
} }
@ -248,30 +251,28 @@ public class PathGroupTest {
} }
UnaryOperator<Path[]> removeExcludes = paths -> { UnaryOperator<Path[]> removeExcludes = paths -> {
return Stream.of(paths) return Stream.of(paths)
.filter(path -> !excludedPaths.stream().anyMatch( .filter(path -> !excludedPaths.stream().anyMatch(path::startsWith))
path::startsWith)) .toArray(Path[]::new);
.collect(Collectors.toList()).toArray(Path[]::new);
}; };
var dstFiles = walkFiles(dstDir); var dstFiles = walkFiles(dstDir);
assertArrayEquals(removeExcludes.apply(srcFilesBeforeTransform), dstFiles); assertArrayEquals(removeExcludes.apply(srcFilesBeforeTransform), dstFiles);
if (transform == TransformType.Copy) { if (transform == TransformType.COPY) {
assertArrayEquals(dstFiles, removeExcludes.apply(walkFiles(srcDir))); assertArrayEquals(dstFiles, removeExcludes.apply(walkFiles(srcDir)));
} else if (transform == TransformType.Move) { } else if (transform == TransformType.MOVE) {
assertFalse(Files.exists(srcDir)); assertFalse(Files.exists(srcDir));
} }
} }
private static Path[] walkFiles(Path root) throws IOException { private static Path[] walkFiles(Path root) throws IOException {
try (var files = Files.walk(root)) { try (var files = Files.walk(root)) {
return files.map(root::relativize).sorted().collect( return files.map(root::relativize).sorted().toArray(Path[]::new);
Collectors.toList()).toArray(Path[]::new);
} }
} }
private final static Path PATH_FOO = Path.of("foo"); private static final Path PATH_FOO = Path.of("foo");
private final static Path PATH_BAR = Path.of("bar"); private static final Path PATH_BAR = Path.of("bar");
private final static Path PATH_BAZ = Path.of("baz"); private static final Path PATH_BAZ = Path.of("baz");
private final static Path PATH_EMPTY = Path.of(""); private static final Path PATH_EMPTY = Path.of("");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,95 +23,76 @@
package jdk.jpackage.internal; package jdk.jpackage.internal;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Rule; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import org.junit.Test; import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.rules.ExpectedException; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runner.RunWith; import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class PlatformVersionTest { public class PlatformVersionTest {
public PlatformVersionTest(Function<String, DottedVersion> parser, @ParameterizedTest
String version, boolean valid) { @ValueSource(strings = {
this.parser = parser; "0.0",
this.version = version; "255.255",
this.valid = valid; "0.0.0",
"255.255.65535",
"0.0.0.0",
"255.255.65535.999999"
})
public void testValidMsiProductVersion(String version) {
testImpl(PlatformVersion.WIN_MSI_PRODUCT_VERSION_CLASS, version, true);
} }
@Parameters @ParameterizedTest
public static List<Object[]> data() { @ValueSource(strings = {
List<Object[]> data = new ArrayList<>(); "0",
addTo(data, WIN_MSI_PRODUCT_VERSION_PARSER, true, "256.01",
"0.0", "255.256",
"255.255", "255.255.65536",
"0.0.0", "1.2.3.4.5"
"255.255.65535", })
"0.0.0.0", public void testInvalidMsiProductVersion(String version) {
"255.255.65535.999999" testImpl(PlatformVersion.WIN_MSI_PRODUCT_VERSION_CLASS, version, false);
);
addTo(data, WIN_MSI_PRODUCT_VERSION_PARSER, false,
"0",
"256.01",
"255.256",
"255.255.65536",
"1.2.3.4.5"
);
addTo(data, MAC_CFBUNDLE_VERSION_PARSER, true,
"1",
"1.2",
"1.2.3"
);
addTo(data, MAC_CFBUNDLE_VERSION_PARSER, false,
"0",
"0.1",
"1.2.3.4"
);
return data;
} }
private static void addTo(List<Object[]> data, @ParameterizedTest
Function<String, DottedVersion> parser, boolean valid, @ValueSource(strings = {"1", "1.2", "1.2.3"})
String... values) { public void testValidCfBundleVersion(String version) {
if (parser != null) { testImpl(PlatformVersion.MAC_CFBUNDLE_VERSION_CLASS, version, true);
data.addAll(Stream.of(values).map(version -> new Object[]{parser,
version, valid}).collect(Collectors.toList()));
}
} }
@Rule @ParameterizedTest
public ExpectedException exceptionRule = ExpectedException.none(); @ValueSource(strings = {"0", "0.1", "1.2.3.4"})
public void testInvalidCfBundleVersion(String version) {
testImpl(PlatformVersion.MAC_CFBUNDLE_VERSION_CLASS, version, false);
}
@Test private static void testImpl(PlatformVersion parser, String version, boolean valid) {
public void testIt() { assumeTrue(parser.parser != null);
if (valid) { if (valid) {
assertEquals(parser.apply(version).toString(), version); assertEquals(parser.parse(version).toString(), version);
} else { } else {
exceptionRule.expect(IllegalArgumentException.class); assertThrowsExactly(IllegalArgumentException.class, () -> parser.parse(version));
parser.apply(version);
} }
} }
private final Function<String, DottedVersion> parser; enum PlatformVersion {
private final String version; MAC_CFBUNDLE_VERSION_CLASS("jdk.jpackage.internal.CFBundleVersion"),
private final boolean valid; WIN_MSI_PRODUCT_VERSION_CLASS("jdk.jpackage.internal.MsiVersion");
private final static Function<String, DottedVersion> MAC_CFBUNDLE_VERSION_PARSER = findParser( PlatformVersion(String className) {
"jdk.jpackage.internal.CFBundleVersion"); parser = findParser(className);
private final static Function<String, DottedVersion> WIN_MSI_PRODUCT_VERSION_PARSER = findParser( }
"jdk.jpackage.internal.MsiVersion");
DottedVersion parse(String versionString) {
return parser.apply(versionString);
}
private Function<String, DottedVersion> parser;
}
private static Function<String, DottedVersion> findParser(String className) { private static Function<String, DottedVersion> findParser(String className) {
try { try {
@ -124,8 +105,8 @@ public class PlatformVersionTest {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} catch (InvocationTargetException ex) { } catch (InvocationTargetException ex) {
Throwable causeEx = ex.getCause(); Throwable causeEx = ex.getCause();
if (causeEx instanceof RuntimeException) { if (causeEx instanceof RuntimeException rtEx) {
throw (RuntimeException)causeEx; throw rtEx;
} }
throw new RuntimeException(causeEx); throw new RuntimeException(causeEx);
} }
@ -136,4 +117,5 @@ public class PlatformVersionTest {
throw new IllegalArgumentException(ex); throw new IllegalArgumentException(ex);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,12 +25,10 @@ package jdk.jpackage.internal;
import java.nio.file.Path; import java.nio.file.Path;
import jdk.internal.util.OperatingSystem; import jdk.internal.util.OperatingSystem;
import static org.hamcrest.CoreMatchers.is; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.hamcrest.CoreMatchers.not; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNotNull; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class ToolValidatorTest { public class ToolValidatorTest {
@ -70,11 +68,10 @@ public class ToolValidatorTest {
new DottedVersion("8")).setVersionParser(unused -> "10").validate()); new DottedVersion("8")).setVersionParser(unused -> "10").validate());
} }
private static void assertValidationFailure(ConfigException v, private static void assertValidationFailure(ConfigException v, boolean withCause) {
boolean withCause) {
assertNotNull(v); assertNotNull(v);
assertThat("", is(not(v.getMessage().strip()))); assertNotEquals("", v.getMessage().strip());
assertThat("", is(not(v.advice.strip()))); assertNotEquals("", v.getAdvice().strip());
if (withCause) { if (withCause) {
assertNotNull(v.getCause()); assertNotNull(v.getCause());
} else { } else {
@ -82,8 +79,8 @@ public class ToolValidatorTest {
} }
} }
private final static String TOOL_JAVA; private static final String TOOL_JAVA;
private final static String TOOL_UNKNOWN = Path.of(System.getProperty( private static final String TOOL_UNKNOWN = Path.of(System.getProperty(
"java.home"), "bin").toString(); "java.home"), "bin").toString();
static { static {