1
1
/*
2
- * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
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
26
26
import static jdk .jpackage .internal .util .function .ThrowingSupplier .toSupplier ;
27
27
28
28
import java .lang .reflect .Method ;
29
+ import java .nio .file .Files ;
30
+ import java .nio .file .Path ;
29
31
import java .util .ArrayList ;
30
32
import java .util .Arrays ;
31
33
import java .util .Collection ;
32
34
import java .util .List ;
33
35
import java .util .Objects ;
36
+ import java .util .Optional ;
37
+ import java .util .function .Consumer ;
34
38
import java .util .stream .Stream ;
39
+ import jdk .jpackage .internal .util .function .ThrowingFunction ;
35
40
import jdk .jpackage .internal .util .function .ThrowingRunnable ;
36
41
import jdk .jpackage .test .Annotations .ParameterSupplier ;
37
42
import jdk .jpackage .test .Annotations .Test ;
38
43
39
44
public class TKitTest extends JUnitAdapter {
40
45
41
- public static Collection <Object []> assertTestsData () {
46
+ public static Collection <Object []> test () {
42
47
List <MethodCallConfig > data = new ArrayList <>();
43
48
44
49
var assertFunc = MethodCallConfig .build ("assertTrue" , boolean .class , String .class );
@@ -57,6 +62,12 @@ public static Collection<Object[]> assertTestsData() {
57
62
data .addAll (List .of (assertFunc .args (7 , 7 ).pass ().expectLog ("assertEquals(7)" ).createForMessage ("Owl" )));
58
63
data .addAll (List .of (assertFunc .args (7 , 10 ).fail ().expectLog ("Expected [7]. Actual [10]" ).createForMessage ("Owl" )));
59
64
65
+ assertFunc = MethodCallConfig .build ("assertEquals" , boolean .class , boolean .class , String .class );
66
+ data .addAll (List .of (assertFunc .args (true , true ).pass ().expectLog ("assertEquals(true)" ).createForMessage ("Emu" )));
67
+ data .addAll (List .of (assertFunc .args (false , false ).pass ().expectLog ("assertEquals(false)" ).createForMessage ("Emu" )));
68
+ data .addAll (List .of (assertFunc .args (true , false ).fail ().expectLog ("Expected [true]. Actual [false]" ).createForMessage ("Emu" )));
69
+ data .addAll (List .of (assertFunc .args (false , true ).fail ().expectLog ("Expected [false]. Actual [true]" ).createForMessage ("Emu" )));
70
+
60
71
assertFunc = MethodCallConfig .build ("assertNotEquals" , String .class , String .class , String .class );
61
72
data .addAll (List .of (assertFunc .args ("a" , "b" ).pass ().expectLog ("assertNotEquals(a, b)" ).createForMessage ("Tit" )));
62
73
data .addAll (List .of (assertFunc .args ("a" , "a" ).fail ().expectLog ("Unexpected [a] value" ).createForMessage ("Tit" )));
@@ -65,6 +76,12 @@ public static Collection<Object[]> assertTestsData() {
65
76
data .addAll (List .of (assertFunc .args (7 , 10 ).pass ().expectLog ("assertNotEquals(7, 10)" ).createForMessage ("Duck" )));
66
77
data .addAll (List .of (assertFunc .args (7 , 7 ).fail ().expectLog ("Unexpected [7] value" ).createForMessage ("Duck" )));
67
78
79
+ assertFunc = MethodCallConfig .build ("assertNotEquals" , boolean .class , boolean .class , String .class );
80
+ data .addAll (List .of (assertFunc .args (true , false ).pass ().expectLog ("assertNotEquals(true, false)" ).createForMessage ("Sparrow" )));
81
+ data .addAll (List .of (assertFunc .args (false , true ).pass ().expectLog ("assertNotEquals(false, true)" ).createForMessage ("Sparrow" )));
82
+ data .addAll (List .of (assertFunc .args (true , true ).fail ().expectLog ("Unexpected [true] value" ).createForMessage ("Sparrow" )));
83
+ data .addAll (List .of (assertFunc .args (false , false ).fail ().expectLog ("Unexpected [false] value" ).createForMessage ("Sparrow" )));
84
+
68
85
assertFunc = MethodCallConfig .build ("assertNull" , Object .class , String .class );
69
86
data .addAll (List .of (assertFunc .args ((Object ) null ).pass ().expectLog ("assertNull()" ).createForMessage ("Ibis" )));
70
87
data .addAll (List .of (assertFunc .args ("v" ).fail ().expectLog ("Unexpected not null value [v]" ).createForMessage ("Ibis" )));
@@ -186,13 +203,136 @@ Builder withAutoExpectLogPrefix(boolean v) {
186
203
}
187
204
188
205
@ Test
189
- @ ParameterSupplier ( "assertTestsData" )
206
+ @ ParameterSupplier
190
207
public void test (MethodCallConfig methodCall ) {
191
208
runAssertWithExpectedLogOutput (() -> {
192
209
methodCall .method .invoke (null , methodCall .args );
193
210
}, methodCall .expectFail , methodCall .expectLog );
194
211
}
195
212
213
+ @ Test
214
+ @ ParameterSupplier ("testCreateTempPath" )
215
+ public void testCreateTempFile (CreateTempTestSpec testSpec ) throws Throwable {
216
+ testSpec .test (TKit ::createTempFile , TKit ::assertFileExists );
217
+ }
218
+
219
+ @ Test
220
+ @ ParameterSupplier ("testCreateTempPath" )
221
+ public void testCreateTempDirectory (CreateTempTestSpec testSpec ) throws Throwable {
222
+ testSpec .test (TKit ::createTempDirectory , TKit ::assertDirectoryEmpty );
223
+ }
224
+
225
+ record CreateTempTestSpec (String role , Path expectedPath , List <Path > existingFiles ,
226
+ Class <? extends Exception > expectedExceptionClass ) {
227
+
228
+ CreateTempTestSpec {
229
+ Objects .requireNonNull (existingFiles );
230
+ if ((expectedExceptionClass == null ) == (expectedPath == null )) {
231
+ throw new IllegalArgumentException ("Only one of `expectedPath` and `expectedExceptionClass` should be set" );
232
+ }
233
+ }
234
+
235
+ void test (ThrowingFunction <String , Path > createTempPath , Consumer <Path > assertTempPathExists ) throws Throwable {
236
+ for (var existingFile : existingFiles ) {
237
+ existingFile = TKit .workDir ().resolve (existingFile );
238
+
239
+ Files .createDirectories (existingFile .getParent ());
240
+ Files .createFile (existingFile );
241
+ }
242
+
243
+ if (expectedExceptionClass != null ) {
244
+ try {
245
+ createTempPath .apply (role );
246
+ TKit .assertUnexpected ("Exception expected" );
247
+ } catch (Exception ex ) {
248
+ TKit .assertTrue (expectedExceptionClass .isInstance (ex ),
249
+ String .format ("Check exception [%s] is instance of %s" , ex , expectedExceptionClass ));
250
+ }
251
+ } else {
252
+ final var tempPath = createTempPath .apply (role );
253
+
254
+ assertTempPathExists .accept (tempPath );
255
+ TKit .assertTrue (tempPath .startsWith (TKit .workDir ()), "Check temp path created in the work directory" );
256
+
257
+ final var relativeTempPath = TKit .workDir ().relativize (tempPath );
258
+ TKit .assertTrue (expectedPath .equals (relativeTempPath ),
259
+ String .format ("Check [%s]=[%s]" , expectedPath , relativeTempPath ));
260
+ }
261
+ }
262
+
263
+ @ Override
264
+ public String toString () {
265
+ final var sb = new StringBuilder ();
266
+ sb .append ("role=" ).append (role );
267
+ if (expectedPath != null ) {
268
+ sb .append ("; expected=" ).append (expectedPath );
269
+ }
270
+ if (!existingFiles .isEmpty ()) {
271
+ sb .append ("; exits=" ).append (existingFiles );
272
+ }
273
+ if (expectedExceptionClass != null ) {
274
+ sb .append ("; exception=" ).append (expectedExceptionClass );
275
+ }
276
+ return sb .toString ();
277
+ }
278
+
279
+ static Builder role (String role ) {
280
+ return new Builder (role );
281
+ }
282
+
283
+ final static class Builder {
284
+
285
+ private Builder (String role ) {
286
+ this .role = role ;
287
+ }
288
+
289
+ CreateTempTestSpec create () {
290
+ return new CreateTempTestSpec (role , expectedPath , existingFiles , expectedExceptionClass );
291
+ }
292
+
293
+ Builder expectedPath (String v ) {
294
+ expectedPath = Optional .of (v ).map (Path ::of ).orElse (null );
295
+ return this ;
296
+ }
297
+
298
+ Builder existingFiles (String ...v ) {
299
+ existingFiles .addAll (Stream .of (v ).map (Path ::of ).toList ());
300
+ return this ;
301
+ }
302
+
303
+ Builder expectedExceptionClass (Class <? extends Exception > v ) {
304
+ expectedExceptionClass = v ;
305
+ return this ;
306
+ }
307
+
308
+ private final String role ;
309
+ private Path expectedPath ;
310
+ private final List <Path > existingFiles = new ArrayList <>();
311
+ private Class <? extends Exception > expectedExceptionClass ;
312
+ }
313
+ }
314
+
315
+ public static Collection <Object []> testCreateTempPath () {
316
+ return Stream .of (
317
+ CreateTempTestSpec .role ("foo" ).expectedPath ("foo" ),
318
+ CreateTempTestSpec .role ("foo.b" ).expectedPath ("foo.b" ),
319
+ CreateTempTestSpec .role ("foo" ).expectedPath ("foo-0" ).existingFiles ("foo" ),
320
+ CreateTempTestSpec .role ("foo.b" ).expectedPath ("foo-0.b" ).existingFiles ("foo.b" ),
321
+ CreateTempTestSpec .role ("foo..b" ).expectedPath ("foo.-0.b" ).existingFiles ("foo..b" ),
322
+ CreateTempTestSpec .role ("a.b.c.d" ).expectedPath ("a.b.c-0.d" ).existingFiles ("a.b.c.d" ),
323
+ CreateTempTestSpec .role ("foo" ).expectedPath ("foo-1" ).existingFiles ("foo" , "foo-0" ),
324
+ CreateTempTestSpec .role ("foo/bar/buz" ).expectedPath ("foo/bar/buz" ),
325
+ CreateTempTestSpec .role ("foo/bar/buz" ).expectedPath ("foo/bar/buz-0" ).existingFiles ("foo/bar/buz" ),
326
+ CreateTempTestSpec .role ("foo/bar/buz.tmp" ).expectedPath ("foo/bar/buz-0.tmp" ).existingFiles ("foo/bar/buz.tmp" ),
327
+ CreateTempTestSpec .role ("foo/bar" ).expectedPath ("foo/bar-0" ).existingFiles ("foo/bar/buz" ),
328
+ CreateTempTestSpec .role (Path .of ("" ).toAbsolutePath ().toString ()).expectedExceptionClass (IllegalArgumentException .class ),
329
+ CreateTempTestSpec .role (null ).expectedExceptionClass (NullPointerException .class ),
330
+ CreateTempTestSpec .role ("" ).expectedExceptionClass (IllegalArgumentException .class )
331
+ ).map (CreateTempTestSpec .Builder ::create ).map (testSpec -> {
332
+ return new Object [] { testSpec };
333
+ }).toList ();
334
+ }
335
+
196
336
private static void runAssertWithExpectedLogOutput (ThrowingRunnable action ,
197
337
boolean expectFail , String ... expectLogStrings ) {
198
338
runWithExpectedLogOutput (() -> {
0 commit comments