1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
31
31
import java .util .List ;
32
32
import java .util .Map ;
33
33
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 ;
39
43
40
- public class AppImageFileTest {
41
44
42
- @ Rule
43
- public final TemporaryFolder tempFolder = new TemporaryFolder ();
45
+ public class AppImageFileTest {
44
46
45
47
@ Test
46
48
public void testIdentity () throws IOException {
@@ -51,7 +53,7 @@ public void testIdentity() throws IOException {
51
53
params .put (Arguments .CLIOptions .DESCRIPTION .getId (), "Duck is the King" );
52
54
AppImageFile aif = create (params );
53
55
54
- Assert . assertEquals ("Foo" , aif .getLauncherName ());
56
+ assertEquals ("Foo" , aif .getLauncherName ());
55
57
}
56
58
57
59
@ Test
@@ -73,60 +75,79 @@ public void testInvalidCommandLine() throws IOException {
73
75
create (params );
74
76
}
75
77
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\" " ));
95
84
}
96
85
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
+ );
130
151
}
131
152
132
153
@ Test
@@ -137,7 +158,7 @@ public void testMainLauncherName() throws IOException {
137
158
params .put ("description" , "Duck App Description" );
138
159
AppImageFile aif = create (params );
139
160
140
- Assert . assertEquals ("Foo" , aif .getLauncherName ());
161
+ assertEquals ("Foo" , aif .getLauncherName ());
141
162
}
142
163
143
164
@ Test
@@ -148,7 +169,7 @@ public void testMainClass() throws IOException {
148
169
params .put ("description" , "Duck App Description" );
149
170
AppImageFile aif = create (params );
150
171
151
- Assert . assertEquals ("main.Class" , aif .getMainClass ());
172
+ assertEquals ("main.Class" , aif .getMainClass ());
152
173
}
153
174
154
175
@ Test
@@ -160,7 +181,7 @@ public void testMacSign() throws IOException {
160
181
params .put ("mac-sign" , Boolean .TRUE );
161
182
AppImageFile aif = create (params );
162
183
163
- Assert . assertTrue (aif .isSigned ());
184
+ assertTrue (aif .isSigned ());
164
185
}
165
186
166
187
@ Test
@@ -172,10 +193,10 @@ public void testCopyAsSigned() throws IOException {
172
193
params .put ("mac-sign" , Boolean .FALSE );
173
194
174
195
AppImageFile aif = create (params );
175
- Assert . assertFalse (aif .isSigned ());
196
+ assertFalse (aif .isSigned ());
176
197
177
198
aif = aif .copyAsSigned ();
178
- Assert . assertTrue (aif .isSigned ());
199
+ assertTrue (aif .isSigned ());
179
200
}
180
201
181
202
@ Test
@@ -187,7 +208,7 @@ public void testMacAppStore() throws IOException {
187
208
params .put ("mac-app-store" , Boolean .TRUE );
188
209
AppImageFile aif = create (params );
189
210
190
- Assert . assertTrue (aif .isAppStore ());
211
+ assertTrue (aif .isAppStore ());
191
212
}
192
213
193
214
@ Test
@@ -210,32 +231,22 @@ public void testAddLaunchers() throws IOException {
210
231
AppImageFile aif = create (params );
211
232
212
233
List <AppImageFile .LauncherInfo > addLaunchers = aif .getAddLaunchers ();
213
- Assert . assertEquals (2 , addLaunchers .size ());
234
+ assertEquals (2 , addLaunchers .size ());
214
235
List <String > names = new ArrayList <>();
215
236
names .add (addLaunchers .get (0 ).getName ());
216
237
names .add (addLaunchers .get (1 ).getName ());
217
238
218
- Assert . assertTrue (names .contains ("Launcher2Name" ));
219
- Assert . assertTrue (names .contains ("Launcher3Name" ));
239
+ assertTrue (names .contains ("Launcher2Name" ));
240
+ assertTrue (names .contains ("Launcher3Name" ));
220
241
}
221
242
222
243
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 );
234
246
}
235
247
236
248
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 );
239
250
path .toFile ().mkdirs ();
240
251
Files .delete (path );
241
252
@@ -246,11 +257,18 @@ private AppImageFile createFromXml(String... xmlData) throws IOException {
246
257
Files .write (path , data , StandardOpenOption .CREATE ,
247
258
StandardOpenOption .TRUNCATE_EXISTING );
248
259
249
- AppImageFile image = AppImageFile .load (directory );
260
+ AppImageFile image = AppImageFile .load (tempFolder );
250
261
return image ;
251
262
}
252
263
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 (
254
272
"<jpackage-state platform=\" %s\" version=\" %s\" >" ,
255
273
AppImageFile .getPlatform (), AppImageFile .getVersion ());
256
274
0 commit comments