Skip to content

Commit 4a22c1f

Browse files
author
Alexey Semenyuk
committedNov 27, 2024
8344770: Switch jpackage unit tests to use JUnit5
Reviewed-by: almatvee
1 parent 4948062 commit 4a22c1f

11 files changed

+757
-754
lines changed
 

‎test/jdk/tools/jpackage/junit/jdk.jpackage/jdk/jpackage/internal/AppImageFileTest.java

+104-86
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, 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
@@ -31,16 +31,18 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.LinkedHashMap;
34-
import org.junit.Assert;
35-
import org.junit.Test;
36-
import org.junit.Rule;
37-
import org.junit.rules.TemporaryFolder;
38-
import org.junit.function.ThrowingRunnable;
34+
import java.util.stream.Stream;
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.io.TempDir;
41+
import org.junit.jupiter.params.ParameterizedTest;
42+
import org.junit.jupiter.params.provider.MethodSource;
3943

40-
public class AppImageFileTest {
4144

42-
@Rule
43-
public final TemporaryFolder tempFolder = new TemporaryFolder();
45+
public class AppImageFileTest {
4446

4547
@Test
4648
public void testIdentity() throws IOException {
@@ -51,7 +53,7 @@ public void testIdentity() throws IOException {
5153
params.put(Arguments.CLIOptions.DESCRIPTION.getId(), "Duck is the King");
5254
AppImageFile aif = create(params);
5355

54-
Assert.assertEquals("Foo", aif.getLauncherName());
56+
assertEquals("Foo", aif.getLauncherName());
5557
}
5658

5759
@Test
@@ -73,60 +75,79 @@ public void testInvalidCommandLine() throws IOException {
7375
create(params);
7476
}
7577

76-
@Test
77-
public void testInavlidXml() throws IOException {
78-
assertInvalid(() -> createFromXml("<foo/>"));
79-
assertInvalid(() -> createFromXml("<jpackage-state/>"));
80-
assertInvalid(() -> createFromXml(JPACKAGE_STATE_OPEN, "</jpackage-state>"));
81-
assertInvalid(() -> createFromXml(
82-
JPACKAGE_STATE_OPEN,
83-
"<main-launcher></main-launcher>",
84-
"</jpackage-state>"));
85-
assertInvalid(() -> createFromXml(
86-
JPACKAGE_STATE_OPEN,
87-
"<main-launcher>Foo</main-launcher>",
88-
"<main-class></main-class>",
89-
"</jpackage-state>"));
90-
assertInvalid(() -> createFromXml(
91-
JPACKAGE_STATE_OPEN,
92-
"<launcher>A</launcher>",
93-
"<launcher>B</launcher>",
94-
"</jpackage-state>"));
78+
@ParameterizedTest
79+
@MethodSource
80+
public void testInavlidXml(String[] xmlData) throws IOException {
81+
Exception ex = assertThrowsExactly(RuntimeException.class, () -> createFromXml(xmlData));
82+
assertTrue(ex.getMessage().contains("generated by another jpackage version or malformed"));
83+
assertTrue(ex.getMessage().endsWith(".jpackage.xml\""));
9584
}
9685

97-
@Test
98-
public void testValidXml() throws IOException {
99-
Assert.assertEquals("Foo", (createFromXml(
100-
JPACKAGE_STATE_OPEN,
101-
"<app-version>1.0</app-version>",
102-
"<main-launcher>Foo</main-launcher>",
103-
"<main-class>main.Class</main-class>",
104-
"<signed>false</signed>",
105-
"<app-store>false</app-store>",
106-
"</jpackage-state>")).getLauncherName());
107-
108-
Assert.assertEquals("Boo", (createFromXml(
109-
JPACKAGE_STATE_OPEN,
110-
"<app-version>1.0</app-version>",
111-
"<main-launcher>Boo</main-launcher>",
112-
"<main-launcher>Bar</main-launcher>",
113-
"<main-class>main.Class</main-class>",
114-
"<signed>false</signed>",
115-
"<app-store>false</app-store>",
116-
"</jpackage-state>")).getLauncherName());
117-
118-
var file = createFromXml(
119-
JPACKAGE_STATE_OPEN,
120-
"<app-version>1.0</app-version>",
121-
"<main-launcher>Foo</main-launcher>",
122-
"<main-class>main.Class</main-class>",
123-
"<signed>false</signed>",
124-
"<app-store>false</app-store>",
125-
"<launcher></launcher>",
126-
"</jpackage-state>");
127-
Assert.assertEquals("Foo", file.getLauncherName());
128-
129-
Assert.assertEquals(0, file.getAddLaunchers().size());
86+
private static Stream<org.junit.jupiter.params.provider.Arguments> testInavlidXml() {
87+
return Stream.of(
88+
makeArguments((Object)new String[] {"<foo/>"}),
89+
makeArguments((Object)new String[] {"<jpackage-state/>"}),
90+
makeArguments((Object)new String[] {JPACKAGE_STATE_OPEN, "</jpackage-state>"}),
91+
makeArguments((Object)new String[] {
92+
JPACKAGE_STATE_OPEN,
93+
"<main-launcher></main-launcher>",
94+
"</jpackage-state>"
95+
}),
96+
makeArguments((Object)new String[] {
97+
JPACKAGE_STATE_OPEN,
98+
"<main-launcher>Foo</main-launcher>",
99+
"<main-class></main-class>",
100+
"</jpackage-state>"
101+
}),
102+
makeArguments((Object)new String[] {
103+
JPACKAGE_STATE_OPEN,
104+
"<launcher>A</launcher>",
105+
"<launcher>B</launcher>",
106+
"</jpackage-state>"
107+
})
108+
);
109+
}
110+
111+
@ParameterizedTest
112+
@MethodSource
113+
public void testValidXml(String expectedLauncherName, String xmlData[]) throws IOException {
114+
var file = createFromXml(xmlData);
115+
assertEquals(expectedLauncherName, file.getLauncherName());
116+
assertTrue(file.getAddLaunchers().isEmpty());
117+
}
118+
119+
private static Stream<org.junit.jupiter.params.provider.Arguments> testValidXml() {
120+
return Stream.of(
121+
makeArguments("Foo", List.of(
122+
JPACKAGE_STATE_OPEN,
123+
"<app-version>1.0</app-version>",
124+
"<main-launcher>Foo</main-launcher>",
125+
"<main-class>main.Class</main-class>",
126+
"<signed>false</signed>",
127+
"<app-store>false</app-store>",
128+
"</jpackage-state>").toArray(String[]::new)
129+
),
130+
makeArguments("Boo", List.of(
131+
JPACKAGE_STATE_OPEN,
132+
"<app-version>1.0</app-version>",
133+
"<main-launcher>Boo</main-launcher>",
134+
"<main-launcher>Bar</main-launcher>",
135+
"<main-class>main.Class</main-class>",
136+
"<signed>false</signed>",
137+
"<app-store>false</app-store>",
138+
"</jpackage-state>").toArray(String[]::new)
139+
),
140+
makeArguments("duke", List.of(
141+
JPACKAGE_STATE_OPEN,
142+
"<app-version>1.0</app-version>",
143+
"<main-launcher>duke</main-launcher>",
144+
"<main-class>main.Class</main-class>",
145+
"<signed>false</signed>",
146+
"<app-store>false</app-store>",
147+
"<launcher></launcher>",
148+
"</jpackage-state>").toArray(String[]::new)
149+
)
150+
);
130151
}
131152

132153
@Test
@@ -137,7 +158,7 @@ public void testMainLauncherName() throws IOException {
137158
params.put("description", "Duck App Description");
138159
AppImageFile aif = create(params);
139160

140-
Assert.assertEquals("Foo", aif.getLauncherName());
161+
assertEquals("Foo", aif.getLauncherName());
141162
}
142163

143164
@Test
@@ -148,7 +169,7 @@ public void testMainClass() throws IOException {
148169
params.put("description", "Duck App Description");
149170
AppImageFile aif = create(params);
150171

151-
Assert.assertEquals("main.Class", aif.getMainClass());
172+
assertEquals("main.Class", aif.getMainClass());
152173
}
153174

154175
@Test
@@ -160,7 +181,7 @@ public void testMacSign() throws IOException {
160181
params.put("mac-sign", Boolean.TRUE);
161182
AppImageFile aif = create(params);
162183

163-
Assert.assertTrue(aif.isSigned());
184+
assertTrue(aif.isSigned());
164185
}
165186

166187
@Test
@@ -172,10 +193,10 @@ public void testCopyAsSigned() throws IOException {
172193
params.put("mac-sign", Boolean.FALSE);
173194

174195
AppImageFile aif = create(params);
175-
Assert.assertFalse(aif.isSigned());
196+
assertFalse(aif.isSigned());
176197

177198
aif = aif.copyAsSigned();
178-
Assert.assertTrue(aif.isSigned());
199+
assertTrue(aif.isSigned());
179200
}
180201

181202
@Test
@@ -187,7 +208,7 @@ public void testMacAppStore() throws IOException {
187208
params.put("mac-app-store", Boolean.TRUE);
188209
AppImageFile aif = create(params);
189210

190-
Assert.assertTrue(aif.isAppStore());
211+
assertTrue(aif.isAppStore());
191212
}
192213

193214
@Test
@@ -210,32 +231,22 @@ public void testAddLaunchers() throws IOException {
210231
AppImageFile aif = create(params);
211232

212233
List<AppImageFile.LauncherInfo> addLaunchers = aif.getAddLaunchers();
213-
Assert.assertEquals(2, addLaunchers.size());
234+
assertEquals(2, addLaunchers.size());
214235
List<String> names = new ArrayList<>();
215236
names.add(addLaunchers.get(0).getName());
216237
names.add(addLaunchers.get(1).getName());
217238

218-
Assert.assertTrue(names.contains("Launcher2Name"));
219-
Assert.assertTrue(names.contains("Launcher3Name"));
239+
assertTrue(names.contains("Launcher2Name"));
240+
assertTrue(names.contains("Launcher3Name"));
220241
}
221242

222243
private AppImageFile create(Map<String, Object> params) throws IOException {
223-
AppImageFile.save(tempFolder.getRoot().toPath(), params);
224-
return AppImageFile.load(tempFolder.getRoot().toPath());
225-
}
226-
227-
private void assertInvalid(ThrowingRunnable action) {
228-
Exception ex = Assert.assertThrows(RuntimeException.class, action);
229-
Assert.assertTrue(ex instanceof RuntimeException);
230-
Assert.assertTrue(ex.getMessage()
231-
.contains("generated by another jpackage version or malformed"));
232-
Assert.assertTrue(ex.getMessage()
233-
.endsWith(".jpackage.xml\""));
244+
AppImageFile.save(tempFolder, params);
245+
return AppImageFile.load(tempFolder);
234246
}
235247

236248
private AppImageFile createFromXml(String... xmlData) throws IOException {
237-
Path directory = tempFolder.getRoot().toPath();
238-
Path path = AppImageFile.getPathInAppImage(directory);
249+
Path path = AppImageFile.getPathInAppImage(tempFolder);
239250
path.toFile().mkdirs();
240251
Files.delete(path);
241252

@@ -246,11 +257,18 @@ private AppImageFile createFromXml(String... xmlData) throws IOException {
246257
Files.write(path, data, StandardOpenOption.CREATE,
247258
StandardOpenOption.TRUNCATE_EXISTING);
248259

249-
AppImageFile image = AppImageFile.load(directory);
260+
AppImageFile image = AppImageFile.load(tempFolder);
250261
return image;
251262
}
252263

253-
private final static String JPACKAGE_STATE_OPEN = String.format(
264+
static org.junit.jupiter.params.provider.Arguments makeArguments(Object ... args) {
265+
return org.junit.jupiter.params.provider.Arguments.of(args);
266+
}
267+
268+
@TempDir
269+
private Path tempFolder;
270+
271+
private static final String JPACKAGE_STATE_OPEN = String.format(
254272
"<jpackage-state platform=\"%s\" version=\"%s\">",
255273
AppImageFile.getPlatform(), AppImageFile.getVersion());
256274

‎test/jdk/tools/jpackage/junit/jdk.jpackage/jdk/jpackage/internal/ApplicationLayoutTest.java

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, 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
@@ -26,31 +26,44 @@
2626
import java.io.IOException;
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
29-
import org.junit.Test;
30-
import org.junit.Rule;
31-
import org.junit.rules.TemporaryFolder;
32-
import static org.junit.Assert.assertTrue;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
3332

3433

3534
public class ApplicationLayoutTest {
3635

37-
@Rule
38-
public final TemporaryFolder tempFolder = new TemporaryFolder();
36+
private Path newFolder(Path folderName, String ... extraFolderNames) throws IOException {
37+
var path = tempFolder.resolve(folderName);
38+
Files.createDirectories(path);
39+
for (var extraFolderName : extraFolderNames) {
40+
path = path.resolve(extraFolderName);
41+
Files.createDirectories(path);
42+
}
43+
return path;
44+
}
45+
46+
private Path newFile(Path fileName) throws IOException {
47+
var path = tempFolder.resolve(fileName);
48+
Files.createDirectories(path.getParent());
49+
Files.createFile(path);
50+
return path;
51+
}
3952

4053
private void fillLinuxAppImage() throws IOException {
41-
appImage = tempFolder.newFolder("Foo").toPath();
54+
appImage = newFolder(Path.of("Foo"));
4255

4356
Path base = appImage.getFileName();
4457

45-
tempFolder.newFolder(base.toString(), "bin");
46-
tempFolder.newFolder(base.toString(), "lib", "app", "mods");
47-
tempFolder.newFolder(base.toString(), "lib", "runtime", "bin");
48-
tempFolder.newFile(base.resolve("bin/Foo").toString());
49-
tempFolder.newFile(base.resolve("lib/app/Foo.cfg").toString());
50-
tempFolder.newFile(base.resolve("lib/app/hello.jar").toString());
51-
tempFolder.newFile(base.resolve("lib/Foo.png").toString());
52-
tempFolder.newFile(base.resolve("lib/libapplauncher.so").toString());
53-
tempFolder.newFile(base.resolve("lib/runtime/bin/java").toString());
58+
newFolder(base, "bin");
59+
newFolder(base, "lib", "app", "mods");
60+
newFolder(base, "lib", "runtime", "bin");
61+
newFile(base.resolve("bin/Foo"));
62+
newFile(base.resolve("lib/app/Foo.cfg"));
63+
newFile(base.resolve("lib/app/hello.jar"));
64+
newFile(base.resolve("lib/Foo.png"));
65+
newFile(base.resolve("lib/libapplauncher.so"));
66+
newFile(base.resolve("lib/runtime/bin/java"));
5467
}
5568

5669
@Test
@@ -84,5 +97,7 @@ private void assertApplicationLayout(ApplicationLayout layout) throws IOExceptio
8497
assertTrue(Files.isRegularFile(layout.runtimeDirectory().resolve("bin/java")));
8598
}
8699

100+
@TempDir
101+
private Path tempFolder;
87102
private Path appImage;
88103
}

0 commit comments

Comments
 (0)
Please sign in to comment.