11/*
2- * Copyright (c) 2015, 2023 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2026 , 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
3131 * @build ModuleReaderTest
3232 * jdk.test.lib.compiler.CompilerUtils
3333 * jdk.test.lib.util.JarUtils
34- * @run testng ModuleReaderTest
34+ * @run junit ModuleReaderTest
3535 * @summary Basic tests for java.lang.module.ModuleReader
3636 */
3737
4848import java .nio .file .Files ;
4949import java .nio .file .Path ;
5050import java .nio .file .Paths ;
51- import java .util .Arrays ;
5251import java .util .HashSet ;
5352import java .util .List ;
5453import java .util .Optional ;
5554import java .util .Set ;
56- import java .util .stream .Collectors ;
5755import java .util .spi .ToolProvider ;
5856import java .util .stream .Stream ;
5957
6058import jdk .internal .module .ModulePath ;
6159import jdk .test .lib .compiler .CompilerUtils ;
6260import jdk .test .lib .util .JarUtils ;
61+ import org .junit .jupiter .api .BeforeAll ;
62+ import org .junit .jupiter .api .Test ;
6363
64- import org .testng .annotations .BeforeTest ;
65- import org .testng .annotations .Test ;
66- import static org .testng .Assert .*;
64+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
65+ import static org .junit .jupiter .api .Assertions .assertEquals ;
66+ import static org .junit .jupiter .api .Assertions .assertFalse ;
67+ import static org .junit .jupiter .api .Assertions .assertThrows ;
68+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6769
6870public class ModuleReaderTest {
6971 private static final String TEST_SRC = System .getProperty ("test.src" );
@@ -150,8 +152,8 @@ public class ModuleReaderTest {
150152 "p\\ Main.class"
151153 };
152154
153- @ BeforeTest
154- public void compileTestModule () throws Exception {
155+ @ BeforeAll
156+ public static void compileTestModule () throws Exception {
155157 // javac -d mods/$TESTMODULE src/$TESTMODULE/**
156158 boolean compiled = CompilerUtils .compile (SRC_DIR .resolve (TEST_MODULE ),
157159 MODS_DIR .resolve (TEST_MODULE ));
@@ -187,51 +189,29 @@ public void testImage() throws IOException {
187189 Optional <URI > ouri = reader .find (name );
188190 ouri .ifPresent (uri -> {
189191 if (name .endsWith ("/" ))
190- assertTrue (uri .toString ().endsWith ("/" ));
192+ assertTrue (uri .toString ().endsWith ("/" ),
193+ "mismatched directory URI for '" + name + "': " + uri );
191194 });
192195 }
193196
194197 // test "not found" in java.base module
195198 for (String name : NOT_BASE_RESOURCES ) {
196- assertFalse (reader .find (name ).isPresent ());
197- assertFalse (reader .open (name ).isPresent ());
198- assertFalse (reader .read (name ).isPresent ());
199+ assertFalse (reader .find (name ).isPresent (), "Unexpected resource found: " + name );
200+ assertFalse (reader .open (name ).isPresent (), "Unexpected resource opened: " + name );
201+ assertFalse (reader .read (name ).isPresent (), "Unexpected resource read: " + name );
199202 }
200203
201204 // test nulls
202- try {
203- reader .find (null );
204- assertTrue (false );
205- } catch (NullPointerException expected ) { }
206-
207- try {
208- reader .open (null );
209- assertTrue (false );
210- } catch (NullPointerException expected ) { }
211-
212- try {
213- reader .read (null );
214- assertTrue (false );
215- } catch (NullPointerException expected ) { }
216-
217- try {
218- reader .release (null );
219- assertTrue (false );
220- } catch (NullPointerException expected ) { }
221-
205+ assertThrows (NullPointerException .class , () -> reader .find (null ));
206+ assertThrows (NullPointerException .class , () -> reader .open (null ));
207+ assertThrows (NullPointerException .class , () -> reader .read (null ));
208+ assertThrows (NullPointerException .class , () -> reader .release (null ));
222209 }
223210
224211 // test closed ModuleReader
225- try {
226- reader .open (BASE_RESOURCES [0 ]);
227- assertTrue (false );
228- } catch (IOException expected ) { }
229-
230-
231- try {
232- reader .read (BASE_RESOURCES [0 ]);
233- assertTrue (false );
234- } catch (IOException expected ) { }
212+ assertThrows (IOException .class , () -> reader .open (BASE_RESOURCES [0 ]));
213+ assertThrows (IOException .class , () -> reader .read (BASE_RESOURCES [0 ]));
214+ assertThrows (IOException .class , reader ::list );
235215 }
236216
237217 /**
@@ -268,10 +248,10 @@ public void testJMod() throws IOException {
268248 String jmod = dir .resolve ("m.jmod" ).toString ();
269249 String [] args = { "create" , "--class-path" , cp , jmod };
270250 ToolProvider jmodTool = ToolProvider .findFirst ("jmod" )
271- .orElseThrow (() ->
272- new RuntimeException ("jmod tool not found" )
273- );
274- assertEquals (jmodTool .run (System .out , System .out , args ), 0 );
251+ .orElseThrow (() ->
252+ new RuntimeException ("jmod tool not found" )
253+ );
254+ assertEquals (0 , jmodTool .run (System .out , System .out , args ), "jmod tool failed" );
275255
276256 test (dir );
277257 }
@@ -307,57 +287,30 @@ void test(Path mp) throws IOException {
307287 Optional <URI > ouri = reader .find (name );
308288 ouri .ifPresent (uri -> {
309289 if (name .endsWith ("/" ))
310- assertTrue (uri .toString ().endsWith ("/" ));
290+ assertTrue (uri .toString ().endsWith ("/" ),
291+ "mismatched directory URI for '" + name + "': " + uri );
311292 });
312293 }
313294
314295 // test "not found" in test module
315296 for (String name : NOT_TEST_RESOURCES ) {
316297 System .out .println ("resource: " + name );
317- assertFalse (reader .find (name ).isPresent ());
318- assertFalse (reader .open (name ).isPresent ());
319- assertFalse (reader .read (name ).isPresent ());
298+ assertFalse (reader .find (name ).isPresent (), "Unexpected resource found: " + name );
299+ assertFalse (reader .open (name ).isPresent (), "Unexpected resource open: " + name );
300+ assertFalse (reader .read (name ).isPresent (), "Unexpected resource read: " + name );
320301 }
321302
322303 // test nulls
323- try {
324- reader .find (null );
325- assertTrue (false );
326- } catch (NullPointerException expected ) { }
327-
328- try {
329- reader .open (null );
330- assertTrue (false );
331- } catch (NullPointerException expected ) { }
332-
333- try {
334- reader .read (null );
335- assertTrue (false );
336- } catch (NullPointerException expected ) { }
337-
338- try {
339- reader .release (null );
340- throw new RuntimeException ();
341- } catch (NullPointerException expected ) { }
342-
304+ assertThrows (NullPointerException .class , () -> reader .find (null ));
305+ assertThrows (NullPointerException .class , () -> reader .open (null ));
306+ assertThrows (NullPointerException .class , () -> reader .read (null ));
307+ assertThrows (NullPointerException .class , () -> reader .release (null ));
343308 }
344309
345310 // test closed ModuleReader
346- try {
347- reader .open (TEST_RESOURCES [0 ]);
348- assertTrue (false );
349- } catch (IOException expected ) { }
350-
351-
352- try {
353- reader .read (TEST_RESOURCES [0 ]);
354- assertTrue (false );
355- } catch (IOException expected ) { }
356-
357- try {
358- reader .list ();
359- assertTrue (false );
360- } catch (IOException expected ) { }
311+ assertThrows (IOException .class , () -> reader .open (BASE_RESOURCES [0 ]));
312+ assertThrows (IOException .class , () -> reader .read (BASE_RESOURCES [0 ]));
313+ assertThrows (IOException .class , reader ::list );
361314 }
362315
363316 /**
@@ -367,15 +320,15 @@ void testFind(ModuleReader reader, String name, byte[] expectedBytes)
367320 throws IOException
368321 {
369322 Optional <URI > ouri = reader .find (name );
370- assertTrue (ouri .isPresent ());
323+ assertTrue (ouri .isPresent (), "missing URI for: " + name );
371324
372325 URL url = ouri .get ().toURL ();
373326 if (!url .getProtocol ().equalsIgnoreCase ("jmod" )) {
374327 URLConnection uc = url .openConnection ();
375328 uc .setUseCaches (false );
376329 try (InputStream in = uc .getInputStream ()) {
377330 byte [] bytes = in .readAllBytes ();
378- assertTrue ( Arrays . equals ( bytes , expectedBytes ) );
331+ assertArrayEquals ( expectedBytes , bytes , "resource bytes differ for: " + name );
379332 }
380333 }
381334 }
@@ -387,12 +340,10 @@ void testOpen(ModuleReader reader, String name, byte[] expectedBytes)
387340 throws IOException
388341 {
389342 Optional <InputStream > oin = reader .open (name );
390- assertTrue (oin .isPresent ());
391-
392- InputStream in = oin .get ();
393- try (in ) {
343+ assertTrue (oin .isPresent (), "missing input stream for: " + name );
344+ try (InputStream in = oin .get ()) {
394345 byte [] bytes = in .readAllBytes ();
395- assertTrue ( Arrays . equals ( bytes , expectedBytes ) );
346+ assertArrayEquals ( expectedBytes , bytes , "resource bytes differ for: " + name );
396347 }
397348 }
398349
@@ -408,10 +359,10 @@ void testRead(ModuleReader reader, String name, byte[] expectedBytes)
408359 ByteBuffer bb = obb .get ();
409360 try {
410361 int rem = bb .remaining ();
411- assertTrue ( rem == expectedBytes .length );
362+ assertEquals ( expectedBytes .length , rem , "resource lengths differ: " + name );
412363 byte [] bytes = new byte [rem ];
413364 bb .get (bytes );
414- assertTrue ( Arrays . equals ( bytes , expectedBytes ) );
365+ assertArrayEquals ( expectedBytes , bytes , "resource bytes differ: " + name );
415366 } finally {
416367 reader .release (bb );
417368 }
@@ -426,14 +377,14 @@ void testList(ModuleReader reader, String name) throws IOException {
426377 list = stream .toList ();
427378 }
428379 Set <String > names = new HashSet <>(list );
429- assertTrue (names .size () == list .size ()); // no duplicates
380+ assertEquals (names .size (), list .size (), "resource list contains duplicates: " + list );
430381
431- assertTrue (names .contains ("module-info.class" ));
432- assertTrue (names .contains (name ));
382+ assertTrue (names .contains ("module-info.class" ), "resource list did not contain 'module-info.class': " + list );
383+ assertTrue (names .contains (name ), "resource list did not contain '" + name + "'" + list );
433384
434385 // all resources should be locatable via find
435386 for (String e : names ) {
436- assertTrue (reader .find (e ).isPresent ());
387+ assertTrue (reader .find (e ).isPresent (), "resource not found: " + name );
437388 }
438389 }
439390
0 commit comments