From 7695b1f9c25921c25af3a37c70c3830b2c9cd41d Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Mon, 16 Mar 2026 16:01:23 +0000 Subject: [PATCH 01/22] 8379155: Refactor Files TestNG tests to use JUnit Reviewed-by: alanb --- .../java/nio/file/Files/BytesAndLines.java | 147 +++++----- .../nio/file/Files/CallWithInterruptSet.java | 28 +- .../jdk/java/nio/file/Files/CopyProcFile.java | 48 ++-- .../nio/file/Files/CreateDirectories.java | 22 +- test/jdk/java/nio/file/Files/Mismatch.java | 251 +++++++++--------- .../java/nio/file/Files/ReadWriteString.java | 226 ++++++++-------- .../nio/file/Files/SetLastModifiedTime.java | 53 ++-- test/jdk/java/nio/file/Files/StreamTest.java | 165 ++++++------ test/jdk/java/nio/file/Files/SubstDrive.java | 99 ++++--- .../nio/file/Files/walkFileTree/FindTest.java | 20 +- .../jdk/java/nio/file/spi/TestDelegation.java | 87 +++--- 11 files changed, 580 insertions(+), 566 deletions(-) diff --git a/test/jdk/java/nio/file/Files/BytesAndLines.java b/test/jdk/java/nio/file/Files/BytesAndLines.java index ca486ab87fc..1d51021e786 100644 --- a/test/jdk/java/nio/file/Files/BytesAndLines.java +++ b/test/jdk/java/nio/file/Files/BytesAndLines.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2026, 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 @@ -24,38 +24,47 @@ /* @test * @bug 7006126 8020669 8024788 8019526 * @build BytesAndLines PassThroughFileSystem - * @run testng BytesAndLines + * @run junit BytesAndLines * @summary Unit test for methods for Files readAllBytes, readAllLines and * and write methods. * @key randomness */ -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.OpenOption; -import static java.nio.file.StandardOpenOption.*; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.CharacterCodingException; import java.nio.charset.MalformedInputException; import java.nio.charset.UnmappableCharacterException; -import static java.nio.charset.StandardCharsets.*; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.file.Files; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; -import java.io.IOException; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import static org.testng.Assert.*; +import static java.nio.charset.StandardCharsets.*; +import static java.nio.file.StandardOpenOption.*; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -@Test(groups = "unit") public class BytesAndLines { // data for text files @@ -66,15 +75,15 @@ public class BytesAndLines { private static Random RAND = new Random(); // file used by most tests - private Path tmpfile; + private static Path tmpfile; - @BeforeClass - void setup() throws IOException { + @BeforeAll + static void setup() throws IOException { tmpfile = Files.createTempFile("blah", null); } - @AfterClass - void cleanup() throws IOException { + @AfterAll + static void cleanup() throws IOException { Files.deleteIfExists(tmpfile); } @@ -90,6 +99,7 @@ public class BytesAndLines { /** * Exercise NullPointerException */ + @Test public void testNulls() { Path file = Paths.get("foo"); byte[] bytes = new byte[100]; @@ -118,18 +128,13 @@ public class BytesAndLines { } private void checkNullPointerException(Callable c) { - try { - c.call(); - fail("NullPointerException expected"); - } catch (NullPointerException ignore) { - } catch (Exception e) { - fail(e + " not expected"); - } + assertThrows(NullPointerException.class, () -> c.call()); } /** * Exercise Files.readAllBytes(Path) on varied file sizes */ + @Test public void testReadAllBytes() throws IOException { int size = 0; while (size <= 16*1024) { @@ -145,7 +150,7 @@ public class BytesAndLines { // check expected bytes are read byte[] read = Files.readAllBytes(tmpfile); - assertTrue(Arrays.equals(read, expected), "Bytes read not the same as written"); + assertArrayEquals(expected, read, "Bytes read not the same as written"); } /** @@ -153,13 +158,14 @@ public class BytesAndLines { * special because file sizes are reported as 0 even though the file * has content. */ + @Test + @EnabledOnOs(OS.LINUX) public void testReadAllBytesOnProcFS() throws IOException { // read from procfs - if (System.getProperty("os.name").equals("Linux")) { - Path statFile = Paths.get("/proc/self/stat"); - byte[] data = Files.readAllBytes(statFile); - assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read"); - } + Path statFile = Paths.get("/proc/self/stat"); + byte[] data = Files.readAllBytes(statFile); + assertTrue(data.length > 0, + "Files.readAllBytes('" + statFile + "') failed to read"); } /** @@ -167,6 +173,7 @@ public class BytesAndLines { * because readAllBytes was originally implemented to use FileChannel * and so may not be supported by custom file system providers. */ + @Test public void testReadAllBytesOnCustomFS() throws IOException { Path myfile = PassThroughFileSystem.create().getPath("myfile"); try { @@ -175,7 +182,7 @@ public class BytesAndLines { byte[] b1 = genBytes(size); Files.write(myfile, b1); byte[] b2 = Files.readAllBytes(myfile); - assertTrue(Arrays.equals(b1, b2), "bytes not equal"); + assertArrayEquals(b1, b2, "bytes not equal"); size += 512; } } finally { @@ -186,6 +193,7 @@ public class BytesAndLines { /** * Exercise Files.write(Path, byte[], OpenOption...) on various sizes */ + @Test public void testWriteBytes() throws IOException { int size = 0; while (size < 16*1024) { @@ -198,10 +206,10 @@ public class BytesAndLines { private void testWriteBytes(int size, boolean append) throws IOException { byte[] bytes = genBytes(size); Path result = Files.write(tmpfile, bytes); - assertTrue(result == tmpfile); + assertSame(tmpfile, result); if (append) { Files.write(tmpfile, bytes, APPEND); - assertTrue(Files.size(tmpfile) == size*2); + assertEquals(size*2, Files.size(tmpfile)); } byte[] expected; @@ -214,12 +222,13 @@ public class BytesAndLines { } byte[] read = Files.readAllBytes(tmpfile); - assertTrue(Arrays.equals(read, expected), "Bytes read not the same as written"); + assertArrayEquals(expected, read, "Bytes read not the same as written"); } /** * Exercise Files.readAllLines(Path, Charset) */ + @Test public void testReadAllLines() throws IOException { // zero lines Files.write(tmpfile, new byte[0]); @@ -230,23 +239,22 @@ public class BytesAndLines { byte[] hi = { (byte)'h', (byte)'i' }; Files.write(tmpfile, hi); lines = Files.readAllLines(tmpfile, US_ASCII); - assertTrue(lines.size() == 1, "One line expected"); - assertTrue(lines.get(0).equals("hi"), "'Hi' expected"); + assertEquals(1, lines.size(), "One line expected"); + assertEquals("hi", lines.get(0), "'Hi' expected"); // two lines using platform's line separator List expected = Arrays.asList("hi", "there"); Files.write(tmpfile, expected, US_ASCII); assertTrue(Files.size(tmpfile) > 0, "File is empty"); lines = Files.readAllLines(tmpfile, US_ASCII); - assertTrue(lines.equals(expected), "Unexpected lines"); + assertLinesMatch(expected, lines, "Unexpected lines"); // MalformedInputException byte[] bad = { (byte)0xff, (byte)0xff }; Files.write(tmpfile, bad); - try { - Files.readAllLines(tmpfile, US_ASCII); - fail("MalformedInputException expected"); - } catch (MalformedInputException ignore) { } + assertThrows(MalformedInputException.class, + () -> Files.readAllLines(tmpfile, US_ASCII), + "MalformedInputException expected"); } /** @@ -254,24 +262,27 @@ public class BytesAndLines { * is special because file sizes are reported as 0 even though the file * has content. */ + @Test + @EnabledOnOs(OS.LINUX) public void testReadAllLinesOnProcFS() throws IOException { - if (System.getProperty("os.name").equals("Linux")) { - Path statFile = Paths.get("/proc/self/stat"); - List lines = Files.readAllLines(statFile); - assertTrue(lines.size() > 0, "Files.readAllLines('" + statFile + "') failed to read"); - } + Path statFile = Paths.get("/proc/self/stat"); + List lines = Files.readAllLines(statFile); + assertTrue(lines.size() > 0, + "Files.readAllLines('" + statFile + "') failed to read"); } /** * Exercise Files.readAllLines(Path) */ + @Test public void testReadAllLinesUTF8() throws IOException { Files.write(tmpfile, encodeAsUTF8(EN_STRING + "\n" + JA_STRING)); List lines = Files.readAllLines(tmpfile); - assertTrue(lines.size() == 2, "Read " + lines.size() + " lines instead of 2"); - assertTrue(lines.get(0).equals(EN_STRING)); - assertTrue(lines.get(1).equals(JA_STRING)); + assertEquals(2, lines.size(), + "Read " + lines.size() + " lines instead of 2"); + assertEquals(EN_STRING, lines.get(0)); + assertEquals(JA_STRING, lines.get(1)); // a sample of malformed sequences testReadAllLinesMalformedUTF8((byte)0xFF); // one-byte sequence @@ -284,57 +295,55 @@ public class BytesAndLines { ByteBuffer bb = UTF_8.newEncoder().encode(CharBuffer.wrap(s)); byte[] result = new byte[bb.limit()]; bb.get(result); - assertTrue(bb.remaining() == 0); + assertEquals(0, bb.remaining()); return result; } private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException { Files.write(tmpfile, bytes); - try { - Files.readAllLines(tmpfile); - fail("MalformedInputException expected"); - } catch (MalformedInputException ignore) { } + assertThrows(MalformedInputException.class, + () -> Files.readAllLines(tmpfile)); } /** * Exercise Files.write(Path, Iterable, Charset, OpenOption...) */ + @Test public void testWriteLines() throws IOException { // zero lines Path result = Files.write(tmpfile, Collections.emptyList(), US_ASCII); - assert(Files.size(tmpfile) == 0); - assert(result == tmpfile); + assertEquals(0, Files.size(tmpfile)); + assertSame(tmpfile, result); // two lines List lines = Arrays.asList("hi", "there"); Files.write(tmpfile, lines, US_ASCII); List actual = Files.readAllLines(tmpfile, US_ASCII); - assertTrue(actual.equals(lines), "Unexpected lines"); + assertLinesMatch(lines, actual, "Unexpected lines"); // append two lines Files.write(tmpfile, lines, US_ASCII, APPEND); List expected = new ArrayList<>(); expected.addAll(lines); expected.addAll(lines); - assertTrue(expected.size() == 4, "List should have 4 elements"); + assertEquals(4, expected.size()); actual = Files.readAllLines(tmpfile, US_ASCII); - assertTrue(actual.equals(expected), "Unexpected lines"); + assertLinesMatch(expected, actual, "Unexpected lines"); // UnmappableCharacterException - try { - String s = "\u00A0\u00A1"; - Files.write(tmpfile, Arrays.asList(s), US_ASCII); - fail("UnmappableCharacterException expected"); - } catch (UnmappableCharacterException ignore) { } + String s = "\u00A0\u00A1"; + assertThrows(UnmappableCharacterException.class, + () -> Files.write(tmpfile, Arrays.asList(s), US_ASCII)); } /** * Exercise Files.write(Path, Iterable, OpenOption...) */ + @Test public void testWriteLinesUTF8() throws IOException { List lines = Arrays.asList(EN_STRING, JA_STRING); Files.write(tmpfile, lines); List actual = Files.readAllLines(tmpfile, UTF_8); - assertTrue(actual.equals(lines), "Unexpected lines"); + assertLinesMatch(lines, actual, "Unexpected lines"); } } diff --git a/test/jdk/java/nio/file/Files/CallWithInterruptSet.java b/test/jdk/java/nio/file/Files/CallWithInterruptSet.java index 3d7bfd2c518..59d6a6a7ecb 100644 --- a/test/jdk/java/nio/file/Files/CallWithInterruptSet.java +++ b/test/jdk/java/nio/file/Files/CallWithInterruptSet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, 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 @@ -24,22 +24,24 @@ /* * @test * @bug 8205612 - * @run testng CallWithInterruptSet + * @run junit CallWithInterruptSet * @summary Test invoking Files methods with the interrupted status set */ -import java.io.InputStream; -import java.io.OutputStream; -import java.io.IOException; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; -import org.testng.annotations.Test; -import static org.testng.Assert.*; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class CallWithInterruptSet { @@ -49,7 +51,7 @@ public class CallWithInterruptSet { Thread.currentThread().interrupt(); try { byte[] bytes = Files.readAllBytes(file); - assertTrue(bytes.length == 100); + assertEquals(100, bytes.length); } finally { assertTrue(Thread.interrupted()); // clear interrupt } @@ -76,7 +78,7 @@ public class CallWithInterruptSet { } finally { assertTrue(Thread.interrupted()); // clear interrupt } - assertTrue(Files.size(file) == 10); + assertEquals(10, Files.size(file)); } @Test @@ -86,7 +88,7 @@ public class CallWithInterruptSet { Thread.currentThread().interrupt(); try { String msg = Files.readString(file); - assertEquals(msg, "hello"); + assertEquals("hello", msg); } finally { assertTrue(Thread.interrupted()); // clear interrupt } @@ -102,7 +104,7 @@ public class CallWithInterruptSet { assertTrue(Thread.interrupted()); // clear interrupt } String msg = Files.readString(file); - assertEquals(msg, "hello"); + assertEquals("hello", msg); } @Test @@ -112,7 +114,7 @@ public class CallWithInterruptSet { Thread.currentThread().interrupt(); try (BufferedReader reader = Files.newBufferedReader(file)) { String msg = reader.readLine(); - assertEquals(msg, "hello"); + assertEquals("hello", msg); } finally { assertTrue(Thread.interrupted()); // clear interrupt } @@ -128,7 +130,7 @@ public class CallWithInterruptSet { assertTrue(Thread.interrupted()); // clear interrupt } String msg = Files.readString(file); - assertEquals(msg, "hello"); + assertEquals("hello", msg); } private Path mkfile() throws IOException { diff --git a/test/jdk/java/nio/file/Files/CopyProcFile.java b/test/jdk/java/nio/file/Files/CopyProcFile.java index 892ac6b0eb4..c187aaed861 100644 --- a/test/jdk/java/nio/file/Files/CopyProcFile.java +++ b/test/jdk/java/nio/file/Files/CopyProcFile.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2026, 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 @@ -33,20 +33,23 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.function.ToLongBiFunction; +import java.util.stream.Stream; import static java.nio.file.StandardOpenOption.*; -import org.testng.Assert; -import org.testng.annotations.AfterTest; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.*; /* * @test * @bug 8293502 * @requires (os.family == "linux") * @summary Ensure that copying from a file in /proc works - * @run testng/othervm CopyProcFile + * @run junit/othervm CopyProcFile */ public class CopyProcFile { static final String SOURCE = "/proc/version"; @@ -112,8 +115,8 @@ public class CopyProcFile { } } - @BeforeTest(alwaysRun=true) - public void createBufferedCopy() throws IOException { + @BeforeAll + public static void createBufferedCopy() throws IOException { System.out.printf("Using source file \"%s\"%n", SOURCE); try { theSize = bufferedCopy(SOURCE, BUFFERED_COPY); @@ -129,8 +132,8 @@ public class CopyProcFile { } } - @AfterTest(alwaysRun=true) - public void deleteBufferedCopy() { + @AfterAll + public static void deleteBufferedCopy() { try { Files.delete(Path.of(BUFFERED_COPY)); } catch (IOException ignore) {} @@ -148,18 +151,17 @@ public class CopyProcFile { } } - @DataProvider - static Object[][] functions() throws IOException { - List funcs = new ArrayList<>(); - funcs.add(new Object[] {new FHolder((s, d) -> copy(s, d))}); - funcs.add(new Object[] {new FHolder((s, d) -> transferToIO(s, d))}); - funcs.add(new Object[] {new FHolder((s, d) -> transferToNIO(s, d))}); - funcs.add(new Object[] {new FHolder((s, d) -> transferFrom(s, d))}); - return funcs.toArray(Object[][]::new); + static Stream functions() throws IOException { + return Stream.of + (new FHolder((s, d) -> copy(s, d)), + new FHolder((s, d) -> transferToIO(s, d)), + new FHolder((s, d) -> transferToNIO(s, d)), + new FHolder((s, d) -> transferFrom(s, d))); } - @Test(dataProvider = "functions") - public static void testCopyAndTransfer(FHolder f) throws IOException { + @ParameterizedTest + @MethodSource("functions") + public void testCopyAndTransfer(FHolder f) throws IOException { try { long size = f.apply(SOURCE, TARGET); if (size != theSize) { @@ -168,9 +170,7 @@ public class CopyProcFile { } long mismatch = Files.mismatch(Path.of(BUFFERED_COPY), Path.of(TARGET)); - if (mismatch != -1) { - throw new RuntimeException("Target does not match copy"); - } + assertEquals(-1, mismatch, "Target does not match copy"); } finally { try { Files.delete(Path.of(TARGET)); diff --git a/test/jdk/java/nio/file/Files/CreateDirectories.java b/test/jdk/java/nio/file/Files/CreateDirectories.java index fc5a5025955..9912b442cfd 100644 --- a/test/jdk/java/nio/file/Files/CreateDirectories.java +++ b/test/jdk/java/nio/file/Files/CreateDirectories.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2026, 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 @@ -26,16 +26,18 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; -import static org.testng.Assert.*; -import org.testng.SkipException; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; + +import org.junit.jupiter.api.Assumptions; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /* * @test * @bug 8032220 8293792 8307976 * @summary Test java.nio.file.Files.createDirectories method * @library .. - * @run testng CreateDirectories + * @run junit CreateDirectories */ public class CreateDirectories { @@ -46,12 +48,10 @@ public class CreateDirectories { public void testSymlinkDir() throws Exception { // create a temp dir as the "root" in which we will run our tests. final Path top = TestUtil.createTemporaryDirectory(); - if (!TestUtil.supportsSymbolicLinks(top)) { - System.out.println("Skipping tests since symbolic links isn't " + - "supported under directory "+ top); - throw new SkipException("Symbolic links not supported"); - } - System.out.println("Running tests under directory " + top.toAbsolutePath()); + Assumptions.assumeTrue(TestUtil.supportsSymbolicLinks(top), + "Skipping as symbolic links are not supported under in " + top); + System.err.println("Running tests under directory " + + top.toAbsolutePath()); final Path fooDir = Files.createDirectory(top.resolve("foo")); assertTrue(Files.isDirectory(fooDir), fooDir + " was expected to be a directory but wasn't"); diff --git a/test/jdk/java/nio/file/Files/Mismatch.java b/test/jdk/java/nio/file/Files/Mismatch.java index 8746f448ad0..0d0446ed96e 100644 --- a/test/jdk/java/nio/file/Files/Mismatch.java +++ b/test/jdk/java/nio/file/Files/Mismatch.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, 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 @@ -21,12 +21,6 @@ * questions. */ -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -36,13 +30,23 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.spi.FileSystemProvider; import java.util.Map; +import java.util.stream.Stream; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /* @test * @bug 8202285 * @build Mismatch - * @run testng Mismatch + * @run junit Mismatch * @summary Unit test for the Files.mismatch method. */ public class Mismatch { @@ -55,29 +59,28 @@ public class Mismatch { private static final Map ZIPFS_MAP = Map.of("create", "true"); // temporary test directory where all test files will be created - Path testDir; + static Path testDir; - @BeforeClass - void setup() throws IOException { + @BeforeAll + static void setup() throws IOException { testDir = Files.createTempDirectory("testMismatch"); } - @AfterClass - void cleanup() throws IOException { + @AfterAll + static void cleanup() throws IOException { // clean up files created under the test directory Files.walk(testDir).map(Path::toFile).forEach(File::delete); Files.deleteIfExists(testDir); } /* - * DataProvider for mismatch test. Provides the following fields: + * MethodSource for mismatch test. Provides the following fields: * path1 -- the path to a file * path2 -- the path to another file * expected -- expected result of the mismatch method * note -- a note about the test */ - @DataProvider(name = "testMismatch") - public Object[][] getDataForMismatch() throws IOException { + public static Stream getDataForMismatch() throws IOException { // an non-existent file Path foo = Paths.get("nonexistentfile"); @@ -147,151 +150,148 @@ public class Mismatch { Path test1065000m532500 = createASCIIFile(testDir, "test1065000m532500", size, size >> 1, '%'); Path test1065000m1064999 = createASCIIFile(testDir, "test1065000m1064999", size, 1064999, '$'); - return new Object[][]{ - // Spec Case 1: the two paths locate the same file , even if one does not exist - {foo, foo, MISMATCH_NO, "Same file, no mismatch"}, - {test1024a, test1024a, MISMATCH_NO, "Same file, no mismatch"}, + return Stream.of + (// Spec Case 1: the two paths locate the same file , even if one does not exist + Arguments.of(foo, foo, MISMATCH_NO, "Same file, no mismatch"), + Arguments.of(test1024a, test1024a, MISMATCH_NO, "Same file, no mismatch"), - // Spec Case 2: The two files are the same size, and every byte in the first file - // is identical to the corresponding byte in the second file. - {test0a, test0b, MISMATCH_NO, "Sizes == 0, no mismatch"}, - {test147a, test147b, MISMATCH_NO, "size = 147 < buffer = 8192, no mismatch"}, - {test1024a, test1024b, MISMATCH_NO, "size = 1024 < buffer = 8192, no mismatch"}, - {test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"}, - {test65536a, test65536b, MISMATCH_NO, "read 8 * full buffer, no mismatch"}, - {test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a partial buffer, no mismatch"}, + // Spec Case 2: The two files are the same size, and every byte in the first file + // is identical to the corresponding byte in the second file. + Arguments.of(test0a, test0b, MISMATCH_NO, "Sizes == 0, no mismatch"), + Arguments.of(test147a, test147b, MISMATCH_NO, "size = 147 < buffer = 8192, no mismatch"), + Arguments.of(test1024a, test1024b, MISMATCH_NO, "size = 1024 < buffer = 8192, no mismatch"), + Arguments.of(test8192a, test8192b, MISMATCH_NO, "size = 8192 = buffer = 8192, no mismatch"), + Arguments.of(test65536a, test65536b, MISMATCH_NO, "read 8 * full buffer, no mismatch"), + Arguments.of(test70025a, test70025b, MISMATCH_NO, "read 8 * full buffer plus a partial buffer, no mismatch"), - /** - * Spec Case 3: the value returned is the position of the first mismatched byte - * Impl: the impl uses a buffer 8192. The testcases below covers a range of files - * with sizes <= and > the buffer size. The last buffer is either full or partially full. - */ + /* + * Spec Case 3: the value returned is the position of the first mismatched byte + * Impl: the impl uses a buffer 8192. The testcases below covers a range of files + * with sizes <= and > the buffer size. The last buffer is either full or partially full. + */ - // edge case, one of the file sizes is zero - // also covers Spec Case 4 and 6 - {test147a, test147m0, 0, "mismatch = 0 (at the beginning)"}, - {test65536m0, test65536a, 0, "mismatch = 0 (at the beginning)"}, + // edge case, one of the file sizes is zero + // also covers Spec Case 4 and 6 + Arguments.of(test147a, test147m0, 0, "mismatch = 0 (at the beginning)"), + Arguments.of(test65536m0, test65536a, 0, "mismatch = 0 (at the beginning)"), - /** - * Compares files of equal sizes - */ - // small files - {test147a, test147m70, 70, "read one partial buffer, mismatch = 70"}, - {test147a, test147m146, 146, "read one partial buffer, mismatch = 146 (end)"}, - {test1024a, test1024m512, 512, "read one partial buffer, mismatch = 512"}, - {test1024a, test1024m1023, 1023, "read one partial buffer, mismatch = 1023 (end)"}, + /* + * Compares files of equal sizes + */ + // small files + Arguments.of(test147a, test147m70, 70, "read one partial buffer, mismatch = 70"), + Arguments.of(test147a, test147m146, 146, "read one partial buffer, mismatch = 146 (end)"), + Arguments.of(test1024a, test1024m512, 512, "read one partial buffer, mismatch = 512"), + Arguments.of(test1024a, test1024m1023, 1023, "read one partial buffer, mismatch = 1023 (end)"), - // file size >= Impl's Buffer Size - {test8192a, test8192m4096, 4096, "read one buffer, mismatch = 4096 "}, - {test8192a, test8192m8191, 8191, "read one buffer, mismatch = 8191 (at the end)"}, + // file size >= Impl's Buffer Size + Arguments.of(test8192a, test8192m4096, 4096, "read one buffer, mismatch = 4096 "), + Arguments.of(test8192a, test8192m8191, 8191, "read one buffer, mismatch = 8191 (at the end)"), - // file size = n * Impl's Buffer Size - {test65536a, test65536m32768, 32768, "read through half of the file, mismatch = 32768"}, - {test65536a, test65536m65535, 65535, "read through the whole file, mismatch = 65535 (at the end)"}, + // file size = n * Impl's Buffer Size + Arguments.of(test65536a, test65536m32768, 32768, "read through half of the file, mismatch = 32768"), + Arguments.of(test65536a, test65536m65535, 65535, "read through the whole file, mismatch = 65535 (at the end)"), - // file size = n * Impl's Buffer Size + x - {test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, - {test70025a, test70025m35000, 35000, "read about half of the file, mismatch = 35000"}, - {test70025a, test70025m70024, 70024, "read through the whole file, mismatch = 70024 (at the end)"}, + // file size = n * Impl's Buffer Size + x + Arguments.of(test70025a, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"), + Arguments.of(test70025a, test70025m35000, 35000, "read about half of the file, mismatch = 35000"), + Arguments.of(test70025a, test70025m70024, 70024, "read through the whole file, mismatch = 70024 (at the end)"), - /** - * Compares files of unequal sizes - */ - {test8192m8191, test70025m35000, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"}, - {test65536m32768, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, - {test70025m70024, test1065000m532500, 70024, "mismatch at the end of the 1st file, mismatch = 70024"}, + /* + * Compares files of unequal sizes + */ + Arguments.of(test8192m8191, test70025m35000, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"), + Arguments.of(test65536m32768, test70025m8400, 8400, "mismatch in the 2nd buffer, mismatch = 8400"), + Arguments.of(test70025m70024, test1065000m532500, 70024, "mismatch at the end of the 1st file, mismatch = 70024"), - /** - * Spec Case 4: returns the size of the smaller file (in bytes) when the files are - * different sizes and every byte of the smaller file is identical to the corresponding - * byte of the larger file. - * Impl: similar to case 3, covers a range of file sizes - */ - {test147a, test1024a, 147, "mismatch is the length of the smaller file: 147"}, - {test1024a, test8192a, 1024, "mismatch is the length of the smaller file: 1024"}, - {test1024a, test65536a, 1024, "mismatch is the length of the smaller file: 1024"}, - {test8192a, test65536a, 8192, "mismatch is the length of the smaller file: 8192"}, - {test70025a, test65536a, 65536, "mismatch is the length of the smaller file: 65536"}, - {test1048576a, test1065000m1064999, 1048576, "mismatch is the length of the smaller file: 1048576"}, + /* + * Spec Case 4: returns the size of the smaller file (in bytes) when the files are + * different sizes and every byte of the smaller file is identical to the corresponding + * byte of the larger file. + * Impl: similar to case 3, covers a range of file sizes + */ + Arguments.of(test147a, test1024a, 147, "mismatch is the length of the smaller file: 147"), + Arguments.of(test1024a, test8192a, 1024, "mismatch is the length of the smaller file: 1024"), + Arguments.of(test1024a, test65536a, 1024, "mismatch is the length of the smaller file: 1024"), + Arguments.of(test8192a, test65536a, 8192, "mismatch is the length of the smaller file: 8192"), + Arguments.of(test70025a, test65536a, 65536, "mismatch is the length of the smaller file: 65536"), + Arguments.of(test1048576a, test1065000m1064999, 1048576, "mismatch is the length of the smaller file: 1048576"), - // Spec Case 5: This method is always reflexive (for Path f , mismatch(f,f) returns -1L) - // See tests for Spec Case 1. + // Spec Case 5: This method is always reflexive (for Path f , mismatch(f,f) returns -1L) + // See tests for Spec Case 1. - // Spec Case 6: If the file system and files remain static, then this method is symmetric - // (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)). - // The following tests are selected from tests for Spec Case 3 with the order of - // file paths switched, the returned values are the same as those for Case 3: - {test147m70, test147a, 70, "read one partial buffer, mismatch = 70"}, - {test147m146, test147a, 146, "read one partial buffer, mismatch = 146 (end)"}, - {test1024m512, test1024a, 512, "read one partial buffer, mismatch = 512"}, - {test1024m1023, test1024a, 1023, "read one partial buffer, mismatch = 1023 (end)"}, + // Spec Case 6: If the file system and files remain static, then this method is symmetric + // (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)). + // The following tests are selected from tests for Spec Case 3 with the order of + // file paths switched, the returned values are the same as those for Case 3: + Arguments.of(test147m70, test147a, 70, "read one partial buffer, mismatch = 70"), + Arguments.of(test147m146, test147a, 146, "read one partial buffer, mismatch = 146 (end)"), + Arguments.of(test1024m512, test1024a, 512, "read one partial buffer, mismatch = 512"), + Arguments.of(test1024m1023, test1024a, 1023, "read one partial buffer, mismatch = 1023 (end)"), - {test70025m35000, test8192m8191, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"}, - {test70025m8400, test65536m32768, 8400, "mismatch in the 2nd buffer, mismatch = 8400"}, - {test1065000m532500, test70025m70024, 70024, "mismatch at the end of the 1st file, mismatch = 70024"}, - }; + Arguments.of(test70025m35000, test8192m8191, 8191, "mismatch at the end of the 1st file/buffer, mismatch = 8191"), + Arguments.of(test70025m8400, test65536m32768, 8400, "mismatch in the 2nd buffer, mismatch = 8400"), + Arguments.of(test1065000m532500, test70025m70024, 70024, "mismatch at the end of the 1st file, mismatch = 70024") + ); } /* - * DataProvider for mismatch tests involving ZipFS using a few test cases selected + * MethodSource for mismatch tests involving ZipFS using a few test cases selected * from those of the original mismatch tests. */ - @DataProvider(name = "testMismatchZipfs") - public Object[][] getDataForMismatchZipfs() throws IOException { + public static Stream getDataForMismatchZipfs() throws IOException { Path test1200 = createASCIIFile(testDir, "test1200", 1200, -1, ' '); Path test9500 = createASCIIFile(testDir, "test9500", 9500, -1, ' '); Path test9500m4200 = createASCIIFile(testDir, "test9500m4200", 9500, 4200, '!'); Path test80025 = createASCIIFile(testDir, "test80025", 80025, -1, ' '); Path test1028500 = createASCIIFile(testDir, "test1028500", 1028500, -1, ' '); - return new Object[][]{ - {test1200, test1200, MISMATCH_NO, "Compares the file and its copy in zip, no mismatch"}, - {test9500, test9500m4200, 4200, - "Compares a copy of test9500m4200 in zip with test9500, shall return 4200"}, - {test80025, test1028500, 80025, "mismatch is the length of the smaller file: 80025"}, - }; + return Stream.of + ( + Arguments.of(test1200, test1200, MISMATCH_NO, "Compares the file and its copy in zip, no mismatch"), + Arguments.of(test9500, test9500m4200, 4200, + "Compares a copy of test9500m4200 in zip with test9500, shall return 4200"), + Arguments.of(test80025, test1028500, 80025, + "mismatch is the length of the smaller file: 80025") + ); } /* - * DataProvider for verifying null handling. + * MethodSource for verifying null handling. */ - @DataProvider(name = "testFileNull") - public Object[][] getDataForNull() throws IOException { + public static Stream getDataForNull() throws IOException { Path test = createASCIIFile(testDir, "testNonNull", 2200, -1, ' '); - return new Object[][]{ - {(Path)null, (Path)null}, - {(Path)null, test}, - {test, (Path)null}, - }; + return Stream.of(Arguments.of((Path)null, (Path)null), + Arguments.of((Path)null, test), + Arguments.of(test, (Path)null)); } /* - * DataProvider for verifying how the mismatch method handles the situation + * MethodSource for verifying how the mismatch method handles the situation * when one or both files do not exist. */ - @DataProvider(name = "testFileNotExist") - public Object[][] getDataForFileNotExist() throws IOException { + public static Stream getDataForFileNotExist() throws IOException { Path test = createASCIIFile(testDir, "testFileNotExist", 3200, -1, ' '); - return new Object[][]{ - {Paths.get("foo"), Paths.get("bar")}, - {Paths.get("foo"), test}, - {test, Paths.get("bar")}, - }; + return Stream.of + (Arguments.of(Paths.get("foo"), Paths.get("bar")), + Arguments.of("foo", test), + Arguments.of(test, Paths.get("bar"))); } /** - * Tests the mismatch method. Refer to the dataProvider testMismatch for more - * details about the cases. + * Tests the mismatch method. Refer to the dataProvider getDataForNull for + * more details about the cases. * @param path the path to a file * @param path2 the path to another file * @param expected the expected result * @param msg the message about the test * @throws IOException if the test fails */ - @Test(dataProvider = "testMismatch", priority = 0) + @ParameterizedTest + @MethodSource("getDataForMismatch") public void testMismatch(Path path, Path path2, long expected, String msg) throws IOException { - Assert.assertEquals(Files.mismatch(path, path2), expected, msg); + assertEquals(expected, Files.mismatch(path, path2), msg); } /** @@ -302,7 +302,8 @@ public class Mismatch { * @param msg the message about the test * @throws IOException if the test fails */ - @Test(dataProvider = "testMismatchZipfs", priority = 1) + @ParameterizedTest + @MethodSource("getDataForMismatchZipfs") public void testMismatchZipfs(Path path, Path path2, long expected, String msg) throws IOException { Path zipPath = Paths.get(testDir.toString(), "TestWithFSZip.zip"); @@ -311,9 +312,9 @@ public class Mismatch { Files.copy(path, copy, REPLACE_EXISTING); if (path2 == null) { - Assert.assertEquals(Files.mismatch(copy, path), expected, msg); + assertEquals(expected, Files.mismatch(copy, path), msg); } else { - Assert.assertEquals(Files.mismatch(copy, path2), expected, msg); + assertEquals(expected, Files.mismatch(copy, path2), msg); } } } @@ -324,9 +325,11 @@ public class Mismatch { * @param path2 the path to another file * @throws NullPointerException as expected */ - @Test(dataProvider = "testFileNull", priority = 2, expectedExceptions = NullPointerException.class) + @ParameterizedTest + @MethodSource("getDataForNull") public void testMismatchNull(Path path, Path path2) throws Exception { - long result = Files.mismatch(path, path2); + assertThrows(NullPointerException.class, + () -> Files.mismatch(path, path2)); } /** @@ -335,9 +338,11 @@ public class Mismatch { * @param path2 the path to another file * @throws IOException as expected */ - @Test(dataProvider = "testFileNotExist", priority = 2, expectedExceptions = IOException.class) + @ParameterizedTest + @MethodSource("getDataForFileNotExist") public void testMismatchNotExist(Path path, Path path2) throws IOException { - long result = Files.mismatch(path, path2); + assertThrows(IOException.class, + () -> {long result = Files.mismatch(path, path2);}); } /** diff --git a/test/jdk/java/nio/file/Files/ReadWriteString.java b/test/jdk/java/nio/file/Files/ReadWriteString.java index 8b5241fa1cf..3510f41e979 100644 --- a/test/jdk/java/nio/file/Files/ReadWriteString.java +++ b/test/jdk/java/nio/file/Files/ReadWriteString.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, 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 @@ -27,6 +27,21 @@ import java.nio.charset.Charset; import java.nio.charset.CharacterCodingException; import java.nio.charset.MalformedInputException; import java.nio.charset.UnmappableCharacterException; +import java.nio.file.Files; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + import static java.nio.charset.StandardCharsets.ISO_8859_1; import static java.nio.charset.StandardCharsets.US_ASCII; import static java.nio.charset.StandardCharsets.UTF_8; @@ -36,36 +51,31 @@ import static java.nio.charset.StandardCharsets.UTF_16LE; import static java.nio.charset.StandardCharsets.UTF_32; import static java.nio.charset.StandardCharsets.UTF_32BE; import static java.nio.charset.StandardCharsets.UTF_32LE; -import java.nio.file.Files; -import java.nio.file.OpenOption; -import java.nio.file.Path; -import java.nio.file.Paths; + import static java.nio.file.StandardOpenOption.APPEND; import static java.nio.file.StandardOpenOption.CREATE; -import java.util.Arrays; -import java.util.Random; -import java.util.concurrent.Callable; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /* @test * @bug 8201276 8205058 8209576 8287541 8288589 8325590 * @build ReadWriteString PassThroughFileSystem - * @run testng ReadWriteString + * @run junit ReadWriteString * @summary Unit test for methods for Files readString and write methods. * @key randomness * @modules jdk.charsets */ -@Test(groups = "readwrite") public class ReadWriteString { // data for text files - final String TEXT_UNICODE = "\u201CHello\u201D"; - final String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n"; + final static String TEXT_UNICODE = "\u201CHello\u201D"; + final static String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n"; final static String TEXT_PERSON_CART_WHEELING = "\ud83e\udd38"; private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217"; private static final Charset WINDOWS_1252 = Charset.forName("windows-1252"); @@ -85,96 +95,85 @@ public class ReadWriteString { return baos.toByteArray(); } catch (IOException ex) { // in case it happens, fail the test - throw new RuntimeException(ex); + fail(ex); + return null; // appease the compiler } } // file used by testReadWrite, testReadString and testWriteString - private Path[] testFiles = new Path[3]; + private static Path[] testFiles = new Path[3]; /* - * DataProvider for malformed write test. Provides the following fields: + * MethodSource for malformed write test. Provides the following fields: * file path, malformed input string, charset */ - @DataProvider(name = "malformedWrite") - public Object[][] getMalformedWrite() throws IOException { + public static Stream getMalformedWrite() throws IOException { Path path = Files.createFile(Path.of("malformedWrite")); - return new Object[][]{ - {path, "\ud800", null}, //the default Charset is UTF_8 - {path, "\u00A0\u00A1", US_ASCII}, - {path, "\ud800", UTF_8}, - {path, JA_STRING, ISO_8859_1}, - {path, "\u041e", WINDOWS_1252}, // cyrillic capital letter O - {path, "\u091c", WINDOWS_31J}, // devanagari letter ja - }; + return Stream.of + (Arguments.of(path, "\ud800", null), //the default Charset is UTF_8 + Arguments.of(path, "\u00A0\u00A1", US_ASCII), + Arguments.of(path, "\ud800", UTF_8), + Arguments.of(path, JA_STRING, ISO_8859_1), + Arguments.of(path, "\u041e", WINDOWS_1252), // cyrillic capital letter O + Arguments.of(path, "\u091c", WINDOWS_31J)); // devanagari letter ja } /* - * DataProvider for illegal input test + * MethodSource for illegal input test * Writes the data in ISO8859 and reads with UTF_8, expects MalformedInputException */ - @DataProvider(name = "illegalInput") - public Object[][] getIllegalInput() throws IOException { + public static Stream getIllegalInput() throws IOException { Path path = Files.createFile(Path.of("illegalInput")); - return new Object[][]{ - {path, data, ISO_8859_1, null}, - {path, data, ISO_8859_1, UTF_8} - }; + return Stream.of(Arguments.of(path, data, ISO_8859_1, null), + Arguments.of(path, data, ISO_8859_1, UTF_8)); } /* - * DataProvider for illegal input bytes test + * MethodSource for illegal input bytes test */ - @DataProvider(name = "illegalInputBytes") - public Object[][] getIllegalInputBytes() throws IOException { - return new Object[][]{ - {new byte[] {(byte)0x00, (byte)0x20, (byte)0x00}, UTF_16, MalformedInputException.class}, - {new byte[] {-50}, UTF_16, MalformedInputException.class}, - {new byte[] {(byte)0x81}, WINDOWS_1252, UnmappableCharacterException.class}, // unused in Cp1252 - {new byte[] {(byte)0x81, (byte)0xff}, WINDOWS_31J, UnmappableCharacterException.class}, // invalid trailing byte - }; + public static Stream getIllegalInputBytes() throws IOException { + return Stream.of + (Arguments.of(new byte[] {(byte)0x00, (byte)0x20, (byte)0x00}, UTF_16, MalformedInputException.class), + Arguments.of(new byte[] {-50}, UTF_16, MalformedInputException.class), + Arguments.of(new byte[] {(byte)0x81}, WINDOWS_1252, UnmappableCharacterException.class), // unused in Cp1252 + Arguments.of(new byte[] {(byte)0x81, (byte)0xff}, WINDOWS_31J, UnmappableCharacterException.class)); // invalid trailing byte } /* - * DataProvider for writeString test + * MethodSource for writeString test * Writes the data using both the existing and new method and compares the results. */ - @DataProvider(name = "testWriteString") - public Object[][] getWriteString() { - - return new Object[][]{ - {testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, null}, - {testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, US_ASCII}, - {testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, null}, - {testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, UTF_8} - }; + public static Stream getWriteString() { + return Stream.of + (Arguments.of(testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, null), + Arguments.of(testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, US_ASCII), + Arguments.of(testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, null), + Arguments.of(testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, UTF_8)); } /* - * DataProvider for readString test + * MethodSource for readString test * Reads the file using both the existing and new method and compares the results. */ - @DataProvider(name = "testReadString") - public Object[][] getReadString() { - return new Object[][]{ - {testFiles[1], TEXT_ASCII, US_ASCII, US_ASCII}, - {testFiles[1], TEXT_ASCII, US_ASCII, UTF_8}, - {testFiles[1], TEXT_UNICODE, UTF_8, null}, - {testFiles[1], TEXT_UNICODE, UTF_8, UTF_8}, - {testFiles[1], TEXT_ASCII, US_ASCII, ISO_8859_1}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16, UTF_16}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16BE, UTF_16BE}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16LE, UTF_16LE}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32, UTF_32}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32BE, UTF_32BE}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32LE, UTF_32LE}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_1252, WINDOWS_1252}, - {testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_31J, WINDOWS_31J} - }; + public static Stream getReadString() { + return Stream.of + (Arguments.of(testFiles[1], TEXT_ASCII, US_ASCII, US_ASCII), + Arguments.of(testFiles[1], TEXT_ASCII, US_ASCII, UTF_8), + Arguments.of(testFiles[1], TEXT_UNICODE, UTF_8, null), + Arguments.of(testFiles[1], TEXT_UNICODE, UTF_8, UTF_8), + Arguments.of(testFiles[1], TEXT_ASCII, US_ASCII, ISO_8859_1), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16, UTF_16), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16BE, UTF_16BE), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16LE, UTF_16LE), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32, UTF_32), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32BE, UTF_32BE), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32LE, UTF_32LE), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_1252, WINDOWS_1252), + Arguments.of(testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_31J, WINDOWS_31J)); } - @BeforeClass - void setup() throws IOException { + @BeforeAll + static void setup() throws IOException { testFiles[0] = Files.createFile(Path.of("readWriteString")); testFiles[1] = Files.createFile(Path.of("writeString_file1")); testFiles[2] = Files.createFile(Path.of("writeString_file2")); @@ -225,7 +224,8 @@ public class ReadWriteString { * This method compares the results written by the existing write method and * the writeString method added since 11. */ - @Test(dataProvider = "testWriteString") + @ParameterizedTest + @MethodSource("getWriteString") public void testWriteString(Path path, Path path2, String text, Charset cs, Charset cs2) throws IOException { Files.write(path, text.getBytes(cs)); @@ -237,14 +237,15 @@ public class ReadWriteString { } byte[] bytes = Files.readAllBytes(path); byte[] bytes2 = Files.readAllBytes(path2); - assertTrue((Arrays.compare(bytes, bytes2) == 0), "The bytes should be the same"); + assertArrayEquals(bytes2, bytes, "The bytes should be the same"); } /** * Verifies that the readString method added since 11 behaves the same as * constructing a string from the existing readAllBytes method. */ - @Test(dataProvider = "testReadString") + @ParameterizedTest + @MethodSource("getReadString") public void testReadString(Path path, String text, Charset cs, Charset cs2) throws IOException { Files.write(path, text.getBytes(cs)); String str = new String(Files.readAllBytes(path), cs); @@ -252,7 +253,7 @@ public class ReadWriteString { // readString @since 11 String str2 = (cs2 == null) ? Files.readString(path) : Files.readString(path, cs2); - assertTrue((str.equals(str2)), "The strings should be the same"); + assertEquals(str, str2, "The strings should be the same"); } /** @@ -264,13 +265,17 @@ public class ReadWriteString { * @param cs the Charset * @throws IOException if the input is malformed */ - @Test(dataProvider = "malformedWrite", expectedExceptions = UnmappableCharacterException.class) + @ParameterizedTest + @MethodSource("getMalformedWrite") public void testMalformedWrite(Path path, String s, Charset cs) throws IOException { - if (cs == null) { - Files.writeString(path, s); - } else { - Files.writeString(path, s, cs); - } + assertThrows(UnmappableCharacterException.class, + () -> { + if (cs == null) { + Files.writeString(path, s); + } else { + Files.writeString(path, s, cs); + } + }); } /** @@ -283,15 +288,19 @@ public class ReadWriteString { * @param csRead the Charset to use for reading the file * @throws IOException when the Charset used for reading the file is incorrect */ - @Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class) + @ParameterizedTest + @MethodSource("getIllegalInput") public void testMalformedRead(Path path, byte[] data, Charset csWrite, Charset csRead) throws IOException { String temp = new String(data, csWrite); Files.writeString(path, temp, csWrite); - if (csRead == null) { - Files.readString(path); - } else { - Files.readString(path, csRead); - } + assertThrows(MalformedInputException.class, + () -> { + if (csRead == null) { + Files.readString(path); + } else { + Files.readString(path, csRead); + } + }); } /** @@ -303,20 +312,19 @@ public class ReadWriteString { * @param expected exception class * @throws IOException when the Charset used for reading the file is incorrect */ - @Test(dataProvider = "illegalInputBytes") + @ParameterizedTest + @MethodSource("getIllegalInputBytes") public void testMalformedReadBytes(byte[] data, Charset csRead, Class expected) throws IOException { Path path = Path.of("illegalInputBytes"); Files.write(path, data); try { Files.readString(path, csRead); - } catch (MalformedInputException | UnmappableCharacterException e) { - if (expected.isInstance(e)) { - // success - return; - } + } catch (MalformedInputException e) { + assertInstanceOf(MalformedInputException.class, e); + } catch (UnmappableCharacterException e) { + assertInstanceOf(UnmappableCharacterException.class, e); } - throw new RuntimeException("An instance of " + expected + " should be thrown"); } // Verify File.readString with UTF16 to confirm proper string length and contents. @@ -326,22 +334,16 @@ public class ReadWriteString { String original = "🤸"; // "\ud83e\udd38"; Files.writeString(testFiles[0], original, UTF_16); String actual = Files.readString(testFiles[0], UTF_16); - if (!actual.equals(original)) { + if (!original.equals(actual)) { System.out.printf("expected (%s), was (%s)\n", original, actual); System.out.printf("expected UTF_16 bytes: %s\n", Arrays.toString(original.getBytes(UTF_16))); System.out.printf("actual UTF_16 bytes: %s\n", Arrays.toString(actual.getBytes(UTF_16))); } - assertEquals(actual, original, "Round trip string mismatch with multi-byte encoding"); + assertEquals(original, actual, "Round trip string mismatch with multi-byte encoding"); } private void checkNullPointerException(Callable c) { - try { - c.call(); - fail("NullPointerException expected"); - } catch (NullPointerException ignore) { - } catch (Exception e) { - fail(e + " not expected"); - } + assertThrows(NullPointerException.class, () -> c.call()); } private void testReadWrite(int size, Charset cs, boolean append) throws IOException { @@ -355,14 +357,14 @@ public class ReadWriteString { } //System.out.println(result.toUri().toASCIIString()); - assertTrue(result == testFiles[0]); + assertSame(result, testFiles[0]); if (append) { if (cs == null) { Files.writeString(testFiles[0], str, APPEND); } else { Files.writeString(testFiles[0], str, cs, APPEND); } - assertTrue(Files.size(testFiles[0]) == size * 2); + assertEquals(size * 2, Files.size(testFiles[0])); } @@ -379,7 +381,7 @@ public class ReadWriteString { read = Files.readString(result, cs); } - assertTrue(read.equals(expected), "String read not the same as written"); + assertEquals(expected, read, "String read not the same as written"); } static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray(); diff --git a/test/jdk/java/nio/file/Files/SetLastModifiedTime.java b/test/jdk/java/nio/file/Files/SetLastModifiedTime.java index d06b78b2922..c4e5547fb14 100644 --- a/test/jdk/java/nio/file/Files/SetLastModifiedTime.java +++ b/test/jdk/java/nio/file/Files/SetLastModifiedTime.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2026, 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 @@ -27,19 +27,20 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; -import org.testng.annotations.Test; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @test * @bug 4313887 8062949 8191872 * @library .. - * @run testng SetLastModifiedTime + * @run junit SetLastModifiedTime * @summary Unit test for Files.setLastModifiedTime */ @@ -47,13 +48,13 @@ public class SetLastModifiedTime { static Path testDir; - @BeforeClass - void createTestDirectory() throws Exception { + @BeforeAll + static void createTestDirectory() throws Exception { testDir = TestUtil.createTemporaryDirectory(); } - @AfterClass - void removeTestDirectory() throws Exception { + @AfterAll + static void removeTestDirectory() throws Exception { TestUtil.removeAll(testDir); } @@ -65,12 +66,12 @@ public class SetLastModifiedTime { FileTime zero = FileTime.fromMillis(0L); Path result = Files.setLastModifiedTime(path, zero); - assertTrue(result == path); - assertEquals(Files.getLastModifiedTime(path), zero); + assertSame(path, result); + assertEquals(zero, Files.getLastModifiedTime(path)); result = Files.setLastModifiedTime(path, now); - assertTrue(result == path); - assertEquals(Files.getLastModifiedTime(path), now); + assertSame(path, result); + assertEquals(now, Files.getLastModifiedTime(path)); } @Test @@ -100,20 +101,14 @@ public class SetLastModifiedTime { Path path = Paths.get("foo"); FileTime zero = FileTime.fromMillis(0L); - try { - Files.setLastModifiedTime(null, zero); - assertTrue(false); - } catch (NullPointerException expected) { } + assertThrows(NullPointerException.class, + () -> Files.setLastModifiedTime(null, zero)); - try { - Files.setLastModifiedTime(path, null); - assertTrue(false); - } catch (NullPointerException expected) { } + assertThrows(NullPointerException.class, + () -> Files.setLastModifiedTime(path, null)); - try { - Files.setLastModifiedTime(null, null); - assertTrue(false); - } catch (NullPointerException expected) { } + assertThrows(NullPointerException.class, + () -> Files.setLastModifiedTime(null, null)); } @Test @@ -127,7 +122,7 @@ public class SetLastModifiedTime { long nioTime = Files.getLastModifiedTime(path).toMillis(); assertTrue(ioTime == timeMillis || ioTime == 1000*(timeMillis/1000), "File.lastModified() not in {time, 1000*(time/1000)}"); - assertEquals(nioTime, ioTime, + assertEquals(ioTime, nioTime, "File.lastModified() != Files.getLastModifiedTime().toMillis()"); } } diff --git a/test/jdk/java/nio/file/Files/StreamTest.java b/test/jdk/java/nio/file/Files/StreamTest.java index 0c93a622322..e9e6195ec0a 100644 --- a/test/jdk/java/nio/file/Files/StreamTest.java +++ b/test/jdk/java/nio/file/Files/StreamTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2026, 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 @@ -25,7 +25,7 @@ * @bug 8006884 8019526 8132539 * @library .. * @build PassThroughFileSystem FaultyFileSystem - * @run testng StreamTest + * @run junit StreamTest * @summary Unit test for java.nio.file.Files methods that return a Stream */ @@ -35,9 +35,9 @@ import java.nio.charset.Charset; import java.nio.charset.MalformedInputException; import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.FileSystemLoopException; import java.nio.file.FileVisitOption; -import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; @@ -51,14 +51,23 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.Callable; import java.util.function.BiPredicate; -import java.util.stream.Stream; import java.util.stream.Collectors; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import static org.testng.Assert.*; +import java.util.stream.Stream; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -@Test(groups = "unit") public class StreamTest { /** * Default test folder @@ -77,8 +86,8 @@ public class StreamTest { static Path[] all; static Path[] allFollowLinks; - @BeforeClass - void setupTestFolder() throws IOException { + @BeforeAll + static void setupTestFolder() throws IOException { testFolder = TestUtil.createTemporaryDirectory(); supportsSymbolicLinks = TestUtil.supportsSymbolicLinks(testFolder); TreeSet set = new TreeSet<>(); @@ -133,53 +142,57 @@ public class StreamTest { allFollowLinks = set.toArray(new Path[0]); } - @AfterClass - void cleanupTestFolder() throws IOException { + @AfterAll + static void cleanupTestFolder() throws IOException { TestUtil.removeAll(testFolder); } + @Test public void testBasic() { try (Stream s = Files.list(testFolder)) { Object[] actual = s.sorted().toArray(); - assertEquals(actual, level1); + assertArrayEquals(level1, actual); } catch (IOException ioe) { fail("Unexpected IOException"); } try (Stream s = Files.list(testFolder.resolve("empty"))) { int count = s.mapToInt(p -> 1).reduce(0, Integer::sum); - assertEquals(count, 0, "Expect empty stream."); + assertEquals(0, count, "Expect empty stream."); } catch (IOException ioe) { fail("Unexpected IOException"); } } + @Test public void testWalk() { try (Stream s = Files.walk(testFolder)) { Object[] actual = s.sorted().toArray(); - assertEquals(actual, all); + assertArrayEquals(all, actual); } catch (IOException ioe) { fail("Unexpected IOException"); } } + @Test public void testWalkOneLevel() { try (Stream s = Files.walk(testFolder, 1)) { Object[] actual = s.filter(path -> ! path.equals(testFolder)) .sorted() .toArray(); - assertEquals(actual, level1); + assertArrayEquals(level1, actual); } catch (IOException ioe) { fail("Unexpected IOException"); } } + @Test public void testWalkFollowLink() { // If link is not supported, the directory structure won't have link. // We still want to test the behavior with FOLLOW_LINKS option. try (Stream s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) { Object[] actual = s.sorted().toArray(); - assertEquals(actual, allFollowLinks); + assertArrayEquals(allFollowLinks, actual); } catch (IOException ioe) { fail("Unexpected IOException"); } @@ -211,6 +224,7 @@ public class StreamTest { } } + @Test public void testWalkFollowLinkLoop() { if (!supportsSymbolicLinks) { return; @@ -257,6 +271,7 @@ public class StreamTest { dir2.resolve(Paths.get("lnDir", "lnDir2"))); validateFileSystemLoopException(linkdir, linkdir.resolve(Paths.get("lnDir2", "lnDir"))); + Files.delete(cause); } catch(IOException ioe) { fail("Unexpected IOException " + ioe); } @@ -280,44 +295,46 @@ public class StreamTest { } } + @Test public void testFind() throws IOException { PathBiPredicate pred = new PathBiPredicate((path, attrs) -> true); try (Stream s = Files.find(testFolder, Integer.MAX_VALUE, pred)) { Set result = s.collect(Collectors.toCollection(TreeSet::new)); - assertEquals(pred.visited(), all); - assertEquals(result.toArray(new Path[0]), pred.visited()); + assertArrayEquals(all, pred.visited()); + assertArrayEquals(pred.visited(), result.toArray(new Path[0])); } pred = new PathBiPredicate((path, attrs) -> attrs.isSymbolicLink()); try (Stream s = Files.find(testFolder, Integer.MAX_VALUE, pred)) { s.forEach(path -> assertTrue(Files.isSymbolicLink(path))); - assertEquals(pred.visited(), all); + assertArrayEquals(all, pred.visited()); } pred = new PathBiPredicate((path, attrs) -> path.getFileName().toString().startsWith("e")); try (Stream s = Files.find(testFolder, Integer.MAX_VALUE, pred)) { s.forEach(path -> assertEquals(path.getFileName().toString(), "empty")); - assertEquals(pred.visited(), all); + assertArrayEquals(all, pred.visited()); } pred = new PathBiPredicate((path, attrs) -> path.getFileName().toString().startsWith("l") && attrs.isRegularFile()); try (Stream s = Files.find(testFolder, Integer.MAX_VALUE, pred)) { s.forEach(path -> fail("Expect empty stream")); - assertEquals(pred.visited(), all); + assertArrayEquals(all, pred.visited()); } } // Test borrowed from BytesAndLines + @Test public void testLines() throws IOException { final Charset US_ASCII = Charset.forName("US-ASCII"); Path tmpfile = Files.createTempFile("blah", "txt"); try { // zero lines - assertTrue(Files.size(tmpfile) == 0, "File should be empty"); + assertEquals(0, Files.size(tmpfile), "File should be empty"); try (Stream s = Files.lines(tmpfile)) { checkLines(s, Collections.emptyList()); } @@ -367,31 +384,21 @@ public class StreamTest { private void checkLines(Stream s, List expected) { List lines = s.collect(Collectors.toList()); - assertTrue(lines.size() == expected.size(), "Unexpected number of lines"); + assertEquals(expected.size(), lines.size(), "Unexpected number of lines"); assertTrue(lines.equals(expected), "Unexpected content"); } private void checkMalformedInputException(Stream s) { - try { - List lines = s.collect(Collectors.toList()); - fail("UncheckedIOException expected"); - } catch (UncheckedIOException ex) { - IOException cause = ex.getCause(); - assertTrue(cause instanceof MalformedInputException, - "MalformedInputException expected"); - } + assertThrows(UncheckedIOException.class, + () -> s.collect(Collectors.toList()), + "MalformedInputException expected"); } private void checkNullPointerException(Callable c) { - try { - c.call(); - fail("NullPointerException expected"); - } catch (NullPointerException ignore) { - } catch (Exception e) { - fail(e + " not expected"); - } + assertThrows(NullPointerException.class, () -> c.call()); } + @Test public void testDirectoryIteratorException() throws IOException { Path dir = testFolder.resolve("dir2"); Path trigger = dir.resolve("DirectoryIteratorException"); @@ -404,22 +411,21 @@ public class StreamTest { Path fakeRoot = fs.getRoot(); try { try (Stream s = Files.list(fakeRoot)) { - s.forEach(path -> assertEquals(path.getFileName().toString(), "DirectoryIteratorException")); + s.forEach(path -> assertEquals("DirectoryIteratorException", path.getFileName().toString())); } } catch (UncheckedIOException uioe) { fail("Unexpected exception."); } fsp.setFaultyMode(true); - try { - try (DirectoryStream ds = Files.newDirectoryStream(fakeRoot)) { - Iterator itor = ds.iterator(); - while (itor.hasNext()) { - itor.next(); - } - } - fail("Shoule throw DirectoryIteratorException"); - } catch (DirectoryIteratorException die) { + try (DirectoryStream ds = Files.newDirectoryStream(fakeRoot)) { + Iterator itor = ds.iterator(); + assertThrows(DirectoryIteratorException.class, + () -> { + while (itor.hasNext()) { + itor.next(); + } + }); } try { @@ -427,7 +433,8 @@ public class StreamTest { s.forEach(path -> fail("should not get here")); } } catch (UncheckedIOException uioe) { - assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); + assertInstanceOf(FaultyFileSystem.FaultyException.class, + uioe.getCause()); } catch (DirectoryIteratorException die) { fail("Should have been converted into UncheckedIOException."); } @@ -440,6 +447,7 @@ public class StreamTest { } } + @Test public void testUncheckedIOException() throws IOException { Path triggerFile = testFolder.resolve(Paths.get("dir2", "IOException")); Files.createFile(triggerFile); @@ -454,21 +462,22 @@ public class StreamTest { Path fakeRoot = fs.getRoot(); try (Stream s = Files.list(fakeRoot.resolve("dir2"))) { // only one file - s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException")); + s.forEach(path -> assertEquals("IOException", path.getFileName().toString())); } try (Stream s = Files.walk(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()) .toArray(String[]::new); // ordered as depth-first - assertEquals(result, new String[] { "empty", "IOException", "file"}); + assertArrayEquals(new String[] { "empty", "IOException", "file"}, result); } fsp.setFaultyMode(true); try (Stream s = Files.list(fakeRoot.resolve("dir2"))) { s.forEach(path -> fail("should have caused exception")); } catch (UncheckedIOException uioe) { - assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); + assertInstanceOf(FaultyFileSystem.FaultyException.class, + uioe.getCause()); } try (Stream s = Files.walk(fakeRoot.resolve("empty"))) { @@ -476,7 +485,8 @@ public class StreamTest { .toArray(String[]::new); fail("should not reach here due to IOException"); } catch (UncheckedIOException uioe) { - assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); + assertInstanceOf(FaultyFileSystem.FaultyException.class, + uioe.getCause()); } try (Stream s = Files.walk( @@ -486,7 +496,7 @@ public class StreamTest { .toArray(String[]::new); fail("should not reach here due to IOException"); } catch (IOException ioe) { - assertTrue(ioe instanceof FaultyFileSystem.FaultyException); + assertInstanceOf(FaultyFileSystem.FaultyException.class, ioe); } catch (UncheckedIOException ex) { fail("Top level should be repored as is"); } @@ -500,52 +510,47 @@ public class StreamTest { } } + @Test public void testConstructException() { try (Stream s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) { - s.forEach(l -> fail("File is not even exist!")); + s.forEach(l -> fail("File does not even exist!")); } catch (IOException ioe) { - assertTrue(ioe instanceof NoSuchFileException); + assertInstanceOf(NoSuchFileException.class, ioe); } } + @Test public void testClosedStream() throws IOException { try (Stream s = Files.list(testFolder)) { s.close(); - Object[] actual = s.sorted().toArray(); - fail("Operate on closed stream should throw IllegalStateException"); - } catch (IllegalStateException ex) { - // expected + assertThrows(IllegalStateException.class, + () -> s.sorted().toArray()); } try (Stream s = Files.walk(testFolder)) { s.close(); - Object[] actual = s.sorted().toArray(); - fail("Operate on closed stream should throw IllegalStateException"); - } catch (IllegalStateException ex) { - // expected + assertThrows(IllegalStateException.class, + () -> s.sorted().toArray()); } try (Stream s = Files.find(testFolder, Integer.MAX_VALUE, (p, attr) -> true)) { s.close(); - Object[] actual = s.sorted().toArray(); - fail("Operate on closed stream should throw IllegalStateException"); - } catch (IllegalStateException ex) { - // expected + assertThrows(IllegalStateException.class, + () -> s.sorted().toArray()); } } + @Test + @EnabledOnOs(OS.LINUX) public void testProcFile() throws IOException { - if (System.getProperty("os.name").equals("Linux")) { - Path path = Paths.get("/proc/cpuinfo"); - if (Files.exists(path)) { - String NEW_LINE = System.getProperty("line.separator"); - String s = - Files.lines(path).collect(Collectors.joining(NEW_LINE)); - if (s.length() == 0) { - fail("Files.lines(\"" + path + "\") returns no data"); - } - } + Path path = Paths.get("/proc/cpuinfo"); + if (Files.exists(path)) { + String NEW_LINE = System.getProperty("line.separator"); + String s = + Files.lines(path).collect(Collectors.joining(NEW_LINE)); + assertNotEquals(0, s.length(), + "Files.lines(\"" + path + "\") returns no data"); } } } diff --git a/test/jdk/java/nio/file/Files/SubstDrive.java b/test/jdk/java/nio/file/Files/SubstDrive.java index 94fdeadcef3..fd21b9f007e 100644 --- a/test/jdk/java/nio/file/Files/SubstDrive.java +++ b/test/jdk/java/nio/file/Files/SubstDrive.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2020 Microsoft Corporation. All rights reserved. - * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, 2026, 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 @@ -23,32 +23,35 @@ * */ +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.*; - -import static org.testng.Assert.*; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.AfterClass; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; -import org.testng.SkipException; import jdk.test.lib.process.ProcessTools; import jdk.test.lib.process.OutputAnalyzer; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + /* @test * @summary Test Files' public APIs with drives created using the subst command on Windows. * @requires (os.family == "windows") * @library /test/lib .. * @build SubstDrive - * @run testng SubstDrive + * @run junit SubstDrive */ public class SubstDrive { @@ -62,26 +65,24 @@ public class SubstDrive { * deleted when the test finishes. * + Find a drive that is available for use with subst. */ - @BeforeClass - public void setup() throws IOException { + @BeforeAll + public static void setup() throws IOException { TEST_TEMP_DIRECTORY = Files.createTempDirectory("tmp"); - System.out.printf("Test directory is at %s\n", TEST_TEMP_DIRECTORY); + System.err.printf("Test directory is at %s\n", TEST_TEMP_DIRECTORY); Optional substDrive = findAvailableDrive(TEST_TEMP_DIRECTORY); - if (!substDrive.isPresent()) { - throw new SkipException( - "Could not find any available drive to use with subst, skipping the tests"); - } + Assumptions.assumeTrue(substDrive.isPresent(), + "Could not find any available drive to use with subst"); SUBST_DRIVE = substDrive.get(); - System.out.printf("Using drive %s\n with subst", SUBST_DRIVE); + System.err.printf("Using drive %s\n with subst", SUBST_DRIVE); } /** * Delete the root temporary directory together with all of its contents * when all tests finish. */ - @AfterClass - public void removeRootTempDirectory() throws IOException { + @AfterAll + public static void removeRootTempDirectory() throws IOException { TestUtil.removeAll(TEST_TEMP_DIRECTORY); } @@ -90,7 +91,7 @@ public class SubstDrive { * unmap the drive after every test so that subsequent ones can reuse * the drive. */ - @AfterMethod + @AfterEach public void deleteSubstDrive() throws IOException { Stream substitutedDrives = substFindMappedDrives(); // Only delete `SUBST_DRIVE` if it is currently being substituted @@ -114,7 +115,7 @@ public class SubstDrive { assertTrue(Files.exists(p)); Files.writeString(p, fileContents); - assertEquals(Files.readString(p), fileContents); + assertEquals(fileContents, Files.readString(p)); } /** @@ -193,7 +194,7 @@ public class SubstDrive { Map attr1 = Files.readAttributes(SUBST_DRIVE, "*"); Map attr2 = Files.readAttributes(tempDirectory, "*"); - assertEquals(attr1, attr2); + assertEquals(attr2, attr1); } /** @@ -287,19 +288,19 @@ public class SubstDrive { Path tempFile = Path.of(SUBST_DRIVE.toString(), "test.txt"); String contents = "Hello world!"; Files.writeString(tempFile, contents); - assertEquals(Files.readString(tempFile), contents); + assertEquals(contents, Files.readString(tempFile)); Path link = Path.of(SUBST_DRIVE.toString(), "link"); Files.createSymbolicLink(link, tempFile); - assertEquals(Files.readString(link), contents); - assertEquals(Files.isExecutable(link), Files.isExecutable(tempFile)); - assertEquals(Files.isReadable(link), Files.isReadable(tempFile)); - assertEquals(Files.isDirectory(link), Files.isDirectory(tempFile)); - assertEquals(Files.isHidden(link), Files.isHidden(tempFile)); - assertEquals(Files.isRegularFile(link), Files.isRegularFile(tempFile)); - assertEquals(Files.isWritable(link), Files.isWritable(tempFile)); - assertEquals(Files.getOwner(link), Files.getOwner(tempFile)); + assertEquals(contents, Files.readString(link)); + assertEquals(Files.isExecutable(tempFile), Files.isExecutable(link)); + assertEquals(Files.isReadable(tempFile), Files.isReadable(link)); + assertEquals(Files.isDirectory(tempFile), Files.isDirectory(link)); + assertEquals(Files.isHidden(tempFile), Files.isHidden(link)); + assertEquals(Files.isRegularFile(tempFile), Files.isRegularFile(link)); + assertEquals(Files.isWritable(tempFile), Files.isWritable(link)); + assertEquals(Files.getOwner(tempFile), Files.getOwner(link)); } /** @@ -319,16 +320,15 @@ public class SubstDrive { substCreate(SUBST_DRIVE, tempLink); - assertEquals( - Files.readAttributes(SUBST_DRIVE, "*"), - Files.readAttributes(tempDirectory, "*")); + assertTrue(Files.readAttributes(tempDirectory, "*") + .equals(Files.readAttributes(SUBST_DRIVE, "*"))); assertTrue(Files.isWritable(SUBST_DRIVE)); Path tempFile = Files.createTempFile(SUBST_DRIVE, "prefix", "suffix"); String contents = "Hello world!"; Files.writeString(tempFile, contents); - assertEquals(Files.readString(tempFile), contents); + assertEquals(contents, Files.readString(tempFile)); Path tempDirectory2 = Files.createTempDirectory(TEST_TEMP_DIRECTORY, "tmp"); Path copy = Path.of(tempDirectory2.toString(), "copied"); @@ -341,7 +341,7 @@ public class SubstDrive { Files.move(tempFile, cut); assertTrue(Files.notExists(tempFile)); assertTrue(Files.exists(cut)); - assertEquals(Files.readString(cut), contents); + assertEquals(contents, Files.readString(cut)); } /** @@ -361,9 +361,8 @@ public class SubstDrive { substCreate(SUBST_DRIVE, tempLink); - assertEquals( - Files.readAttributes(SUBST_DRIVE, "*"), - Files.readAttributes(tempDirectory, "*")); + assertTrue(Files.readAttributes(tempDirectory, "*") + .equals(Files.readAttributes(SUBST_DRIVE, "*"))); assertTrue(Files.isWritable(SUBST_DRIVE)); } @@ -372,7 +371,7 @@ public class SubstDrive { * Run a command and optionally prints stdout contents to * `customOutputStream`. */ - private void runCmd(ProcessBuilder pb, PrintStream customOutputStream) { + private static void runCmd(ProcessBuilder pb, PrintStream customOutputStream) { try { PrintStream ps = customOutputStream != null ? customOutputStream : @@ -383,8 +382,8 @@ public class SubstDrive { int exitCode = outputAnalyzer.getExitValue(); assertEquals( - exitCode /* actual value */, 0 /* expected value */, + exitCode /* actual value */, String.format( "Command `%s` failed with exit code %d", pb.command(), @@ -402,7 +401,7 @@ public class SubstDrive { * For reference, see: * https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/subst */ - private void substCreate(Path drive, Path path) { + private static void substCreate(Path drive, Path path) { runCmd( new ProcessBuilder( "cmd", "/c", "subst", drive.toString(), path.toString()), @@ -414,7 +413,7 @@ public class SubstDrive { * For reference, see: * https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/subst */ - private void substDelete(Path drive) throws IOException { + private static void substDelete(Path drive) throws IOException { runCmd( new ProcessBuilder( "cmd", "/c", "subst", drive.toString(), "/D"), @@ -454,7 +453,7 @@ public class SubstDrive { * subst can fail if the drive to be mapped already exists. The method returns * a drive that is available. */ - private Optional findAvailableDrive(Path tempDirectory) { + private static Optional findAvailableDrive(Path tempDirectory) { for (char letter = 'Z'; letter >= 'A'; letter--) { try { Path p = Path.of(letter + ":"); diff --git a/test/jdk/java/nio/file/Files/walkFileTree/FindTest.java b/test/jdk/java/nio/file/Files/walkFileTree/FindTest.java index 9fb16a7b14c..beacee44bbb 100644 --- a/test/jdk/java/nio/file/Files/walkFileTree/FindTest.java +++ b/test/jdk/java/nio/file/Files/walkFileTree/FindTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2026, 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 @@ -34,15 +34,15 @@ * jdk.test.lib.Platform * jdk.test.lib.process.* * CreateFileTree - * @run testng/othervm -Djava.io.tmpdir=. FindTest + * @run junit/othervm -Djava.io.tmpdir=. FindTest */ import java.io.IOException; +import java.nio.file.Files; import java.nio.file.FileSystemLoopException; import java.nio.file.FileVisitOption; -import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; -import java.nio.file.Files; +import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; @@ -56,11 +56,11 @@ import java.util.stream.Collectors; import jdk.test.lib.process.OutputAnalyzer; import jdk.test.lib.process.ProcessTools; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class FindTest { @@ -69,7 +69,7 @@ public class FindTest { private static Path top; private static String TOP; - @BeforeClass + @BeforeAll public static void createFileTree() throws Exception { top = CreateFileTree.create(); TOP = top.toAbsolutePath().toString(); @@ -107,7 +107,7 @@ public class FindTest { throws IOException { List expectedList = Arrays.asList(expected.getStdout() .split(System.lineSeparator())); - assertEquals(actual.size(), expectedList.size()); + assertEquals(expectedList.size(), actual.size()); assertTrue(actual.removeAll(expectedList)); } diff --git a/test/jdk/java/nio/file/spi/TestDelegation.java b/test/jdk/java/nio/file/spi/TestDelegation.java index 0b9bc9de7f7..f09b636c156 100644 --- a/test/jdk/java/nio/file/spi/TestDelegation.java +++ b/test/jdk/java/nio/file/spi/TestDelegation.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2026, 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 @@ -21,11 +21,6 @@ * questions. */ -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import java.io.IOException; import java.net.URI; import java.nio.file.*; @@ -34,26 +29,34 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Stream; -import static org.testng.AssertJUnit.assertEquals; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @test * @summary Verifies that a FileSystemProvider's implementation of the exists * and readAttributesIfExists methods are invoked * @compile testfsp/testfsp/TestProvider.java - * @run testng TestDelegation + * @run junit TestDelegation */ public class TestDelegation { // Non-existent Path to be used by the test - private Path nonExistentFile; + private static Path nonExistentFile; // Path to Temp directory used by the test - private Path tempDirectory; + private static Path tempDirectory; // Valid file Path used by the test - private Path fileThatExists; + private static Path fileThatExists; // The FileSystemProvider used by the test - private MyProvider myProvider; + private static MyProvider myProvider; /** * Create the FileSystemProvider, the FileSystem and @@ -61,8 +64,8 @@ public class TestDelegation { * * @throws IOException if an error occurs */ - @BeforeClass - public void setup() throws IOException { + @BeforeAll + public static void setup() throws IOException { myProvider = new MyProvider(); FileSystem fs = myProvider.getFileSystem(URI.create("/")); // Path to Current Working Directory @@ -73,7 +76,7 @@ public class TestDelegation { } /** - * DataProvider that is used to test Files::exists. The DataProvider's + * MethodSource that is used to test Files::exists. The Arguments' * elements are: *
    *
  • Path to validate
  • @@ -81,17 +84,14 @@ public class TestDelegation { *
* @return The test parameter data */ - @DataProvider - private Object[][] testExists() { - return new Object[][]{ - {tempDirectory, true}, - {fileThatExists, true}, - {nonExistentFile, false} - }; + private static Stream testExists() { + return Stream.of(Arguments.of(tempDirectory, true), + Arguments.of(fileThatExists, true), + Arguments.of(nonExistentFile, false)); } /** - * DataProvider that is used to test Files::isDirectory. The DataProvider's + * MethodSource that is used to test Files::isDirectory. The Arguments' * elements are: *
    *
  • Path to validate
  • @@ -99,16 +99,13 @@ public class TestDelegation { *
* @return The test parameter data */ - @DataProvider - private Object[][] testIsDirectory() { - return new Object[][]{ - {tempDirectory, true}, - {fileThatExists, false}, - {nonExistentFile, false} - }; + private static Stream testIsDirectory() { + return Stream.of(Arguments.of(tempDirectory, true), + Arguments.of(fileThatExists, false), + Arguments.of(nonExistentFile, false)); } /** - * DataProvider that is used to test Files::isRegularFile. The DataProvider's + * MethodSource that is used to test Files::isRegularFile. The MethodSource's * elements are: *
    *
  • Path to validate
  • @@ -116,19 +113,16 @@ public class TestDelegation { *
* @return The test parameter data */ - @DataProvider - private Object[][] testIsRegularFile() { - return new Object[][]{ - {tempDirectory, false}, - {fileThatExists, true}, - {nonExistentFile, false} - }; + private static Stream testIsRegularFile() { + return Stream.of(Arguments.of(tempDirectory, false), + Arguments.of(fileThatExists, true), + Arguments.of(nonExistentFile, false)); } /** * Clear our Map prior to each test run */ - @BeforeMethod + @BeforeEach public void resetParams() { myProvider.resetCalls(); } @@ -140,9 +134,10 @@ public class TestDelegation { * @param p the path to the file to test * @param exists does the path exist */ - @Test(dataProvider = "testExists") + @ParameterizedTest + @MethodSource("testExists") public void testExists(Path p, boolean exists) { - assertEquals(Files.exists(p), exists); + assertEquals(exists, Files.exists(p)); // We should only have called exists once assertEquals(1, myProvider.findCall("exists").size()); assertEquals(0, myProvider.findCall("readAttributesIfExists").size()); @@ -155,9 +150,10 @@ public class TestDelegation { * @param p the path to the file to test * @param isDir is the path a directory */ - @Test(dataProvider = "testIsDirectory") + @ParameterizedTest + @MethodSource("testIsDirectory") public void testIsDirectory(Path p, boolean isDir) { - assertEquals(Files.isDirectory(p), isDir); + assertEquals(isDir, Files.isDirectory(p)); // We should only have called readAttributesIfExists once assertEquals(0, myProvider.findCall("exists").size()); assertEquals(1, myProvider.findCall("readAttributesIfExists").size()); @@ -170,9 +166,10 @@ public class TestDelegation { * @param p the path to the file to test * @param isFile is the path a regular file */ - @Test(dataProvider = "testIsRegularFile") + @ParameterizedTest + @MethodSource("testIsRegularFile") public void testIsRegularFile(Path p, boolean isFile) { - assertEquals(Files.isRegularFile(p), isFile); + assertEquals(isFile, Files.isRegularFile(p)); // We should only have called readAttributesIfExists once assertEquals(0, myProvider.findCall("exists").size()); assertEquals(1, myProvider.findCall("readAttributesIfExists").size()); From 9dc3f488b4da333f436fcdddb92b34ec9dbbb925 Mon Sep 17 00:00:00 2001 From: Mohamed Issa Date: Mon, 16 Mar 2026 19:21:05 +0000 Subject: [PATCH 02/22] 8380079: Add separate flag for platforms on which copy and clear operations are faster with AVX3Threshold set to 0 Reviewed-by: kvn, asmehra, sviswanathan --- src/hotspot/cpu/x86/globals_x86.hpp | 15 ++++++++++-- src/hotspot/cpu/x86/macroAssembler_x86.cpp | 13 +++++------ src/hotspot/cpu/x86/stubGenerator_x86_64.hpp | 4 ++-- .../cpu/x86/stubGenerator_x86_64_adler.cpp | 2 +- .../x86/stubGenerator_x86_64_arraycopy.cpp | 20 ++++++++-------- src/hotspot/cpu/x86/vm_version_x86.cpp | 23 ++++++++++--------- src/hotspot/cpu/x86/vm_version_x86.hpp | 2 -- .../flags/jvmFlagConstraintsCompiler.cpp | 11 +++++++++ .../flags/jvmFlagConstraintsCompiler.hpp | 1 + 9 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/hotspot/cpu/x86/globals_x86.hpp b/src/hotspot/cpu/x86/globals_x86.hpp index 4f5b6d31e75..084793dc262 100644 --- a/src/hotspot/cpu/x86/globals_x86.hpp +++ b/src/hotspot/cpu/x86/globals_x86.hpp @@ -168,16 +168,27 @@ define_pd_global(intx, InitArrayShortSize, 8*BytesPerLong); "Perform Ecore Optimization") \ \ /* Minimum array size in bytes to use AVX512 intrinsics */ \ - /* for copy, inflate and fill which don't bail out early based on any */ \ + /* for inflate and fill which don't bail out early based on any */ \ /* condition. When this value is set to zero compare operations like */ \ /* compare, vectorizedMismatch, compress can also use AVX512 intrinsics.*/\ product(int, AVX3Threshold, 4096, DIAGNOSTIC, \ "Minimum array size in bytes to use AVX512 intrinsics" \ - "for copy, inflate and fill. When this value is set as zero" \ + "for inflate and fill. When this value is set as zero" \ "compare operations can also use AVX512 intrinsics.") \ range(0, max_jint) \ constraint(AVX3ThresholdConstraintFunc,AfterErgo) \ \ + /* Minimum array size in bytes to use AVX512 intrinsics */ \ + /* for copy and fill which don't bail out early based on any */ \ + /* condition. When this value is set to zero clear operations that */ \ + /* work on memory blocks can also use AVX512 intrinsics. */ \ + product(int, CopyAVX3Threshold, 4096, DIAGNOSTIC, \ + "Minimum array size in bytes to use AVX512 intrinsics" \ + "for copy and fill. When this value is set as zero" \ + "clear operations can also use AVX512 intrinsics.") \ + range(0, max_jint) \ + constraint(CopyAVX3ThresholdConstraintFunc,AfterErgo) \ + \ product(bool, IntelJccErratumMitigation, true, DIAGNOSTIC, \ "Turn off JVM mitigations related to Intel micro code " \ "mitigations for the Intel JCC erratum") \ diff --git a/src/hotspot/cpu/x86/macroAssembler_x86.cpp b/src/hotspot/cpu/x86/macroAssembler_x86.cpp index 2d46a50d426..1d77be26bd9 100644 --- a/src/hotspot/cpu/x86/macroAssembler_x86.cpp +++ b/src/hotspot/cpu/x86/macroAssembler_x86.cpp @@ -5820,7 +5820,7 @@ void MacroAssembler::xmm_clear_mem(Register base, Register cnt, Register rtmp, X // cnt - number of qwords (8-byte words). // base - start address, qword aligned. Label L_zero_64_bytes, L_loop, L_sloop, L_tail, L_end; - bool use64byteVector = (MaxVectorSize == 64) && (VM_Version::avx3_threshold() == 0); + bool use64byteVector = (MaxVectorSize == 64) && (CopyAVX3Threshold == 0); if (use64byteVector) { vpxor(xtmp, xtmp, xtmp, AVX_512bit); } else if (MaxVectorSize >= 32) { @@ -5884,7 +5884,7 @@ void MacroAssembler::xmm_clear_mem(Register base, Register cnt, Register rtmp, X // Clearing constant sized memory using YMM/ZMM registers. void MacroAssembler::clear_mem(Register base, int cnt, Register rtmp, XMMRegister xtmp, KRegister mask) { assert(UseAVX > 2 && VM_Version::supports_avx512vl(), ""); - bool use64byteVector = (MaxVectorSize > 32) && (VM_Version::avx3_threshold() == 0); + bool use64byteVector = (MaxVectorSize > 32) && (CopyAVX3Threshold == 0); int vector64_count = (cnt & (~0x7)) >> 3; cnt = cnt & 0x7; @@ -6109,8 +6109,8 @@ void MacroAssembler::generate_fill(BasicType t, bool aligned, // Fill 64-byte chunks Label L_fill_64_bytes_loop_avx3, L_check_fill_64_bytes_avx2; - // If number of bytes to fill < VM_Version::avx3_threshold(), perform fill using AVX2 - cmpptr(count, VM_Version::avx3_threshold()); + // If number of bytes to fill < CopyAVX3Threshold, perform fill using AVX2 + cmpptr(count, CopyAVX3Threshold); jccb(Assembler::below, L_check_fill_64_bytes_avx2); vpbroadcastd(xtmp, xtmp, Assembler::AVX_512bit); @@ -9483,7 +9483,6 @@ void MacroAssembler::generate_fill_avx3(BasicType type, Register to, Register va Label L_fill_zmm_sequence; int shift = -1; - int avx3threshold = VM_Version::avx3_threshold(); switch(type) { case T_BYTE: shift = 0; break; @@ -9499,10 +9498,10 @@ void MacroAssembler::generate_fill_avx3(BasicType type, Register to, Register va fatal("Unhandled type: %s\n", type2name(type)); } - if ((avx3threshold != 0) || (MaxVectorSize == 32)) { + if ((CopyAVX3Threshold != 0) || (MaxVectorSize == 32)) { if (MaxVectorSize == 64) { - cmpq(count, avx3threshold >> shift); + cmpq(count, CopyAVX3Threshold >> shift); jcc(Assembler::greater, L_fill_zmm_sequence); } diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp index 64b56442c90..332add6dcd4 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp @@ -166,12 +166,12 @@ class StubGenerator: public StubCodeGenerator { // - If target supports AVX3 features (BW+VL+F) then implementation uses 32 byte vectors (YMMs) // for both special cases (various small block sizes) and aligned copy loop. This is the // default configuration. - // - If copy length is above AVX3Threshold, then implementation use 64 byte vectors (ZMMs) + // - If copy length is above CopyAVX3Threshold, then implementation use 64 byte vectors (ZMMs) // for main copy loop (and subsequent tail) since bulk of the cycles will be consumed in it. // - If user forces MaxVectorSize=32 then above 4096 bytes its seen that REP MOVs shows a // better performance for disjoint copies. For conjoint/backward copy vector based // copy performs better. - // - If user sets AVX3Threshold=0, then special cases for small blocks sizes operate over + // - If user sets CopyAVX3Threshold=0, then special cases for small blocks sizes operate over // 64 byte vector registers (ZMMs). address generate_disjoint_copy_avx3_masked(StubId stub_id, address* entry); diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64_adler.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64_adler.cpp index 2799997a761..1d3e7afde1d 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64_adler.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64_adler.cpp @@ -144,7 +144,7 @@ address StubGenerator::generate_updateBytesAdler32() { __ align32(); if (VM_Version::supports_avx512vl()) { // AVX2 performs better for smaller inputs because of leaner post loop reduction sequence.. - __ cmpl(s, MAX2(128, VM_Version::avx3_threshold())); + __ cmpl(s, MAX2(128, CopyAVX3Threshold)); __ jcc(Assembler::belowEqual, SLOOP1A_AVX2); __ lea(end, Address(s, data, Address::times_1, - (2*CHUNKSIZE -1))); diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64_arraycopy.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64_arraycopy.cpp index d53fafafdb4..01e004b7b43 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64_arraycopy.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64_arraycopy.cpp @@ -511,12 +511,12 @@ void StubGenerator::copy_bytes_backward(Register from, Register dest, // - If target supports AVX3 features (BW+VL+F) then implementation uses 32 byte vectors (YMMs) // for both special cases (various small block sizes) and aligned copy loop. This is the // default configuration. -// - If copy length is above AVX3Threshold, then implementation use 64 byte vectors (ZMMs) +// - If copy length is above CopyAVX3Threshold, then implementation use 64 byte vectors (ZMMs) // for main copy loop (and subsequent tail) since bulk of the cycles will be consumed in it. // - If user forces MaxVectorSize=32 then above 4096 bytes its seen that REP MOVs shows a // better performance for disjoint copies. For conjoint/backward copy vector based // copy performs better. -// - If user sets AVX3Threshold=0, then special cases for small blocks sizes operate over +// - If user sets CopyAVX3Threshold=0, then special cases for small blocks sizes operate over // 64 byte vector registers (ZMMs). // Inputs: @@ -575,8 +575,7 @@ address StubGenerator::generate_disjoint_copy_avx3_masked(StubId stub_id, addres StubCodeMark mark(this, stub_id); address start = __ pc(); - int avx3threshold = VM_Version::avx3_threshold(); - bool use64byteVector = (MaxVectorSize > 32) && (avx3threshold == 0); + bool use64byteVector = (MaxVectorSize > 32) && (CopyAVX3Threshold == 0); const int large_threshold = 2621440; // 2.5 MB Label L_main_loop, L_main_loop_64bytes, L_tail, L_tail64, L_exit, L_entry; Label L_repmovs, L_main_pre_loop, L_main_pre_loop_64bytes, L_pre_main_post_64; @@ -647,7 +646,7 @@ address StubGenerator::generate_disjoint_copy_avx3_masked(StubId stub_id, addres __ cmpq(temp2, large_threshold); __ jcc(Assembler::greaterEqual, L_copy_large); } - if (avx3threshold != 0) { + if (CopyAVX3Threshold != 0) { __ cmpq(count, threshold[shift]); if (MaxVectorSize == 64) { // Copy using 64 byte vectors. @@ -659,7 +658,7 @@ address StubGenerator::generate_disjoint_copy_avx3_masked(StubId stub_id, addres } } - if ((MaxVectorSize < 64) || (avx3threshold != 0)) { + if ((MaxVectorSize < 64) || (CopyAVX3Threshold != 0)) { // Partial copy to make dst address 32 byte aligned. __ movq(temp2, to); __ andq(temp2, 31); @@ -913,8 +912,7 @@ address StubGenerator::generate_conjoint_copy_avx3_masked(StubId stub_id, addres StubCodeMark mark(this, stub_id); address start = __ pc(); - int avx3threshold = VM_Version::avx3_threshold(); - bool use64byteVector = (MaxVectorSize > 32) && (avx3threshold == 0); + bool use64byteVector = (MaxVectorSize > 32) && (CopyAVX3Threshold == 0); Label L_main_pre_loop, L_main_pre_loop_64bytes, L_pre_main_post_64; Label L_main_loop, L_main_loop_64bytes, L_tail, L_tail64, L_exit, L_entry; @@ -979,12 +977,12 @@ address StubGenerator::generate_conjoint_copy_avx3_masked(StubId stub_id, addres // PRE-MAIN-POST loop for aligned copy. __ BIND(L_entry); - if ((MaxVectorSize > 32) && (avx3threshold != 0)) { + if ((MaxVectorSize > 32) && (CopyAVX3Threshold != 0)) { __ cmpq(temp1, threshold[shift]); __ jcc(Assembler::greaterEqual, L_pre_main_post_64); } - if ((MaxVectorSize < 64) || (avx3threshold != 0)) { + if ((MaxVectorSize < 64) || (CopyAVX3Threshold != 0)) { // Partial copy to make dst address 32 byte aligned. __ leaq(temp2, Address(to, temp1, (Address::ScaleFactor)(shift), 0)); __ andq(temp2, 31); @@ -1199,7 +1197,7 @@ void StubGenerator::arraycopy_avx3_special_cases_conjoint(XMMRegister xmm, KRegi bool use64byteVector, Label& L_entry, Label& L_exit) { Label L_entry_64, L_entry_96, L_entry_128; Label L_entry_160, L_entry_192; - bool avx3 = (MaxVectorSize > 32) && (VM_Version::avx3_threshold() == 0); + bool avx3 = (MaxVectorSize > 32) && (CopyAVX3Threshold == 0); int size_mat[][6] = { /* T_BYTE */ {32 , 64, 96 , 128 , 160 , 192 }, diff --git a/src/hotspot/cpu/x86/vm_version_x86.cpp b/src/hotspot/cpu/x86/vm_version_x86.cpp index b352de77d6f..a800feea0a8 100644 --- a/src/hotspot/cpu/x86/vm_version_x86.cpp +++ b/src/hotspot/cpu/x86/vm_version_x86.cpp @@ -1967,6 +1967,18 @@ void VM_Version::get_processor_features() { if (FLAG_IS_DEFAULT(UseCopySignIntrinsic)) { FLAG_SET_DEFAULT(UseCopySignIntrinsic, true); } + // CopyAVX3Threshold is the threshold at which 64-byte instructions are used + // for implementing the array copy and clear operations. + // The Intel platforms that supports the serialize instruction + // have improved implementation of 64-byte load/stores and so the default + // threshold is set to 0 for these platforms. + if (FLAG_IS_DEFAULT(CopyAVX3Threshold)) { + if (is_intel() && is_intel_server_family() && supports_serialize()) { + FLAG_SET_DEFAULT(CopyAVX3Threshold, 0); + } else { + FLAG_SET_DEFAULT(CopyAVX3Threshold, AVX3Threshold); + } + } } void VM_Version::print_platform_virtualization_info(outputStream* st) { @@ -2122,17 +2134,6 @@ bool VM_Version::is_intel_darkmont() { return is_intel() && is_intel_server_family() && (_model == 0xCC || _model == 0xDD); } -// avx3_threshold() sets the threshold at which 64-byte instructions are used -// for implementing the array copy and clear operations. -// The Intel platforms that supports the serialize instruction -// has improved implementation of 64-byte load/stores and so the default -// threshold is set to 0 for these platforms. -int VM_Version::avx3_threshold() { - return (is_intel_server_family() && - supports_serialize() && - FLAG_IS_DEFAULT(AVX3Threshold)) ? 0 : AVX3Threshold; -} - void VM_Version::clear_apx_test_state() { clear_apx_test_state_stub(); } diff --git a/src/hotspot/cpu/x86/vm_version_x86.hpp b/src/hotspot/cpu/x86/vm_version_x86.hpp index 9f0446df7c6..a42558a8023 100644 --- a/src/hotspot/cpu/x86/vm_version_x86.hpp +++ b/src/hotspot/cpu/x86/vm_version_x86.hpp @@ -958,8 +958,6 @@ public: static bool is_intel_darkmont(); - static int avx3_threshold(); - static bool is_intel_tsc_synched_at_init(); static void insert_features_names(VM_Version::VM_Features features, stringStream& ss); diff --git a/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.cpp b/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.cpp index 444ce321759..36eece6f013 100644 --- a/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.cpp +++ b/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.cpp @@ -274,6 +274,17 @@ JVMFlag::Error AVX3ThresholdConstraintFunc(int value, bool verbose) { return JVMFlag::SUCCESS; } +JVMFlag::Error CopyAVX3ThresholdConstraintFunc(int value, bool verbose) { + if (value != 0 && !is_power_of_2(value)) { + JVMFlag::printError(verbose, + "CopyAVX3Threshold ( %d ) must be 0 or " + "a power of two value between 0 and MAX_INT\n", value); + return JVMFlag::VIOLATES_CONSTRAINT; + } + + return JVMFlag::SUCCESS; +} + JVMFlag::Error ArraycopySrcPrefetchDistanceConstraintFunc(uintx value, bool verbose) { if (value >= 4032) { JVMFlag::printError(verbose, diff --git a/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.hpp b/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.hpp index cf785800cfc..45e91058e0b 100644 --- a/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.hpp +++ b/src/hotspot/share/runtime/flags/jvmFlagConstraintsCompiler.hpp @@ -46,6 +46,7 @@ f(uintx, ArraycopyDstPrefetchDistanceConstraintFunc) \ f(uintx, ArraycopySrcPrefetchDistanceConstraintFunc) \ f(int, AVX3ThresholdConstraintFunc) \ + f(int, CopyAVX3ThresholdConstraintFunc) \ f(uint, TypeProfileLevelConstraintFunc) \ f(uint, VerifyIterativeGVNConstraintFunc) \ f(intx, InitArrayShortSizeConstraintFunc) \ From 8d11b977def8356b6fd8504562538d2af88c082b Mon Sep 17 00:00:00 2001 From: Erik Gahlin Date: Mon, 16 Mar 2026 22:20:04 +0000 Subject: [PATCH 03/22] =?UTF-8?q?8380037:=20JFR:=20Don=E2=80=99t=20format?= =?UTF-8?q?=20identifiers=20as=20numbers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: mgronlun --- src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java | 5 ++++- .../share/classes/jdk/jfr/internal/query/FieldBuilder.java | 3 ++- .../share/classes/jdk/jfr/internal/query/FieldFormatter.java | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java index b7fc4381670..f9c2ab351bf 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2025, 2026, 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 @@ -118,6 +118,9 @@ final class Field { // An integral type (byte, short, int, long) boolean integralType; + // An integral type that should be treated like a symbol, e.g. PID. + boolean identifier; + // A java.time.Duration boolean timespan; diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldBuilder.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldBuilder.java index 64791b1976a..8d0b3371c7d 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldBuilder.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2026, 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 @@ -274,6 +274,7 @@ final class FieldBuilder { case "int", "long", "short", "byte": field.integralType = true; field.alignLeft = false; + field.identifier = fieldName.equals("id") || fieldName.endsWith("Id") || field.label.endsWith("Identifier"); break; case "float", "double": field.fractionalType = true; diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldFormatter.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldFormatter.java index 989e2231eb1..ec032ce9f34 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldFormatter.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/query/FieldFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2026, 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 @@ -165,7 +165,7 @@ public class FieldFormatter { return object + " Hz"; } } - if (object instanceof Number number) { + if (object instanceof Number number && !field.identifier) { return ValueFormatter.formatNumber(number); } return object.toString(); From 921da0a9734d9fae2e7b0e129d2cc6949ad0b5a6 Mon Sep 17 00:00:00 2001 From: Ioi Lam Date: Tue, 17 Mar 2026 01:28:03 +0000 Subject: [PATCH 04/22] 8378211: Test ChangedJarFile.java failed: missing "timestamp has changed" Reviewed-by: dholmes, liach --- .../jtreg/runtime/cds/appcds/aotCache/ChangedJarFile.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ChangedJarFile.java b/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ChangedJarFile.java index a717b267347..e86defdf816 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ChangedJarFile.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ChangedJarFile.java @@ -59,7 +59,7 @@ public class ChangedJarFile { tester.productionRun(new String[] {"-XX:AOTMode=auto", "-Xlog:aot"}, new String[] {"jarHasChanged"}); out.shouldMatch("This file is not the one used while building the " + - "AOT cache: '.*app.jar', timestamp has changed, size has changed"); + "AOT cache: '.*app.jar',.* size has changed"); } static class Tester extends CDSAppTester { From 3e231755a03e75a29b66aaa32784397cf5022da1 Mon Sep 17 00:00:00 2001 From: Dingli Zhang Date: Tue, 17 Mar 2026 02:41:24 +0000 Subject: [PATCH 05/22] 8380083: Enable some vector mask cast IR matching tests for RISC-V Reviewed-by: fyang --- .../vectorapi/VectorMaskCastIdentityTest.java | 6 ++-- .../vectorapi/VectorMaskCastTest.java | 28 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastIdentityTest.java b/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastIdentityTest.java index be9d8e6390c..e4f166f510a 100644 --- a/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastIdentityTest.java +++ b/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastIdentityTest.java @@ -50,7 +50,7 @@ public class VectorMaskCastIdentityTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 2" }, applyIfCPUFeatureOr = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 2" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static int testTwoCastToDifferentType() { // The types before and after the two casts are not the same, so the cast cannot be eliminated. VectorMask mFloat64 = VectorMask.fromArray(FloatVector.SPECIES_64, mr, 0); @@ -84,7 +84,7 @@ public class VectorMaskCastIdentityTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static int testTwoCastToSameType() { // The types before and after the two casts are the same, so the cast will be eliminated. VectorMask mInt128 = VectorMask.fromArray(IntVector.SPECIES_128, mr, 0); @@ -101,7 +101,7 @@ public class VectorMaskCastIdentityTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 1" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "= 1" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static int testOneCastToDifferentType() { // The types before and after the only cast are different, the cast will not be eliminated. VectorMask mFloat128 = VectorMask.fromArray(FloatVector.SPECIES_128, mr, 0).not(); diff --git a/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastTest.java b/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastTest.java index 25594d5caf9..1f346cffd2f 100644 --- a/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastTest.java +++ b/test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastTest.java @@ -74,7 +74,7 @@ public class VectorMaskCastTest { // Byte @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testByte64ToShort128(VectorMask v) { return v.cast(ShortVector.SPECIES_128); } @@ -201,7 +201,7 @@ public class VectorMaskCastTest { // Short @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testShort64ToInt128(VectorMask v) { return v.cast(IntVector.SPECIES_128); } @@ -215,7 +215,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testShort64ToFloat128(VectorMask v) { return v.cast(FloatVector.SPECIES_128); } @@ -257,7 +257,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testShort128ToByte64(VectorMask v) { return v.cast(ByteVector.SPECIES_64); } @@ -384,7 +384,7 @@ public class VectorMaskCastTest { // Int @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testInt64ToLong128(VectorMask v) { return v.cast(LongVector.SPECIES_128); } @@ -398,7 +398,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testInt64ToDouble128(VectorMask v) { return v.cast(DoubleVector.SPECIES_128); } @@ -412,7 +412,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testInt128ToShort64(VectorMask v) { return v.cast(ShortVector.SPECIES_64); } @@ -539,7 +539,7 @@ public class VectorMaskCastTest { // Float @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testFloat64ToLong128(VectorMask v) { return v.cast(LongVector.SPECIES_128); } @@ -553,7 +553,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testFloat64ToDouble128(VectorMask v) { return v.cast(DoubleVector.SPECIES_128); } @@ -567,7 +567,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"avx2", "true", "asimd", "true", "rvv", "true"}) public static VectorMask testFloat128ToShort64(VectorMask v) { return v.cast(ShortVector.SPECIES_64); } @@ -694,7 +694,7 @@ public class VectorMaskCastTest { // Long @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testLong128ToInt64(VectorMask v) { return v.cast(IntVector.SPECIES_64); } @@ -708,7 +708,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testLong128ToFloat64(VectorMask v) { return v.cast(FloatVector.SPECIES_64); } @@ -821,7 +821,7 @@ public class VectorMaskCastTest { // Double @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testDouble128ToInt64(VectorMask v) { return v.cast(IntVector.SPECIES_64); } @@ -835,7 +835,7 @@ public class VectorMaskCastTest { } @Test - @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeature = {"asimd", "true"}) + @IR(counts = { IRNode.VECTOR_MASK_CAST, "> 0" }, applyIfCPUFeatureOr = {"asimd", "true", "rvv", "true"}) public static VectorMask testDouble128ToFloat64(VectorMask v) { return v.cast(FloatVector.SPECIES_64); } From a1e4621b30e08c8b20bd4542e4f1655aeb0ec576 Mon Sep 17 00:00:00 2001 From: Ioi Lam Date: Tue, 17 Mar 2026 05:58:33 +0000 Subject: [PATCH 06/22] 8378152: Upstream AOT heap object improvements from Leyden repo Reviewed-by: jrose, kvn --- src/hotspot/share/cds/aotMappedHeapWriter.cpp | 52 +++--- src/hotspot/share/cds/aotMappedHeapWriter.hpp | 18 +- .../share/cds/aotReferenceObjSupport.cpp | 2 +- .../share/cds/aotStreamedHeapWriter.cpp | 22 +-- .../share/cds/aotStreamedHeapWriter.hpp | 2 - src/hotspot/share/cds/cdsHeapVerifier.hpp | 2 +- src/hotspot/share/cds/heapShared.cpp | 154 +++++++++++++++--- src/hotspot/share/cds/heapShared.hpp | 35 ++-- src/hotspot/share/classfile/stringTable.cpp | 2 +- 9 files changed, 185 insertions(+), 104 deletions(-) diff --git a/src/hotspot/share/cds/aotMappedHeapWriter.cpp b/src/hotspot/share/cds/aotMappedHeapWriter.cpp index 64c0e3c40e8..2e21a1d15d8 100644 --- a/src/hotspot/share/cds/aotMappedHeapWriter.cpp +++ b/src/hotspot/share/cds/aotMappedHeapWriter.cpp @@ -64,6 +64,11 @@ HeapRootSegments AOTMappedHeapWriter::_heap_root_segments; address AOTMappedHeapWriter::_requested_bottom; address AOTMappedHeapWriter::_requested_top; +static size_t _num_strings = 0; +static size_t _string_bytes = 0; +static size_t _num_packages = 0; +static size_t _num_protection_domains = 0; + GrowableArrayCHeap* AOTMappedHeapWriter::_native_pointers; GrowableArrayCHeap* AOTMappedHeapWriter::_source_objs; GrowableArrayCHeap* AOTMappedHeapWriter::_source_objs_order; @@ -71,8 +76,6 @@ GrowableArrayCHeap* AOTMappedH AOTMappedHeapWriter::BufferOffsetToSourceObjectTable* AOTMappedHeapWriter::_buffer_offset_to_source_obj_table = nullptr; -DumpedInternedStrings *AOTMappedHeapWriter::_dumped_interned_strings = nullptr; - typedef HashTable< size_t, // offset of a filler from AOTMappedHeapWriter::buffer_bottom() size_t, // size of this filler (in bytes) @@ -87,7 +90,6 @@ void AOTMappedHeapWriter::init() { Universe::heap()->collect(GCCause::_java_lang_system_gc); _buffer_offset_to_source_obj_table = new (mtClassShared) BufferOffsetToSourceObjectTable(/*size (prime)*/36137, /*max size*/1 * M); - _dumped_interned_strings = new (mtClass)DumpedInternedStrings(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE); _fillers = new (mtClassShared) FillersTable(); _requested_bottom = nullptr; _requested_top = nullptr; @@ -141,9 +143,6 @@ int AOTMappedHeapWriter::narrow_oop_shift() { void AOTMappedHeapWriter::delete_tables_with_raw_oops() { delete _source_objs; _source_objs = nullptr; - - delete _dumped_interned_strings; - _dumped_interned_strings = nullptr; } void AOTMappedHeapWriter::add_source_obj(oop src_obj) { @@ -181,25 +180,6 @@ bool AOTMappedHeapWriter::is_too_large_to_archive(size_t size) { } } -// Keep track of the contents of the archived interned string table. This table -// is used only by CDSHeapVerifier. -void AOTMappedHeapWriter::add_to_dumped_interned_strings(oop string) { - assert_at_safepoint(); // DumpedInternedStrings uses raw oops - assert(!is_string_too_large_to_archive(string), "must be"); - bool created; - _dumped_interned_strings->put_if_absent(string, true, &created); - if (created) { - // Prevent string deduplication from changing the value field to - // something not in the archive. - java_lang_String::set_deduplication_forbidden(string); - _dumped_interned_strings->maybe_grow(); - } -} - -bool AOTMappedHeapWriter::is_dumped_interned_string(oop o) { - return _dumped_interned_strings->get(o) != nullptr; -} - // Various lookup functions between source_obj, buffered_obj and requested_obj bool AOTMappedHeapWriter::is_in_requested_range(oop o) { assert(_requested_bottom != nullptr, "do not call before _requested_bottom is initialized"); @@ -430,6 +410,7 @@ void AOTMappedHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeapset_buffer_offset(buffer_offset); + assert(buffer_offset <= 0x7fffffff, "sanity"); OopHandle handle(Universe::vm_global(), src_obj); _buffer_offset_to_source_obj_table->put_when_absent(buffer_offset, handle); @@ -442,6 +423,9 @@ void AOTMappedHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeaplength() + 1, roots->length(), _num_native_ptrs); + log_info(aot)(" strings = %8zu (%zu bytes)", _num_strings, _string_bytes); + log_info(aot)(" packages = %8zu", _num_packages); + log_info(aot)(" protection domains = %8zu", _num_protection_domains); } size_t AOTMappedHeapWriter::filler_array_byte_size(int length) { @@ -530,7 +514,25 @@ void update_buffered_object_field(address buffered_obj, int field_offset, T valu *field_addr = value; } +void AOTMappedHeapWriter::update_stats(oop src_obj) { + if (java_lang_String::is_instance(src_obj)) { + _num_strings ++; + _string_bytes += src_obj->size() * HeapWordSize; + _string_bytes += java_lang_String::value(src_obj)->size() * HeapWordSize; + } else { + Klass* k = src_obj->klass(); + Symbol* name = k->name(); + if (name->equals("java/lang/NamedPackage") || name->equals("java/lang/Package")) { + _num_packages ++; + } else if (name->equals("java/security/ProtectionDomain")) { + _num_protection_domains ++; + } + } +} + size_t AOTMappedHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) { + update_stats(src_obj); + assert(!is_too_large_to_archive(src_obj), "already checked"); size_t byte_size = src_obj->size() * HeapWordSize; assert(byte_size > 0, "no zero-size objects"); diff --git a/src/hotspot/share/cds/aotMappedHeapWriter.hpp b/src/hotspot/share/cds/aotMappedHeapWriter.hpp index 7481e7922a0..2420e68d9fe 100644 --- a/src/hotspot/share/cds/aotMappedHeapWriter.hpp +++ b/src/hotspot/share/cds/aotMappedHeapWriter.hpp @@ -40,20 +40,6 @@ class MemRegion; #if INCLUDE_CDS_JAVA_HEAP -class DumpedInternedStrings : - public ResizeableHashTable -{ -public: - DumpedInternedStrings(unsigned size, unsigned max_size) : - ResizeableHashTable(size, max_size) {} -}; - class AOTMappedHeapWriter : AllStatic { friend class HeapShared; friend class AOTMappedHeapLoader; @@ -131,7 +117,6 @@ private: static GrowableArrayCHeap* _native_pointers; static GrowableArrayCHeap* _source_objs; - static DumpedInternedStrings *_dumped_interned_strings; // We sort _source_objs_order to minimize the number of bits in ptrmap and oopmap. // See comments near the body of AOTMappedHeapWriter::compare_objs_by_oop_fields(). @@ -190,6 +175,7 @@ private: static void copy_roots_to_buffer(GrowableArrayCHeap* roots); static void copy_source_objs_to_buffer(GrowableArrayCHeap* roots); static size_t copy_one_source_obj_to_buffer(oop src_obj); + static void update_stats(oop src_obj); static void maybe_fill_gc_region_gap(size_t required_byte_size); static size_t filler_array_byte_size(int length); @@ -227,8 +213,6 @@ public: static bool is_too_large_to_archive(size_t size); static bool is_too_large_to_archive(oop obj); static bool is_string_too_large_to_archive(oop string); - static bool is_dumped_interned_string(oop o); - static void add_to_dumped_interned_strings(oop string); static void write(GrowableArrayCHeap*, AOTMappedHeapInfo* heap_info); static address requested_address(); // requested address of the lowest achived heap object static size_t get_filler_size_at(address buffered_addr); diff --git a/src/hotspot/share/cds/aotReferenceObjSupport.cpp b/src/hotspot/share/cds/aotReferenceObjSupport.cpp index 0c27c8ce5f0..2d5fc8c7f21 100644 --- a/src/hotspot/share/cds/aotReferenceObjSupport.cpp +++ b/src/hotspot/share/cds/aotReferenceObjSupport.cpp @@ -96,7 +96,7 @@ class KeepAliveObjectsTable : public HashTable {}; + HeapShared::oop_address_hash> {}; static KeepAliveObjectsTable* _keep_alive_objs_table; static OopHandle _keep_alive_objs_array; diff --git a/src/hotspot/share/cds/aotStreamedHeapWriter.cpp b/src/hotspot/share/cds/aotStreamedHeapWriter.cpp index f52532b2f2a..ad363f21fdb 100644 --- a/src/hotspot/share/cds/aotStreamedHeapWriter.cpp +++ b/src/hotspot/share/cds/aotStreamedHeapWriter.cpp @@ -242,20 +242,6 @@ void AOTStreamedHeapWriter::copy_roots_max_dfs_to_buffer(int roots_length) { } } -static bool is_interned_string(oop obj) { - if (!java_lang_String::is_instance(obj)) { - return false; - } - - ResourceMark rm; - int len; - jchar* name = java_lang_String::as_unicode_string_or_null(obj, len); - if (name == nullptr) { - fatal("Insufficient memory for dumping"); - } - return StringTable::lookup(name, len) == obj; -} - static BitMap::idx_t bit_idx_for_buffer_offset(size_t buffer_offset) { if (UseCompressedOops) { return BitMap::idx_t(buffer_offset / sizeof(narrowOop)); @@ -264,10 +250,6 @@ static BitMap::idx_t bit_idx_for_buffer_offset(size_t buffer_offset) { } } -bool AOTStreamedHeapWriter::is_dumped_interned_string(oop obj) { - return is_interned_string(obj) && HeapShared::get_cached_oop_info(obj) != nullptr; -} - void AOTStreamedHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap* roots) { for (int i = 0; i < _source_objs->length(); i++) { oop src_obj = _source_objs->at(i); @@ -325,7 +307,7 @@ size_t AOTStreamedHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) { ensure_buffer_space(new_used); - if (is_interned_string(src_obj)) { + if (HeapShared::is_interned_string(src_obj)) { java_lang_String::hash_code(src_obj); // Sets the hash code field(s) java_lang_String::set_deduplication_forbidden(src_obj); // Allows faster interning at runtime assert(java_lang_String::hash_is_set(src_obj), "hash must be set"); @@ -402,7 +384,7 @@ void AOTStreamedHeapWriter::update_header_for_buffered_addr(address buffered_add mw = mw.copy_set_hash(src_hash); } - if (is_interned_string(src_obj)) { + if (HeapShared::is_interned_string(src_obj)) { // Mark the mark word of interned string so the loader knows to link these to // the string table at runtime. mw = mw.set_marked(); diff --git a/src/hotspot/share/cds/aotStreamedHeapWriter.hpp b/src/hotspot/share/cds/aotStreamedHeapWriter.hpp index ab5aec0327b..c3cc9f2c092 100644 --- a/src/hotspot/share/cds/aotStreamedHeapWriter.hpp +++ b/src/hotspot/share/cds/aotStreamedHeapWriter.hpp @@ -148,8 +148,6 @@ public: return size_t(buffered_addr) - size_t(buffer_bottom()); } - static bool is_dumped_interned_string(oop obj); - static size_t source_obj_to_buffered_offset(oop src_obj); static address source_obj_to_buffered_addr(oop src_obj); diff --git a/src/hotspot/share/cds/cdsHeapVerifier.hpp b/src/hotspot/share/cds/cdsHeapVerifier.hpp index 7f1bdb1d249..f8e090801bb 100644 --- a/src/hotspot/share/cds/cdsHeapVerifier.hpp +++ b/src/hotspot/share/cds/cdsHeapVerifier.hpp @@ -53,7 +53,7 @@ class CDSHeapVerifier : public KlassClosure { 15889, // prime number AnyObj::C_HEAP, mtClassShared, - HeapShared::oop_hash> _table; + HeapShared::oop_address_hash> _table; GrowableArray _exclusions; GrowableArray _shared_secret_accessors; diff --git a/src/hotspot/share/cds/heapShared.cpp b/src/hotspot/share/cds/heapShared.cpp index 0c0f70eac0a..8cab56dda8d 100644 --- a/src/hotspot/share/cds/heapShared.cpp +++ b/src/hotspot/share/cds/heapShared.cpp @@ -175,23 +175,39 @@ oop HeapShared::CachedOopInfo::orig_referrer() const { return _orig_referrer.resolve(); } -unsigned HeapShared::oop_hash(oop const& p) { +// This is a simple hashing of the oop's address. This function is used +// while copying the oops into the AOT heap region. We don't want to +// have any side effects during the copying, so we avoid calling +// p->identity_hash() which can update the object header. +unsigned HeapShared::oop_address_hash(oop const& p) { assert(SafepointSynchronize::is_at_safepoint() || JavaThread::current()->is_in_no_safepoint_scope(), "sanity"); - // Do not call p->identity_hash() as that will update the - // object header. return primitive_hash(cast_from_oop(p)); } -unsigned int HeapShared::oop_handle_hash_raw(const OopHandle& oh) { - return oop_hash(oh.resolve()); -} - -unsigned int HeapShared::oop_handle_hash(const OopHandle& oh) { +// About the hashcode in the cached objects: +// - If a source object has a hashcode, it must be copied into the cache. +// That's because some cached hashtables are laid out using this hashcode. +// - If a source object doesn't have a hashcode, we avoid computing it while +// copying the objects into the cache. This will allow the hashcode to be +// dynamically and randomly computed in each production, which generally +// desirable to make the hashcodes more random between runs. +unsigned HeapShared::archived_object_cache_hash(OopHandle const& oh) { oop o = oh.resolve(); if (o == nullptr) { return 0; + } + if (!_use_identity_hash_for_archived_object_cache) { + // This is called while we are copying the objects. Don't call o->identity_hash() + // as that will update the object header. + return oop_address_hash(o); } else { + // This is called after all objects are copied. It's OK to update + // the object's hashcode. + // + // This may be called after we have left the AOT dumping safepoint. + // Objects in archived_object_cache() may be moved by the GC, so we + // can't use the address of o for computing the hash. return o->identity_hash(); } } @@ -271,6 +287,12 @@ void HeapShared::prepare_for_archiving(TRAPS) { HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = nullptr; +// Controls the hashing method for the _archived_object_cache. +// Changes from false to true once, after all objects are copied, +// inside make_archived_object_cache_gc_safe(). +// See archived_object_cache_hash() for more details. +bool HeapShared::_use_identity_hash_for_archived_object_cache = false; + bool HeapShared::is_archived_heap_in_use() { if (HeapShared::is_loading()) { if (HeapShared::is_loading_streaming_mode()) { @@ -384,9 +406,8 @@ void HeapShared::materialize_thread_object() { } } -void HeapShared::add_to_dumped_interned_strings(oop string) { +void HeapShared::archive_interned_string(oop string) { assert(HeapShared::is_writing_mapping_mode(), "Only used by this mode"); - AOTMappedHeapWriter::add_to_dumped_interned_strings(string); bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, string); assert(success, "shared strings array must not point to arrays or strings that are too large to archive"); } @@ -404,6 +425,24 @@ void HeapShared::finalize_initialization(FileMapInfo* static_mapinfo) { } } +void HeapShared::make_archived_object_cache_gc_safe() { + ArchivedObjectCache* new_cache = new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE); + + // It's safe to change the behavior of the hash function now, because iterate_all() + // doesn't call the hash function. + // See archived_object_cache_hash() for more details. + assert(_use_identity_hash_for_archived_object_cache == false, "happens only once"); + _use_identity_hash_for_archived_object_cache = true; + + // Copy all CachedOopInfo into a new table using a different hashing algorithm + archived_object_cache()->iterate_all([&] (OopHandle oh, CachedOopInfo info) { + new_cache->put_when_absent(oh, info); + }); + + destroy_archived_object_cache(); + _archived_object_cache = new_cache; +} + HeapShared::CachedOopInfo* HeapShared::get_cached_oop_info(oop obj) { OopHandle oh(Universe::vm_global(), obj); CachedOopInfo* result = _archived_object_cache->get(oh); @@ -417,14 +456,53 @@ bool HeapShared::has_been_archived(oop obj) { } int HeapShared::append_root(oop obj) { + assert(SafepointSynchronize::is_at_safepoint(), "sanity"); assert(CDSConfig::is_dumping_heap(), "dump-time only"); - if (obj != nullptr) { - assert(has_been_archived(obj), "must be"); - } - // No GC should happen since we aren't scanning _pending_roots. - assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); + assert(_pending_roots != nullptr, "sanity"); - return _pending_roots->append(obj); + if (obj == nullptr) { + assert(_pending_roots->at(0) == nullptr, "root index 0 always maps to null"); + return 0; + } else if (CDSConfig::is_dumping_aot_linked_classes()) { + // The AOT compiler may refer the same obj many times, so we + // should use the same index for this oop to avoid excessive entries + // in the roots array. + CachedOopInfo* obj_info = get_cached_oop_info(obj); + assert(obj_info != nullptr, "must be archived"); + + if (obj_info->root_index() > 0) { + return obj_info->root_index(); + } else { + assert(obj_info->root_index() < 0, "must not be zero"); + int i = _pending_roots->append(obj); + obj_info->set_root_index(i); + return i; + } + } else { + return _pending_roots->append(obj); + } +} + +int HeapShared::get_root_index(oop obj) { + if (java_lang_Class::is_instance(obj)) { + obj = scratch_java_mirror(obj); + } + + CachedOopInfo* obj_info = get_cached_oop_info(obj); + const char* error = nullptr; + if (obj_info == nullptr) { + error = "Not a cached oop"; + } else if (obj_info->root_index() < 0) { + error = "Not a cached oop root"; + } else { + return obj_info->root_index(); + } + + ResourceMark rm; + log_debug(aot, codecache, oops)("%s: " INTPTR_FORMAT " (%s)", error, + cast_from_oop(obj), + obj->klass()->external_name()); + return -1; } oop HeapShared::get_root(int index, bool clear) { @@ -453,6 +531,13 @@ void HeapShared::finish_materialize_objects() { } void HeapShared::clear_root(int index) { + if (CDSConfig::is_using_aot_linked_classes()) { + // When AOT linked classes are in use, all roots will be in use all + // the time, there's no benefit for clearing the roots. Also, we + // can't clear the roots as they can be shared. + return; + } + assert(index >= 0, "sanity"); assert(CDSConfig::is_using_archive(), "must be"); if (is_archived_heap_in_use()) { @@ -600,9 +685,10 @@ objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) { return (objArrayOop)_scratch_objects_table->get_oop(src); } - void HeapShared::init_dumping() { - _scratch_objects_table = new (mtClass)MetaspaceObjToOopHandleTable(); - _pending_roots = new GrowableArrayCHeap(500); +void HeapShared::init_dumping() { + _scratch_objects_table = new (mtClass)MetaspaceObjToOopHandleTable(); + _pending_roots = new GrowableArrayCHeap(500); + _pending_roots->append(nullptr); // root index 0 represents a null oop } void HeapShared::init_scratch_objects_for_basic_type_mirrors(TRAPS) { @@ -883,6 +969,11 @@ void HeapShared::write_heap(AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeap ArchiveBuilder::OtherROAllocMark mark; write_subgraph_info_table(); + + delete _pending_roots; + _pending_roots = nullptr; + + make_archived_object_cache_gc_safe(); } void HeapShared::scan_java_mirror(oop orig_mirror) { @@ -1911,6 +2002,11 @@ void HeapShared::verify_subgraph_from(oop orig_obj) { void HeapShared::verify_reachable_objects_from(oop obj) { _num_total_verifications ++; if (java_lang_Class::is_instance(obj)) { + Klass* k = java_lang_Class::as_Klass(obj); + if (RegeneratedClasses::has_been_regenerated(k)) { + k = RegeneratedClasses::get_regenerated_object(k); + obj = k->java_mirror(); + } obj = scratch_java_mirror(obj); assert(obj != nullptr, "must be"); } @@ -2264,12 +2360,22 @@ void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[], #endif } -bool HeapShared::is_dumped_interned_string(oop o) { - if (is_writing_mapping_mode()) { - return AOTMappedHeapWriter::is_dumped_interned_string(o); - } else { - return AOTStreamedHeapWriter::is_dumped_interned_string(o); +bool HeapShared::is_interned_string(oop obj) { + if (!java_lang_String::is_instance(obj)) { + return false; } + + ResourceMark rm; + int len = 0; + jchar* name = java_lang_String::as_unicode_string_or_null(obj, len); + if (name == nullptr) { + fatal("Insufficient memory for dumping"); + } + return StringTable::lookup(name, len) == obj; +} + +bool HeapShared::is_dumped_interned_string(oop o) { + return is_interned_string(o) && has_been_archived(o); } // These tables should be used only within the CDS safepoint, so diff --git a/src/hotspot/share/cds/heapShared.hpp b/src/hotspot/share/cds/heapShared.hpp index 2cb330160e4..92f41784268 100644 --- a/src/hotspot/share/cds/heapShared.hpp +++ b/src/hotspot/share/cds/heapShared.hpp @@ -40,7 +40,6 @@ #include "utilities/hashTable.hpp" #if INCLUDE_CDS_JAVA_HEAP -class DumpedInternedStrings; class FileMapInfo; class KlassSubGraphInfo; class MetaspaceObjToOopHandleTable; @@ -176,7 +175,7 @@ public: static void initialize_streaming() NOT_CDS_JAVA_HEAP_RETURN; static void enable_gc() NOT_CDS_JAVA_HEAP_RETURN; static void materialize_thread_object() NOT_CDS_JAVA_HEAP_RETURN; - static void add_to_dumped_interned_strings(oop string) NOT_CDS_JAVA_HEAP_RETURN; + static void archive_interned_string(oop string); static void finalize_initialization(FileMapInfo* static_mapinfo) NOT_CDS_JAVA_HEAP_RETURN; private: @@ -195,13 +194,8 @@ private: static void print_stats(); public: static void debug_trace(); - static unsigned oop_hash(oop const& p); - static unsigned oop_handle_hash(OopHandle const& oh); - static unsigned oop_handle_hash_raw(OopHandle const& oh); + static unsigned oop_address_hash(oop const& p); static bool oop_handle_equals(const OopHandle& a, const OopHandle& b); - static unsigned string_oop_hash(oop const& string) { - return java_lang_String::hash_code(string); - } class CopyKlassSubGraphInfoToArchive; @@ -217,27 +211,37 @@ public: // One or more fields in this object are pointing to MetaspaceObj bool _has_native_pointers; + + // >= 0 if this oop has been append to the list of roots + int _root_index; public: CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers) : _orig_referrer(orig_referrer), _buffer_offset(0), _has_oop_pointers(has_oop_pointers), - _has_native_pointers(false) {} + _has_native_pointers(false), + _root_index(-1) {} oop orig_referrer() const; void set_buffer_offset(size_t offset) { _buffer_offset = offset; } size_t buffer_offset() const { return _buffer_offset; } bool has_oop_pointers() const { return _has_oop_pointers; } bool has_native_pointers() const { return _has_native_pointers; } void set_has_native_pointers() { _has_native_pointers = true; } + int root_index() const { return _root_index; } + void set_root_index(int i) { _root_index = i; } }; private: static const int INITIAL_TABLE_SIZE = 15889; // prime number static const int MAX_TABLE_SIZE = 1000000; + static bool _use_identity_hash_for_archived_object_cache; + + static unsigned archived_object_cache_hash(OopHandle const& oh); + typedef ResizeableHashTable ArchivedObjectCache; static ArchivedObjectCache* _archived_object_cache; @@ -297,7 +301,7 @@ private: typedef ResizeableHashTable SeenObjectsTable; + HeapShared::oop_address_hash> SeenObjectsTable; static SeenObjectsTable *_seen_objects_table; @@ -394,6 +398,7 @@ private: delete _archived_object_cache; _archived_object_cache = nullptr; } + static void make_archived_object_cache_gc_safe(); static ArchivedObjectCache* archived_object_cache() { return _archived_object_cache; } @@ -406,6 +411,7 @@ private: KlassSubGraphInfo* subgraph_info, oop orig_obj); + static bool is_interned_string(oop obj); static bool is_dumped_interned_string(oop o); // Scratch objects for archiving Klass::java_mirror() @@ -437,6 +443,11 @@ private: // Dump-time only. Returns the index of the root, which can be used at run time to read // the root using get_root(index, ...). static int append_root(oop obj); + + // AOT-compile time only. + // Returns -1 if obj is not in the heap root set. + static int get_root_index(oop obj) NOT_CDS_JAVA_HEAP_RETURN_(-1); + static GrowableArrayCHeap* pending_roots() { return _pending_roots; } // Dump-time and runtime @@ -445,9 +456,7 @@ private: // Run-time only static void clear_root(int index); - static void get_segment_indexes(int index, int& segment_index, int& internal_index); - static void setup_test_class(const char* test_class_name) PRODUCT_RETURN; #endif // INCLUDE_CDS_JAVA_HEAP diff --git a/src/hotspot/share/classfile/stringTable.cpp b/src/hotspot/share/classfile/stringTable.cpp index 2b8b7780a41..c3f60487b9c 100644 --- a/src/hotspot/share/classfile/stringTable.cpp +++ b/src/hotspot/share/classfile/stringTable.cpp @@ -946,7 +946,7 @@ void StringTable::init_shared_table() { // so we are all good. // - If there's a reference to it, we will report an error inside HeapShared.cpp and // dumping will fail. - HeapShared::add_to_dumped_interned_strings(string); + HeapShared::archive_interned_string(string); } n++; return true; From b0831572e2cd9dbff9ee2abcdf81a493ddcecc7e Mon Sep 17 00:00:00 2001 From: Kieran Farrell Date: Tue, 17 Mar 2026 06:00:32 +0000 Subject: [PATCH 07/22] 8359706: Add file descriptor count to VM.info Reviewed-by: kevinw, stuefe --- src/hotspot/os/aix/os_aix.cpp | 4 ++ src/hotspot/os/bsd/os_bsd.cpp | 44 +++++++++++++++++++++ src/hotspot/os/bsd/os_bsd.hpp | 2 + src/hotspot/os/linux/os_linux.cpp | 34 ++++++++++++++++ src/hotspot/os/windows/os_windows.cpp | 4 ++ src/hotspot/share/runtime/os.hpp | 3 ++ src/hotspot/share/utilities/vmError.cpp | 9 +++++ test/jdk/sun/tools/jcmd/TestJcmdSanity.java | 6 ++- 8 files changed, 105 insertions(+), 1 deletion(-) diff --git a/src/hotspot/os/aix/os_aix.cpp b/src/hotspot/os/aix/os_aix.cpp index 7c08d6de2db..3cad24d388c 100644 --- a/src/hotspot/os/aix/os_aix.cpp +++ b/src/hotspot/os/aix/os_aix.cpp @@ -2667,3 +2667,7 @@ void os::print_memory_mappings(char* addr, size_t bytes, outputStream* st) {} void os::jfr_report_memory_info() {} #endif // INCLUDE_JFR + +void os::print_open_file_descriptors(outputStream* st) { + // File descriptor counting not implemented on AIX +} diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index 29ebe65e0db..207968a7a59 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -76,6 +76,7 @@ # include # include # include +# include # include # include # include @@ -102,6 +103,7 @@ #endif #ifdef __APPLE__ + #include #include #include #endif @@ -2596,3 +2598,45 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { return res; } // end: os::pd_dll_unload() + +void os::print_open_file_descriptors(outputStream* st) { +#ifdef __APPLE__ + char buf[1024 * sizeof(struct proc_fdinfo)]; + os::Bsd::print_open_file_descriptors(st, buf, sizeof(buf)); +#else + st->print_cr("Open File Descriptors: unknown"); +#endif +} + +void os::Bsd::print_open_file_descriptors(outputStream* st, char* buf, size_t buflen) { +#ifdef __APPLE__ + pid_t my_pid; + + // ensure the scratch buffer is big enough for at least one FD info struct + assert(buflen >= sizeof(struct proc_fdinfo)); + kern_return_t kres = pid_for_task(mach_task_self(), &my_pid); + if (kres != KERN_SUCCESS) { + st->print_cr("Open File Descriptors: unknown"); + return; + } + size_t max_fds = buflen / sizeof(struct proc_fdinfo); + struct proc_fdinfo* fds = reinterpret_cast(buf); + + // fill our buffer with FD info, up to the available buffer size + int res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, max_fds * sizeof(struct proc_fdinfo)); + if (res <= 0) { + st->print_cr("Open File Descriptors: unknown"); + return; + } + + // print lower threshold if count exceeds buffer size + int nfiles = res / sizeof(struct proc_fdinfo); + if ((size_t)nfiles >= max_fds) { + st->print_cr("Open File Descriptors: > %zu", max_fds); + return; + } + st->print_cr("Open File Descriptors: %d", nfiles); +#else + st->print_cr("Open File Descriptors: unknown"); +#endif +} \ No newline at end of file diff --git a/src/hotspot/os/bsd/os_bsd.hpp b/src/hotspot/os/bsd/os_bsd.hpp index da73211b9a7..e87a680b2d2 100644 --- a/src/hotspot/os/bsd/os_bsd.hpp +++ b/src/hotspot/os/bsd/os_bsd.hpp @@ -123,6 +123,8 @@ class os::Bsd { static int get_node_by_cpu(int cpu_id); static void print_uptime_info(outputStream* st); + static void print_open_file_descriptors(outputStream* st, char* buf, size_t buflen); + static void print_open_file_descriptors(outputStream* st); }; #endif // OS_BSD_OS_BSD_HPP diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 17cd0d05f9a..5e911ce0f44 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -83,6 +83,7 @@ #endif # include +# include # include # include # include @@ -113,6 +114,7 @@ # include # include # include +# include # include #ifdef __GLIBC__ # include @@ -2161,6 +2163,8 @@ void os::print_os_info(outputStream* st) { os::Posix::print_rlimit_info(st); + os::print_open_file_descriptors(st); + os::Posix::print_load_average(st); st->cr(); @@ -5429,3 +5433,33 @@ bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) { return res; } // end: os::pd_dll_unload() + +void os::print_open_file_descriptors(outputStream* st) { + DIR* dirp = opendir("/proc/self/fd"); + int fds = 0; + struct dirent* dentp; + const jlong TIMEOUT_NS = 50000000L; // 50 ms in nanoseconds + bool timed_out = false; + + // limit proc file read to 50ms + jlong start = os::javaTimeNanos(); + assert(dirp != nullptr, "No proc fs?"); + while ((dentp = readdir(dirp)) != nullptr && !timed_out) { + if (isdigit(dentp->d_name[0])) fds++; + if (fds % 100 == 0) { + jlong now = os::javaTimeNanos(); + if ((now - start) > TIMEOUT_NS) { + timed_out = true; + } + } + } + + closedir(dirp); + if (timed_out) { + st->print_cr("Open File Descriptors: > %d", fds); + } else { + st->print_cr("Open File Descriptors: %d", fds); + } +} + + diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index 76f47640e5a..01162ef930d 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -6276,6 +6276,10 @@ const void* os::get_saved_assert_context(const void** sigInfo) { return nullptr; } +void os::print_open_file_descriptors(outputStream* st) { + // File descriptor counting not supported on Windows. +} + /* * Windows/x64 does not use stack frames the way expected by Java: * [1] in most cases, there is no frame pointer. All locals are addressed via RSP diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index e185188384f..d0544cb5b51 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -893,6 +893,9 @@ class os: AllStatic { static void print_date_and_time(outputStream* st, char* buf, size_t buflen); static void print_elapsed_time(outputStream* st, double time); + // Prints the number of open file descriptors for the current process + static void print_open_file_descriptors(outputStream* st); + static void print_user_info(outputStream* st); static void print_active_locale(outputStream* st); diff --git a/src/hotspot/share/utilities/vmError.cpp b/src/hotspot/share/utilities/vmError.cpp index e79c9cb694e..48fae6868ab 100644 --- a/src/hotspot/share/utilities/vmError.cpp +++ b/src/hotspot/share/utilities/vmError.cpp @@ -1329,6 +1329,13 @@ void VMError::report(outputStream* st, bool _verbose) { STEP_IF("printing OS information", _verbose) os::print_os_info(st); st->cr(); +#ifdef __APPLE__ + // Avoid large stack allocation on Mac for FD count during signal-handling. + os::Bsd::print_open_file_descriptors(st, buf, sizeof(buf)); + st->cr(); +#else + os::print_open_file_descriptors(st); +#endif STEP_IF("printing CPU info", _verbose) os::print_cpu_info(st, buf, sizeof(buf)); @@ -1550,6 +1557,8 @@ void VMError::print_vm_info(outputStream* st) { os::print_os_info(st); st->cr(); + os::print_open_file_descriptors(st); + st->cr(); // STEP("printing CPU info") diff --git a/test/jdk/sun/tools/jcmd/TestJcmdSanity.java b/test/jdk/sun/tools/jcmd/TestJcmdSanity.java index 92f73597ce6..559b6b7d5a4 100644 --- a/test/jdk/sun/tools/jcmd/TestJcmdSanity.java +++ b/test/jdk/sun/tools/jcmd/TestJcmdSanity.java @@ -33,7 +33,6 @@ import java.util.List; import jdk.test.lib.process.OutputAnalyzer; import jdk.test.lib.process.ProcessTools; import jdk.test.lib.Platform; -import jdk.test.lib.Utils; /* * @test @@ -182,5 +181,10 @@ public class TestJcmdSanity { output.shouldNotContain("*** Handler was modified!"); output.shouldNotContain("*** Expected: "); // e.g. *** Expected: javaSignalHandler in ... } + + // Should find file descriptor counting on Mac and Linux + if (Platform.isLinux() || Platform.isOSX()) { + output.shouldMatch("Open File Descriptors: \\d+"); + } } } From 3a109f49feb19f313632be6a2aa24ba7d9b7269b Mon Sep 17 00:00:00 2001 From: David Holmes Date: Tue, 17 Mar 2026 07:18:40 +0000 Subject: [PATCH 08/22] 8380236: macOS build is broken by JDK-8359706 Reviewed-by: kbarrett --- src/hotspot/os/bsd/os_bsd.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index 207968a7a59..a4d9a2197a5 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -2613,7 +2613,7 @@ void os::Bsd::print_open_file_descriptors(outputStream* st, char* buf, size_t bu pid_t my_pid; // ensure the scratch buffer is big enough for at least one FD info struct - assert(buflen >= sizeof(struct proc_fdinfo)); + precond(buflen >= sizeof(struct proc_fdinfo)); kern_return_t kres = pid_for_task(mach_task_self(), &my_pid); if (kres != KERN_SUCCESS) { st->print_cr("Open File Descriptors: unknown"); @@ -2639,4 +2639,4 @@ void os::Bsd::print_open_file_descriptors(outputStream* st, char* buf, size_t bu #else st->print_cr("Open File Descriptors: unknown"); #endif -} \ No newline at end of file +} From 0b17e0093d35972db7c228098d991a684ab6f07f Mon Sep 17 00:00:00 2001 From: Harshit Dhiman Date: Tue, 17 Mar 2026 09:25:00 +0000 Subject: [PATCH 09/22] 8347396: Efficient TypeFunc creations Reviewed-by: dlong, vlivanov, adinn --- .../share/gc/shared/c2/barrierSetC2.cpp | 12 +++++- .../share/gc/shared/c2/barrierSetC2.hpp | 6 +++ .../shenandoah/c2/shenandoahBarrierSetC2.cpp | 40 ++++++++++++++++--- .../shenandoah/c2/shenandoahBarrierSetC2.hpp | 9 +++++ src/hotspot/share/opto/type.cpp | 8 ++++ 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/hotspot/share/gc/shared/c2/barrierSetC2.cpp b/src/hotspot/share/gc/shared/c2/barrierSetC2.cpp index 53577bad1d8..ad7fc31dc09 100644 --- a/src/hotspot/share/gc/shared/c2/barrierSetC2.cpp +++ b/src/hotspot/share/gc/shared/c2/barrierSetC2.cpp @@ -813,7 +813,10 @@ Node* BarrierSetC2::obj_allocate(PhaseMacroExpand* macro, Node* mem, Node* toobi return old_tlab_top; } -static const TypeFunc* clone_type() { +const TypeFunc* BarrierSetC2::_clone_type_Type = nullptr; + +void BarrierSetC2::make_clone_type() { + assert(BarrierSetC2::_clone_type_Type == nullptr, "should be"); // Create input type (domain) int argcnt = NOT_LP64(3) LP64_ONLY(4); const Type** const domain_fields = TypeTuple::fields(argcnt); @@ -829,7 +832,12 @@ static const TypeFunc* clone_type() { const Type** const range_fields = TypeTuple::fields(0); const TypeTuple* const range = TypeTuple::make(TypeFunc::Parms + 0, range_fields); - return TypeFunc::make(domain, range); + BarrierSetC2::_clone_type_Type = TypeFunc::make(domain, range); +} + +inline const TypeFunc* BarrierSetC2::clone_type() { + assert(BarrierSetC2::_clone_type_Type != nullptr, "should be initialized"); + return BarrierSetC2::_clone_type_Type; } #define XTOP LP64_ONLY(COMMA phase->top()) diff --git a/src/hotspot/share/gc/shared/c2/barrierSetC2.hpp b/src/hotspot/share/gc/shared/c2/barrierSetC2.hpp index 7b9cb985cff..a486a88c48f 100644 --- a/src/hotspot/share/gc/shared/c2/barrierSetC2.hpp +++ b/src/hotspot/share/gc/shared/c2/barrierSetC2.hpp @@ -270,6 +270,9 @@ public: // various GC barrier sets inherit from the BarrierSetC2 class to sprinkle // barriers into the accesses. class BarrierSetC2: public CHeapObj { +private: + static const TypeFunc* _clone_type_Type; + protected: virtual void resolve_address(C2Access& access) const; virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const; @@ -379,6 +382,9 @@ public: static int arraycopy_payload_base_offset(bool is_array); + static void make_clone_type(); + static const TypeFunc* clone_type(); + #ifndef PRODUCT virtual void dump_barrier_data(const MachNode* mach, outputStream* st) const { st->print("%x", mach->barrier_data()); diff --git a/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.cpp b/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.cpp index fdfde866cd7..1947c75314e 100644 --- a/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.cpp +++ b/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.cpp @@ -519,7 +519,33 @@ void ShenandoahBarrierSetC2::post_barrier(GraphKit* kit, #undef __ -const TypeFunc* ShenandoahBarrierSetC2::write_barrier_pre_Type() { +const TypeFunc* ShenandoahBarrierSetC2::_write_barrier_pre_Type = nullptr; +const TypeFunc* ShenandoahBarrierSetC2::_clone_barrier_Type = nullptr; +const TypeFunc* ShenandoahBarrierSetC2::_load_reference_barrier_Type = nullptr; + +inline const TypeFunc* ShenandoahBarrierSetC2::write_barrier_pre_Type() { + assert(ShenandoahBarrierSetC2::_write_barrier_pre_Type != nullptr, "should be initialized"); + return ShenandoahBarrierSetC2::_write_barrier_pre_Type; +} + +inline const TypeFunc* ShenandoahBarrierSetC2::clone_barrier_Type() { + assert(ShenandoahBarrierSetC2::_clone_barrier_Type != nullptr, "should be initialized"); + return ShenandoahBarrierSetC2::_clone_barrier_Type; +} + +const TypeFunc* ShenandoahBarrierSetC2::load_reference_barrier_Type() { + assert(ShenandoahBarrierSetC2::_load_reference_barrier_Type != nullptr, "should be initialized"); + return ShenandoahBarrierSetC2::_load_reference_barrier_Type; +} + +void ShenandoahBarrierSetC2::init() { + ShenandoahBarrierSetC2::make_write_barrier_pre_Type(); + ShenandoahBarrierSetC2::make_clone_barrier_Type(); + ShenandoahBarrierSetC2::make_load_reference_barrier_Type(); +} + +void ShenandoahBarrierSetC2::make_write_barrier_pre_Type() { + assert(ShenandoahBarrierSetC2::_write_barrier_pre_Type == nullptr, "should be"); const Type **fields = TypeTuple::fields(1); fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); @@ -528,10 +554,11 @@ const TypeFunc* ShenandoahBarrierSetC2::write_barrier_pre_Type() { fields = TypeTuple::fields(0); const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); - return TypeFunc::make(domain, range); + ShenandoahBarrierSetC2::_write_barrier_pre_Type = TypeFunc::make(domain, range); } -const TypeFunc* ShenandoahBarrierSetC2::clone_barrier_Type() { +void ShenandoahBarrierSetC2::make_clone_barrier_Type() { + assert(ShenandoahBarrierSetC2::_clone_barrier_Type == nullptr, "should be"); const Type **fields = TypeTuple::fields(1); fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); @@ -540,10 +567,11 @@ const TypeFunc* ShenandoahBarrierSetC2::clone_barrier_Type() { fields = TypeTuple::fields(0); const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); - return TypeFunc::make(domain, range); + ShenandoahBarrierSetC2::_clone_barrier_Type = TypeFunc::make(domain, range); } -const TypeFunc* ShenandoahBarrierSetC2::load_reference_barrier_Type() { +void ShenandoahBarrierSetC2::make_load_reference_barrier_Type() { + assert(ShenandoahBarrierSetC2::_load_reference_barrier_Type == nullptr, "should be"); const Type **fields = TypeTuple::fields(2); fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; // original field value fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // original load address @@ -555,7 +583,7 @@ const TypeFunc* ShenandoahBarrierSetC2::load_reference_barrier_Type() { fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields); - return TypeFunc::make(domain, range); + ShenandoahBarrierSetC2::_load_reference_barrier_Type = TypeFunc::make(domain, range); } Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const { diff --git a/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.hpp b/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.hpp index dd9e9bcc1a5..108eaa0998b 100644 --- a/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.hpp +++ b/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.hpp @@ -82,6 +82,13 @@ private: static bool clone_needs_barrier(Node* src, PhaseGVN& gvn); + static const TypeFunc* _write_barrier_pre_Type; + static const TypeFunc* _clone_barrier_Type; + static const TypeFunc* _load_reference_barrier_Type; + static void make_write_barrier_pre_Type(); + static void make_clone_barrier_Type(); + static void make_load_reference_barrier_Type(); + protected: virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const; virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const; @@ -106,6 +113,8 @@ public: static const TypeFunc* write_barrier_pre_Type(); static const TypeFunc* clone_barrier_Type(); static const TypeFunc* load_reference_barrier_Type(); + static void init(); + virtual bool has_load_barrier_nodes() const { return true; } // This is the entry-point for the backend to perform accesses through the Access API. diff --git a/src/hotspot/share/opto/type.cpp b/src/hotspot/share/opto/type.cpp index 1a0872ee0e6..aab2ea3cd3b 100644 --- a/src/hotspot/share/opto/type.cpp +++ b/src/hotspot/share/opto/type.cpp @@ -49,6 +49,9 @@ #include "utilities/ostream.hpp" #include "utilities/powerOfTwo.hpp" #include "utilities/stringUtils.hpp" +#if INCLUDE_SHENANDOAHGC +#include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp" +#endif // INCLUDE_SHENANDOAHGC // Portions of code courtesy of Clifford Click @@ -732,6 +735,11 @@ void Type::Initialize_shared(Compile* current) { mreg2type[Op_VecY] = TypeVect::VECTY; mreg2type[Op_VecZ] = TypeVect::VECTZ; +#if INCLUDE_SHENANDOAHGC + ShenandoahBarrierSetC2::init(); +#endif //INCLUDE_SHENANDOAHGC + + BarrierSetC2::make_clone_type(); LockNode::initialize_lock_Type(); ArrayCopyNode::initialize_arraycopy_Type(); OptoRuntime::initialize_types(); From da85cafd796655c13e75e8bf49159386605285d5 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 17 Mar 2026 16:11:38 +0000 Subject: [PATCH 10/22] 8380198: Convert java/util/prefs/PrefsSpiTest.java to JUnit Reviewed-by: jlu --- test/jdk/java/util/prefs/PrefsSpiTest.java | 39 +++++++++++----------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/test/jdk/java/util/prefs/PrefsSpiTest.java b/test/jdk/java/util/prefs/PrefsSpiTest.java index 0369c60cae2..08fff81a552 100644 --- a/test/jdk/java/util/prefs/PrefsSpiTest.java +++ b/test/jdk/java/util/prefs/PrefsSpiTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, 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 @@ -28,7 +28,7 @@ * @library /test/lib * @build jdk.test.lib.util.JarUtils jdk.test.lib.process.* * PrefsSpi StubPreferencesFactory StubPreferences - * @run testng PrefsSpiTest + * @run junit PrefsSpiTest */ import java.io.File; @@ -36,16 +36,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; import jdk.test.lib.JDKToolFinder; import jdk.test.lib.Utils; import jdk.test.lib.process.ProcessTools; import jdk.test.lib.util.JarUtils; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static java.nio.file.StandardOpenOption.CREATE; import static java.util.Arrays.asList; @@ -53,14 +50,19 @@ import static java.util.Arrays.asList; import static jdk.test.lib.Utils.TEST_CLASSES; import static jdk.test.lib.Utils.TEST_CLASS_PATH; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + public class PrefsSpiTest { private static final Path SPIJAR = Path.of("extDir", "PrefsSpi.jar"); private static final String SPIJAR_CP = TEST_CLASS_PATH + File.pathSeparator + SPIJAR.toString(); - @BeforeClass - public void initialize() throws Exception { + @BeforeAll + public static void initialize() throws Exception { Path xdir = Path.of("jarDir"); Path config = xdir.resolve("META-INF/services/java.util.prefs.PreferencesFactory"); @@ -77,19 +79,18 @@ public class PrefsSpiTest { JarUtils.createJarFile(SPIJAR, xdir); } - @DataProvider - public Object[][] testCases() { - return new Object[][]{ - // CLI options, runtime arguments - {List.of("-cp", SPIJAR_CP, - "-Djava.util.prefs.PreferencesFactory=StubPreferencesFactory"), - "StubPreferences"}, - {List.of("-cp", TEST_CLASS_PATH), "java.util.prefs.*"}, - {List.of("-cp", SPIJAR_CP), "StubPreferences"} - }; + public static Stream testCases() { + return Stream.of + (// CLI options, runtime arguments + Arguments.of(List.of("-cp", SPIJAR_CP, + "-Djava.util.prefs.PreferencesFactory=StubPreferencesFactory"), + "StubPreferences"), + Arguments.of(List.of("-cp", TEST_CLASS_PATH), "java.util.prefs.*"), + Arguments.of(List.of("-cp", SPIJAR_CP), "StubPreferences")); } - @Test(dataProvider = "testCases") + @ParameterizedTest + @MethodSource("testCases") public void testProvider(List opts, String pattern) throws Throwable { List args = new ArrayList<>(); args.add(JDKToolFinder.getJDKTool("java")); From 773c375f1813afb0acf6bd1731eac39d5a43d0b4 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 17 Mar 2026 16:12:02 +0000 Subject: [PATCH 11/22] 8380221: Change jdk/nio/Basic.java to use JUnit Reviewed-by: jlu, alanb --- test/jdk/jdk/nio/Basic.java | 81 +++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/test/jdk/jdk/nio/Basic.java b/test/jdk/jdk/nio/Basic.java index 02fcfbc4a4d..68f8f00e309 100644 --- a/test/jdk/jdk/nio/Basic.java +++ b/test/jdk/jdk/nio/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, 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 @@ -25,7 +25,7 @@ * @test * @bug 8198372 * @modules jdk.net java.base/sun.nio.ch:+open - * @run testng Basic + * @run junit Basic * @summary Basic tests for jdk.nio.Channels */ @@ -48,10 +48,14 @@ import jdk.nio.Channels.SelectableChannelCloser; import sun.nio.ch.IOUtil; -import org.testng.annotations.Test; -import static org.testng.Assert.*; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -@Test public class Basic { /** @@ -119,6 +123,7 @@ public class Basic { /** * Basic test of channel registered with Selector */ + @Test public void testSelect() throws IOException { Selector sel = Selector.open(); try (Connection connection = Connection.open()) { @@ -131,7 +136,7 @@ public class Basic { ch.configureBlocking(false); SelectionKey key = ch.register(sel, SelectionKey.OP_READ); int n = sel.selectNow(); - assertTrue(n == 0); + assertEquals(0, n); // write bytes to other end of connection SocketChannel peer = connection.channel2(); @@ -141,7 +146,7 @@ public class Basic { // channel should be selected n = sel.select(); - assertTrue(n == 1); + assertEquals(1, n); assertTrue(sel.selectedKeys().contains(key)); assertTrue(key.isReadable()); assertFalse(key.isWritable()); @@ -150,7 +155,7 @@ public class Basic { // change interest set for writing, channel should be selected key.interestOps(SelectionKey.OP_WRITE); n = sel.select(); - assertTrue(n == 1); + assertEquals(1, n); assertTrue(sel.selectedKeys().contains(key)); assertTrue(key.isWritable()); assertFalse(key.isReadable()); @@ -159,7 +164,7 @@ public class Basic { // change interest set for reading + writing, channel should be selected key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); n = sel.select(); - assertTrue(n == 1); + assertEquals(1, n); assertTrue(sel.selectedKeys().contains(key)); assertTrue(key.isWritable()); assertTrue(key.isReadable()); @@ -168,7 +173,7 @@ public class Basic { // change interest set to 0 to deregister, channel should not be selected key.interestOps(0); n = sel.selectNow(); - assertTrue(n == 0); + assertEquals(0, n); } finally { sel.close(); @@ -178,6 +183,7 @@ public class Basic { /** * Test that the SelectableChannelCloser implCloseChannel method is invoked. */ + @Test public void testImplCloseChannel() throws IOException { try (Connection connection = Connection.open()) { FileDescriptor fd = getFD(connection.channel1()); @@ -189,11 +195,11 @@ public class Basic { ch.close(); // implCloseChannel should been invoked once - assertTrue(closer.closeCount == 1); - assertTrue(closer.invokedToClose == ch); + assertEquals(1, closer.closeCount); + assertSame(ch, closer.invokedToClose); // implReleaseChannel should not have been invoked - assertTrue(closer.releaseCount == 0); + assertEquals(0, closer.releaseCount); } } } @@ -201,6 +207,7 @@ public class Basic { /** * Test that the SelectableChannelCloser implReleaseChannel method is invoked. */ + @Test public void testImplReleaseChannel() throws IOException { Selector sel = Selector.open(); try (Connection connection = Connection.open()) { @@ -217,50 +224,53 @@ public class Basic { ch.close(); // implCloseChannel should have been invoked - assertTrue(closer.closeCount == 1); - assertTrue(closer.invokedToClose == ch); + assertEquals(1, closer.closeCount); + assertSame(ch, closer.invokedToClose); // implReleaseChannel should not have been invoked - assertTrue(closer.releaseCount == 0); + assertEquals(0, closer.releaseCount); // flush the selector sel.selectNow(); // implReleaseChannel should have been invoked - assertTrue(closer.releaseCount == 1); - assertTrue(closer.invokedToRelease == ch); + assertEquals(1, closer.releaseCount); + assertSame(ch, closer.invokedToRelease); } finally { sel.close(); } } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test public void testInvalidFileDescriptor() throws IOException { FileDescriptor fd = IOUtil.newFD(-1); - Channels.readWriteSelectableChannel(fd, new SelectableChannelCloser() { - @Override - public void implCloseChannel(SelectableChannel sc) { } - @Override - public void implReleaseChannel(SelectableChannel sc) { } - }); + assertThrows + (IllegalArgumentException.class, + () -> Channels.readWriteSelectableChannel(fd, new SelectableChannelCloser() { + @Override + public void implCloseChannel(SelectableChannel sc) { } + @Override + public void implReleaseChannel(SelectableChannel sc) { }})); } - @Test(expectedExceptions = NullPointerException.class) + @Test public void testNullFileDescriptor() throws IOException { - Channels.readWriteSelectableChannel(null, new SelectableChannelCloser() { - @Override - public void implCloseChannel(SelectableChannel sc) { } - @Override - public void implReleaseChannel(SelectableChannel sc) { } - }); + assertThrows + (NullPointerException.class, + () -> Channels.readWriteSelectableChannel(null, new SelectableChannelCloser() { + @Override + public void implCloseChannel(SelectableChannel sc) { } + @Override + public void implReleaseChannel(SelectableChannel sc) { }})); } - @Test(expectedExceptions = NullPointerException.class) + @Test public void testNullCloser() throws IOException { try (Connection connection = Connection.open()) { FileDescriptor fd = getFD(connection.channel1()); - Channels.readWriteSelectableChannel(fd, null); + assertThrows(NullPointerException.class, + () -> Channels.readWriteSelectableChannel(fd, null)); } } @@ -271,7 +281,8 @@ public class Basic { f.setAccessible(true); return (FileDescriptor) f.get(sc); } catch (Exception e) { - throw new Error(e); + fail(e); + return null; // appease compiler } } } From 9394749f9e788fc538a7876e338e536aa296b70c Mon Sep 17 00:00:00 2001 From: Leo Korinth Date: Tue, 17 Mar 2026 16:16:56 +0000 Subject: [PATCH 12/22] 8377769: Only use large pages sizes that have any pages configured Reviewed-by: ayang, stefank --- src/hotspot/os/linux/hugepages.cpp | 26 +++++++++++++++++++++++--- src/hotspot/os/linux/hugepages.hpp | 6 +++++- src/hotspot/os/linux/os_linux.cpp | 10 +++++----- src/hotspot/share/runtime/os.hpp | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/hotspot/os/linux/hugepages.cpp b/src/hotspot/os/linux/hugepages.cpp index 5472c093d3f..a78f355c179 100644 --- a/src/hotspot/os/linux/hugepages.cpp +++ b/src/hotspot/os/linux/hugepages.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2024, Red Hat Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -35,13 +35,18 @@ #include ExplicitHugePageSupport::ExplicitHugePageSupport() : - _initialized(false), _pagesizes(), _default_hugepage_size(SIZE_MAX), _inconsistent(false) {} + _initialized(false), _pagesizes(), _pre_allocated_pagesizes(), _default_hugepage_size(SIZE_MAX), _inconsistent(false) {} os::PageSizes ExplicitHugePageSupport::pagesizes() const { assert(_initialized, "Not initialized"); return _pagesizes; } +os::PageSizes ExplicitHugePageSupport::pre_allocated_pagesizes() const { + assert(_initialized, "Not initialized"); + return _pre_allocated_pagesizes; +} + size_t ExplicitHugePageSupport::default_hugepage_size() const { assert(_initialized, "Not initialized"); return _default_hugepage_size; @@ -129,6 +134,20 @@ static os::PageSizes scan_hugepages() { return pagesizes; } +static os::PageSizes filter_pre_allocated_hugepages(os::PageSizes pagesizes) { + os::PageSizes pre_allocated{}; + char filename[PATH_MAX]; + for (size_t ps = pagesizes.smallest(); ps != 0; ps = pagesizes.next_larger(ps)) { + os::snprintf_checked(filename, sizeof(filename), "%s/hugepages-%zukB/nr_hugepages", sys_hugepages, ps / K); + size_t pages; + bool read_success = read_number_file(filename, &pages); + if (read_success && pages > 0) { + pre_allocated.add(ps); + } + } + return pre_allocated; +} + void ExplicitHugePageSupport::print_on(outputStream* os) { if (_initialized) { os->print_cr("Explicit hugepage support:"); @@ -148,13 +167,14 @@ void ExplicitHugePageSupport::scan_os() { _default_hugepage_size = scan_default_hugepagesize(); if (_default_hugepage_size > 0) { _pagesizes = scan_hugepages(); + _pre_allocated_pagesizes = filter_pre_allocated_hugepages(_pagesizes); // See https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt: /proc/meminfo should match // /sys/kernel/mm/hugepages/hugepages-xxxx. However, we may run on a broken kernel (e.g. on WSL) // that only exposes /proc/meminfo but not /sys/kernel/mm/hugepages. In that case, we are not // sure about the state of hugepage support by the kernel, so we won't use explicit hugepages. if (!_pagesizes.contains(_default_hugepage_size)) { log_info(pagesize)("Unexpected configuration: default pagesize (%zu) " - "has no associated directory in /sys/kernel/mm/hugepages..", _default_hugepage_size); + "has no associated directory in /sys/kernel/mm/hugepages.", _default_hugepage_size); _inconsistent = true; } } diff --git a/src/hotspot/os/linux/hugepages.hpp b/src/hotspot/os/linux/hugepages.hpp index efd27c55fd6..5c190feae97 100644 --- a/src/hotspot/os/linux/hugepages.hpp +++ b/src/hotspot/os/linux/hugepages.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2024, Red Hat Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -47,6 +47,9 @@ class ExplicitHugePageSupport { // in /sys/kernel/mm/hugepages/hugepage-xxx) os::PageSizes _pagesizes; + // Above pages filtered for where the contents of file nr_hugepages was larger than zero + os::PageSizes _pre_allocated_pagesizes; + // Contains the default hugepage. The "default hugepage size" is the one that // - is marked in /proc/meminfo as "Hugepagesize" // - is the size one gets when using mmap(MAP_HUGETLB) when omitting size specifiers like MAP_HUGE_SHIFT) @@ -61,6 +64,7 @@ public: void scan_os(); os::PageSizes pagesizes() const; + os::PageSizes pre_allocated_pagesizes() const; size_t default_hugepage_size() const; void print_on(outputStream* os); diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 5e911ce0f44..7bde0ae08c9 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -3819,7 +3819,7 @@ static int hugetlbfs_page_size_flag(size_t page_size) { static bool hugetlbfs_sanity_check(size_t page_size) { const os::PageSizes page_sizes = HugePages::explicit_hugepage_info().pagesizes(); - assert(page_sizes.contains(page_size), "Invalid page sizes passed"); + assert(page_sizes.contains(page_size), "Invalid page sizes passed (%zu)", page_size); // Include the page size flag to ensure we sanity check the correct page size. int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | hugetlbfs_page_size_flag(page_size); @@ -4066,10 +4066,10 @@ void os::Linux::large_page_init() { _large_page_size = large_page_size; - // Populate _page_sizes with large page sizes less than or equal to - // _large_page_size. - for (size_t page_size = _large_page_size; page_size != 0; - page_size = all_large_pages.next_smaller(page_size)) { + // Populate _page_sizes with _large_page_size (default large page size) even if not pre-allocated. + // Then, populate _page_sizes with all smaller large page sizes that have been pre-allocated. + os::PageSizes pre_allocated = HugePages::explicit_hugepage_info().pre_allocated_pagesizes(); + for (size_t page_size = _large_page_size; page_size != 0; page_size = pre_allocated.next_smaller(page_size)) { _page_sizes.add(page_size); } } diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index d0544cb5b51..76041f256a9 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -454,7 +454,7 @@ class os: AllStatic { static size_t align_down_vm_page_size(size_t size) { return align_down(size, os::vm_page_size()); } // The set of page sizes which the VM is allowed to use (may be a subset of - // the page sizes actually available on the platform). + // the page sizes actually available on the platform). static const PageSizes& page_sizes() { return _page_sizes; } // Returns the page size to use for a region of memory. From ee90f00b3b38b7cf4da340deb48f04bdaee22710 Mon Sep 17 00:00:00 2001 From: Ioi Lam Date: Tue, 17 Mar 2026 17:21:16 +0000 Subject: [PATCH 13/22] 8376822: UseCompactObjectHeaders: fill Klass alignment gaps in AOT cache Reviewed-by: jsjolen, asmehra --- src/hotspot/share/cds/aotMapLogger.cpp | 52 +++-- src/hotspot/share/cds/aotMapLogger.hpp | 7 +- src/hotspot/share/cds/archiveBuilder.cpp | 35 +--- src/hotspot/share/cds/archiveUtils.cpp | 185 +++++++++++++++++- src/hotspot/share/cds/archiveUtils.hpp | 18 ++ src/hotspot/share/cds/dumpAllocStats.cpp | 14 +- src/hotspot/share/cds/dumpAllocStats.hpp | 11 +- src/hotspot/share/utilities/rbTree.hpp | 7 +- .../jtreg/runtime/cds/MetaspaceAllocGaps.java | 73 +++++++ 9 files changed, 324 insertions(+), 78 deletions(-) create mode 100644 test/hotspot/jtreg/runtime/cds/MetaspaceAllocGaps.java diff --git a/src/hotspot/share/cds/aotMapLogger.cpp b/src/hotspot/share/cds/aotMapLogger.cpp index fa769aee1bf..98336ff9b1f 100644 --- a/src/hotspot/share/cds/aotMapLogger.cpp +++ b/src/hotspot/share/cds/aotMapLogger.cpp @@ -98,8 +98,8 @@ void AOTMapLogger::dumptime_log(ArchiveBuilder* builder, FileMapInfo* mapinfo, DumpRegion* rw_region = &builder->_rw_region; DumpRegion* ro_region = &builder->_ro_region; - dumptime_log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs); - dumptime_log_metaspace_region("ro region", ro_region, &builder->_ro_src_objs); + dumptime_log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs, &builder->_ro_src_objs); + dumptime_log_metaspace_region("ro region", ro_region, &builder->_rw_src_objs, &builder->_ro_src_objs); address bitmap_end = address(bitmap + bitmap_size_in_bytes); log_region_range("bitmap", address(bitmap), bitmap_end, nullptr); @@ -122,17 +122,6 @@ void AOTMapLogger::dumptime_log(ArchiveBuilder* builder, FileMapInfo* mapinfo, class AOTMapLogger::RuntimeGatherArchivedMetaspaceObjs : public UniqueMetaspaceClosure { GrowableArrayCHeap _objs; - static int compare_objs_by_addr(ArchivedObjInfo* a, ArchivedObjInfo* b) { - intx diff = a->_src_addr - b->_src_addr; - if (diff < 0) { - return -1; - } else if (diff == 0) { - return 0; - } else { - return 1; - } - } - public: GrowableArrayCHeap* objs() { return &_objs; } @@ -152,7 +141,7 @@ public: void finish() { UniqueMetaspaceClosure::finish(); - _objs.sort(compare_objs_by_addr); + _objs.sort(compare_by_address); } }; // AOTMapLogger::RuntimeGatherArchivedMetaspaceObjs @@ -203,24 +192,47 @@ void AOTMapLogger::runtime_log(FileMapInfo* mapinfo, GrowableArrayCHeapbase()); address region_top = address(region->top()); log_region_range(name, region_base, region_top, region_base + _buffer_to_requested_delta); if (log_is_enabled(Debug, aot, map)) { GrowableArrayCHeap objs; - for (int i = 0; i < src_objs->objs()->length(); i++) { - ArchiveBuilder::SourceObjInfo* src_info = src_objs->at(i); + // With -XX:+UseCompactObjectHeaders, it's possible for small objects (including some from + // ro_objs) to be allocated in the gaps in the RW region. + collect_metaspace_objs(&objs, region_base, region_top, rw_objs); + collect_metaspace_objs(&objs, region_base, region_top, ro_objs); + objs.sort(compare_by_address); + log_metaspace_objects_impl(address(region->base()), address(region->end()), &objs, 0, objs.length()); + } +} + +void AOTMapLogger::collect_metaspace_objs(GrowableArrayCHeap* objs, + address region_base, address region_top , + const ArchiveBuilder::SourceObjList* src_objs) { + for (int i = 0; i < src_objs->objs()->length(); i++) { + ArchiveBuilder::SourceObjInfo* src_info = src_objs->at(i); + address buf_addr = src_info->buffered_addr(); + if (region_base <= buf_addr && buf_addr < region_top) { ArchivedObjInfo info; info._src_addr = src_info->source_addr(); - info._buffered_addr = src_info->buffered_addr(); + info._buffered_addr = buf_addr; info._requested_addr = info._buffered_addr + _buffer_to_requested_delta; info._bytes = src_info->size_in_bytes(); info._type = src_info->type(); - objs.append(info); + objs->append(info); } + } +} - log_metaspace_objects_impl(address(region->base()), address(region->end()), &objs, 0, objs.length()); +int AOTMapLogger::compare_by_address(ArchivedObjInfo* a, ArchivedObjInfo* b) { + if (a->_buffered_addr < b->_buffered_addr) { + return -1; + } else if (a->_buffered_addr > b->_buffered_addr) { + return 1; + } else { + return 0; } } diff --git a/src/hotspot/share/cds/aotMapLogger.hpp b/src/hotspot/share/cds/aotMapLogger.hpp index f495ed97f40..0a89f1e5012 100644 --- a/src/hotspot/share/cds/aotMapLogger.hpp +++ b/src/hotspot/share/cds/aotMapLogger.hpp @@ -127,7 +127,12 @@ private: static void runtime_log(FileMapInfo* mapinfo, GrowableArrayCHeap* objs); static void runtime_log_metaspace_regions(FileMapInfo* mapinfo, GrowableArrayCHeap* objs); static void dumptime_log_metaspace_region(const char* name, DumpRegion* region, - const ArchiveBuilder::SourceObjList* src_objs); + const ArchiveBuilder::SourceObjList* rw_objs, + const ArchiveBuilder::SourceObjList* ro_objs); + static void collect_metaspace_objs(GrowableArrayCHeap* objs, + address region_base, address region_top , + const ArchiveBuilder::SourceObjList* src_objs); + static int compare_by_address(ArchivedObjInfo* a, ArchivedObjInfo* b); // Common code for dumptime/runtime static void log_file_header(FileMapInfo* mapinfo); diff --git a/src/hotspot/share/cds/archiveBuilder.cpp b/src/hotspot/share/cds/archiveBuilder.cpp index 0ea5d6c6ecb..b2d6600e44f 100644 --- a/src/hotspot/share/cds/archiveBuilder.cpp +++ b/src/hotspot/share/cds/archiveBuilder.cpp @@ -627,6 +627,7 @@ void ArchiveBuilder::dump_ro_metadata() { start_dump_region(&_ro_region); make_shallow_copies(&_ro_region, &_ro_src_objs); RegeneratedClasses::record_regenerated_objects(); + DumpRegion::report_gaps(&_alloc_stats); } void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region, @@ -639,33 +640,10 @@ void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region, void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) { address src = src_info->source_addr(); - int bytes = src_info->size_in_bytes(); // word-aligned - size_t alignment = SharedSpaceObjectAlignment; // alignment for the dest pointer + int bytes = src_info->size_in_bytes(); + char* dest = dump_region->allocate_metaspace_obj(bytes, src, src_info->type(), + src_info->read_only(), &_alloc_stats); - char* oldtop = dump_region->top(); - if (src_info->type() == MetaspaceClosureType::ClassType) { - // Allocate space for a pointer directly in front of the future InstanceKlass, so - // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo* - // without building another hashtable. See RunTimeClassInfo::get_for() - // in systemDictionaryShared.cpp. - Klass* klass = (Klass*)src; - if (klass->is_instance_klass()) { - SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); - dump_region->allocate(sizeof(address)); - } -#ifdef _LP64 - // More strict alignments needed for UseCompressedClassPointers - if (UseCompressedClassPointers) { - alignment = nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift()); - } -#endif - } else if (src_info->type() == MetaspaceClosureType::SymbolType) { - // Symbols may be allocated by using AllocateHeap, so their sizes - // may be less than size_in_bytes() indicates. - bytes = ((Symbol*)src)->byte_size(); - } - - char* dest = dump_region->allocate(bytes, alignment); memcpy(dest, src, bytes); // Update the hash of buffered sorted symbols for static dump so that the symbols have deterministic contents @@ -692,11 +670,6 @@ void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* s log_trace(aot)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes); src_info->set_buffered_addr((address)dest); - - char* newtop = dump_region->top(); - _alloc_stats.record(src_info->type(), int(newtop - oldtop), src_info->read_only()); - - DEBUG_ONLY(_alloc_stats.verify((int)dump_region->used(), src_info->read_only())); } // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are diff --git a/src/hotspot/share/cds/archiveUtils.cpp b/src/hotspot/share/cds/archiveUtils.cpp index ea9bde8eb8d..f79b1e134e8 100644 --- a/src/hotspot/share/cds/archiveUtils.cpp +++ b/src/hotspot/share/cds/archiveUtils.cpp @@ -30,6 +30,7 @@ #include "cds/cdsConfig.hpp" #include "cds/classListParser.hpp" #include "cds/classListWriter.hpp" +#include "cds/dumpAllocStats.hpp" #include "cds/dynamicArchive.hpp" #include "cds/filemap.hpp" #include "cds/heapShared.hpp" @@ -46,6 +47,7 @@ #include "utilities/debug.hpp" #include "utilities/formatBuffer.hpp" #include "utilities/globalDefinitions.hpp" +#include "utilities/rbTree.inline.hpp" #include "utilities/spinYield.hpp" CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr; @@ -116,13 +118,17 @@ void ArchivePtrMarker::mark_pointer(address* ptr_loc) { if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) { address value = *ptr_loc; - // We don't want any pointer that points to very bottom of the archive, otherwise when - // AOTMetaspace::default_base_address()==0, we can't distinguish between a pointer - // to nothing (null) vs a pointer to an objects that happens to be at the very bottom - // of the archive. - assert(value != (address)ptr_base(), "don't point to the bottom of the archive"); - if (value != nullptr) { + // We don't want any pointer that points to very bottom of the AOT metaspace, otherwise + // when AOTMetaspace::default_base_address()==0, we can't distinguish between a pointer + // to nothing (null) vs a pointer to an objects that happens to be at the very bottom + // of the AOT metaspace. + // + // This should never happen because the protection zone prevents any valid objects from + // being allocated at the bottom of the AOT metaspace. + assert(AOTMetaspace::protection_zone_size() > 0, "must be"); + assert(ArchiveBuilder::current()->any_to_offset(value) > 0, "cannot point to bottom of AOT metaspace"); + assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); size_t idx = ptr_loc - ptr_base(); if (_ptrmap->size() <= idx) { @@ -130,7 +136,6 @@ void ArchivePtrMarker::mark_pointer(address* ptr_loc) { } assert(idx < _ptrmap->size(), "must be"); _ptrmap->set_bit(idx); - //tty->print_cr("Marking pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ %5zu", p2i(ptr_loc), p2i(*ptr_loc), idx); } } } @@ -144,7 +149,6 @@ void ArchivePtrMarker::clear_pointer(address* ptr_loc) { size_t idx = ptr_loc - ptr_base(); assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked"); _ptrmap->clear_bit(idx); - //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ %5zu", p2i(ptr_loc), p2i(*ptr_loc), idx); } class ArchivePtrBitmapCleaner: public BitMapClosure { @@ -249,16 +253,179 @@ void DumpRegion::commit_to(char* newtop) { which, commit, _vs->actual_committed_size(), _vs->high()); } +// Basic allocation. Any alignment gaps will be wasted. char* DumpRegion::allocate(size_t num_bytes, size_t alignment) { // Always align to at least minimum alignment alignment = MAX2(SharedSpaceObjectAlignment, alignment); char* p = (char*)align_up(_top, alignment); - char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment); + char* newtop = p + align_up(num_bytes, SharedSpaceObjectAlignment); expand_top_to(newtop); memset(p, 0, newtop - p); return p; } +class DumpRegion::AllocGap { + size_t _gap_bytes; // size of this gap in bytes + char* _gap_bottom; // must be SharedSpaceObjectAlignment aligned +public: + size_t gap_bytes() const { return _gap_bytes; } + char* gap_bottom() const { return _gap_bottom; } + + AllocGap(size_t bytes, char* bottom) : _gap_bytes(bytes), _gap_bottom(bottom) { + precond(is_aligned(gap_bytes(), SharedSpaceObjectAlignment)); + precond(is_aligned(gap_bottom(), SharedSpaceObjectAlignment)); + } +}; + +struct DumpRegion::AllocGapCmp { + static RBTreeOrdering cmp(AllocGap a, AllocGap b) { + RBTreeOrdering order = rbtree_primitive_cmp(a.gap_bytes(), b.gap_bytes()); + if (order == RBTreeOrdering::EQ) { + order = rbtree_primitive_cmp(a.gap_bottom(), b.gap_bottom()); + } + return order; + } +}; + +struct Empty {}; +using AllocGapNode = RBNode; + +class DumpRegion::AllocGapTree : public RBTreeCHeap { +public: + size_t add_gap(char* gap_bottom, char* gap_top) { + precond(gap_bottom < gap_top); + size_t gap_bytes = pointer_delta(gap_top, gap_bottom, 1); + precond(gap_bytes > 0); + + _total_gap_bytes += gap_bytes; + + AllocGap gap(gap_bytes, gap_bottom); // constructor checks alignment + AllocGapNode* node = allocate_node(gap, Empty{}); + insert(gap, node); + + log_trace(aot, alloc)("adding a gap of %zu bytes @ %p (total = %zu) in %zu blocks", gap_bytes, gap_bottom, _total_gap_bytes, size()); + return gap_bytes; + } + + char* allocate_from_gap(size_t num_bytes) { + // The gaps are sorted in ascending order of their sizes. When two gaps have the same + // size, the one with a lower gap_bottom comes first. + // + // Find the first gap that's big enough, with the lowest gap_bottom. + AllocGap target(num_bytes, nullptr); + AllocGapNode* node = closest_ge(target); + if (node == nullptr) { + return nullptr; // Didn't find any usable gap. + } + + size_t gap_bytes = node->key().gap_bytes(); + char* gap_bottom = node->key().gap_bottom(); + char* result = gap_bottom; + precond(is_aligned(result, SharedSpaceObjectAlignment)); + + remove(node); + + precond(_total_gap_bytes >= num_bytes); + _total_gap_bytes -= num_bytes; + _total_gap_bytes_used += num_bytes; + _total_gap_allocs++; + DEBUG_ONLY(node = nullptr); // Don't use it anymore! + + precond(gap_bytes >= num_bytes); + if (gap_bytes > num_bytes) { + gap_bytes -= num_bytes; + gap_bottom += num_bytes; + + AllocGap gap(gap_bytes, gap_bottom); // constructor checks alignment + AllocGapNode* new_node = allocate_node(gap, Empty{}); + insert(gap, new_node); + } + log_trace(aot, alloc)("%zu bytes @ %p in a gap of %zu bytes (used gaps %zu times, remain gap = %zu bytes in %zu blocks)", + num_bytes, result, gap_bytes, _total_gap_allocs, _total_gap_bytes, size()); + return result; + } +}; + +size_t DumpRegion::_total_gap_bytes = 0; +size_t DumpRegion::_total_gap_bytes_used = 0; +size_t DumpRegion::_total_gap_allocs = 0; +DumpRegion::AllocGapTree DumpRegion::_gap_tree; + +// Alignment gaps happen only for the RW space. Collect the gaps into the _gap_tree so they can be +// used for future small object allocation. +char* DumpRegion::allocate_metaspace_obj(size_t num_bytes, address src, MetaspaceClosureType type, bool read_only, DumpAllocStats* stats) { + num_bytes = align_up(num_bytes, SharedSpaceObjectAlignment); + size_t alignment = SharedSpaceObjectAlignment; // alignment for the dest pointer + bool is_class = (type == MetaspaceClosureType::ClassType); + bool is_instance_class = is_class && ((Klass*)src)->is_instance_klass(); + +#ifdef _LP64 + // More strict alignments needed for UseCompressedClassPointers + if (is_class && UseCompressedClassPointers) { + size_t klass_alignment = checked_cast(nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift())); + alignment = MAX2(alignment, klass_alignment); + precond(is_aligned(alignment, SharedSpaceObjectAlignment)); + } +#endif + + if (alignment == SharedSpaceObjectAlignment && type != MetaspaceClosureType::SymbolType) { + // The addresses of Symbols must be in the same order as they are in ArchiveBuilder::SourceObjList. + // If we put them in gaps, their order will change. + // + // We have enough small objects that all gaps are usually filled. + char* p = _gap_tree.allocate_from_gap(num_bytes); + if (p != nullptr) { + // Already memset to 0 when adding the gap + stats->record(type, checked_cast(num_bytes), /*read_only=*/false); // all gaps are from RW space (for classes) + return p; + } + } + + // Reserve space for a pointer directly in front of the buffered InstanceKlass, so + // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo* + // without building another hashtable. See RunTimeClassInfo::get_for() + // in systemDictionaryShared.cpp. + const size_t RuntimeClassInfoPtrSize = is_instance_class ? sizeof(address) : 0; + + if (is_class && !is_aligned(top() + RuntimeClassInfoPtrSize, alignment)) { + // We need to add a gap to align the buffered Klass. Save the gap for future small allocations. + assert(read_only == false, "only gaps in RW region are reusable"); + char* gap_bottom = top(); + char* gap_top = align_up(gap_bottom + RuntimeClassInfoPtrSize, alignment) - RuntimeClassInfoPtrSize; + size_t gap_bytes = _gap_tree.add_gap(gap_bottom, gap_top); + allocate(gap_bytes); + } + + char* oldtop = top(); + if (is_instance_class) { + SystemDictionaryShared::validate_before_archiving((InstanceKlass*)src); + allocate(RuntimeClassInfoPtrSize); + } + + precond(is_aligned(top(), alignment)); + char* result = allocate(num_bytes); + log_trace(aot, alloc)("%zu bytes @ %p", num_bytes, result); + stats->record(type, pointer_delta_as_int(top(), oldtop), read_only); // includes RuntimeClassInfoPtrSize for classes + + return result; +} + +// Usually we have no gaps left. +void DumpRegion::report_gaps(DumpAllocStats* stats) { + _gap_tree.visit_in_order([&](const AllocGapNode* node) { + stats->record_gap(checked_cast(node->key().gap_bytes())); + return true; + }); + if (_gap_tree.size() > 0) { + log_warning(aot)("Unexpected %zu gaps (%zu bytes) for Klass alignment", + _gap_tree.size(), _total_gap_bytes); + } + if (_total_gap_allocs > 0) { + log_info(aot)("Allocated %zu objects of %zu bytes in gaps (remain = %zu bytes)", + _total_gap_allocs, _total_gap_bytes_used, _total_gap_bytes); + } +} + void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) { assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment"); intptr_t *p = (intptr_t*)_top; diff --git a/src/hotspot/share/cds/archiveUtils.hpp b/src/hotspot/share/cds/archiveUtils.hpp index e5d1efa5eab..84ad8e6bdf3 100644 --- a/src/hotspot/share/cds/archiveUtils.hpp +++ b/src/hotspot/share/cds/archiveUtils.hpp @@ -28,7 +28,9 @@ #include "cds/cds_globals.hpp" #include "cds/serializeClosure.hpp" #include "logging/log.hpp" +#include "memory/allocation.hpp" #include "memory/metaspace.hpp" +#include "memory/metaspaceClosureType.hpp" #include "memory/virtualspace.hpp" #include "runtime/nonJavaThread.hpp" #include "runtime/semaphore.hpp" @@ -37,6 +39,7 @@ #include "utilities/macros.hpp" class BootstrapInfo; +class DumpAllocStats; class ReservedSpace; class VirtualSpace; @@ -159,6 +162,18 @@ private: void commit_to(char* newtop); +public: + // Allocation gaps (due to Klass alignment) + class AllocGapTree; + class AllocGap; + struct AllocGapCmp; + +private: + static AllocGapTree _gap_tree; + static size_t _total_gap_bytes; + static size_t _total_gap_bytes_used; + static size_t _total_gap_allocs; + public: DumpRegion(const char* name) : _name(name), _base(nullptr), _top(nullptr), _end(nullptr), @@ -167,6 +182,7 @@ public: char* expand_top_to(char* newtop); char* allocate(size_t num_bytes, size_t alignment = 0); + char* allocate_metaspace_obj(size_t num_bytes, address src, MetaspaceClosureType type, bool read_only, DumpAllocStats* stats); void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN; @@ -191,6 +207,8 @@ public: bool contains(char* p) { return base() <= p && p < top(); } + + static void report_gaps(DumpAllocStats* stats); }; // Closure for serializing initialization data out to a data area to be diff --git a/src/hotspot/share/cds/dumpAllocStats.cpp b/src/hotspot/share/cds/dumpAllocStats.cpp index 5f324566103..ddd4bac6086 100644 --- a/src/hotspot/share/cds/dumpAllocStats.cpp +++ b/src/hotspot/share/cds/dumpAllocStats.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, 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 @@ -129,15 +129,3 @@ void DumpAllocStats::print_stats(int ro_all, int rw_all) { _bytes [RW][MethodTrainingDataType]); } - -#ifdef ASSERT -void DumpAllocStats::verify(int expected_byte_size, bool read_only) const { - int bytes = 0; - const int what = (int)(read_only ? RO : RW); - for (int type = 0; type < int(_number_of_types); type ++) { - bytes += _bytes[what][type]; - } - assert(bytes == expected_byte_size, "counter mismatch (%s: %d vs %d)", - (read_only ? "RO" : "RW"), bytes, expected_byte_size); -} -#endif // ASSERT diff --git a/src/hotspot/share/cds/dumpAllocStats.hpp b/src/hotspot/share/cds/dumpAllocStats.hpp index 4553f0f6a01..4daef9195a6 100644 --- a/src/hotspot/share/cds/dumpAllocStats.hpp +++ b/src/hotspot/share/cds/dumpAllocStats.hpp @@ -41,6 +41,7 @@ public: f(StringHashentry) \ f(StringBucket) \ f(CppVTables) \ + f(Gap) \ f(Other) #define DUMPED_TYPE_DECLARE(name) name ## Type, @@ -111,12 +112,19 @@ public: _bytes [which][t] += byte_size; } + void record_gap(int byte_size) { + _counts[RW][GapType] += 1; + _bytes [RW][GapType] += byte_size; + } + void record_other_type(int byte_size, bool read_only) { int which = (read_only) ? RO : RW; + _counts[which][OtherType] += 1; _bytes [which][OtherType] += byte_size; } void record_cpp_vtables(int byte_size) { + _counts[RW][CppVTablesType] += 1; _bytes[RW][CppVTablesType] += byte_size; } @@ -145,9 +153,6 @@ public: } void print_stats(int ro_all, int rw_all); - - DEBUG_ONLY(void verify(int expected_byte_size, bool read_only) const); - }; #endif // SHARE_CDS_DUMPALLOCSTATS_HPP diff --git a/src/hotspot/share/utilities/rbTree.hpp b/src/hotspot/share/utilities/rbTree.hpp index c522d787466..9c04ccbe9ab 100644 --- a/src/hotspot/share/utilities/rbTree.hpp +++ b/src/hotspot/share/utilities/rbTree.hpp @@ -429,7 +429,12 @@ public: void free(void* ptr); }; - +template +RBTreeOrdering rbtree_primitive_cmp(T a, T b) { // handy function + if (a < b) return RBTreeOrdering::LT; + if (a > b) return RBTreeOrdering::GT; + return RBTreeOrdering::EQ; +} template using RBTreeCHeap = RBTree>; diff --git a/test/hotspot/jtreg/runtime/cds/MetaspaceAllocGaps.java b/test/hotspot/jtreg/runtime/cds/MetaspaceAllocGaps.java new file mode 100644 index 00000000000..65459263e36 --- /dev/null +++ b/test/hotspot/jtreg/runtime/cds/MetaspaceAllocGaps.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2026, 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. + */ + +/** + * @test + * @requires vm.cds + * @requires vm.flagless + * @requires vm.bits == 64 + * @bug 8376822 + * @summary Allocation gaps in the RW region caused by -XX:+UseCompactObjectHeaders should be reused + * @library /test/lib + * @build MetaspaceAllocGaps + * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar hello.jar Hello + * @run driver MetaspaceAllocGaps + */ + +import jdk.test.lib.cds.SimpleCDSAppTester; +import jdk.test.lib.helpers.ClassFileInstaller; +import jdk.test.lib.process.OutputAnalyzer; + +public class MetaspaceAllocGaps { + public static void main(String[] args) throws Exception { + String appJar = ClassFileInstaller.getJarPath("hello.jar"); + for (int i = 0; i < 2; i++) { + String compressedOops = "-XX:" + (i == 0 ? "-" : "+") + "UseCompressedOops"; + SimpleCDSAppTester.of("MetaspaceAllocGaps" + i) + .addVmArgs("-Xlog:aot=debug,aot+alloc=trace", + "-XX:+UseCompactObjectHeaders") + .classpath(appJar) + .appCommandLine("Hello") + .setTrainingChecker((OutputAnalyzer out) -> { + // Typically all gaps should be filled. If not, we probably have a regression in C++ class ArchiveUtils. + // + // [0.422s][debug][aot ] Detailed metadata info (excluding heap region): + // [...] + // [0.422s][debug][aot ] Gap : 0 0 0.0 | 0 0 0.0 | 0 0 0.0 <<< look for this pattern + out.shouldMatch("Allocated [1-9][0-9]+ objects of [1-9][0-9]+ bytes in gaps .remain = 0 bytes") + .shouldMatch("debug.* Gap .*0[.]0.*0[.]0.*0[.]0") + .shouldNotMatch("Unexpected .* gaps .* for Klass alignment"); + }) + .setProductionChecker((OutputAnalyzer out) -> { + out.shouldContain("HelloWorld"); + }) + .runAOTWorkflow(); + } + } +} + +class Hello { + public static void main(String[] args) { + System.out.println("HelloWorld"); + } +} From 00c1f4b6248e116c8839b1fb19ce20182711667a Mon Sep 17 00:00:00 2001 From: Ioi Lam Date: Tue, 17 Mar 2026 20:33:44 +0000 Subject: [PATCH 14/22] 8377512: AOT cache creation fails with invalid native pointer Reviewed-by: kvn, jrose, liach --- .../share/cds/aotConstantPoolResolver.cpp | 55 ++++++++++++++++++- .../share/cds/aotConstantPoolResolver.hpp | 7 ++- src/hotspot/share/cds/aotMappedHeapWriter.cpp | 10 +++- src/hotspot/share/oops/cpCache.cpp | 26 +++++++-- .../cds/appcds/aotCache/ExcludedClasses.java | 48 +++++++++++++++- 5 files changed, 132 insertions(+), 14 deletions(-) diff --git a/src/hotspot/share/cds/aotConstantPoolResolver.cpp b/src/hotspot/share/cds/aotConstantPoolResolver.cpp index 93145940955..f1a704d4bee 100644 --- a/src/hotspot/share/cds/aotConstantPoolResolver.cpp +++ b/src/hotspot/share/cds/aotConstantPoolResolver.cpp @@ -81,6 +81,7 @@ bool AOTConstantPoolResolver::is_resolution_deterministic(ConstantPool* cp, int bool AOTConstantPoolResolver::is_class_resolution_deterministic(InstanceKlass* cp_holder, Klass* resolved_class) { assert(!is_in_archivebuilder_buffer(cp_holder), "sanity"); assert(!is_in_archivebuilder_buffer(resolved_class), "sanity"); + assert_at_safepoint(); // try_add_candidate() is called below and requires to be at safepoint. if (resolved_class->is_instance_klass()) { InstanceKlass* ik = InstanceKlass::cast(resolved_class); @@ -346,7 +347,15 @@ void AOTConstantPoolResolver::maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m break; case Bytecodes::_invokehandle: - InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK); + if (CDSConfig::is_dumping_method_handles()) { + ResolvedMethodEntry* method_entry = cp->resolved_method_entry_at(raw_index); + int cp_index = method_entry->constant_pool_index(); + Symbol* sig = cp->uncached_signature_ref_at(cp_index); + Klass* k; + if (check_methodtype_signature(cp(), sig, &k, true)) { + InterpreterRuntime::cds_resolve_invokehandle(raw_index, cp, CHECK); + } + } break; default: @@ -400,7 +409,7 @@ void AOTConstantPoolResolver::preresolve_indy_cp_entries(JavaThread* current, In // Check the MethodType signatures used by parameters to the indy BSMs. Make sure we don't // use types that have been excluded, or else we might end up creating MethodTypes that cannot be stored // in the AOT cache. -bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret) { +bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret, bool is_invokehandle) { ResourceMark rm; for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { if (ss.is_reference()) { @@ -413,11 +422,18 @@ bool AOTConstantPoolResolver::check_methodtype_signature(ConstantPool* cp, Symbo if (SystemDictionaryShared::should_be_excluded(k)) { if (log_is_enabled(Warning, aot, resolve)) { ResourceMark rm; - log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is excluded", k->external_name()); + log_warning(aot, resolve)("Cannot aot-resolve %s because %s is excluded", + is_invokehandle ? "invokehandle" : "Lambda proxy", + k->external_name()); } return false; } + // cp->pool_holder() must be able to resolve k in production run + precond(CDSConfig::is_dumping_aot_linked_classes()); + precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data())); + precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data())); + if (ss.at_return_type() && return_type_ret != nullptr) { *return_type_ret = k; } @@ -475,11 +491,44 @@ bool AOTConstantPoolResolver::check_lambda_metafactory_methodhandle_arg(Constant return false; } + // klass and sigature of the method (no need to check the method name) Symbol* sig = cp->method_handle_signature_ref_at(mh_index); + Symbol* klass_name = cp->klass_name_at(cp->method_handle_klass_index_at(mh_index)); + if (log_is_enabled(Debug, aot, resolve)) { ResourceMark rm; log_debug(aot, resolve)("Checking MethodType of MethodHandle for LambdaMetafactory BSM arg %d: %s", arg_i, sig->as_C_string()); } + + { + Klass* k = find_loaded_class(Thread::current(), cp->pool_holder()->class_loader(), klass_name); + if (k == nullptr) { + // Dumping AOT cache: all classes should have been loaded by FinalImageRecipes::load_all_classes(). k must have + // been a class that was excluded when FinalImageRecipes recorded all classes at the end of the training run. + // + // Dumping static CDS archive: all classes in the classlist have already been loaded, before we resolve + // constants. k must have been a class that was excluded when the classlist was written + // at the end of the training run. + if (log_is_enabled(Warning, aot, resolve)) { + ResourceMark rm; + log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is not loaded", klass_name->as_C_string()); + } + return false; + } + if (SystemDictionaryShared::should_be_excluded(k)) { + if (log_is_enabled(Warning, aot, resolve)) { + ResourceMark rm; + log_warning(aot, resolve)("Cannot aot-resolve Lambda proxy because %s is excluded", k->external_name()); + } + return false; + } + + // cp->pool_holder() must be able to resolve k in production run + precond(CDSConfig::is_dumping_aot_linked_classes()); + precond(SystemDictionaryShared::is_builtin_loader(cp->pool_holder()->class_loader_data())); + precond(SystemDictionaryShared::is_builtin_loader(k->class_loader_data())); + } + return check_methodtype_signature(cp, sig); } diff --git a/src/hotspot/share/cds/aotConstantPoolResolver.hpp b/src/hotspot/share/cds/aotConstantPoolResolver.hpp index e49d9d1ad0b..ecf2ac27061 100644 --- a/src/hotspot/share/cds/aotConstantPoolResolver.hpp +++ b/src/hotspot/share/cds/aotConstantPoolResolver.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2026, 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 @@ -74,7 +74,10 @@ class AOTConstantPoolResolver : AllStatic { static void maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index, GrowableArray* resolve_fmi_list, TRAPS); - static bool check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret = nullptr); +public: + static bool check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret = nullptr, bool is_invokehandle = false); + +private: static bool check_lambda_metafactory_signature(ConstantPool* cp, Symbol* sig); static bool check_lambda_metafactory_methodtype_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i); static bool check_lambda_metafactory_methodhandle_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i); diff --git a/src/hotspot/share/cds/aotMappedHeapWriter.cpp b/src/hotspot/share/cds/aotMappedHeapWriter.cpp index 2e21a1d15d8..3456c845938 100644 --- a/src/hotspot/share/cds/aotMappedHeapWriter.cpp +++ b/src/hotspot/share/cds/aotMappedHeapWriter.cpp @@ -898,8 +898,14 @@ void AOTMappedHeapWriter::compute_ptrmap(AOTMappedHeapInfo* heap_info) { native_ptr = RegeneratedClasses::get_regenerated_object(native_ptr); } - guarantee(ArchiveBuilder::current()->has_been_archived((address)native_ptr), - "Metadata %p should have been archived", native_ptr); + if (!ArchiveBuilder::current()->has_been_archived((address)native_ptr)) { + ResourceMark rm; + LogStreamHandle(Error, aot) log; + log.print("Marking native pointer for oop %p (type = %s, offset = %d)", + cast_from_oop(src_obj), src_obj->klass()->external_name(), field_offset); + src_obj->print_on(&log); + fatal("Metadata %p should have been archived", native_ptr); + } address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr); address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr); diff --git a/src/hotspot/share/oops/cpCache.cpp b/src/hotspot/share/oops/cpCache.cpp index 34d7aa10299..edb5f6714c0 100644 --- a/src/hotspot/share/oops/cpCache.cpp +++ b/src/hotspot/share/oops/cpCache.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, 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 @@ -568,6 +568,8 @@ bool ConstantPoolCache::can_archive_resolved_method(ConstantPool* src_cp, Resolv return false; } + int cp_index = method_entry->constant_pool_index(); + if (!method_entry->is_resolved(Bytecodes::_invokevirtual)) { if (method_entry->method() == nullptr) { rejection_reason = "(method entry is not resolved)"; @@ -577,9 +579,24 @@ bool ConstantPoolCache::can_archive_resolved_method(ConstantPool* src_cp, Resolv rejection_reason = "(corresponding stub is generated on demand during method resolution)"; return false; // FIXME: corresponding stub is generated on demand during method resolution (see LinkResolver::resolve_static_call). } - if (method_entry->is_resolved(Bytecodes::_invokehandle) && !CDSConfig::is_dumping_method_handles()) { - rejection_reason = "(not dumping method handles)"; - return false; + if (method_entry->is_resolved(Bytecodes::_invokehandle)) { + if (!CDSConfig::is_dumping_method_handles()) { + rejection_reason = "(not dumping method handles)"; + return false; + } + + Symbol* sig = constant_pool()->uncached_signature_ref_at(cp_index); + Klass* k; + if (!AOTConstantPoolResolver::check_methodtype_signature(constant_pool(), sig, &k, true)) { + // invokehandles that were resolved in the training run should have been filtered in + // AOTConstantPoolResolver::maybe_resolve_fmi_ref so we shouldn't come to here. + // + // If we come here it's because the AOT assembly phase has executed an invokehandle + // that uses an excluded type like jdk.jfr.Event. This should not happen because the + // AOT assembly phase should execute only a very limited set of Java code. + ResourceMark rm; + fatal("AOT assembly phase must not resolve any invokehandles whose signatures include an excluded type"); + } } if (method_entry->method()->is_method_handle_intrinsic() && !CDSConfig::is_dumping_method_handles()) { rejection_reason = "(not dumping intrinsic method handles)"; @@ -587,7 +604,6 @@ bool ConstantPoolCache::can_archive_resolved_method(ConstantPool* src_cp, Resolv } } - int cp_index = method_entry->constant_pool_index(); assert(src_cp->tag_at(cp_index).is_method() || src_cp->tag_at(cp_index).is_interface_method(), "sanity"); if (!AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index)) { diff --git a/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ExcludedClasses.java b/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ExcludedClasses.java index 9a9524eb2f1..728428afef2 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ExcludedClasses.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/aotCache/ExcludedClasses.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2026, 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 @@ -32,6 +32,7 @@ * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar * TestApp * TestApp$Foo + * TestApp$Foo$A * TestApp$Foo$Bar * TestApp$Foo$ShouldBeExcluded * TestApp$Foo$ShouldBeExcludedChild @@ -43,6 +44,8 @@ */ import java.io.File; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -165,6 +168,8 @@ class TestApp { long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < 1000) { lambdaHotSpot(); + lambdaHotSpot2(); + invokeHandleHotSpot(); s.hotSpot2(); b.hotSpot3(); Taz.hotSpot4(); @@ -207,12 +212,52 @@ class TestApp { } } + interface A { + Object get(); + } + + // Lambdas that refer to excluded classes should not be AOT-resolved + static void lambdaHotSpot2() { + long start = System.currentTimeMillis(); + A a = ShouldBeExcluded::new; + while (System.currentTimeMillis() - start < 20) { + Object obj = (ShouldBeExcluded)a.get(); + } + } + + static void invokeHandleHotSpot() { + try { + invokeHandleHotSpotImpl(); + } catch (Throwable t) { + throw new RuntimeException("Unexpected", t); + } + } + + static void invokeHandleHotSpotImpl() throws Throwable { + MethodHandle constructorHandle = + MethodHandles.lookup().unreflectConstructor(ShouldBeExcluded.class.getConstructor()); + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < 20) { + // The JVM rewrites this to: + // invokehandle + // + // The AOT cache must not contain a java.lang.invoke.MethodType that refers to the + // ShouldBeExcluded class. + ShouldBeExcluded o = (ShouldBeExcluded)constructorHandle.invoke(); + if (o.getClass() != ShouldBeExcluded.class) { + throw new RuntimeException("Unexpected object: " + o); + } + } + } + static void doit(Runnable r) { r.run(); } // All subclasses of jdk.jfr.Event are excluded from the CDS archive. static class ShouldBeExcluded extends jdk.jfr.Event { + public ShouldBeExcluded() {} + int f = (int)(System.currentTimeMillis()) + 123; int m() { return f + 456; @@ -275,4 +320,3 @@ class TestApp { } } } - From 8378d289104a60faa0c9ee048dc299fa7ea2c06e Mon Sep 17 00:00:00 2001 From: David Holmes Date: Tue, 17 Mar 2026 20:43:22 +0000 Subject: [PATCH 15/22] 8379481: Recent JNI methods are missing DTrace probes Reviewed-by: kbarrett, iklam, jwaters --- src/hotspot/os/posix/dtrace/hotspot_jni.d | 4 +++- src/hotspot/share/prims/jni.cpp | 15 ++++++++++----- src/hotspot/share/utilities/dtrace_disabled.hpp | 16 +++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/hotspot/os/posix/dtrace/hotspot_jni.d b/src/hotspot/os/posix/dtrace/hotspot_jni.d index c5676921b37..1937769dcb2 100644 --- a/src/hotspot/os/posix/dtrace/hotspot_jni.d +++ b/src/hotspot/os/posix/dtrace/hotspot_jni.d @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, 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 @@ -366,6 +366,8 @@ provider hotspot_jni { probe IsInstanceOf__return(uintptr_t); probe IsSameObject__entry(void*, void*, void*); probe IsSameObject__return(uintptr_t); + probe IsVirtualThread__entry(void*, void*); + probe IsVirtualThread__return(uintptr_t); probe MonitorEnter__entry(void*, void*); probe MonitorEnter__return(uint32_t); probe MonitorExit__entry(void*, void*); diff --git a/src/hotspot/share/prims/jni.cpp b/src/hotspot/share/prims/jni.cpp index 2297ce9b790..85207fddf29 100644 --- a/src/hotspot/share/prims/jni.cpp +++ b/src/hotspot/share/prims/jni.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2024 Red Hat, Inc. * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. @@ -3129,16 +3129,21 @@ JNI_END JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz)) - return Modules::get_module(clazz, THREAD); + HOTSPOT_JNI_GETMODULE_ENTRY(env, clazz); + jobject ret = Modules::get_module(clazz, THREAD); + HOTSPOT_JNI_GETMODULE_RETURN(ret); + return ret; JNI_END JNI_ENTRY(jboolean, jni_IsVirtualThread(JNIEnv* env, jobject obj)) + HOTSPOT_JNI_ISVIRTUALTHREAD_ENTRY(env, obj); + jboolean ret = JNI_FALSE; oop thread_obj = JNIHandles::resolve_external_guard(obj); if (thread_obj != nullptr && thread_obj->is_a(vmClasses::BaseVirtualThread_klass())) { - return JNI_TRUE; - } else { - return JNI_FALSE; + ret = JNI_TRUE; } + HOTSPOT_JNI_ISVIRTUALTHREAD_RETURN(ret); + return ret; JNI_END diff --git a/src/hotspot/share/utilities/dtrace_disabled.hpp b/src/hotspot/share/utilities/dtrace_disabled.hpp index 6cbd79326ac..99d7cbb2f75 100644 --- a/src/hotspot/share/utilities/dtrace_disabled.hpp +++ b/src/hotspot/share/utilities/dtrace_disabled.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2026, 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 @@ -683,6 +683,10 @@ #define HOTSPOT_JNI_GETMETHODID_ENTRY_ENABLED() 0 #define HOTSPOT_JNI_GETMETHODID_RETURN(arg0) #define HOTSPOT_JNI_GETMETHODID_RETURN_ENABLED() 0 +#define HOTSPOT_JNI_GETMODULE_ENTRY(arg0, arg1) +#define HOTSPOT_JNI_GETMODULE_ENTRY_ENABLED() 0 +#define HOTSPOT_JNI_GETMODULE_RETURN(arg0) +#define HOTSPOT_JNI_GETMODULE_RETURN_ENABLED() 0 #define HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(arg0, arg1, arg2) #define HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY_ENABLED() 0 #define HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(arg0) @@ -811,6 +815,10 @@ #define HOTSPOT_JNI_ISSAMEOBJECT_ENTRY_ENABLED() 0 #define HOTSPOT_JNI_ISSAMEOBJECT_RETURN(arg0) #define HOTSPOT_JNI_ISSAMEOBJECT_RETURN_ENABLED() 0 +#define HOTSPOT_JNI_ISVIRTUALTHREAD_ENTRY(arg0, arg1) +#define HOTSPOT_JNI_ISVIRTUALTHREAD_ENTRY_ENABLED() 0 +#define HOTSPOT_JNI_ISVIRTUALTHREAD_RETURN(arg0) +#define HOTSPOT_JNI_ISVIRTUALTHREAD_RETURN_ENABLED() 0 #define HOTSPOT_JNI_MONITORENTER_ENTRY(arg0, arg1) #define HOTSPOT_JNI_MONITORENTER_ENTRY_ENABLED() 0 #define HOTSPOT_JNI_MONITORENTER_RETURN(arg0) @@ -1080,12 +1088,6 @@ #define HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(arg0) #define HOTSPOT_JNI_UNREGISTERNATIVES_RETURN_ENABLED() 0 -/* Modules */ -#define HOTSPOT_JNI_GETMODULE_ENTRY(arg0, arg1) -#define HOTSPOT_JNI_GETMODULE_ENTRY_ENABLED() 0 -#define HOTSPOT_JNI_GETMODULE_RETURN(arg0) -#define HOTSPOT_JNI_GETMODULE_RETURN_ENABLED() - #else /* !defined(DTRACE_ENABLED) */ #error This file should only be included when dtrace is not enabled #endif /* !defined(DTRACE_ENABLED) */ From 50f81c5d7c72c3085b076f897bd6608a4c016013 Mon Sep 17 00:00:00 2001 From: David Beaumont Date: Tue, 17 Mar 2026 21:43:58 +0000 Subject: [PATCH 16/22] 8379798: Refactor remaining tests in javax/xml/jaxp/functional to JUnit Reviewed-by: joehw --- .../dom/ptests/AbstractCharacterDataTest.java | 87 +++++----- .../org/w3c/dom/ptests/AttrTest.java | 27 ++-- .../org/w3c/dom/ptests/CommentTest.java | 17 +- .../org/w3c/dom/ptests/DocumentTest.java | 73 +++++---- .../org/w3c/dom/ptests/DocumentTypeTest.java | 39 +++-- .../w3c/dom/ptests/DomImplementationTest.java | 41 ++--- .../org/w3c/dom/ptests/ElementTest.java | 102 ++++++------ .../org/w3c/dom/ptests/EntityChildTest.java | 25 ++- .../org/w3c/dom/ptests/NamedNodeMapTest.java | 28 ++-- .../org/w3c/dom/ptests/NodeListTest.java | 23 ++- .../org/w3c/dom/ptests/NodeTest.java | 82 +++++----- .../org/w3c/dom/ptests/NotationTest.java | 23 ++- .../functional/org/w3c/dom/ptests/PITest.java | 18 +-- .../org/w3c/dom/ptests/TextTest.java | 24 ++- .../org/w3c/dom/ptests/TypeInfoTest.java | 128 ++++++++------- .../org/xml/sax/ptests/AttrImplTest.java | 59 +++---- .../org/xml/sax/ptests/AttributesNSTest.java | 25 +-- .../org/xml/sax/ptests/AttributesTest.java | 25 +-- .../xml/sax/ptests/ContentHandlerTest.java | 47 +++--- .../xml/sax/ptests/DefaultHandlerTest.java | 54 +++---- .../org/xml/sax/ptests/EHFatalTest.java | 57 +++---- .../org/xml/sax/ptests/NSSupportTest.java | 29 ++-- .../org/xml/sax/ptests/NSTableTest.java | 12 +- .../org/xml/sax/ptests/ParserAdapterTest.java | 42 +++-- .../org/xml/sax/ptests/ResolverTest.java | 44 +++-- .../org/xml/sax/ptests/XMLFilterCBTest.java | 54 +++---- .../org/xml/sax/ptests/XMLFilterTest.java | 62 +++---- .../xml/sax/ptests/XMLReaderAdapterTest.java | 29 ++-- .../xml/sax/ptests/XMLReaderFactoryTest.java | 37 +++-- .../xml/sax/ptests/XMLReaderNSTableTest.java | 54 ++++--- .../org/xml/sax/ptests/XMLReaderTest.java | 56 +++---- .../jaxp/functional/test/astro/AstroTest.java | 46 +++--- .../functional/test/astro/DocumentLSTest.java | 52 +++--- .../test/astro/NamespaceContextTest.java | 32 ++-- .../functional/test/astro/SAX201Test.java | 10 +- .../test/astro/SchemaValidationTest.java | 27 ++-- .../functional/test/astro/XPathAPITest.java | 94 ++++++----- .../test/auctionportal/AuctionController.java | 151 ++++++++++-------- .../auctionportal/AuctionItemRepository.java | 127 ++++++++------- .../{movies.xml.data => movies-utf16.xml} | 0 .../functional/test/gaptest/Bug4511326.java | 9 +- .../functional/test/gaptest/Bug4512806.java | 23 ++- .../functional/test/gaptest/Bug4515047.java | 8 +- .../functional/test/gaptest/Bug4515660.java | 52 +++--- .../functional/test/gaptest/Bug4848653.java | 28 ++-- .../functional/test/gaptest/Bug4858685.java | 38 ++--- .../libs/org/w3c/dom/ptests/DOMTestUtil.java | 15 +- .../libs/org/xml/sax/ptests/SAXTestConst.java | 18 ++- .../jaxp/libs/test/astro/AstroConstants.java | 13 +- .../libs/test/astro/DOMFilterFactoryImpl.java | 5 +- .../astro/DOML3InputSourceFactoryImpl.java | 34 ++-- .../test/astro/InputSourceFactoryImpl.java | 7 +- .../libs/test/astro/SAXFilterFactoryImpl.java | 28 ++-- .../test/astro/StreamFilterFactoryImpl.java | 5 +- .../astro/TemplatesFilterFactoryImpl.java | 8 +- .../test/auctionportal/HiBidConstants.java | 14 +- .../jaxp/libs/test/gaptest/GapTestConst.java | 15 +- 57 files changed, 1146 insertions(+), 1136 deletions(-) rename test/jaxp/javax/xml/jaxp/functional/test/auctionportal/content/{movies.xml.data => movies-utf16.xml} (100%) diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java index c1b3381cb98..b46829de8e1 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AbstractCharacterDataTest.java @@ -22,27 +22,26 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; -import static org.w3c.dom.DOMException.INDEX_SIZE_ERR; -import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.CharacterData; import org.w3c.dom.DOMException; import org.xml.sax.SAXException; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.w3c.dom.DOMException.INDEX_SIZE_ERR; +import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; + /* * @summary common test for the CharacterData Interface */ public abstract class AbstractCharacterDataTest { - @DataProvider(name = "data-for-length") - public Object[][] getDataForTestLength() { + public static Object[][] getDataForTestLength() { return new Object[][] { { "", 0 }, { "test", 4 } }; @@ -52,11 +51,11 @@ public abstract class AbstractCharacterDataTest { * Verify getLength method works as the spec, for an empty string, should * return zero */ - @Test(dataProvider = "data-for-length") + @ParameterizedTest + @MethodSource("getDataForTestLength") public void testGetLength(String text, int length) throws Exception { CharacterData cd = createCharacterData(text); - assertEquals(cd.getLength(), length); - + assertEquals(length, cd.getLength()); } /* @@ -66,12 +65,11 @@ public abstract class AbstractCharacterDataTest { public void testAppendData() throws Exception { CharacterData cd = createCharacterData("DOM"); cd.appendData("2"); - assertEquals(cd.getData(), "DOM2"); + assertEquals("DOM2", cd.getData()); } - @DataProvider(name = "data-for-delete") - public Object[][] getDataForTestDelete() { + public static Object[][] getDataForTestDelete() { return new Object[][] { { "DOM", 2, 1, "DO" }, { "DOM", 0, 2, "M" }, @@ -81,15 +79,15 @@ public abstract class AbstractCharacterDataTest { /* * Verify deleteData method works as the spec. */ - @Test(dataProvider = "data-for-delete") + @ParameterizedTest + @MethodSource("getDataForTestDelete") public void testDeleteData(String text, int offset, int count, String result) throws Exception { CharacterData cd = createCharacterData(text); cd.deleteData(offset, count); assertEquals(cd.getData(), result); } - @DataProvider(name = "data-for-replace") - public Object[][] getDataForTestReplace() { + public static Object[][] getDataForTestReplace() { return new Object[][] { { "DOM", 0, 3, "SAX", "SAX" }, { "DOM", 1, 1, "AA", "DAAM" }, @@ -100,15 +98,15 @@ public abstract class AbstractCharacterDataTest { /* * Verify replaceData method works as the spec. */ - @Test(dataProvider = "data-for-replace") + @ParameterizedTest + @MethodSource("getDataForTestReplace") public void testReplaceData(String text, int offset, int count, String arg, String result) throws Exception { CharacterData cd = createCharacterData(text); cd.replaceData(offset, count, arg); assertEquals(cd.getData(), result); } - @DataProvider(name = "data-for-replace-neg") - public Object[][] getDataForTestReplaceNeg() { + public static Object[][] getDataForTestReplaceNeg() { return new Object[][] { { "DOM", -1, 3, "SAX" }, //offset if neg { "DOM", 0, -1, "SAX" }, //count is neg @@ -119,19 +117,19 @@ public abstract class AbstractCharacterDataTest { * Test for replaceData method: verifies that DOMException with * INDEX_SIZE_ERR is thrown if offset or count is out of the bound. */ - @Test(dataProvider = "data-for-replace-neg") + @ParameterizedTest + @MethodSource("getDataForTestReplaceNeg") public void testReplaceDataNeg(String text, int offset, int count, String arg) throws Exception { CharacterData cd = createCharacterData(text); try { cd.replaceData(offset, count, arg); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { - assertEquals(e.code, INDEX_SIZE_ERR); + assertEquals(INDEX_SIZE_ERR, e.code); } } - @DataProvider(name = "data-for-insert") - public Object[][] getDataForTestInsert() { + public static Object[][] getDataForTestInsert() { return new Object[][] { { "DOM", 0, "SAX", "SAXDOM" }, { "DOM", 3, "SAX", "DOMSAX" } }; @@ -140,15 +138,15 @@ public abstract class AbstractCharacterDataTest { /* * Verify insertData method works as the spec. */ - @Test(dataProvider = "data-for-insert") + @ParameterizedTest + @MethodSource("getDataForTestInsert") public void testInsertData(String text, int offset, String arg, String result) throws Exception { CharacterData cd = createCharacterData(text); cd.insertData(offset, arg); assertEquals(cd.getData(), result); } - @DataProvider(name = "data-for-insert-neg") - public Object[][] getDataForTestInsertNeg() { + public static Object[][] getDataForTestInsertNeg() { return new Object[][] { { "DOM", -1 }, //offset is neg { "DOM", 4 } };//offset is greater than length @@ -158,14 +156,15 @@ public abstract class AbstractCharacterDataTest { * Test for insertData method: verifies that DOMException with * INDEX_SIZE_ERR is thrown if offset is out of the bound. */ - @Test(dataProvider = "data-for-insert-neg") + @ParameterizedTest + @MethodSource("getDataForTestInsertNeg") public void testInsertDataNeg(String text, int offset) throws Exception { CharacterData cd = createCharacterData(text); try { cd.insertData(offset, "TEST"); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { - assertEquals(e.code, INDEX_SIZE_ERR); + assertEquals(INDEX_SIZE_ERR, e.code); } } @@ -176,11 +175,10 @@ public abstract class AbstractCharacterDataTest { public void testSetData() throws Exception { CharacterData cd = createCharacterData("DOM"); cd.setData("SAX"); - assertEquals(cd.getData(), "SAX"); + assertEquals("SAX", cd.getData()); } - @DataProvider(name = "data-for-substring") - public Object[][] getDataForTestSubstring() { + public static Object[][] getDataForTestSubstring() { return new Object[][] { { "DOM Level 2", 0, 3, "DOM" }, { "DOM", 0, 3, "DOM" }, @@ -190,15 +188,14 @@ public abstract class AbstractCharacterDataTest { /* * Verify substringData method works as the spec. */ - @Test(dataProvider = "data-for-substring") + @ParameterizedTest + @MethodSource("getDataForTestSubstring") public void testSubstringData(String text, int offset, int count, String result) throws Exception { CharacterData cd = createCharacterData(text); - String retStr = cd.substringData(offset, count); - assertEquals(retStr, result); + assertEquals(result, cd.substringData(offset, count)); } - @DataProvider(name = "data-for-substring-neg") - public Object[][] getDataForTestSubstringNeg() { + public static Object[][] getDataForTestSubstringNeg() { return new Object[][] { { "DOM Level 2", -1, 3 }, //offset is neg { "DOM", 0, -1 }, //count is neg @@ -209,14 +206,15 @@ public abstract class AbstractCharacterDataTest { * Test for substringData method: verifies that DOMException with * INDEX_SIZE_ERR is thrown if offset or count is out of the bound. */ - @Test(dataProvider = "data-for-substring-neg") + @ParameterizedTest + @MethodSource("getDataForTestSubstringNeg") public void testSubstringDataNeg(String text, int offset, int count) throws Exception { CharacterData cd = createCharacterData(text); try { cd.substringData(offset, count); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { - assertEquals(e.code, INDEX_SIZE_ERR); + assertEquals(INDEX_SIZE_ERR, e.code); } } @@ -225,5 +223,4 @@ public abstract class AbstractCharacterDataTest { * Return a concrete CharacterData instance. */ abstract protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException; - } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java index bd7dfb10eab..08ba9b81055 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/AttrTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,23 +23,23 @@ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.AttrTest + * @run junit/othervm org.w3c.dom.ptests.AttrTest * @summary Test for the Attr Interface */ public class AttrTest { @@ -51,12 +51,12 @@ public class AttrTest { Document document = createDOM("Attr01.xml"); //test a new created Attr Attr attr = document.createAttribute("newAttribute"); - assertEquals(attr.getName(), "newAttribute"); + assertEquals("newAttribute", attr.getName()); //test a Attr loaded from xml file Element elemNode = (Element) document.getElementsByTagName("book").item(1); Attr attr2 = (Attr) elemNode.getAttributes().item(0); - assertEquals(attr2.getName(), "category1"); + assertEquals("category1", attr2.getName()); } /* @@ -71,7 +71,7 @@ public class AttrTest { NamedNodeMap nnMap = elemNode.getAttributes(); for (int i = 0; i < nnMap.getLength(); i++) { Attr attr = (Attr) nnMap.item(i); - assertEquals(attr.getOwnerElement().getNodeName(), "book"); + assertEquals("book", attr.getOwnerElement().getNodeName()); } //test an Attr without owner node @@ -143,8 +143,7 @@ public class AttrTest { Document document = createDOM("Attr01.xml"); Attr attr = document.createAttribute("newAttribute"); attr.setValue("newVal"); - assertEquals(attr.getValue(), "newVal"); - + assertEquals("newVal", attr.getValue()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java index c3cff46c714..2f0664a8139 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/CommentTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,25 +22,24 @@ */ package org.w3c.dom.ptests; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.xml.sax.SAXException; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; + +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs /javax/xml/jaxp/functional - * @run testng/othervm org.w3c.dom.ptests.CommentTest + * @run junit/othervm org.w3c.dom.ptests.CommentTest * @summary Test for Comment implementation returned by Document.createComment(String) */ public class CommentTest extends AbstractCharacterDataTest { @Override - protected CharacterData createCharacterData(String text) throws IOException, SAXException, ParserConfigurationException { + protected CharacterData createCharacterData(String text) throws ParserConfigurationException { Document document = createNewDocument(); return document.createComment(text); } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java index 3ebf28afaa7..b7b5c2669ea 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,34 +22,35 @@ */ package org.w3c.dom.ptests; -import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; -import static javax.xml.XMLConstants.XML_NS_URI; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.fail; -import static org.w3c.dom.DOMException.NAMESPACE_ERR; -import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; +import static javax.xml.XMLConstants.XML_NS_URI; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; +import static org.w3c.dom.DOMException.NAMESPACE_ERR; +import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.DocumentTest + * @run junit/othervm org.w3c.dom.ptests.DocumentTest * @summary Test createAttributeNS, getElementsByTagNameNS and createElementNS method of Document */ public class DocumentTest { - @DataProvider(name = "invalid-nsuri") - public Object[][] getInvalidNamespaceURI() { + public static Object[][] getInvalidNamespaceURI() { return new Object[][] { { " ", "xml:novel" }, //blank { "hello", "xml:novel" }, //unqualified @@ -61,14 +62,14 @@ public class DocumentTest { * Test for createAttributeNS method: verifies that DOMException is thrown * if reserved prefixes are used with an arbitrary namespace name. */ - @Test(dataProvider = "invalid-nsuri", expectedExceptions = DOMException.class) + @ParameterizedTest + @MethodSource("getInvalidNamespaceURI") public void testCreateAttributeNSNeg(String namespaceURI, String name) throws Exception { Document document = createDOMWithNS("DocumentTest01.xml"); - document.createAttributeNS(namespaceURI, name); + assertThrows(DOMException.class, () -> document.createAttributeNS(namespaceURI, name)); } - @DataProvider(name = "valid-nsuri") - public Object[][] getValidNamespaceURI() { + public static Object[][] getValidNamespaceURI() { return new Object[][] { { XML_NS_URI, "xml:novel" }, { XMLNS_ATTRIBUTE_NS_URI, "xmlns:novel" }, @@ -79,16 +80,16 @@ public class DocumentTest { /* * Verify the Attr from createAttributeNS. */ - @Test(dataProvider = "valid-nsuri") + @ParameterizedTest + @MethodSource("getValidNamespaceURI") public void testCreateAttributeNS(String namespaceURI, String name) throws Exception { Document document = createDOMWithNS("DocumentTest01.xml"); Attr attr = document.createAttributeNS(namespaceURI, name); - assertEquals(attr.getNamespaceURI(), namespaceURI); - assertEquals(attr.getName(), name); + assertEquals(namespaceURI, attr.getNamespaceURI()); + assertEquals(name, attr.getName()); } - @DataProvider(name = "elementName") - public Object[][] getElementName() { + public static Object[][] getElementName() { return new Object[][] { { "author", 1 }, { "b:author", 0 } }; @@ -97,25 +98,27 @@ public class DocumentTest { /* * Verify the NodeList from getElementsByTagNameNS. */ - @Test(dataProvider = "elementName") - public void testGetElementsByTagNameNS(String localName, int number) throws Exception { + @ParameterizedTest + @MethodSource("getElementName") + public void testGetElementsByTagNameNS(String localName, int expectedLength) throws Exception { Document document = createDOMWithNS("DocumentTest01.xml"); NodeList nodeList = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", localName); - assertEquals(nodeList.getLength(), number); + assertEquals(expectedLength, nodeList.getLength()); } /* * Test for createElementNS method: verifies that DOMException is thrown * if reserved prefixes are used with an arbitrary namespace name. */ - @Test(dataProvider = "invalid-nsuri") + @ParameterizedTest + @MethodSource("getInvalidNamespaceURI") public void testCreateElementNSNeg(String namespaceURI, String name) throws Exception { Document document = createDOMWithNS("DocumentTest01.xml"); try { document.createElementNS(namespaceURI, name); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException e) { - assertEquals(e.code, NAMESPACE_ERR); + assertEquals(NAMESPACE_ERR, e.code); } } @@ -129,9 +132,9 @@ public class DocumentTest { final String localName = "novel"; Document document = createDOMWithNS("DocumentTest01.xml"); Element element = document.createElementNS(nsURI, name); - assertEquals(element.getNamespaceURI(), nsURI); - assertEquals(element.getNodeName(), name); - assertEquals(element.getLocalName(), localName); + assertEquals(nsURI, element.getNamespaceURI()); + assertEquals(name, element.getNodeName()); + assertEquals(localName, element.getLocalName()); } /* @@ -166,9 +169,9 @@ public class DocumentTest { /* * Test createElement with unqualified xml name. */ - @Test(expectedExceptions = DOMException.class) + @Test public void testCreateElementNeg() throws Exception { Document doc = createNewDocument(); - doc.createElement("!nc$%^*(!"); + assertThrows(DOMException.class, () -> doc.createElement("!nc$%^*(!")); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java index b5265b825b0..448e358c97a 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DocumentTypeTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,18 +23,18 @@ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; - -import org.testng.Assert; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.DocumentType; import org.w3c.dom.NamedNodeMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.DocumentTypeTest + * @run junit/othervm org.w3c.dom.ptests.DocumentTypeTest * @summary Test DocumentType */ public class DocumentTypeTest { @@ -48,10 +48,10 @@ public class DocumentTypeTest { NamedNodeMap namedNodeMap = documentType.getEntities(); // should return both external and internal. Parameter entities are not // contained. Duplicates are discarded. - assertEquals(namedNodeMap.getLength(), 3); - assertEquals(namedNodeMap.item(0).getNodeName(), "author"); - assertEquals(namedNodeMap.item(1).getNodeName(), "test"); - assertEquals(namedNodeMap.item(2).getNodeName(), "writer"); + assertEquals(3, namedNodeMap.getLength()); + assertEquals("author", namedNodeMap.item(0).getNodeName()); + assertEquals("test", namedNodeMap.item(1).getNodeName()); + assertEquals("writer", namedNodeMap.item(2).getNodeName()); } /* @@ -61,12 +61,11 @@ public class DocumentTypeTest { public void testGetNotations() throws Exception { DocumentType documentType = createDOM("DocumentType03.xml").getDoctype(); NamedNodeMap nm = documentType.getNotations(); - assertEquals(nm.getLength(), 2); // should return 2 because the notation - // name is repeated and - // it considers only the first - // occurence - assertEquals(nm.item(0).getNodeName(), "gs"); - assertEquals(nm.item(1).getNodeName(), "name"); + // should return 2 because the notation name is repeated, + // and it considers only the first occurrence + assertEquals(2, nm.getLength()); + assertEquals("gs", nm.item(0).getNodeName()); + assertEquals("name", nm.item(1).getNodeName()); } /* @@ -75,7 +74,7 @@ public class DocumentTypeTest { @Test public void testGetName() throws Exception { DocumentType documentType = createDOM("DocumentType03.xml").getDoctype(); - assertEquals(documentType.getName(), "note"); + assertEquals("note", documentType.getName()); } /* @@ -84,8 +83,8 @@ public class DocumentTypeTest { @Test public void testGetSystemId() throws Exception { DocumentType documentType = createDOM("DocumentType05.xml").getDoctype(); - assertEquals(documentType.getSystemId(), "DocumentBuilderImpl02.dtd"); - Assert.assertNull(documentType.getPublicId()); + assertEquals("DocumentBuilderImpl02.dtd", documentType.getSystemId()); + assertNull(documentType.getPublicId()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java index 8e8191aa2a3..251279ced4f 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/DomImplementationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,21 +22,22 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import javax.xml.parsers.ParserConfigurationException; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; +import javax.xml.parsers.ParserConfigurationException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.DomImplementationTest + * @run junit/othervm org.w3c.dom.ptests.DomImplementationTest * @summary Test DomImplementation API */ public class DomImplementationTest { @@ -50,8 +51,8 @@ public class DomImplementationTest { final String name = "document:localName"; DOMImplementation domImpl = getDOMImplementation(); Document document = domImpl.createDocument(nsURI, name, null); - assertEquals(document.getDocumentElement().getNamespaceURI(), nsURI); - assertEquals(document.getDocumentElement().getNodeName(), name); + assertEquals(nsURI, document.getDocumentElement().getNamespaceURI()); + assertEquals(name, document.getDocumentElement().getNodeName()); } /* @@ -85,8 +86,7 @@ public class DomImplementationTest { verifyDocumentType(document.getDoctype(), name, publicId, systemId); } - @DataProvider(name = "feature-supported") - public Object[][] getFeatureSupportedList() throws ParserConfigurationException { + public static Object[][] getFeatureSupportedList() throws ParserConfigurationException { DOMImplementation impl = getDOMImplementation(); return new Object[][] { { impl, "XML", "2.0", true }, @@ -109,20 +109,21 @@ public class DomImplementationTest { /* * Verify DOMImplementation for feature supporting. */ - @Test(dataProvider = "feature-supported") + @ParameterizedTest + @MethodSource("getFeatureSupportedList") public void testHasFeature(DOMImplementation impl, String feature, String version, boolean isSupported) { - assertEquals(impl.hasFeature(feature,version), isSupported); + assertEquals(isSupported, impl.hasFeature(feature, version)); } - private DOMImplementation getDOMImplementation() throws ParserConfigurationException { + private static DOMImplementation getDOMImplementation() throws ParserConfigurationException { return createNewDocument().getImplementation(); } - private void verifyDocumentType(DocumentType documentType, String name, String publicId, String systemId) { - assertEquals(documentType.getPublicId(), publicId); - assertEquals(documentType.getSystemId(), systemId); - assertEquals(documentType.getName(), name); + private static void verifyDocumentType(DocumentType documentType, String name, String publicId, String systemId) { + assertEquals(publicId, documentType.getPublicId()); + assertEquals(systemId, documentType.getSystemId()); + assertEquals(name, documentType.getName()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java index b538f7d1738..b861e086b3e 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/ElementTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,23 +22,9 @@ */ package org.w3c.dom.ptests; -import static javax.xml.XMLConstants.XML_NS_URI; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; -import static org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR; -import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import java.io.StringReader; - -import javax.xml.parsers.DocumentBuilderFactory; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; @@ -47,10 +33,25 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.StringReader; + +import static javax.xml.XMLConstants.XML_NS_URI; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR; +import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.ElementTest + * @run junit/othervm org.w3c.dom.ptests.ElementTest * @summary Test for the methods of Element Interface */ public class ElementTest { @@ -59,7 +60,7 @@ public class ElementTest { Document document = createDOMWithNS("ElementSample01.xml"); Element elemNode = (Element) document.getElementsByTagName("book").item(0); String s = elemNode.getAttributeNS("urn:BooksAreUs.org:BookInfo", "category"); - assertEquals(s, "research"); + assertEquals("research", s); } @Test @@ -67,7 +68,7 @@ public class ElementTest { Document document = createDOMWithNS("ElementSample01.xml"); Element elemNode = (Element) document.getElementsByTagName("book").item(0); Attr attr = elemNode.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "category"); - assertEquals(attr.getValue(), "research"); + assertEquals("research", attr.getValue()); } @@ -80,11 +81,11 @@ public class ElementTest { Document document = createDOMWithNS("ElementSample01.xml"); Element elemNode = (Element) document.getElementsByTagName("book").item(1); Attr attr = elemNode.getAttributeNode("category1"); - assertEquals(attr.getValue(), "research"); + assertEquals("research", attr.getValue()); - assertEquals(elemNode.getTagName(), "book"); + assertEquals("book", elemNode.getTagName()); elemNode.removeAttributeNode(attr); - assertEquals(elemNode.getAttribute("category1"), ""); + assertEquals("", elemNode.getAttribute("category1")); } /* @@ -112,8 +113,8 @@ public class ElementTest { elemNode.normalize(); Node firstChild = elemNode.getFirstChild(); Node lastChild = elemNode.getLastChild(); - assertEquals(firstChild.getNodeValue(), "fjfjf"); - assertEquals(lastChild.getNodeValue(), "fjfjf"); + assertEquals("fjfjf", firstChild.getNodeValue()); + assertEquals("fjfjf", lastChild.getNodeValue()); } /* @@ -129,32 +130,32 @@ public class ElementTest { myAttr.setValue(attrValue); assertNull(elemNode.setAttributeNode(myAttr)); - assertEquals(elemNode.getAttribute(attrName), attrValue); + assertEquals(attrValue, elemNode.getAttribute(attrName)); } - @DataProvider(name = "attribute") - public Object[][] getAttributeData() { + public static Object[][] getAttributeData() { return new Object[][] { { "thisisname", "thisisitsvalue" }, { "style", "font-Family" } }; } - @Test(dataProvider = "attribute") + @ParameterizedTest + @MethodSource("getAttributeData") public void testSetAttribute(String name, String value) throws Exception { Document document = createDOM("ElementSample02.xml"); Element elemNode = document.createElement("pricetag2"); elemNode.setAttribute(name, value); - assertEquals(elemNode.getAttribute(name), value); + assertEquals(value, elemNode.getAttribute(name)); } /* * Negative test for setAttribute, null is not a valid name. */ - @Test(expectedExceptions = DOMException.class) + @Test public void testSetAttributeNeg() throws Exception { Document document = createDOM("ElementSample02.xml"); Element elemNode = document.createElement("pricetag2"); - elemNode.setAttribute(null, null); + assertThrows(DOMException.class, () -> elemNode.setAttribute(null, null)); } /* @@ -182,7 +183,7 @@ public class ElementTest { element3.setAttributeNode(attr); fail(DOMEXCEPTION_EXPECTED); } catch (DOMException doe) { - assertEquals(doe.code, INUSE_ATTRIBUTE_ERR); + assertEquals(INUSE_ATTRIBUTE_ERR, doe.code); } } @@ -201,8 +202,7 @@ public class ElementTest { assertNull(nl.item(0)); } - @DataProvider(name = "nsattribute") - public Object[][] getNSAttributeData() { + public static Object[][] getNSAttributeData() { return new Object[][] { { "h:html", "html", "attrValue" }, { "b:style", "style", "attrValue" } }; @@ -211,14 +211,15 @@ public class ElementTest { /* * setAttributeNodeNS and verify it with getAttributeNS. */ - @Test(dataProvider = "nsattribute") + @ParameterizedTest + @MethodSource("getNSAttributeData") public void testSetAttributeNodeNS(String qualifiedName, String localName, String value) throws Exception { Document document = createDOM("ElementSample03.xml"); Element elemNode = document.createElement("pricetag2"); Attr myAttr = document.createAttributeNS(XML_NS_URI, qualifiedName); myAttr.setValue(value); assertNull(elemNode.setAttributeNodeNS(myAttr)); - assertEquals(elemNode.getAttributeNS(XML_NS_URI, localName), value); + assertEquals(value, elemNode.getAttributeNS(XML_NS_URI, localName)); } @Test @@ -233,22 +234,23 @@ public class ElementTest { @Test public void testToString() throws Exception { final String xml = - "" - + "" - + "" - + " \n" - + " " - + " " - + ""; + """ + \ + \ + \ + + \ + \ + """; Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))); Element root = doc.getDocumentElement(); - assertEquals(root.toString(), "[datacenterlist: null]"); + assertEquals("[datacenterlist: null]", root.toString()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/EntityChildTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/EntityChildTest.java index 65fe0e046ab..447100f45c9 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/EntityChildTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/EntityChildTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,23 +22,22 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.XML_DIR; - -import java.io.File; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.XML_DIR; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.EntityChildTest + * @run junit/othervm org.w3c.dom.ptests.EntityChildTest * @summary Test DOM Parser: parsing an xml file that contains external entities. */ public class EntityChildTest { @@ -54,7 +53,7 @@ public class EntityChildTest { Element root = document.getDocumentElement(); NodeList n = root.getElementsByTagName("table"); NodeList nl = n.item(0).getChildNodes(); - assertEquals(n.getLength(), 1); - assertEquals(nl.getLength(), 3); + assertEquals(1, n.getLength()); + assertEquals(3, nl.getLength()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NamedNodeMapTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NamedNodeMapTest.java index 86cd3aa7bbb..5cf6bc2387c 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NamedNodeMapTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NamedNodeMapTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,21 +22,21 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.NamedNodeMapTest + * @run junit/othervm org.w3c.dom.ptests.NamedNodeMapTest * @summary Test for the methods of NamedNodeMap Interface */ public class NamedNodeMapTest { @@ -61,9 +61,9 @@ public class NamedNodeMapTest { // setting to a new Value attr.setValue("newValue"); Node replacedAttr = namedNodeMap.setNamedItemNS(attr); // return the replaced attr - assertEquals(replacedAttr.getNodeValue(), "font-family"); + assertEquals("font-family", replacedAttr.getNodeValue()); Node updatedAttr = namedNodeMap.getNamedItemNS(nsURI, "style"); - assertEquals(updatedAttr.getNodeValue(), "newValue"); + assertEquals("newValue", updatedAttr.getNodeValue()); // creating a non existing attribute node @@ -75,7 +75,7 @@ public class NamedNodeMapTest { // checking if the node could be accessed // using the getNamedItemNS method Node newAttr = namedNodeMap.getNamedItemNS(nsURI, "newNode"); - assertEquals(newAttr.getNodeValue(), "newValue"); + assertEquals("newValue", newAttr.getNodeValue()); } /* @@ -89,7 +89,7 @@ public class NamedNodeMapTest { Node n = nodeList.item(7); NamedNodeMap namedNodeMap = n.getAttributes(); Node node = namedNodeMap.getNamedItemNS("urn:BooksAreUs.org:BookInfo", "aaa"); - assertEquals(node.getNodeValue(), "value"); + assertEquals("value", node.getNodeValue()); } @@ -107,14 +107,14 @@ public class NamedNodeMapTest { NamedNodeMap namedNodeMap = n.getAttributes(); Attr attr = document.createAttribute("name"); Node replacedAttr = namedNodeMap.setNamedItem(attr); - assertEquals(replacedAttr.getNodeValue(), "attributeValue"); + assertEquals("attributeValue", replacedAttr.getNodeValue()); Node updatedAttrNode = namedNodeMap.getNamedItem("name"); - assertEquals(updatedAttrNode.getNodeValue(), ""); + assertEquals("", updatedAttrNode.getNodeValue()); Attr newAttr = document.createAttribute("nonExistingName"); assertNull(namedNodeMap.setNamedItem(newAttr)); Node newAttrNode = namedNodeMap.getNamedItem("nonExistingName"); - assertEquals(newAttrNode.getNodeValue(), ""); + assertEquals("", newAttrNode.getNodeValue()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeListTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeListTest.java index 740c28279f6..e04b24bd9ae 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeListTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeListTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,31 +22,31 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.NodeListTest + * @run junit/othervm org.w3c.dom.ptests.NodeListTest * @summary Verifies a bug found in jaxp1.0.1 and 1.1FCS. After going out of * bound, the last element of a NodeList returns null. The bug has been fixed * in jaxp 1.1.1 build. */ public class NodeListTest { - @DataProvider(name = "xml") - public Object[][] getTestData() { + public static Object[][] getTestData() { return new Object[][] { { "nodelist.xml", "document" }, { "Node01.xml", "body" } }; } - @Test(dataProvider = "xml") + @ParameterizedTest + @MethodSource("getTestData") public void lastItemTest(String xmlFileName, String nodeName) throws Exception { Document document = createDOM(xmlFileName); @@ -56,8 +56,7 @@ public class NodeListTest { Element elem1 = (Element) nl.item(n - 1); nl.item(n); Element elem3 = (Element) nl.item(n - 1); - assertEquals(elem3, elem1); - + assertEquals(elem1, elem3); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeTest.java index 8df0808f9d4..828503d0e0b 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NodeTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,29 +22,10 @@ */ package org.w3c.dom.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotEquals; -import static org.testng.Assert.assertTrue; -import static org.w3c.dom.ptests.DOMTestUtil.GOLDEN_DIR; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import java.io.File; -import java.util.PropertyPermission; - -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.TransformerFactoryConfigurationError; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.DocumentFragment; @@ -52,15 +33,35 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.w3c.dom.ptests.DOMTestUtil.GOLDEN_DIR; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.NodeTest + * @run junit/othervm org.w3c.dom.ptests.NodeTest * @summary Test Node interface */ public class NodeTest { - @DataProvider(name = "feature-supported") - public Object[][] getFeatureSupportedList() throws Exception { + public static Object[][] getFeatureSupportedList() throws Exception { Document document = createDOMWithNS("Node01.xml"); Node node = document.getElementsByTagName("body").item(0); return new Object[][] { @@ -81,9 +82,10 @@ public class NodeTest { /* * Verify Node for feature supporting. */ - @Test(dataProvider = "feature-supported") + @ParameterizedTest + @MethodSource("getFeatureSupportedList") public void testHasFeature(Node node, String feature, String version, boolean supported) { - assertEquals(node.isSupported(feature, version), supported); + assertEquals(supported, node.isSupported(feature, version)); } /* @@ -98,7 +100,7 @@ public class NodeTest { Node node = document.getElementsByTagName("title").item(0); node.appendChild(document.createTextNode("test")); root.normalize(); - assertEquals(node.getChildNodes().item(0).getNodeValue(), "Typographytest"); + assertEquals("Typographytest", node.getChildNodes().item(0).getNodeValue()); } /* @@ -154,10 +156,10 @@ public class NodeTest { Element element = (Element) document.getElementsByTagName("sender").item(0); parentElement.insertBefore(createTestDocumentFragment(document), element); - String outputfile = USER_DIR + "InsertBefore.out"; + String outputfile = "InsertBefore.out"; String goldfile = GOLDEN_DIR + "InsertBeforeGF.out"; outputXml(document, outputfile); - assertTrue(compareWithGold(goldfile, outputfile)); + assertLinesMatch(goldfile, outputfile); } @@ -172,10 +174,10 @@ public class NodeTest { Element element = (Element) document.getElementsByTagName("sender").item(0); parentElement.replaceChild(createTestDocumentFragment(document), element); - String outputfile = USER_DIR + "ReplaceChild3.out"; + String outputfile = "ReplaceChild3.out"; String goldfile = GOLDEN_DIR + "ReplaceChild3GF.out"; outputXml(document, outputfile); - assertTrue(compareWithGold(goldfile, outputfile)); + assertLinesMatch(goldfile, outputfile); } /* @@ -183,14 +185,16 @@ public class NodeTest { * with a node which was created from a different document than the one * which is trying to use this method. It should throw a DOMException. */ - @Test(expectedExceptions = DOMException.class) + @Test public void testReplaceChildNeg() throws Exception { Document document = createDOM("Node04.xml"); Document doc2 = createNewDocument(); Element parentElement = (Element) document.getElementsByTagName("to").item(0); Element element = (Element) document.getElementsByTagName("sender").item(0); - parentElement.replaceChild(createTestDocumentFragment(doc2), element); + assertThrows( + DOMException.class, () -> + parentElement.replaceChild(createTestDocumentFragment(doc2), element)); } private DocumentFragment createTestDocumentFragment(Document document) { @@ -207,4 +211,10 @@ public class NodeTest { StreamResult streamResult = new StreamResult(new File(outputFileName)); transformer.transform(domSource, streamResult); } + + private static void assertLinesMatch(String goldenFile, String actual) throws IOException { + Assertions.assertLinesMatch( + Files.readAllLines(Path.of(goldenFile)), + Files.readAllLines(Path.of(actual))); + } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NotationTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NotationTest.java index 301df8e51c3..60630deb477 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NotationTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/NotationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,23 +22,22 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createDOM; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Notation; import org.xml.sax.SAXException; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.createDOM; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.NotationTest + * @run junit/othervm org.w3c.dom.ptests.NotationTest * @summary Test for Notation interface */ public class NotationTest { @@ -47,7 +46,7 @@ public class NotationTest { */ @Test public void testGetSystemId() throws Exception { - assertEquals(findNotation("gs").getSystemId(), "http://who.knows.where/"); + assertEquals("http://who.knows.where/", findNotation("gs").getSystemId()); } /* @@ -55,7 +54,7 @@ public class NotationTest { */ @Test public void testGetPublicId() throws Exception { - assertEquals(findNotation("pubname").getPublicId(), "pubId"); + assertEquals("pubId", findNotation("pubname").getPublicId()); } //find notation in Notation01.xml diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/PITest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/PITest.java index ea6a9072002..0dbff540c97 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/PITest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/PITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,17 +22,17 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.ProcessingInstruction; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.PITest + * @run junit/othervm org.w3c.dom.ptests.PITest * @summary Test for the methods of Processing Instruction */ public class PITest { @@ -43,11 +43,11 @@ public class PITest { public void test() throws Exception { Document document = createDOMWithNS("PITest01.xml"); ProcessingInstruction pi = document.createProcessingInstruction("PI", "processing"); - assertEquals(pi.getData(), "processing"); - assertEquals(pi.getTarget(), "PI"); + assertEquals("processing", pi.getData()); + assertEquals("PI", pi.getTarget()); pi.setData("newProcessing"); - assertEquals(pi.getData(), "newProcessing"); + assertEquals("newProcessing", pi.getData()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TextTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TextTest.java index e763eda5c35..507801d1a2d 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TextTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TextTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,15 +22,7 @@ */ package org.w3c.dom.ptests; -import static org.testng.Assert.assertEquals; -import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; -import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -38,10 +30,17 @@ import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.SAXException; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; +import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; + /* * @test * @library /javax/xml/jaxp/libs /javax/xml/jaxp/functional - * @run testng/othervm org.w3c.dom.ptests.TextTest + * @run junit/othervm org.w3c.dom.ptests.TextTest * @summary Test for Text implementation returned by Document.createTextNode(String) */ public class TextTest extends AbstractCharacterDataTest { @@ -60,8 +59,7 @@ public class TextTest extends AbstractCharacterDataTest { textNode.splitText(0); int increased = node.getChildNodes().getLength() - rawChildNum; - assertEquals(increased, 1); - + assertEquals(1, increased); } @Override diff --git a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TypeInfoTest.java b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TypeInfoTest.java index 1013d5f4baf..d101b45d5f1 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TypeInfoTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/w3c/dom/ptests/TypeInfoTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, 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 @@ -22,24 +22,23 @@ */ package org.w3c.dom.ptests; -import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; -import static org.testng.Assert.assertEquals; - -import java.io.StringReader; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.TypeInfo; import org.xml.sax.InputSource; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.StringReader; + +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; +import static org.junit.jupiter.api.Assertions.assertEquals; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.w3c.dom.ptests.TypeInfoTest + * @run junit/othervm org.w3c.dom.ptests.TypeInfoTest * @summary Test getTypeName and getTypeNamespace methods of TypeInfo interface */ public class TypeInfoTest { @@ -48,11 +47,14 @@ public class TypeInfoTest { */ @Test public void test() throws Exception { - TypeInfo typeInfo = getTypeOfRoot(SCHEMA_INSTANCE, "\n" + "\n"); - - assertEquals(typeInfo.getTypeName(), "Test"); - assertEquals(typeInfo.getTypeNamespace(), "testNS"); + TypeInfo typeInfo = getTypeOfRoot(SCHEMA_INSTANCE, + """ + + + """); + assertEquals("Test", typeInfo.getTypeName()); + assertEquals("testNS", typeInfo.getTypeNamespace()); } private TypeInfo getTypeOfRoot(String schemaText, String docText) throws Exception { @@ -87,53 +89,55 @@ public class TypeInfoTest { * Schema instance */ private static final String SCHEMA_INSTANCE = - "\n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "\n" - + " \n" - + "\n" - + " \n" - + "\n" - + " \n" - + " \n" - + " \n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "\n" - + "\n"; + """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """; } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttrImplTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttrImplTest.java index b43012a6197..2af17996158 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttrImplTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttrImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,19 +22,20 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.helpers.AttributesImpl; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * Class containing the test cases for AttributesImpl API. */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.AttrImplTest + * @run junit/othervm org.xml.sax.ptests.AttrImplTest */ public class AttrImplTest { private static final String CAR_URI = "http://www.cars.com/xml"; @@ -66,8 +67,8 @@ public class AttrImplTest { attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); - assertEquals(attr.getIndex(CAR_QNAME), 0); - assertEquals(attr.getIndex(JEEP_QNAME), 1); + assertEquals(0, attr.getIndex(CAR_QNAME)); + assertEquals(1, attr.getIndex(JEEP_QNAME)); } /** @@ -79,7 +80,7 @@ public class AttrImplTest { attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); - assertEquals(attr.getIndex(JEEP_URI, JEEP_LOCALNAME), 1); + assertEquals(1, attr.getIndex(JEEP_URI, JEEP_LOCALNAME)); } /** @@ -88,7 +89,7 @@ public class AttrImplTest { @Test public void testcase03() { AttributesImpl attr = new AttributesImpl(); - assertEquals(attr.getIndex(JEEP_URI, "whl"), -1); + assertEquals(-1, attr.getIndex(JEEP_URI, "whl")); } /** @@ -100,8 +101,8 @@ public class AttrImplTest { attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); - assertEquals(attr.getType(1), JEEP_TYPE); - assertEquals(attr.getType(JEEP_QNAME), JEEP_TYPE); + assertEquals(JEEP_TYPE, attr.getType(1)); + assertEquals(JEEP_TYPE, attr.getType(JEEP_QNAME)); } /** @@ -113,9 +114,9 @@ public class AttrImplTest { attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); - assertEquals(attr.getValue(1), JEEP_VALUE); - assertEquals(attr.getValue(attr.getQName(1)), JEEP_VALUE); - assertEquals(attr.getValue(attr.getURI(1), attr.getLocalName(1)), JEEP_VALUE); + assertEquals(JEEP_VALUE, attr.getValue(1)); + assertEquals(JEEP_VALUE, attr.getValue(attr.getQName(1))); + assertEquals(JEEP_VALUE, attr.getValue(attr.getURI(1), attr.getLocalName(1))); } /** @@ -129,11 +130,11 @@ public class AttrImplTest { attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); attr.setAttribute(1, "www.megginson.com", "author", "meg", "s", "SAX2"); - assertEquals(attr.getLocalName(1), "author"); - assertEquals(attr.getQName(1), "meg"); - assertEquals(attr.getType(1), "s"); - assertEquals(attr.getType("meg"), "s"); - assertEquals(attr.getURI(1), "www.megginson.com"); + assertEquals("author", attr.getLocalName(1)); + assertEquals("meg", attr.getQName(1)); + assertEquals("s", attr.getType(1)); + assertEquals("s", attr.getType("meg")); + assertEquals("www.megginson.com", attr.getURI(1)); } /** @@ -152,11 +153,11 @@ public class AttrImplTest { attr.setValue(1, "SAX01"); attr.setURI(1, "www.megginson.com/sax/sax01"); - assertEquals(attr.getLocalName(1), "speclead"); - assertEquals(attr.getQName(1), "megi"); - assertEquals(attr.getType(1), "sax"); - assertEquals(attr.getType("megi"), "sax"); - assertEquals(attr.getURI(1), "www.megginson.com/sax/sax01"); + assertEquals("speclead", attr.getLocalName(1)); + assertEquals("megi", attr.getQName(1)); + assertEquals("sax", attr.getType(1)); + assertEquals("sax", attr.getType("megi")); + assertEquals("www.megginson.com/sax/sax01", attr.getURI(1)); } /** @@ -165,11 +166,11 @@ public class AttrImplTest { @Test public void testcase08() { AttributesImpl attr = new AttributesImpl(); - assertEquals(attr.getLength(), 0); + assertEquals(0, attr.getLength()); attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); - assertEquals(attr.getLength(), 2); + assertEquals(2, attr.getLength()); } /** @@ -189,13 +190,13 @@ public class AttrImplTest { * Javadoc says java.lang.ArrayIndexOutOfBoundsException is thrown When the * supplied index does not point to an attribute in the list. */ - @Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) + @Test public void testcase10() { AttributesImpl attr = new AttributesImpl(); attr.addAttribute(CAR_URI, CAR_LOCALNAME, CAR_QNAME, CAR_TYPE, CAR_VALUE); attr.addAttribute(JEEP_URI, JEEP_LOCALNAME, JEEP_QNAME, JEEP_TYPE, JEEP_VALUE); attr.removeAttribute(1); - attr.removeAttribute(1); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> attr.removeAttribute(1)); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesNSTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesNSTest.java index 5e7db17c746..687ea92c9c9 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesNSTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesNSTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,18 +22,17 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.File; +import org.junit.jupiter.api.Test; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.testng.annotations.Test; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** * This tests the Attributes interface. Here the startElement() callback of @@ -45,7 +44,7 @@ import org.testng.annotations.Test; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.AttributesNSTest + * @run junit/othervm org.xml.sax.ptests.AttributesNSTest */ public class AttributesNSTest { /** @@ -55,7 +54,7 @@ public class AttributesNSTest { */ @Test public void testcase01() throws Exception { - String outputFile = USER_DIR + "AttributesNS.out"; + String outputFile = "AttributesNS.out"; String goldFile = GOLDEN_DIR + "AttributesNSGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); @@ -68,6 +67,8 @@ public class AttributesNSTest { MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile); saxParser.parse(new File(xmlFile), myAttrCHandler); myAttrCHandler.flushAndClose(); - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesTest.java index 6ebdf070060..e37d0b93e5d 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,18 +22,17 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.File; +import org.junit.jupiter.api.Test; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.testng.annotations.Test; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** * This tests the Attributes interface. Here the startElement() callback of @@ -46,7 +45,7 @@ import org.testng.annotations.Test; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.AttributesTest + * @run junit/othervm org.xml.sax.ptests.AttributesTest */ public class AttributesTest { /** @@ -57,7 +56,7 @@ public class AttributesTest { */ @Test public void testcase01() throws Exception { - String outputFile = USER_DIR + "Attributes.out"; + String outputFile = "Attributes.out"; String goldFile = GOLDEN_DIR + "AttributesGF.out"; String xmlFile = XML_DIR + "family.xml"; @@ -70,6 +69,8 @@ public class AttributesTest { MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile); saxParser.parse(new File(xmlFile), myAttrCHandler); myAttrCHandler.flushAndClose(); - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ContentHandlerTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ContentHandlerTest.java index b450caba7aa..0c06cd27b7e 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ContentHandlerTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ContentHandlerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,20 +22,7 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.Locator; @@ -43,6 +30,18 @@ import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParserFactory; +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Class registers a content event handler to XMLReader. Content event handler * transverses XML and print all visited node when XMLreader parses XML. Test @@ -51,7 +50,7 @@ import org.xml.sax.helpers.XMLFilterImpl; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.ContentHandlerTest + * @run junit/othervm org.xml.sax.ptests.ContentHandlerTest */ public class ContentHandlerTest { /** @@ -61,7 +60,7 @@ public class ContentHandlerTest { */ @Test public void testcase01() throws Exception { - String outputFile = USER_DIR + "Content.out"; + String outputFile = "Content.out"; String goldFile = GOLDEN_DIR + "ContentGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; @@ -73,7 +72,9 @@ public class ContentHandlerTest { xmlReader.setContentHandler(cHandler); xmlReader.parse(new InputSource(instream)); } - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } @@ -112,7 +113,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write characters tag along with content of characters when meet * characters event. - * @throws IOException error happen when writing file. */ @Override public void characters(char[] ch, int start, int length) throws SAXException { @@ -123,7 +123,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write endDocument tag then flush the content and close the file when meet * endDocument event. - * @throws IOException error happen when writing file or closing file. */ @Override public void endDocument() throws SAXException { @@ -139,7 +138,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write endElement tag with namespaceURI, localName, qName to the file when * meet endElement event. - * @throws IOException error happen when writing file. */ @Override public void endElement(String namespaceURI,String localName,String qName) throws SAXException{ @@ -150,7 +148,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write endPrefixMapping tag along with prefix to the file when meet * endPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void endPrefixMapping(String prefix) throws SAXException { @@ -160,7 +157,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write ignorableWhitespace tag along with white spaces when meet * ignorableWhitespace event. - * @throws IOException error happen when writing file. */ @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { @@ -172,7 +168,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write processingInstruction tag along with target name and target data * when meet processingInstruction event. - * @throws IOException error happen when writing file. */ @Override public void processingInstruction(String target, String data) throws SAXException { @@ -196,7 +191,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write skippedEntity tag along with entity name when meet skippedEntity * event. - * @throws IOException error happen when writing file. */ @Override public void skippedEntity(String name) throws SAXException { @@ -205,7 +199,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write startDocument tag when meet startDocument event. - * @throws IOException error happen when writing file. */ @Override public void startDocument() throws SAXException { @@ -215,7 +208,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write startElement tag along with namespaceURI, localName, qName, number * of attributes and line number when meet startElement event. - * @throws IOException error happen when writing file. */ @Override public void startElement(String namespaceURI, String localName, @@ -229,7 +221,6 @@ class MyContentHandler extends XMLFilterImpl implements AutoCloseable { /** * Write startPrefixMapping tag along with prefix and uri when meet * startPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/DefaultHandlerTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/DefaultHandlerTest.java index f65a290cb2c..548cb6b4e43 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/DefaultHandlerTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/DefaultHandlerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,27 +22,26 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * XMLReader parse XML with default handler that transverses XML and * print all visited node. Test verifies output is same as the golden file. @@ -50,7 +49,7 @@ import org.xml.sax.helpers.DefaultHandler; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.DefaultHandlerTest + * @run junit/othervm org.xml.sax.ptests.DefaultHandlerTest */ public class DefaultHandlerTest { /** @@ -60,7 +59,7 @@ public class DefaultHandlerTest { */ @Test public void testDefaultHandler() throws Exception { - String outputFile = USER_DIR + "DefaultHandler.out"; + String outputFile = "DefaultHandler.out"; String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; @@ -75,9 +74,9 @@ public class DefaultHandlerTest { if (File.separatorChar == '\\') newAbsolutePath = Absolutepath.replace('\\', '/'); saxparser.parse("file:///" + newAbsolutePath, handler); - - assertTrue(compareWithGold(goldFile, outputFile)); - + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } @@ -108,7 +107,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write characters tag along with content of characters when meet * characters event. - * @throws IOException error happen when writing file. */ @Override public void characters(char[] ch, int start, int length) throws SAXException { @@ -118,7 +116,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write endDocument tag then flush the content and close the file when meet * endDocument event. - * @throws IOException error happen when writing file or closing file. */ @Override public void endDocument() throws SAXException { @@ -134,7 +131,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write endElement tag with namespaceURI, localName, qName to the file when * meet endElement event. - * @throws IOException error happen when writing file. */ @Override public void endElement(String namespaceURI,String localName,String qName) throws SAXException{ @@ -145,7 +141,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write endPrefixMapping tag along with prefix to the file when meet * endPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void endPrefixMapping(String prefix) throws SAXException { @@ -155,7 +150,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write error tag along with exception to the file when meet recoverable * error event. - * @throws IOException error happen when writing file. */ @Override public void error(SAXParseException e) throws SAXException { @@ -165,7 +159,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write fatalError tag along with exception to the file when meet * unrecoverable error event. - * @throws IOException error happen when writing file. */ @Override public void fatalError(SAXParseException e) throws SAXException { @@ -174,7 +167,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write warning tag along with exception to the file when meet warning event. - * @throws IOException error happen when writing file. */ @Override public void warning(SAXParseException e) throws SAXException { @@ -184,7 +176,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write ignorableWhitespace tag along with white spaces when meet * ignorableWhitespace event. - * @throws IOException error happen when writing file. */ @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { @@ -196,7 +187,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write processingInstruction tag along with target name and target data * when meet processingInstruction event. - * @throws IOException error happen when writing file. */ @Override public void processingInstruction(String target, String data) throws SAXException { @@ -216,7 +206,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write skippedEntity tag along with entity name when meet skippedEntity * event. - * @throws IOException error happen when writing file. */ @Override public void skippedEntity(String name) throws SAXException { @@ -225,7 +214,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write startDocument tag when meet startDocument event. - * @throws IOException error happen when writing file. */ @Override public void startDocument() throws SAXException { @@ -235,7 +223,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write startElement tag along with namespaceURI, localName, qName, number * of attributes and line number when meet startElement event. - * @throws IOException error happen when writing file. */ @Override public void startElement(String namespaceURI, String localName, @@ -248,7 +235,6 @@ class MyDefaultHandler extends DefaultHandler { /** * Write startPrefixMapping tag along with prefix and uri when meet * startPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/EHFatalTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/EHFatalTest.java index 507e9d28384..578ca881e48 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/EHFatalTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/EHFatalTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,28 +22,27 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; - -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * ErrorHandler unit test. Set a ErrorHandle to XMLReader. Capture fatal error * events in ErrorHandler. @@ -51,34 +50,31 @@ import org.xml.sax.helpers.XMLFilterImpl; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.EHFatalTest + * @run junit/othervm org.xml.sax.ptests.EHFatalTest */ public class EHFatalTest { /** * Error Handler to capture all error events to output file. Verifies the * output file is same as golden file. - * - * @throws Exception If any errors occur. */ @Test public void testEHFatal() throws Exception { - String outputFile = USER_DIR + "EHFatal.out"; + String outputFile = "EHFatal.out"; String goldFile = GOLDEN_DIR + "EHFatalGF.out"; String xmlFile = XML_DIR + "invalid.xml"; - try(MyErrorHandler eHandler = new MyErrorHandler(outputFile); - FileInputStream instream = new FileInputStream(xmlFile)) { - SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); - XMLReader xmlReader = saxParser.getXMLReader(); + SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); + XMLReader xmlReader = saxParser.getXMLReader(); + try (MyErrorHandler eHandler = new MyErrorHandler(outputFile); + FileInputStream instream = new FileInputStream(xmlFile)) { xmlReader.setErrorHandler(eHandler); InputSource is = new InputSource(instream); - xmlReader.parse(is); - fail("Parse should throw SAXException"); - } catch (SAXException expected) { - // This is expected. + assertThrows(SAXException.class, () -> xmlReader.parse(is)); } // Need close the output file before we compare it with golden file. - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } @@ -109,7 +105,6 @@ class MyErrorHandler extends XMLFilterImpl implements AutoCloseable { /** * Write fatalError tag along with exception to the file when meet * unrecoverable error event. - * @throws IOException error happen when writing file. */ @Override public void fatalError(SAXParseException e) throws SAXException { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSSupportTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSSupportTest.java index 4ee4bf510a8..988bc3c92c9 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSSupportTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSSupportTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,13 +22,14 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; +import org.junit.jupiter.api.Test; +import org.xml.sax.helpers.NamespaceSupport; import java.util.Enumeration; -import org.testng.annotations.Test; -import org.xml.sax.helpers.NamespaceSupport; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; /** * Unit test cases for NamespaceSupport API @@ -36,7 +37,7 @@ import org.xml.sax.helpers.NamespaceSupport; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.NSSupportTest + * @run junit/othervm org.xml.sax.ptests.NSSupportTest */ public class NSSupportTest { /** @@ -70,14 +71,14 @@ public class NSSupportTest { support.declarePrefix(EMPTY_PREFIX, W3_URI); support.declarePrefix(DC_PREFIX, PURL_URI); - Enumeration e = support.getDeclaredPrefixes(); + Enumeration e = support.getDeclaredPrefixes(); int i = 0; - while(e.hasMoreElements()) { - prefixes[i++] = e.nextElement().toString(); + while (e.hasMoreElements()) { + prefixes[i++] = e.nextElement(); } support.popContext(); - assertEquals(prefixes, new String[]{EMPTY_PREFIX, DC_PREFIX}); + assertArrayEquals(new String[] { EMPTY_PREFIX, DC_PREFIX }, prefixes); } /** @@ -92,7 +93,7 @@ public class NSSupportTest { support.declarePrefix(DC_PREFIX, PURL_URI); parts = support.processName("dc:title", parts, false); support.popContext(); - assertEquals(parts, new String[]{PURL_URI, "title", "dc:title"}); + assertArrayEquals(new String[] { PURL_URI, "title", "dc:title" }, parts); } /** @@ -106,7 +107,7 @@ public class NSSupportTest { support.declarePrefix(EMPTY_PREFIX, W3_URI); parts = support.processName("a", parts, false); support.popContext(); - assertEquals(parts, new String[]{W3_URI, "a", "a"}); + assertArrayEquals(new String[] { W3_URI, "a", "a" }, parts); } @@ -121,8 +122,8 @@ public class NSSupportTest { support.declarePrefix(EMPTY_PREFIX, W3_URI); support.declarePrefix(DC_PREFIX, PURL_URI); - assertEquals(support.getURI(EMPTY_PREFIX), W3_URI); - assertEquals(support.getURI(DC_PREFIX), PURL_URI); + assertEquals(W3_URI, support.getURI(EMPTY_PREFIX)); + assertEquals(PURL_URI, support.getURI(DC_PREFIX)); support.popContext(); assertNull(support.getURI(EMPTY_PREFIX)); assertNull(support.getURI(DC_PREFIX)); diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSTableTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSTableTest.java index 81da8f25b88..eda3dc89fed 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSTableTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/NSTableTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,14 +22,14 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; +import org.junit.jupiter.api.Test; +import org.xml.sax.XMLReader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import org.testng.annotations.Test; -import org.xml.sax.XMLReader; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Class containing the test cases for Namespace Table defined at @@ -38,7 +38,7 @@ import org.xml.sax.XMLReader; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.NSTableTest + * @run junit/othervm org.xml.sax.ptests.NSTableTest */ public class NSTableTest { private static final String NAMESPACES = diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ParserAdapterTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ParserAdapterTest.java index b4a59180bac..4aaefefaa31 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ParserAdapterTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ParserAdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,16 +22,7 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.FileInputStream; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -41,6 +32,15 @@ import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLReaderAdapter; +import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Unit test cases for ParserAdapter API. By default the only features recognized @@ -49,7 +49,7 @@ import org.xml.sax.helpers.XMLReaderAdapter; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.ParserAdapterTest + * @run junit/othervm org.xml.sax.ptests.ParserAdapterTest */ public class ParserAdapterTest { /** @@ -177,9 +177,9 @@ public class ParserAdapterTest { * * @exception Exception If any errors occur. */ - @Test(expectedExceptions = SAXNotRecognizedException.class) + @Test public void getFeature03() throws Exception { - parserAdapter.getFeature("no-meaning-feature"); + assertThrows(SAXNotRecognizedException.class, () -> parserAdapter.getFeature("no-meaning-feature")); } /** @@ -228,31 +228,25 @@ public class ParserAdapterTest { /** * NPE expected when parsing a null object by ParserAdapter. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = NullPointerException.class) + @Test public void parse01() throws Exception { - parserAdapter.parse((InputSource)null); + assertThrows(NullPointerException.class, () -> parserAdapter.parse((InputSource) null)); } /** * SAXException expected when parsing a wrong-formatter XML with ParserAdapter. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXException.class) + @Test public void parse02() throws Exception { try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { InputSource is = new InputSource(fis); - parserAdapter.parse(is); + assertThrows(SAXException.class, () -> parserAdapter.parse(is)); } } /** * Parse a well-formatter XML with ParserAdapter. - * - * @throws Exception If any errors occur. */ @Test public void parse03() throws Exception { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ResolverTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ResolverTest.java index a7463edb53c..483e8b87c1d 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ResolverTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/ResolverTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,13 +22,14 @@ */ package org.xml.sax.ptests; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; +import org.junit.jupiter.api.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; @@ -36,16 +37,13 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLFilterImpl; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** * Entity resolver should be invoked in XML parse. This test verifies parsing @@ -54,24 +52,22 @@ import org.xml.sax.helpers.XMLFilterImpl; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.ResolverTest + * @run junit/othervm org.xml.sax.ptests.ResolverTest */ -@Test public class ResolverTest { /** * Unit test for entityResolver setter. - * - * @throws Exception If any errors occur. */ + @Test public void testResolver() throws Exception { - String outputFile = USER_DIR + "EntityResolver.out"; + String outputFile = "EntityResolver.out"; String goldFile = GOLDEN_DIR + "EntityResolverGF.out"; String xmlFile = XML_DIR + "publish.xml"; Files.copy(Paths.get(XML_DIR + "publishers.dtd"), - Paths.get(USER_DIR + "publishers.dtd"), REPLACE_EXISTING); + Paths.get("publishers.dtd"), REPLACE_EXISTING); Files.copy(Paths.get(XML_DIR + "familytree.dtd"), - Paths.get(USER_DIR + "familytree.dtd"), REPLACE_EXISTING); + Paths.get("familytree.dtd"), REPLACE_EXISTING); try(FileInputStream instream = new FileInputStream(xmlFile); MyEntityResolver eResolver = new MyEntityResolver(outputFile)) { @@ -81,7 +77,9 @@ public class ResolverTest { InputSource is = new InputSource(instream); xmlReader.parse(is); } - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java index 4090b6b0733..377cd02ac7d 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterCBTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,20 +22,7 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.Locator; @@ -44,6 +31,18 @@ import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParserFactory; +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter * to write to output file. Test verifies output is same as the golden file. @@ -51,17 +50,15 @@ import org.xml.sax.helpers.XMLFilterImpl; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLFilterCBTest + * @run junit/othervm org.xml.sax.ptests.XMLFilterCBTest */ -@Test public class XMLFilterCBTest { /** * Test XMLFilter working with XML reader. - * - * @throws Exception If any errors occur. */ + @Test public void testXMLFilterCB() throws Exception { - String outputFile = USER_DIR + "XMLFilter.out"; + String outputFile = "XMLFilter.out"; String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; String xmlFile = XML_DIR + "namespace1.xml"; @@ -74,7 +71,9 @@ public class XMLFilterCBTest { myXmlFilter.parse(new InputSource(fis)); } // Need close the output file before we compare it with golden file. - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldFile)), + Files.readAllLines(Path.of(outputFile))); } } @@ -103,7 +102,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write characters tag along with content of characters when meet * characters event. - * @throws IOException error happen when writing file. */ @Override public void characters(char[] ch, int start, int length) throws SAXException { @@ -114,7 +112,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write endDocument tag then flush the content and close the file when meet * endDocument event. - * @throws IOException error happen when writing file or closing file. */ @Override public void endDocument() throws SAXException { @@ -130,7 +127,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write endElement tag with namespaceURI, localName, qName to the file when * meet endElement event. - * @throws IOException error happen when writing file. */ @Override public void endElement(String namespaceURI,String localName,String qName) @@ -142,7 +138,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write endPrefixMapping tag along with prefix to the file when meet * endPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void endPrefixMapping(String prefix) throws SAXException { @@ -152,7 +147,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write error tag along with exception to the file when meet recoverable * error event. - * @throws IOException error happen when writing file. */ @Override public void error(SAXParseException e) throws SAXException { @@ -162,7 +156,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write fatalError tag along with exception to the file when meet * unrecoverable error event. - * @throws IOException error happen when writing file. */ @Override public void fatalError(SAXParseException e) throws SAXException { @@ -171,7 +164,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write warning tag along with exception to the file when meet warning event. - * @throws IOException error happen when writing file. */ @Override public void warning(SAXParseException e) throws SAXException { @@ -181,7 +173,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write ignorableWhitespace tag along with white spaces when meet * ignorableWhitespace event. - * @throws IOException error happen when writing file. */ @Override public void ignorableWhitespace(char[] ch, int start, int length) @@ -194,7 +185,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write processingInstruction tag along with target name and target data * when meet processingInstruction event. - * @throws IOException error happen when writing file. */ @Override public void processingInstruction(String target, String data) @@ -218,7 +208,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write skippedEntity tag along with entity name when meet skippedEntity * event. - * @throws IOException error happen when writing file. */ @Override public void skippedEntity(String name) throws SAXException { @@ -227,7 +216,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write startDocument tag when meet startDocument event. - * @throws IOException error happen when writing file. */ @Override public void startDocument() throws SAXException { @@ -237,7 +225,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write startElement tag along with namespaceURI, localName, qName, number * of attributes and line number when meet startElement event. - * @throws IOException error happen when writing file. */ @Override public void startElement(String namespaceURI, String localName, @@ -250,7 +237,6 @@ class MyXMLFilter extends XMLFilterImpl implements AutoCloseable { /** * Write startPrefixMapping tag along with prefix and uri when meet * startPrefixMapping event. - * @throws IOException error happen when writing file. */ @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterTest.java index 7ba882a9d1d..ee62b896b33 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,28 +22,28 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.FileInputStream; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Unit test for XMLFilter. */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLFilterTest + * @run junit/othervm org.xml.sax.ptests.XMLFilterTest */ public class XMLFilterTest { /** @@ -132,8 +132,6 @@ public class XMLFilterTest { /** * By default true is expected get namespaces feature. - * - * @throws Exception If any errors occur. */ @Test public void getFeature01() throws Exception { @@ -148,8 +146,6 @@ public class XMLFilterTest { /** * By default false is expected get namespaces-prefix feature. - * - * @throws Exception If any errors occur. */ @Test public void getFeature02() throws Exception { @@ -163,19 +159,15 @@ public class XMLFilterTest { /** * SAXNotRecognizedException is expected when get a feature by an invalid * feature name. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXNotRecognizedException.class) + @Test public void getFeature03() throws Exception { - new XMLFilterImpl().getFeature("no-meaning-feature"); + assertThrows(SAXNotRecognizedException.class, () -> new XMLFilterImpl().getFeature("no-meaning-feature")); } /** * Set namespaces feature to a value to XMLFilter. it's expected same when * obtain it again. - * - * @throws Exception If any errors occur. */ @Test public void setFeature01() throws Exception { @@ -193,8 +185,6 @@ public class XMLFilterTest { /** * Set namespaces-prefix feature to a value to XMLFilter. it's expected same * when obtain it again. - * - * @throws Exception If any errors occur. */ @Test public void setFeature02() throws Exception { @@ -211,35 +201,31 @@ public class XMLFilterTest { /** * NullPointerException is expected when parse a null InputSource. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = NullPointerException.class) + @Test public void parse01() throws Exception { - new XMLFilterImpl().parse((InputSource)null); + assertThrows(NullPointerException.class, () -> new XMLFilterImpl().parse((InputSource) null)); } /** * SAXException is expected when parsing a invalid formatted XML file. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = NullPointerException.class) + @Test public void parse02() throws Exception { - try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { - new XMLFilterImpl().parse(new InputSource(fis)); + try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { + InputSource input = new InputSource(fis); + assertThrows(NullPointerException.class, () -> new XMLFilterImpl().parse(input)); } } /** * No exception when parse a normal XML file. - * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = NullPointerException.class) + @Test public void parse03() throws Exception { - try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { - new XMLFilterImpl().parse(new InputSource(fis)); + try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { + InputSource input = new InputSource(fis); + assertThrows(NullPointerException.class, () -> new XMLFilterImpl().parse(input)); } } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderAdapterTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderAdapterTest.java index 40462fcf076..65dbaba9971 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderAdapterTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderAdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,28 +22,27 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.FileInputStream; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.HandlerBase; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderAdapter; +import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Class containing the test cases for XMLReaderAdapter API */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLReaderAdapterTest + * @run junit/othervm org.xml.sax.ptests.XMLReaderAdapterTest */ public class XMLReaderAdapterTest { /** @@ -64,21 +63,17 @@ public class XMLReaderAdapterTest { /** * To test the constructor that uses XMLReader. - * - * @throws Exception If any errors occur. */ @Test public void constructor02() throws Exception { XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); - assertNotNull(new XMLReaderAdapter(xmlReader)); + new XMLReaderAdapter(xmlReader); } /** * To test the parse method. The specification says that this method * will throw an exception if the embedded XMLReader does not support * the http://xml.org/sax/features/namespace-prefixes property. - * - * @throws Exception If any errors occur. */ @Test public void nsfeature01() throws Exception { @@ -93,8 +88,6 @@ public class XMLReaderAdapterTest { * To test the parse method. The specification says that this method * will throw an exception if the embedded XMLReader does not support * the http://xml.org/sax/features/namespace-prefixes property. - * - * @throws Exception If any errors occur. */ @Test public void parse01() throws Exception { diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderFactoryTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderFactoryTest.java index a109a27bce4..c8272d186ea 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderFactoryTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,21 +22,21 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.setSystemProperty; - -import static org.testng.Assert.assertNotNull; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.SAXException; import org.xml.sax.helpers.XMLReaderFactory; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * Unit test for XMLReaderFactory.createXMLReader API. */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLReaderFactoryTest + * @run junit/othervm org.xml.sax.ptests.XMLReaderFactoryTest */ public class XMLReaderFactoryTest { /** @@ -56,20 +56,25 @@ public class XMLReaderFactoryTest { */ @Test public void createReader02() throws SAXException { - setSystemProperty("org.xml.sax.driver", - "com.sun.org.apache.xerces.internal.parsers.SAXParser"); - assertNotNull(XMLReaderFactory. - createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser")); + System.setProperty("org.xml.sax.driver", + "com.sun.org.apache.xerces.internal.parsers.SAXParser"); + try { + assertNotNull(XMLReaderFactory.createXMLReader( + "com.sun.org.apache.xerces.internal.parsers.SAXParser")); + } finally { + System.clearProperty("org.xml.sax.driver"); + } } /** * SAXException expected when create XMLReader with an invalid driver name. * @throws org.xml.sax.SAXException expected Exception */ - @Test(expectedExceptions = SAXException.class, - expectedExceptionsMessageRegExp = - "SAX2 driver class org.apache.crimson.parser.ABCD not found") - public void createReader03() throws SAXException{ - XMLReaderFactory.createXMLReader("org.apache.crimson.parser.ABCD"); + @Test + public void createReader03() throws SAXException { + SAXException e = assertThrows( + SAXException.class, + () -> XMLReaderFactory.createXMLReader("org.apache.crimson.parser.ABCD")); + assertEquals("SAX2 driver class org.apache.crimson.parser.ABCD not found", e.getMessage()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderNSTableTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderNSTableTest.java index 9874866c7ec..2e7d0bc548f 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderNSTableTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderNSTableTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,20 +22,20 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.FileInputStream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; -import org.testng.annotations.Test; -import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; +import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** This class contains the testcases to test XMLReader with regard to * Namespace Table defined at @@ -44,9 +44,8 @@ import org.xml.sax.XMLReader; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLReaderNSTableTest + * @run junit/othervm org.xml.sax.ptests.XMLReaderNSTableTest */ -@Test public class XMLReaderNSTableTest { /** * XML file that used to be parsed. @@ -62,11 +61,10 @@ public class XMLReaderNSTableTest { * namespace processing is enabled. namespace-prefix is also is enabled. * So it is a True-True combination. * The test is to test XMLReader with these conditions. - * - * @throws Exception If any errors occur. */ + @Test public void testWithTrueTrue() throws Exception { - String outputFile = USER_DIR + "XRNSTableTT.out"; + String outputFile = "XRNSTableTT.out"; String goldFile = GOLDEN_DIR + "NSTableTTGF.out"; SAXParserFactory spf = SAXParserFactory.newInstance(); @@ -79,18 +77,17 @@ public class XMLReaderNSTableTest { xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(fis)); } - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch(goldFile, outputFile); } /** * Namespace processing is enabled. Hence namespace-prefix is * expected to be automatically off. So it is a True-False combination. * The test is to test XMLReader with these conditions. - * - * @throws Exception If any errors occur. */ + @Test public void testWithTrueFalse() throws Exception { - String outputFile = USER_DIR + "XRNSTableTF.out"; + String outputFile = "XRNSTableTF.out"; String goldFile = GOLDEN_DIR + "NSTableTFGF.out"; SAXParserFactory spf = SAXParserFactory.newInstance(); @@ -103,18 +100,17 @@ public class XMLReaderNSTableTest { xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(fis)); } - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch(goldFile, outputFile); } /** * namespace processing is not enabled. Hence namespace-prefix is * expected to be automaically on. So it is a False-True combination. * The test is to test XMLReader with these conditions. - * - * @throws Exception If any errors occur. */ - public void testWithFalseTrue()throws Exception { - String outputFile = USER_DIR + "XRNSTableFT.out"; + @Test + public void testWithFalseTrue() throws Exception { + String outputFile = "XRNSTableFT.out"; String goldFile = GOLDEN_DIR + "NSTableFTGF.out"; SAXParserFactory spf = SAXParserFactory.newInstance(); @@ -125,6 +121,12 @@ public class XMLReaderNSTableTest { xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(fis)); } - assertTrue(compareWithGold(goldFile, outputFile)); + assertLinesMatch(goldFile, outputFile); + } + + private static void assertLinesMatch(String goldenFile, String actual) throws IOException { + Assertions.assertLinesMatch( + Files.readAllLines(Path.of(goldenFile)), + Files.readAllLines(Path.of(actual))); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderTest.java b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderTest.java index 497df48f7e4..f726e19922c 100644 --- a/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,18 +22,7 @@ */ package org.xml.sax.ptests; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.xml.sax.ptests.SAXTestConst.XML_DIR; - -import java.io.FileInputStream; - -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; @@ -43,13 +32,24 @@ import org.xml.sax.ext.DeclHandler; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.XMLFilterImpl; +import javax.xml.parsers.SAXParserFactory; +import java.io.FileInputStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xml.sax.ptests.SAXTestConst.XML_DIR; + /** * Class containing the test cases for SAXParser API */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm org.xml.sax.ptests.XMLReaderTest + * @run junit/othervm org.xml.sax.ptests.XMLReaderTest */ public class XMLReaderTest { @@ -309,11 +309,11 @@ public class XMLReaderTest { * * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXNotRecognizedException.class) + @Test public void featureNE01() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); - spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature"); + assertThrows(SAXNotRecognizedException.class, () -> spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature")); } /** @@ -328,7 +328,7 @@ public class XMLReaderTest { XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setEntityResolver(xmlFilter); - assertEquals(xmlReader.getEntityResolver(), xmlFilter); + assertEquals(xmlFilter, xmlReader.getEntityResolver()); } /** @@ -355,7 +355,7 @@ public class XMLReaderTest { XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setDTDHandler(xmlFilter); - assertEquals(xmlReader.getDTDHandler(), xmlFilter); + assertEquals(xmlFilter, xmlReader.getDTDHandler()); } /** @@ -382,7 +382,7 @@ public class XMLReaderTest { XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setContentHandler(xmlFilter); - assertEquals(xmlReader.getContentHandler(), xmlFilter); + assertEquals(xmlFilter, xmlReader.getContentHandler()); } /** @@ -429,11 +429,11 @@ public class XMLReaderTest { * * @throws Exception If any errors occur. */ - @Test(expectedExceptions = NullPointerException.class) + @Test public void parse01() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); - spf.newSAXParser().getXMLReader().parse((InputSource) null); + assertThrows(NullPointerException.class, () -> spf.newSAXParser().getXMLReader().parse((InputSource) null)); } /** @@ -441,12 +441,12 @@ public class XMLReaderTest { * * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXException.class) + @Test public void parse02() throws Exception { try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); - spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); + assertThrows(SAXException.class, () -> spf.newSAXParser().getXMLReader().parse(new InputSource(fis))); } } @@ -470,11 +470,11 @@ public class XMLReaderTest { * * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXNotSupportedException.class) + @Test public void xrProperty01() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); - xmlReader.getProperty(XML_STRING); + assertThrows(SAXNotSupportedException.class, () -> xmlReader.getProperty(XML_STRING)); } /** @@ -483,11 +483,11 @@ public class XMLReaderTest { * * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXNotSupportedException.class) + @Test public void xrProperty02() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); - assertNull(xmlReader.getProperty(DOM_NODE)); + assertThrows(SAXNotSupportedException.class, () -> xmlReader.getProperty(DOM_NODE)); } /** @@ -623,7 +623,7 @@ class MyDeclHandler implements DeclHandler { * @param eName The name of the associated element. * @param aName The name of the attribute. * @param type A string representing the attribute type. - * @param mode A string representing the attribute defaulting mode + * @param valueDefault A string representing the attribute defaulting mode * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if none of these applies. * @param value A string representing the attribute's default value, or null * if there is none. diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/AstroTest.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/AstroTest.java index a42fcd9d3d7..ae7b88c5d34 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/AstroTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/AstroTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -23,26 +23,25 @@ package test.astro; -import static java.lang.String.valueOf; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareWithGold; -import static org.testng.Assert.assertTrue; -import static test.astro.AstroConstants.ASTROCAT; -import static test.astro.AstroConstants.GOLDEN_DIR; - -import java.nio.file.Files; -import java.nio.file.Paths; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import javax.xml.transform.sax.TransformerHandler; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import static java.lang.String.valueOf; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static test.astro.AstroConstants.ASTROCAT; +import static test.astro.AstroConstants.GOLDEN_DIR; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.AstroTest + * @run junit/othervm test.astro.AstroTest * @summary run astro application, test xslt * * There are vast amounts of textual astronomical data, typically user is @@ -67,10 +66,11 @@ import org.testng.annotations.Test; * AstroProcessor to test different JAXP classes and features. * */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class AstroTest { private FiltersAndGolden[] data; - @BeforeClass + @BeforeAll public void setup() throws Exception { data = new FiltersAndGolden[4]; data[0] = new FiltersAndGolden(getGoldenFileName(1), astro -> astro.getRAFilter(0.106, 0.108)); @@ -82,8 +82,7 @@ public class AstroTest { /* * Provide permutations of InputSourceFactory and FilterFactory for test. */ - @DataProvider(name = "factories") - public Object[][] getQueryFactories() { + public static Object[][] getQueryFactories() { return new Object[][] { { StreamFilterFactoryImpl.class, InputSourceFactoryImpl.class }, { SAXFilterFactoryImpl.class, InputSourceFactoryImpl.class }, @@ -92,7 +91,8 @@ public class AstroTest { { StreamFilterFactoryImpl.class, DOML3InputSourceFactoryImpl.class } }; } - @Test(dataProvider = "factories") + @ParameterizedTest + @MethodSource("getQueryFactories") public void test(Class fFactClass, Class isFactClass) throws Exception { System.out.println(fFactClass.getName() +" : " + isFactClass.getName()); AstroProcessor astro = new AstroProcessor(fFactClass, ASTROCAT, isFactClass); @@ -108,10 +108,12 @@ public class AstroTest { for (int i = 0; i < filterCreators.length; i++) filters[i] = filterCreators[i].createFilter(astro); - String outputfile = Files.createTempFile(Paths.get(USER_DIR), "query" + processNum + ".out.", null).toString(); + String outputfile = Files.createTempFile(Paths.get("."), "query" + processNum + ".out.", null).toString(); System.out.println("output file: " + outputfile); astro.process(outputfile, filters); - assertTrue(compareWithGold(goldenFileName, outputfile)); + assertLinesMatch( + Files.readAllLines(Path.of(goldenFileName)), + Files.readAllLines(Path.of(outputfile))); } private String getGoldenFileName(int num) { @@ -124,8 +126,8 @@ public class AstroTest { } private static class FiltersAndGolden { - private FilterCreator[] filters; - private String goldenFileName; + private final FilterCreator[] filters; + private final String goldenFileName; FiltersAndGolden(String goldenFileName, FilterCreator... filters) { this.filters = filters; diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/DocumentLSTest.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/DocumentLSTest.java index d337ee1bbad..4082e0603d6 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/DocumentLSTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/DocumentLSTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -22,26 +22,7 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; -import static test.astro.AstroConstants.ASTROCAT; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Writer; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.ls.DOMImplementationLS; @@ -50,10 +31,27 @@ import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSParser; import org.w3c.dom.ls.LSSerializer; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Writer; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; +import static test.astro.AstroConstants.ASTROCAT; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.DocumentLSTest + * @run junit/othervm test.astro.DocumentLSTest * @summary org.w3c.dom.ls tests */ public class DocumentLSTest { @@ -84,11 +82,11 @@ public class DocumentLSTest { assertTrue(src.getCertifiedText()); src.setCertifiedText(origCertified); // set back to orig - src.setSystemId(filenameToURL(ASTROCAT)); + src.setSystemId(Path.of(ASTROCAT).toUri().toASCIIString()); Document doc = domParser.parse(src); Element result = doc.getDocumentElement(); - assertEquals(result.getTagName(), "stardb"); + assertEquals("stardb", result.getTagName()); } } @@ -106,12 +104,12 @@ public class DocumentLSTest { domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); LSInput src = impl.createLSInput(); src.setStringData(xml); - assertEquals(src.getStringData(), xml); + assertEquals(xml, src.getStringData()); Document doc = domParser.parse(src); String result = domSerializer.writeToString(doc); - assertEquals(result, "runDocumentLS_Q6"); + assertEquals("runDocumentLS_Q6", result); } /* @@ -128,7 +126,7 @@ public class DocumentLSTest { impl = (DOMImplementationLS) db.getDOMImplementation(); LSSerializer domSerializer = impl.createLSSerializer(); MyDOMOutput mydomoutput = new MyDOMOutput(); - try (OutputStream os = new FileOutputStream(USER_DIR + "test.out")) { + try (OutputStream os = new FileOutputStream("test.out")) { mydomoutput.setByteStream(os); mydomoutput.setEncoding("UTF-8"); assertTrue(domSerializer.write(doc, mydomoutput)); diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/NamespaceContextTest.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/NamespaceContextTest.java index 75b7880a1ca..7e5000a440e 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/NamespaceContextTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/NamespaceContextTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -22,18 +22,18 @@ */ package test.astro; -import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; -import static javax.xml.XMLConstants.NULL_NS_URI; -import static org.testng.Assert.assertEquals; +import org.junit.jupiter.api.Test; import javax.xml.namespace.QName; -import org.testng.annotations.Test; +import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; +import static javax.xml.XMLConstants.NULL_NS_URI; +import static org.junit.jupiter.api.Assertions.assertEquals; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.NamespaceContextTest + * @run junit/othervm test.astro.NamespaceContextTest * @summary javax.xml.namespace.QName tests */ public class NamespaceContextTest { @@ -47,9 +47,9 @@ public class NamespaceContextTest { @Test public void testQNameConstructor() { QName qname = new QName(NS_URI, LOCAL_PART, PREFIX); - assertEquals(qname.getNamespaceURI(), NS_URI); - assertEquals(qname.getLocalPart(), LOCAL_PART); - assertEquals(qname.getPrefix(), PREFIX); + assertEquals(NS_URI, qname.getNamespaceURI()); + assertEquals(LOCAL_PART, qname.getLocalPart()); + assertEquals(PREFIX, qname.getPrefix()); } /* @@ -59,9 +59,9 @@ public class NamespaceContextTest { @Test public void testDefaultFields() { QName qname = new QName(LOCAL_PART); // just the local part specified - assertEquals(qname.getNamespaceURI(), NULL_NS_URI); - assertEquals(qname.getLocalPart(), LOCAL_PART); - assertEquals(qname.getPrefix(), DEFAULT_NS_PREFIX); + assertEquals(NULL_NS_URI, qname.getNamespaceURI()); + assertEquals(LOCAL_PART, qname.getLocalPart()); + assertEquals(DEFAULT_NS_PREFIX, qname.getPrefix()); } /* @@ -71,9 +71,9 @@ public class NamespaceContextTest { @Test public void testDefaultPrefix() { QName qname = new QName(NS_URI, LOCAL_PART); // no pref - assertEquals(qname.getNamespaceURI(), NS_URI); - assertEquals(qname.getLocalPart(), LOCAL_PART); - assertEquals(qname.getPrefix(), DEFAULT_NS_PREFIX); + assertEquals(NS_URI, qname.getNamespaceURI()); + assertEquals(LOCAL_PART, qname.getLocalPart()); + assertEquals(DEFAULT_NS_PREFIX, qname.getPrefix()); } /* @@ -83,6 +83,6 @@ public class NamespaceContextTest { @Test public void testQNameString() { QName qname = new QName(NS_URI, LOCAL_PART, PREFIX); - assertEquals(QName.valueOf(qname.toString()), qname); + assertEquals(qname, QName.valueOf(qname.toString())); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/SAX201Test.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/SAX201Test.java index 6cdbd93ce82..3384d04161a 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/SAX201Test.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/SAX201Test.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -22,15 +22,15 @@ */ package test.astro; -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.XMLReader; +import javax.xml.parsers.SAXParserFactory; + /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.SAX201Test + * @run junit/othervm test.astro.SAX201Test * @summary verify SAX 2.0.1 allows to use null in setters */ public class SAX201Test { diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/SchemaValidationTest.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/SchemaValidationTest.java index 782831e60b8..d2e5740f1f8 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/SchemaValidationTest.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/SchemaValidationTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -22,25 +22,25 @@ */ package test.astro; -import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; -import static test.astro.AstroConstants.ASTROCAT; -import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE; -import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE; - -import java.io.File; +import org.junit.jupiter.api.Test; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import java.io.File; -import org.testng.annotations.Test; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static test.astro.AstroConstants.ASTROCAT; +import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE; +import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.SchemaValidationTest + * @run junit/othervm test.astro.SchemaValidationTest * @summary test parser sets schema related properties to do validation */ public class SchemaValidationTest { @@ -59,11 +59,10 @@ public class SchemaValidationTest { * Test SAXException shall be thrown if schemaSource is set but * schemaLanguage is not set. */ - @Test(expectedExceptions = SAXException.class) + @Test public void testSchemaValidationNeg() throws Exception { SAXParser sp = getValidatingParser(); - sp.setProperty(JAXP_SCHEMA_SOURCE, "catalog.xsd"); - sp.parse(new File(ASTROCAT), new DefaultHandler()); + assertThrows(SAXException.class, () -> sp.setProperty(JAXP_SCHEMA_SOURCE, "catalog.xsd")); } private SAXParser getValidatingParser() throws ParserConfigurationException, SAXException { diff --git a/test/jaxp/javax/xml/jaxp/functional/test/astro/XPathAPITest.java b/test/jaxp/javax/xml/jaxp/functional/test/astro/XPathAPITest.java index 04bab01fa75..b305a060459 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/astro/XPathAPITest.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/astro/XPathAPITest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2026, 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 @@ -22,18 +22,17 @@ */ package test.astro; -import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; -import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL; -import static javax.xml.xpath.XPathConstants.NODESET; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static org.testng.Assert.assertEquals; -import static test.astro.AstroConstants.ASTROCAT; -import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE; -import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE; - -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.Iterator; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; @@ -44,21 +43,26 @@ import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathVariableResolver; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Iterator; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; +import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL; +import static javax.xml.xpath.XPathConstants.NODESET; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static test.astro.AstroConstants.ASTROCAT; +import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE; +import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.astro.XPathAPITest + * @run junit/othervm test.astro.XPathAPITest * @summary test XPath API */ -@Test(singleThreaded = true) +@Execution(ExecutionMode.SAME_THREAD) +@TestInstance(Lifecycle.PER_CLASS) public class XPathAPITest { private static final String STARDB_STAR_3_CONSTELLATION = "//astro:stardb/astro:star[3]/astro:constellation"; private static final String STARDB_STAR = "//astro:stardb/astro:star"; @@ -66,7 +70,7 @@ public class XPathAPITest { private XPathFactory xpf; private NamespaceContext nsContext; - @BeforeClass + @BeforeAll public void setup() throws Exception { DocumentBuilderFactory df = DocumentBuilderFactory.newInstance(); df.setNamespaceAware(true); @@ -81,45 +85,49 @@ public class XPathAPITest { nsContext = new MyNamespaceContext(); } - @DataProvider(name = "nodelist-evaluator") - public Object[][] getNodeListEvaluator() throws MalformedURLException { - return new Object[][] { { (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement(), NODESET) }, - { (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource(), NODESET) }, - { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement(), NODESET) }, - { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource(), NODESET) } }; + public Object[] getNodeListEvaluator() { + return new Object[] { + (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement(), NODESET), + (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource(), NODESET), + (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement(), NODESET), + (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource(), NODESET), + }; } /* * Test xpath expression evaluations method that returns type indicated by * QName */ - @Test(dataProvider = "nodelist-evaluator") + @ParameterizedTest + @MethodSource("getNodeListEvaluator") public void testEvaluateNodeList(XPathEvaluator evaluator) throws Exception { NodeList o = (NodeList) evaluator.evaluate(STARDB_STAR); - assertEquals(o.getLength(), 10); + assertEquals(10, o.getLength()); } - @DataProvider(name = "string-evaluator") - public Object[][] getStringEvaluator() throws MalformedURLException { - return new Object[][] { { (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement()) }, - { (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource()) }, - { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement()) }, - { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource()) } }; + public Object[] getStringEvaluator() { + return new Object[] { + (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement()), + (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource()), + (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement()), + (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource()), + }; } /* * Test xpath expression evaluations method that returns String */ - @Test(dataProvider = "string-evaluator") + @ParameterizedTest + @MethodSource("getStringEvaluator") public void testEvaluateString(XPathEvaluator evaluator) throws Exception { - assertEquals(evaluator.evaluate(STARDB_STAR_3_CONSTELLATION), "Psc"); + assertEquals("Psc", evaluator.evaluate(STARDB_STAR_3_CONSTELLATION)); } @Test public void testXPathVariableResolver() throws Exception { XPath xpath = getXPath(); xpath.setXPathVariableResolver(new MyXPathVariableResolver()); - assertEquals(xpath.evaluate("//astro:stardb/astro:star[astro:hr=$id]/astro:constellation", doc.getDocumentElement()), "Peg"); + assertEquals("Peg", xpath.evaluate("//astro:stardb/astro:star[astro:hr=$id]/astro:constellation", doc.getDocumentElement())); } @@ -142,15 +150,15 @@ public class XPathAPITest { return "http://www.astro.com/astro".equals(nsURI) ? "astro" : ""; } - public Iterator getPrefixes(String nsURI) { - ArrayList list = new ArrayList(); + public Iterator getPrefixes(String nsURI) { + ArrayList list = new ArrayList<>(); list.add("astro"); return list.iterator(); } } @FunctionalInterface - private interface XPathEvaluator { + public interface XPathEvaluator { Object evaluate(String expression) throws XPathExpressionException; } @@ -165,6 +173,6 @@ public class XPathAPITest { } private InputSource createXMLInputSource() { - return new InputSource(filenameToURL(ASTROCAT)); + return new InputSource(Path.of(ASTROCAT).toUri().toASCIIString()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionController.java b/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionController.java index b2a66629e39..a3cc438f394 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionController.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionController.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,22 +22,18 @@ */ package test.auctionportal; -import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; -import static jaxp.library.JAXPTestUtilities.bomStream; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE; -import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_SOURCE; -import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS; -import static test.auctionportal.HiBidConstants.XML_DIR; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.math.BigInteger; -import java.nio.file.Paths; -import java.util.GregorianCalendar; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.w3c.dom.Attr; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.w3c.dom.TypeInfo; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSSerializer; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.DatatypeFactory; @@ -51,17 +47,28 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.GregorianCalendar; -import org.testng.annotations.Test; -import org.w3c.dom.Attr; -import org.w3c.dom.DOMConfiguration; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.w3c.dom.TypeInfo; -import org.w3c.dom.bootstrap.DOMImplementationRegistry; -import org.w3c.dom.ls.DOMImplementationLS; -import org.w3c.dom.ls.LSSerializer; +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE; +import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_SOURCE; +import static test.auctionportal.HiBidConstants.PORTAL_ACCOUNT_NS; +import static test.auctionportal.HiBidConstants.XML_DIR; /** * This is the user controller class for the Auction portal HiBid.com. @@ -69,14 +76,13 @@ import org.w3c.dom.ls.LSSerializer; /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.auctionportal.AuctionController + * @run junit/othervm test.auctionportal.AuctionController */ public class AuctionController { /** * Check for DOMErrorHandler handling DOMError. Before fix of bug 4890927 * DOMConfiguration.setParameter("well-formed",true) throws an exception. * - * @throws Exception If any errors occur. */ @Test public void testCreateNewItem2Sell() throws Exception { @@ -99,7 +105,6 @@ public class AuctionController { * Check for DOMErrorHandler handling DOMError. Before fix of bug 4896132 * test throws DOM Level 1 node error. * - * @throws Exception If any errors occur. */ @Test public void testCreateNewItem2SellRetry() throws Exception { @@ -131,7 +136,6 @@ public class AuctionController { * Check if setting the attribute to be of type ID works. This will affect * the Attr.isID method according to the spec. * - * @throws Exception If any errors occur. */ @Test public void testCreateID() throws Exception { @@ -152,7 +156,6 @@ public class AuctionController { /** * Check the user data on the node. * - * @throws Exception If any errors occur. */ @Test public void testCheckingUserData() throws Exception { @@ -164,45 +167,44 @@ public class AuctionController { DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document document = docBuilder.parse(xmlFile); - Element account = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0); - assertEquals(account.getNodeName(), "acc:Account"); + Element account = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0); + assertEquals("acc:Account", account.getNodeName()); Element firstName = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "FirstName").item(0); - assertEquals(firstName.getNodeName(), "FirstName"); + assertEquals("FirstName", firstName.getNodeName()); Document doc1 = docBuilder.newDocument(); Element someName = doc1.createElement("newelem"); someName.setUserData("mykey", "dd", - (operation, key, data, src, dst) -> { - System.err.println("In UserDataHandler" + key); - System.out.println("In UserDataHandler"); - }); - Element impAccount = (Element)document.importNode(someName, true); - assertEquals(impAccount.getNodeName(), "newelem"); + (operation, key, data, src, dst) -> { + System.err.println("In UserDataHandler" + key); + System.out.println("In UserDataHandler"); + }); + Element impAccount = (Element) document.importNode(someName, true); + assertEquals("newelem", impAccount.getNodeName()); document.normalizeDocument(); String data = (someName.getUserData("mykey")).toString(); - assertEquals(data, "dd"); + assertEquals("dd", data); } /** * Check the UTF-16 XMLEncoding xml file. * - * @throws Exception If any errors occur. - * @see movies.xml + * @see movies-utf16.xml */ - @Test - public void testCheckingEncoding() throws Exception { - // Note since movies.xml is UTF-16 encoding. We're not using stanard XML - // file suffix. - String xmlFile = XML_DIR + "movies.xml.data"; + @ParameterizedTest + @EnumSource(value=ByteOrder.class) + public void testCheckingEncoding(ByteOrder byteOrder) throws Exception { + String xmlFile = XML_DIR + "movies-utf16.xml"; - try (InputStream source = bomStream("UTF-16", xmlFile)) { + // File is stored as UTF-8, but declares itself as UTF-16 for testing. + try (InputStream source = utf16Stream(xmlFile, byteOrder)) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document document = dbf.newDocumentBuilder().parse(source); - assertEquals(document.getXmlEncoding(), "UTF-16"); - assertEquals(document.getXmlStandalone(), true); + assertEquals("UTF-16", document.getXmlEncoding()); + assertTrue(document.getXmlStandalone()); } } @@ -210,7 +212,6 @@ public class AuctionController { * Check validation API features. A schema which is including in Bug 4909119 * used to be testing for the functionalities. * - * @throws Exception If any errors occur. * @see userDetails.xsd */ @Test @@ -244,7 +245,6 @@ public class AuctionController { /** * Check grammar caching with imported schemas. * - * @throws Exception If any errors occur. * @see coins.xsd * @see coinsImportMe.xsd */ @@ -279,7 +279,6 @@ public class AuctionController { * parsing using the SAXParser. SCHEMA_SOURCE attribute is using for this * test. * - * @throws Exception If any errors occur. * @see coins.xsd * @see coinsImportMe.xsd */ @@ -304,7 +303,6 @@ public class AuctionController { /** * Check usage of javax.xml.datatype.Duration class. * - * @throws Exception If any errors occur. */ @Test public void testGetItemDuration() throws Exception { @@ -326,18 +324,17 @@ public class AuctionController { Duration sellDuration = DatatypeFactory.newInstance().newDuration(childList.item(0).getNodeValue()); assertFalse(sellDuration.isShorterThan(duration)); assertFalse(sellDuration.isLongerThan(duration)); - assertEquals(sellDuration.getField(DatatypeConstants.DAYS), BigInteger.valueOf(365)); - assertEquals(sellDuration.normalizeWith(new GregorianCalendar(1999, 2, 22)), duration); + assertEquals(BigInteger.valueOf(365), sellDuration.getField(DatatypeConstants.DAYS)); + assertEquals(duration, sellDuration.normalizeWith(new GregorianCalendar(1999, 2, 22))); Duration myDuration = sellDuration.add(duration); - assertEquals(myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22)), - DatatypeFactory.newInstance().newDuration("P730D")); + assertEquals(DatatypeFactory.newInstance().newDuration("P730D"), + myDuration.normalizeWith(new GregorianCalendar(2003, 2, 22))); } /** * Check usage of TypeInfo interface introduced in DOM L3. * - * @throws Exception If any errors occur. */ @Test public void testGetTypeInfo() throws Exception { @@ -354,12 +351,36 @@ public class AuctionController { Document document = docBuilder.parse(xmlFile); Element userId = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "UserID").item(0); TypeInfo typeInfo = userId.getSchemaTypeInfo(); - assertTrue(typeInfo.getTypeName().equals("nonNegativeInteger")); - assertTrue(typeInfo.getTypeNamespace().equals(W3C_XML_SCHEMA_NS_URI)); + assertEquals("nonNegativeInteger", typeInfo.getTypeName()); + assertEquals(W3C_XML_SCHEMA_NS_URI, typeInfo.getTypeNamespace()); Element role = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Role").item(0); TypeInfo roletypeInfo = role.getSchemaTypeInfo(); - assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell")); - assertTrue(roletypeInfo.getTypeNamespace().equals(PORTAL_ACCOUNT_NS)); + assertEquals("BuyOrSell", roletypeInfo.getTypeName()); + assertEquals(PORTAL_ACCOUNT_NS, roletypeInfo.getTypeNamespace()); + } + + /** Convert file contents to a given character set with BOM marker. */ + public static InputStream utf16Stream(String file, ByteOrder byteOrder) + throws IOException { + Charset charset; + byte[] head; + switch (byteOrder) { + case BIG_ENDIAN: + charset = StandardCharsets.UTF_16BE; + head = new byte[] { (byte) 0xFE, (byte) 0xFF }; + break; + case LITTLE_ENDIAN: + charset = StandardCharsets.UTF_16LE; + head = new byte[] { (byte) 0xFF, (byte) 0xFE }; + break; + default: + throw new AssertionError("Unsupported byte order: " + byteOrder); + } + byte[] content = Files.readString(Paths.get(file)).getBytes(charset); + ByteBuffer bb = ByteBuffer.allocate(head.length + content.length); + bb.put(head); + bb.put(content); + return new ByteArrayInputStream(bb.array()); } } diff --git a/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionItemRepository.java b/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionItemRepository.java index 854cb7d1ad4..dca34a73d82 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionItemRepository.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/AuctionItemRepository.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,14 +22,31 @@ */ package test.auctionportal; -import static jaxp.library.JAXPTestUtilities.setSystemProperty; +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Paths; import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static jaxp.library.JAXPTestUtilities.compareDocumentWithGold; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static test.auctionportal.HiBidConstants.GOLDEN_DIR; import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_LANGUAGE; import static test.auctionportal.HiBidConstants.JAXP_SCHEMA_SOURCE; @@ -37,30 +54,13 @@ import static test.auctionportal.HiBidConstants.SP_ENTITY_EXPANSION_LIMIT; import static test.auctionportal.HiBidConstants.SP_MAX_OCCUR_LIMIT; import static test.auctionportal.HiBidConstants.XML_DIR; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.testng.annotations.Test; -import org.w3c.dom.Document; -import org.xml.sax.SAXParseException; - /** * This is a test class for the Auction portal HiBid.com. */ /* * @test * @library /javax/xml/jaxp/libs - * @run testng/othervm test.auctionportal.AuctionItemRepository + * @run junit/othervm test.auctionportal.AuctionItemRepository */ public class AuctionItemRepository { /** @@ -79,7 +79,6 @@ public class AuctionItemRepository { * not. Previous system property was changed to jdk.xml.entityExpansionLimit * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html. * - * @throws Exception If any errors occur. */ @Test public void testEntityExpansionSAXPos() throws Exception { @@ -88,7 +87,7 @@ public class AuctionItemRepository { // implementation limits. factory.setFeature(FEATURE_SECURE_PROCESSING, true); // Set entityExpansionLimit as 2 should expect fatalError - setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000)); + System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(128000)); SAXParser parser = factory.newSAXParser(); MyErrorHandler fatalHandler = new MyErrorHandler(); @@ -101,27 +100,25 @@ public class AuctionItemRepository { * not. Previous system property was changed to jdk.xml.entityExpansionLimit * see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html. * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXParseException.class) + @Test public void testEntityExpansionSAXNeg() throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); // Secure processing will limit XML processing to conform to // implementation limits. factory.setFeature(FEATURE_SECURE_PROCESSING, true); // Set entityExpansionLimit as 2 should expect SAXParseException. - setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); + System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); SAXParser parser = factory.newSAXParser(); MyErrorHandler fatalHandler = new MyErrorHandler(); - parser.parse(new File(ENTITY_XML), fatalHandler); + assertThrows(SAXParseException.class, () -> parser.parse(new File(ENTITY_XML), fatalHandler)); } /** * Testing set MaxOccursLimit to 10000 in the secure processing enabled for * SAXParserFactory. * - * @throws Exception If any errors occur. */ @Test public void testMaxOccurLimitPos() throws Exception { @@ -130,7 +127,7 @@ public class AuctionItemRepository { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setFeature(FEATURE_SECURE_PROCESSING, true); - setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000)); + System.setProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000)); SAXParser parser = factory.newSAXParser(); parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file)); @@ -145,13 +142,12 @@ public class AuctionItemRepository { * Use a DocumentBuilder to create a DOM object and see if Secure Processing * feature affects the entity expansion. * - * @throws Exception If any errors occur. */ @Test public void testEntityExpansionDOMPos() throws Exception { DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setFeature(FEATURE_SECURE_PROCESSING, true); - setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000)); + System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(10000)); DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); MyErrorHandler eh = new MyErrorHandler(); dBuilder.setErrorHandler(eh); @@ -164,27 +160,25 @@ public class AuctionItemRepository { * Processing feature and entityExpansionLimit value affects output. * Negative test that when entityExpansionLimit is too small. * - * @throws Exception If any errors occur. */ - @Test(expectedExceptions = SAXParseException.class) + @Test public void testEntityExpansionDOMNeg() throws Exception { DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setFeature(FEATURE_SECURE_PROCESSING, true); - setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); + System.setProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2)); DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); MyErrorHandler eh = new MyErrorHandler(); dBuilder.setErrorHandler(eh); - dBuilder.parse(ENTITY_XML); + assertThrows(SAXParseException.class, () -> dBuilder.parse(ENTITY_XML)); } /** * Test xi:include with a SAXParserFactory. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeSAXPos() throws Exception { - String resultFile = USER_DIR + "doc_xinclude.out"; + String resultFile = "doc_xinclude.out"; String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml"; String xmlFile = XML_DIR + "doc_xinclude.xml"; @@ -203,11 +197,10 @@ public class AuctionItemRepository { * Test the simple case of including a document using xi:include using a * DocumentBuilder. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeDOMPos() throws Exception { - String resultFile = USER_DIR + "doc_xincludeDOM.out"; + String resultFile = "doc_xincludeDOM.out"; String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml"; String xmlFile = XML_DIR + "doc_xinclude.xml"; try (FileOutputStream fos = new FileOutputStream(resultFile)) { @@ -226,11 +219,10 @@ public class AuctionItemRepository { * Test the simple case of including a document using xi:include within a * xi:fallback using a DocumentBuilder. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeFallbackDOMPos() throws Exception { - String resultFile = USER_DIR + "doc_fallbackDOM.out"; + String resultFile = "doc_fallbackDOM.out"; String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml"; String xmlFile = XML_DIR + "doc_fallback.xml"; try (FileOutputStream fos = new FileOutputStream(resultFile)) { @@ -250,11 +242,10 @@ public class AuctionItemRepository { * Test for xi:fallback where the fall back text is parsed as text. This * test uses a nested xi:include for the fallback test. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeFallbackTextPos() throws Exception { - String resultFile = USER_DIR + "doc_fallback_text.out"; + String resultFile = "doc_fallback_text.out"; String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml"; String xmlFile = XML_DIR + "doc_fallback_text.xml"; try (FileOutputStream fos = new FileOutputStream(resultFile)) { @@ -273,11 +264,10 @@ public class AuctionItemRepository { /** * Test the XPointer element() framework with XInclude. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXpointerElementPos() throws Exception { - String resultFile = USER_DIR + "doc_xpointer_element.out"; + String resultFile = "doc_xpointer_element.out"; String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml"; String xmlFile = XML_DIR + "doc_xpointer_element.xml"; try (FileOutputStream fos = new FileOutputStream(resultFile)) { @@ -297,11 +287,10 @@ public class AuctionItemRepository { /** * Test the XPointer framework with a SAX object. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXPointerPos() throws Exception { - String resultFile = USER_DIR + "doc_xpointer.out"; + String resultFile = "doc_xpointer.out"; String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml"; String xmlFile = XML_DIR + "doc_xpointer.xml"; @@ -320,11 +309,10 @@ public class AuctionItemRepository { * Test if xi:include may reference the doc containing the include if the * parse type is text. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeLoopPos() throws Exception { - String resultFile = USER_DIR + "doc_xinc_loops.out"; + String resultFile = "doc_xinc_loops.out"; String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml"; String xmlFile = XML_DIR + "doc_xinc_loops.xml"; @@ -347,11 +335,10 @@ public class AuctionItemRepository { * Test if two non nested xi:include elements can include the same document * with an xi:include statement. * - * @throws Exception If any errors occur. */ - @Test(groups = {"readWriteLocalFiles"}) + @Test public void testXIncludeNestedPos() throws Exception { - String resultFile = USER_DIR + "schedule.out"; + String resultFile = "schedule.out"; String goldFile = GOLDEN_DIR + "scheduleGold.xml"; String xmlFile = XML_DIR + "schedule.xml"; @@ -367,4 +354,20 @@ public class AuctionItemRepository { } assertTrue(compareDocumentWithGold(goldFile, resultFile)); } + + public static boolean compareDocumentWithGold(String goldfile, String resultFile) + throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setCoalescing(true); + factory.setIgnoringElementContentWhitespace(true); + factory.setIgnoringComments(true); + DocumentBuilder db = factory.newDocumentBuilder(); + + Document goldD = db.parse(Paths.get(goldfile).toFile()); + goldD.normalizeDocument(); + Document resultD = db.parse(Paths.get(resultFile).toFile()); + resultD.normalizeDocument(); + return goldD.isEqualNode(resultD); + } } diff --git a/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/content/movies.xml.data b/test/jaxp/javax/xml/jaxp/functional/test/auctionportal/content/movies-utf16.xml similarity index 100% rename from test/jaxp/javax/xml/jaxp/functional/test/auctionportal/content/movies.xml.data rename to test/jaxp/javax/xml/jaxp/functional/test/auctionportal/content/movies-utf16.xml diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4511326.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4511326.java index a8bac488b8e..a9bb95e20a8 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4511326.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4511326.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,19 +23,18 @@ package test.gaptest; -import java.io.StringReader; +import org.junit.jupiter.api.Test; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; - -import org.testng.annotations.Test; +import java.io.StringReader; /* * @test * @bug 4511326 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4511326 + * @run junit/othervm test.gaptest.Bug4511326 * @summary In forwards-compatible mode the attribute isn't ignored */ public class Bug4511326 { diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4512806.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4512806.java index d27186bcf6d..2e7b05eaaac 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4512806.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4512806.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,24 +23,23 @@ package test.gaptest; -import static javax.xml.transform.OutputKeys.ENCODING; -import static javax.xml.transform.OutputKeys.INDENT; -import static org.testng.Assert.assertEquals; - -import java.io.StringReader; +import org.junit.jupiter.api.Test; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; +import java.io.StringReader; -import org.testng.annotations.Test; +import static javax.xml.transform.OutputKeys.ENCODING; +import static javax.xml.transform.OutputKeys.INDENT; +import static org.junit.jupiter.api.Assertions.assertEquals; /* * @test * @bug 4512806 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4512806 + * @run junit/othervm test.gaptest.Bug4512806 * @summary test transformer.setOutputProperties(null) */ public class Bug4512806 { @@ -57,13 +56,13 @@ public class Bug4512806 { transformer.setOutputProperty(INDENT, "no"); transformer.setOutputProperty(ENCODING, "UTF-16"); - assertEquals(printPropertyValue(INDENT), "indent=no"); - assertEquals(printPropertyValue(ENCODING), "encoding=UTF-16"); + assertEquals("indent=no", printPropertyValue(INDENT)); + assertEquals("encoding=UTF-16", printPropertyValue(ENCODING)); transformer.setOutputProperties(null); - assertEquals(printPropertyValue(INDENT), "indent=yes"); - assertEquals(printPropertyValue(ENCODING), "encoding=UTF-8"); + assertEquals("indent=yes", printPropertyValue(INDENT)); + assertEquals("encoding=UTF-8", printPropertyValue(ENCODING)); } diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515047.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515047.java index 5fc68a28a6b..8466c9cff79 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515047.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515047.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,6 +23,8 @@ package test.gaptest; +import org.junit.jupiter.api.Test; + import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; @@ -31,13 +33,11 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.testng.annotations.Test; - /* * @test * @bug 4515047 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4515047 + * @run junit/othervm test.gaptest.Bug4515047 * @summary test transform an empty dom source */ public class Bug4515047 { diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515660.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515660.java index 014765f89d8..303db79f68b 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515660.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4515660.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -23,14 +23,15 @@ package test.gaptest; -import static jaxp.library.JAXPTestUtilities.setSystemProperty; -import static jaxp.library.JAXPTestUtilities.clearSystemProperty; - -import static org.testng.Assert.assertTrue; - -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.XMLFilterImpl; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; @@ -41,32 +42,31 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.XMLFilterImpl; +import static org.junit.jupiter.api.Assertions.assertTrue; /* * @test * @bug 4515660 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4515660 + * @run junit/othervm test.gaptest.Bug4515660 * @summary verify property org.xml.sax.driver is used by SAXTransformerFactory */ -@Test(singleThreaded = true) +@Execution(ExecutionMode.SAME_THREAD) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class Bug4515660 { - @BeforeClass + @BeforeAll public void setSaxDrier() { - setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName()); + System.setProperty("org.xml.sax.driver", ReaderStub.class.getName()); } - @AfterClass + @AfterAll public void clearSaxDrier() { - clearSystemProperty("org.xml.sax.driver"); + System.clearProperty("org.xml.sax.driver"); } @Test @@ -88,9 +88,13 @@ public class Bug4515660 { @Test public void testSAXTransformerFactory() throws TransformerConfigurationException { - final String xsl = "\n" + "\n" - + " Hello World!\n" + "\n"; - + final String xsl = + """ + + + Hello World! + + """; ReaderStub.used = false; TransformerFactory transFactory = TransformerFactory.newInstance(); diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4848653.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4848653.java index b8fe9e06814..edf2ef2d1c1 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4848653.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4848653.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,28 +22,27 @@ */ package test.gaptest; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static test.gaptest.GapTestConst.XML_DIR; - -import java.io.IOException; - -import javax.xml.XMLConstants; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.testng.annotations.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; +import javax.xml.XMLConstants; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.IOException; +import java.nio.file.Path; + +import static test.gaptest.GapTestConst.XML_DIR; + /* * @test * @bug 4848653 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4848653 + * @run junit/othervm test.gaptest.Bug4848653 * @summary Verify JAXP schemaLanguage property is ignored if setValidating(false) */ public class Bug4848653 { @@ -56,7 +55,8 @@ public class Bug4848653 { parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); String filename = XML_DIR + "Bug4848653.xml"; - InputSource is = new InputSource(filenameToURL(filename)); + String uri = Path.of(filename).toUri().toASCIIString(); + InputSource is = new InputSource(uri); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(new MyErrorHandler()); xmlReader.parse(is); diff --git a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4858685.java b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4858685.java index 6ebcae5940d..7667d6ad140 100644 --- a/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4858685.java +++ b/test/jaxp/javax/xml/jaxp/functional/test/gaptest/Bug4858685.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2026, 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 @@ -22,43 +22,43 @@ */ package test.gaptest; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static org.testng.Assert.assertEquals; -import static test.gaptest.GapTestConst.GOLDEN_DIR; -import static test.gaptest.GapTestConst.XML_DIR; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; +import org.junit.jupiter.api.Test; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; -import org.testng.annotations.Test; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static test.gaptest.GapTestConst.GOLDEN_DIR; +import static test.gaptest.GapTestConst.XML_DIR; /* * @test * @bug 4858685 4894410 * @library /javax/xml/jaxp/libs - * @run testng/othervm test.gaptest.Bug4858685 + * @run junit/othervm test.gaptest.Bug4858685 * @summary test transforming text node */ public class Bug4858685 { @Test public void test() throws TransformerException, IOException { - String uri = XML_DIR + "certificate.xml"; + String filename = XML_DIR + "certificate.xml"; TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // use URI as a StreamSource - StreamSource streamSource = new StreamSource(filenameToURL(uri)); + String uri = Path.of(filename).toUri().toASCIIString(); + StreamSource streamSource = new StreamSource(uri); DOMResult domResult = new DOMResult(); @@ -66,12 +66,8 @@ public class Bug4858685 { transformer.transform(streamSource, domResult); // dump DOM in a human readable form - String gotString = DOMDump.dumpDom(domResult.getNode()); - String goldenString = new String(Files.readAllBytes(Paths.get(GOLDEN_DIR + "Bug4858685.txt"))); - - assertEquals(gotString, goldenString); - + assertEquals(goldenString, DOMDump.dumpDom(domResult.getNode())); } /** diff --git a/test/jaxp/javax/xml/jaxp/libs/org/w3c/dom/ptests/DOMTestUtil.java b/test/jaxp/javax/xml/jaxp/libs/org/w3c/dom/ptests/DOMTestUtil.java index 6b32aa36e74..c4239606135 100644 --- a/test/jaxp/javax/xml/jaxp/libs/org/w3c/dom/ptests/DOMTestUtil.java +++ b/test/jaxp/javax/xml/jaxp/libs/org/w3c/dom/ptests/DOMTestUtil.java @@ -22,11 +22,9 @@ */ package org.w3c.dom.ptests; -import static jaxp.library.JAXPTestUtilities.FILE_SEP; -import static jaxp.library.JAXPTestUtilities.getPathByClassName; - import java.io.File; import java.io.IOException; +import java.nio.file.Path; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -39,15 +37,22 @@ import org.xml.sax.SAXException; * This class defines the path constant and common method */ public class DOMTestUtil { + private static final Path TEST_SRC = Path.of(System.getProperty("test.src")).toAbsolutePath(); + + private static String forwardSlashDir(Path p) { + // Convention in these tests is to include trailing '/' in directory strings. + return p.toString().replace(File.separatorChar, '/') + '/'; + } + /* * XML source file directory. */ - public static final String XML_DIR = getPathByClassName(DOMTestUtil.class, ".." + FILE_SEP + "xmlfiles"); + public static final String XML_DIR = forwardSlashDir(TEST_SRC.resolveSibling("xmlfiles")); /* * Golden validation files directory. */ - public static final String GOLDEN_DIR = getPathByClassName(DOMTestUtil.class, ".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out"); + public static final String GOLDEN_DIR = XML_DIR + "out/"; /* * Error Message for DOMException being expected. diff --git a/test/jaxp/javax/xml/jaxp/libs/org/xml/sax/ptests/SAXTestConst.java b/test/jaxp/javax/xml/jaxp/libs/org/xml/sax/ptests/SAXTestConst.java index 6798d7879dc..ea35a484cdf 100644 --- a/test/jaxp/javax/xml/jaxp/libs/org/xml/sax/ptests/SAXTestConst.java +++ b/test/jaxp/javax/xml/jaxp/libs/org/xml/sax/ptests/SAXTestConst.java @@ -22,8 +22,8 @@ */ package org.xml.sax.ptests; -import static jaxp.library.JAXPTestUtilities.FILE_SEP; -import static jaxp.library.JAXPTestUtilities.getPathByClassName; +import java.io.File; +import java.nio.file.Path; /** * This is the Base test class provide basic support for JAXP SAX functional @@ -31,16 +31,20 @@ import static jaxp.library.JAXPTestUtilities.getPathByClassName; * has their own TestBase class. */ public class SAXTestConst { + private static final Path TEST_SRC = Path.of(System.getProperty("test.src")).toAbsolutePath(); + + private static String forwardSlashDir(Path p) { + // Convention in these tests is to include trailing '/' in directory strings. + return p.toString().replace(File.separatorChar, '/') + '/'; + } + /** * XML source file directory. */ - public static final String XML_DIR = getPathByClassName(SAXTestConst.class, - ".." + FILE_SEP + "xmlfiles"); - + public static final String XML_DIR = forwardSlashDir(TEST_SRC.resolveSibling("xmlfiles")); /** * Golden validation files directory. */ - public static final String GOLDEN_DIR = getPathByClassName(SAXTestConst.class, - ".." + FILE_SEP + "xmlfiles" + FILE_SEP + "out"); + public static final String GOLDEN_DIR = XML_DIR + "out/"; } diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/AstroConstants.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/AstroConstants.java index b3f2cb6e5c6..9645832c896 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/AstroConstants.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/AstroConstants.java @@ -23,10 +23,14 @@ package test.astro; +import java.nio.file.Path; + import static java.io.File.separator; -import static jaxp.library.JAXPTestUtilities.getPathByClassName; public class AstroConstants { + private static final Path XML_FILES = + Path.of(System.getProperty("test.src")).resolve("xmlfiles").toAbsolutePath(); + // Query parameters : public static final double RA_MIN = 0.0; // hours @@ -36,7 +40,7 @@ public class AstroConstants { // Stylesheet source paths: - public static final String XSLPATH = getPathByClassName(AstroConstants.class, "xmlfiles" + separator + "xsl"); + public static final String XSLPATH = XML_FILES.resolve("xsl").toString() + separator; public static final String RAXSL = XSLPATH + "ra.xsl"; public static final String DECXSL = XSLPATH + "dec.xsl"; public static final String RADECXSL = XSLPATH + "radec.xsl"; @@ -51,10 +55,9 @@ public class AstroConstants { // Catalog references - public static final String ASTROCAT = getPathByClassName(AstroConstants.class, "xmlfiles") + "catalog.xml"; + public static final String ASTROCAT = XML_FILES.resolve("catalog.xml").toString(); - - public static final String GOLDEN_DIR = getPathByClassName(AstroConstants.class, "xmlfiles" + separator + "gold"); + public static final String GOLDEN_DIR = XML_FILES.resolve("gold").toString() + separator; public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; } diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/DOMFilterFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/DOMFilterFactoryImpl.java index e232fb61fcc..32c34673846 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/DOMFilterFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/DOMFilterFactoryImpl.java @@ -22,12 +22,12 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; import static test.astro.AstroConstants.DECXSL; import static test.astro.AstroConstants.RAXSL; import static test.astro.AstroConstants.STYPEXSL; import java.io.IOException; +import java.nio.file.Path; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -75,6 +75,7 @@ public class DOMFilterFactoryImpl extends SourceFilterFactory { private Document getStylesheetDOM(String xslfilename) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); - return dbf.newDocumentBuilder().parse(filenameToURL(xslfilename)); + String xslUri = Path.of(xslfilename).toUri().toASCIIString(); + return dbf.newDocumentBuilder().parse(xslUri); } } diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/DOML3InputSourceFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/DOML3InputSourceFactoryImpl.java index 2f4170b21ce..6fde37c79ab 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/DOML3InputSourceFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/DOML3InputSourceFactoryImpl.java @@ -22,22 +22,6 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static jaxp.library.JAXPTestUtilities.USER_DIR; -import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; -import static org.w3c.dom.traversal.NodeFilter.SHOW_ELEMENT; - -import java.io.ByteArrayInputStream; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.file.Files; -import java.nio.file.Paths; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - import org.w3c.dom.DOMConfiguration; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -50,6 +34,20 @@ import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSSerializerFilter; import org.xml.sax.InputSource; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayInputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; +import static org.w3c.dom.traversal.NodeFilter.SHOW_ELEMENT; + /* * A specialized implementation of an Input Source factory that utilizes * DOM Level 3 implementations to build a Document (DOM) from the @@ -72,7 +70,7 @@ public class DOML3InputSourceFactoryImpl implements InputSourceFactory { Document doc = null; LSInput src = impl.createLSInput(); // register the input file with the input source... - String systemId = filenameToURL(filename); + String systemId = Path.of(filename).toUri().toASCIIString(); src.setSystemId(systemId); try (Reader reader = new FileReader(filename)) { src.setCharacterStream(reader); @@ -82,7 +80,7 @@ public class DOML3InputSourceFactoryImpl implements InputSourceFactory { // Use DOM L3 LSSerializer (previously called a DOMWriter) // to serialize the xml doc DOM to a file stream. - String tmpCatalog = Files.createTempFile(Paths.get(USER_DIR), "catalog.xml", null).toString(); + String tmpCatalog = Files.createTempFile(Paths.get("."), "catalog.xml", null).toString(); LSSerializer domserializer = impl.createLSSerializer(); domserializer.setFilter(new MyDOMWriterFilter()); diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/InputSourceFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/InputSourceFactoryImpl.java index a2c5b8319c8..d5ccdbf70e6 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/InputSourceFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/InputSourceFactoryImpl.java @@ -22,10 +22,10 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; - import org.xml.sax.InputSource; +import java.nio.file.Path; + /* * Default implementation of a input source factory. This is the most * straight forward way to create a sax input source and set it's @@ -38,7 +38,8 @@ public class InputSourceFactoryImpl implements InputSourceFactory { public InputSource newInputSource(String filename) { InputSource catSrc = new InputSource(filename); - catSrc.setSystemId(filenameToURL(filename)); + String uri = Path.of(filename).toUri().toASCIIString(); + catSrc.setSystemId(uri); return catSrc; } } diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/SAXFilterFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/SAXFilterFactoryImpl.java index 4ef5d3cc259..403c0a70460 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/SAXFilterFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/SAXFilterFactoryImpl.java @@ -22,22 +22,22 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; -import static test.astro.AstroConstants.DECENTXSL; -import static test.astro.AstroConstants.DECXSL; -import static test.astro.AstroConstants.RAENTXSL; -import static test.astro.AstroConstants.STYPEXSL; -import static test.astro.AstroConstants.TOPTEMPLXSL; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.Source; import javax.xml.transform.sax.SAXSource; +import java.nio.file.Path; -import org.xml.sax.EntityResolver; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; +import static test.astro.AstroConstants.DECENTXSL; +import static test.astro.AstroConstants.DECXSL; +import static test.astro.AstroConstants.RAENTXSL; +import static test.astro.AstroConstants.STYPEXSL; +import static test.astro.AstroConstants.TOPTEMPLXSL; /* * Implementation of the filter factory interface that utilizes SAX @@ -51,7 +51,7 @@ import org.xml.sax.XMLReader; * */ public class SAXFilterFactoryImpl extends SourceFilterFactory { - private EntityResolver entityResolver; + private final EntityResolver entityResolver; public SAXFilterFactoryImpl() { super(); @@ -60,7 +60,8 @@ public class SAXFilterFactoryImpl extends SourceFilterFactory { @Override protected Source getSource(String xslFileName) throws SAXException, ParserConfigurationException { - SAXSource saxsource = new SAXSource(new InputSource(filenameToURL(xslFileName))); + String xslUri = Path.of(xslFileName).toUri().toASCIIString(); + SAXSource saxsource = new SAXSource(new InputSource(xslUri)); saxsource.setXMLReader(getXMLReader()); return saxsource; } @@ -97,7 +98,8 @@ public class SAXFilterFactoryImpl extends SourceFilterFactory { public InputSource resolveEntity(String publicid, String sysId) { if (sysId.equals("http://astro.com/stylesheets/toptemplate")) { InputSource retval = new InputSource(TOPTEMPLXSL); - retval.setSystemId(filenameToURL(TOPTEMPLXSL)); + String xslUri = Path.of(TOPTEMPLXSL).toUri().toASCIIString(); + retval.setSystemId(xslUri); return retval; } else { return null; // use default behavior diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/StreamFilterFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/StreamFilterFactoryImpl.java index 0978161312e..38e864f6897 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/StreamFilterFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/StreamFilterFactoryImpl.java @@ -22,7 +22,6 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; import static test.astro.AstroConstants.DECXSL; import static test.astro.AstroConstants.RADECXSL; import static test.astro.AstroConstants.RAXSL; @@ -30,11 +29,13 @@ import static test.astro.AstroConstants.STYPEXSL; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; +import java.nio.file.Path; public class StreamFilterFactoryImpl extends SourceFilterFactory { @Override protected Source getSource(String xslFileName) { - return new StreamSource(filenameToURL(xslFileName)); + String xslUri = Path.of(xslFileName).toUri().toASCIIString(); + return new StreamSource(xslUri); } @Override diff --git a/test/jaxp/javax/xml/jaxp/libs/test/astro/TemplatesFilterFactoryImpl.java b/test/jaxp/javax/xml/jaxp/libs/test/astro/TemplatesFilterFactoryImpl.java index 09a71b1a534..6047d0c5ae8 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/astro/TemplatesFilterFactoryImpl.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/astro/TemplatesFilterFactoryImpl.java @@ -22,13 +22,13 @@ */ package test.astro; -import static jaxp.library.JAXPTestUtilities.filenameToURL; import static test.astro.AstroConstants.DECXSL; import static test.astro.AstroConstants.RAURIXSL; import static test.astro.AstroConstants.STYPEXSL; import static test.astro.AstroConstants.TOPTEMPLINCXSL; import java.io.IOException; +import java.nio.file.Path; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; @@ -94,7 +94,8 @@ public class TemplatesFilterFactoryImpl extends AbstractFilterFactory { // create the stylesheet input source InputSource xslSrc = new InputSource(xslFileName); - xslSrc.setSystemId(filenameToURL(xslFileName)); + String xslUri = Path.of(xslFileName).toUri().toASCIIString(); + xslSrc.setSystemId(xslUri); // hook up the templates handler as the xsl content handler xmlreader.setContentHandler(templatesHandler); // call parse on the xsl input source @@ -113,7 +114,8 @@ public class TemplatesFilterFactoryImpl extends AbstractFilterFactory { public Source resolve(String href, String base) throws TransformerException { if ("http://astro.com/stylesheets/topleveltemplate".equals(href)) { StreamSource ss = new StreamSource(TOPTEMPLINCXSL); - ss.setSystemId(filenameToURL(TOPTEMPLINCXSL)); + String xslUri = Path.of(TOPTEMPLINCXSL).toUri().toASCIIString(); + ss.setSystemId(xslUri); return ss; } else { return null; diff --git a/test/jaxp/javax/xml/jaxp/libs/test/auctionportal/HiBidConstants.java b/test/jaxp/javax/xml/jaxp/libs/test/auctionportal/HiBidConstants.java index 5c812c52b04..7852540a308 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/auctionportal/HiBidConstants.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/auctionportal/HiBidConstants.java @@ -22,21 +22,29 @@ */ package test.auctionportal; -import static jaxp.library.JAXPTestUtilities.getPathByClassName; +import java.io.File; +import java.nio.file.Path; /** * This is the Base test class provide basic support for Auction portal test. */ public class HiBidConstants { + private static final Path TEST_SRC = Path.of(System.getProperty("test.src")).toAbsolutePath(); + + private static String forwardSlashDir(Path p) { + // Convention in these tests is to include trailing '/' in directory strings. + return p.toString().replace(File.separatorChar, '/') + '/'; + } + /** * XML source file directory. */ - public static final String XML_DIR = getPathByClassName(HiBidConstants.class, "content"); + public static final String XML_DIR = forwardSlashDir(TEST_SRC.resolve("content")); /** * Golden validation files directory. */ - public static final String GOLDEN_DIR = getPathByClassName(HiBidConstants.class, "golden"); + public static final String GOLDEN_DIR = forwardSlashDir(TEST_SRC.resolve("golden")); /** * Name space for account operation. diff --git a/test/jaxp/javax/xml/jaxp/libs/test/gaptest/GapTestConst.java b/test/jaxp/javax/xml/jaxp/libs/test/gaptest/GapTestConst.java index 237d0ac6ed2..648a6c2be10 100644 --- a/test/jaxp/javax/xml/jaxp/libs/test/gaptest/GapTestConst.java +++ b/test/jaxp/javax/xml/jaxp/libs/test/gaptest/GapTestConst.java @@ -22,20 +22,27 @@ */ package test.gaptest; -import static jaxp.library.JAXPTestUtilities.FILE_SEP; -import static jaxp.library.JAXPTestUtilities.getPathByClassName; +import java.io.File; +import java.nio.file.Path; /** * This class defines the path constant */ public class GapTestConst { + private static final Path TEST_SRC = Path.of(System.getProperty("test.src")).toAbsolutePath(); + + private static String forwardSlashDir(Path p) { + // Convention in these tests is to include trailing '/' in directory strings. + return p.toString().replace(File.separatorChar, '/') + '/'; + } + /** * XML source file directory. */ - public static final String XML_DIR = getPathByClassName(GapTestConst.class, "xmlfiles"); + public static final String XML_DIR = forwardSlashDir(TEST_SRC.resolve("xmlfiles")); /** * Golden validation files directory. */ - public static final String GOLDEN_DIR = getPathByClassName(GapTestConst.class, "xmlfiles" + FILE_SEP + "out"); + public static final String GOLDEN_DIR = XML_DIR + "out/"; } From abb9c33cedac730753dd3aaebd101532a1aa70a1 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Wed, 18 Mar 2026 01:14:16 +0000 Subject: [PATCH 17/22] 8380125: Formatting issue for few swing classes Reviewed-by: honkar, azvegint --- .../share/classes/javax/swing/AbstractButton.java | 4 ++-- .../javax/swing/ActionPropertyChangeListener.java | 2 +- .../share/classes/javax/swing/AncestorNotifier.java | 2 +- .../share/classes/javax/swing/ArrayTable.java | 6 +++--- .../share/classes/javax/swing/JComboBox.java | 4 ++-- .../share/classes/javax/swing/JList.java | 2 +- .../share/classes/javax/swing/JPopupMenu.java | 6 +++--- .../share/classes/javax/swing/JTextField.java | 4 ++-- .../share/classes/javax/swing/JTree.java | 2 +- .../share/classes/javax/swing/KeyboardManager.java | 2 +- .../share/classes/javax/swing/PopupFactory.java | 2 +- .../share/classes/javax/swing/SwingUtilities.java | 2 +- .../share/classes/javax/swing/UIDefaults.java | 4 ++-- .../javax/swing/plaf/basic/BasicComboPopup.java | 4 ++-- .../classes/javax/swing/plaf/basic/BasicListUI.java | 2 +- .../javax/swing/plaf/basic/BasicMenuBarUI.java | 10 +++++----- .../javax/swing/plaf/basic/BasicPopupMenuUI.java | 2 +- .../javax/swing/plaf/basic/BasicSpinnerUI.java | 2 +- .../javax/swing/plaf/basic/BasicSplitPaneUI.java | 12 ++++++------ .../javax/swing/plaf/basic/BasicTabbedPaneUI.java | 4 ++-- .../javax/swing/plaf/basic/BasicToolBarUI.java | 4 ++-- .../classes/javax/swing/plaf/basic/BasicTreeUI.java | 2 +- .../swing/plaf/nimbus/AbstractRegionPainter.java | 2 +- .../javax/swing/plaf/nimbus/NimbusLookAndFeel.java | 2 +- .../javax/swing/plaf/nimbus/SynthPainterImpl.java | 6 +++--- .../javax/swing/plaf/synth/SynthComboBoxUI.java | 2 +- .../javax/swing/plaf/synth/SynthScrollPaneUI.java | 2 +- .../classes/javax/swing/text/JTextComponent.java | 2 +- .../share/classes/javax/swing/text/TextAction.java | 4 ++-- .../javax/swing/tree/DefaultTreeCellEditor.java | 2 +- .../share/classes/sun/swing/SwingUtilities2.java | 6 +++--- 31 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/AbstractButton.java b/src/java.desktop/share/classes/javax/swing/AbstractButton.java index d4112b5ed04..a1961acce29 100644 --- a/src/java.desktop/share/classes/javax/swing/AbstractButton.java +++ b/src/java.desktop/share/classes/javax/swing/AbstractButton.java @@ -1070,13 +1070,13 @@ public abstract class AbstractButton extends JComponent implements ItemSelectabl Action oldValue = getAction(); if (action==null || !action.equals(a)) { action = a; - if (oldValue!=null) { + if (oldValue != null) { removeActionListener(oldValue); oldValue.removePropertyChangeListener(actionPropertyChangeListener); actionPropertyChangeListener = null; } configurePropertiesFromAction(action); - if (action!=null) { + if (action != null) { // Don't add if it is already a listener if (!isListener(ActionListener.class, action)) { addActionListener(action); diff --git a/src/java.desktop/share/classes/javax/swing/ActionPropertyChangeListener.java b/src/java.desktop/share/classes/javax/swing/ActionPropertyChangeListener.java index db12b6766b8..19b9152a1b5 100644 --- a/src/java.desktop/share/classes/javax/swing/ActionPropertyChangeListener.java +++ b/src/java.desktop/share/classes/javax/swing/ActionPropertyChangeListener.java @@ -111,7 +111,7 @@ abstract class ActionPropertyChangeListener while ((r = (OwnedWeakReference)queue.poll()) != null) { ActionPropertyChangeListener oldPCL = r.getOwner(); Action oldAction = oldPCL.getAction(); - if (oldAction!=null) { + if (oldAction != null) { oldAction.removePropertyChangeListener(oldPCL); } } diff --git a/src/java.desktop/share/classes/javax/swing/AncestorNotifier.java b/src/java.desktop/share/classes/javax/swing/AncestorNotifier.java index f3cfdd9d0ba..a3ed463d2d5 100644 --- a/src/java.desktop/share/classes/javax/swing/AncestorNotifier.java +++ b/src/java.desktop/share/classes/javax/swing/AncestorNotifier.java @@ -213,7 +213,7 @@ class AncestorNotifier implements ComponentListener, PropertyChangeListener, Ser public void propertyChange(PropertyChangeEvent evt) { String s = evt.getPropertyName(); - if (s!=null && (s.equals("parent") || s.equals("ancestor"))) { + if (s != null && (s.equals("parent") || s.equals("ancestor"))) { JComponent component = (JComponent)evt.getSource(); if (evt.getNewValue() != null) { diff --git a/src/java.desktop/share/classes/javax/swing/ArrayTable.java b/src/java.desktop/share/classes/javax/swing/ArrayTable.java index 7c38f9fb577..282bd3454ca 100644 --- a/src/java.desktop/share/classes/javax/swing/ArrayTable.java +++ b/src/java.desktop/share/classes/javax/swing/ArrayTable.java @@ -145,7 +145,7 @@ class ArrayTable implements Cloneable { */ public Object get(Object key) { Object value = null; - if (table !=null) { + if (table != null) { if (isArray()) { Object[] array = (Object[])table; for (int i = 0; i extends JComponent implements Scrollable, Accessible } Rectangle newFirstRect = getCellBounds(newFirst,newFirst); Rectangle firstRect = getCellBounds(first,first); - if ((newFirstRect != null) && (firstRect!=null)) { + if ((newFirstRect != null) && (firstRect != null)) { while ( (newFirstRect.y + visibleRect.height < firstRect.y + firstRect.height) && (newFirstRect.y < firstRect.y) ) { diff --git a/src/java.desktop/share/classes/javax/swing/JPopupMenu.java b/src/java.desktop/share/classes/javax/swing/JPopupMenu.java index 2ecc7cf3b1e..f3d11a29b4a 100644 --- a/src/java.desktop/share/classes/javax/swing/JPopupMenu.java +++ b/src/java.desktop/share/classes/javax/swing/JPopupMenu.java @@ -975,7 +975,7 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { if (newFrame != frame) { // Use the invoker's frame so that events // are propagated properly - if (newFrame!=null) { + if (newFrame != null) { this.frame = newFrame; if(popup != null) { setVisible(false); @@ -1012,7 +1012,7 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { */ JPopupMenu getRootPopupMenu() { JPopupMenu mp = this; - while((mp!=null) && (mp.isPopupMenu()!=true) && + while((mp != null) && (mp.isPopupMenu()!=true) && (mp.getInvoker() != null) && (mp.getInvoker().getParent() instanceof JPopupMenu popupMenu) ) { @@ -1182,7 +1182,7 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { private static Frame getFrame(Component c) { Component w = c; - while(!(w instanceof Frame) && (w!=null)) { + while(!(w instanceof Frame) && (w != null)) { w = w.getParent(); } return (Frame)w; diff --git a/src/java.desktop/share/classes/javax/swing/JTextField.java b/src/java.desktop/share/classes/javax/swing/JTextField.java index 0d66209d8af..b6d8639c994 100644 --- a/src/java.desktop/share/classes/javax/swing/JTextField.java +++ b/src/java.desktop/share/classes/javax/swing/JTextField.java @@ -581,13 +581,13 @@ public class JTextField extends JTextComponent implements SwingConstants { Action oldValue = getAction(); if (action==null || !action.equals(a)) { action = a; - if (oldValue!=null) { + if (oldValue != null) { removeActionListener(oldValue); oldValue.removePropertyChangeListener(actionPropertyChangeListener); actionPropertyChangeListener = null; } configurePropertiesFromAction(action); - if (action!=null) { + if (action != null) { // Don't add if it is already a listener if (!isListener(ActionListener.class, action)) { addActionListener(action); diff --git a/src/java.desktop/share/classes/javax/swing/JTree.java b/src/java.desktop/share/classes/javax/swing/JTree.java index 622c55e2ede..397584b6e4a 100644 --- a/src/java.desktop/share/classes/javax/swing/JTree.java +++ b/src/java.desktop/share/classes/javax/swing/JTree.java @@ -2087,7 +2087,7 @@ public class JTree extends JComponent implements Scrollable, Accessible value = expandedState.get(path); if (value == null || !value) return false; - } while( (path=path.getParentPath())!=null ); + } while( (path=path.getParentPath()) != null ); return true; } diff --git a/src/java.desktop/share/classes/javax/swing/KeyboardManager.java b/src/java.desktop/share/classes/javax/swing/KeyboardManager.java index 42a6403ddb5..8206ab5cea6 100644 --- a/src/java.desktop/share/classes/javax/swing/KeyboardManager.java +++ b/src/java.desktop/share/classes/javax/swing/KeyboardManager.java @@ -330,7 +330,7 @@ class KeyboardManager { return; } Hashtable keyMap = containerMap.get(topContainer); - if (keyMap!=null) { + if (keyMap != null) { Vector v = (Vector)keyMap.get(JMenuBar.class); if (v != null) { v.removeElement(mb); diff --git a/src/java.desktop/share/classes/javax/swing/PopupFactory.java b/src/java.desktop/share/classes/javax/swing/PopupFactory.java index 208177fc55e..7245820c289 100644 --- a/src/java.desktop/share/classes/javax/swing/PopupFactory.java +++ b/src/java.desktop/share/classes/javax/swing/PopupFactory.java @@ -925,7 +925,7 @@ public class PopupFactory { add to that, otherwise add to the window. */ while (!(parent instanceof Window) && - (parent!=null)) { + (parent != null)) { parent = parent.getParent(); } diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index afe1c444c31..ae5faf64f3b 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -506,7 +506,7 @@ public class SwingUtilities implements SwingConstants public static boolean isDescendingFrom(Component a,Component b) { if(a == b) return true; - for(Container p = a.getParent();p!=null;p=p.getParent()) + for(Container p = a.getParent(); p != null; p = p.getParent()) if(p == b) return true; return false; diff --git a/src/java.desktop/share/classes/javax/swing/UIDefaults.java b/src/java.desktop/share/classes/javax/swing/UIDefaults.java index d59a46dba32..67a739360f9 100644 --- a/src/java.desktop/share/classes/javax/swing/UIDefaults.java +++ b/src/java.desktop/share/classes/javax/swing/UIDefaults.java @@ -1169,7 +1169,7 @@ public class UIDefaults extends Hashtable */ private Class[] getClassArray(Object[] args) { Class[] types = null; - if (args!=null) { + if (args != null) { types = new Class[args.length]; for (int i = 0; i< args.length; i++) { /* PENDING(ges): At present only the primitive types @@ -1199,7 +1199,7 @@ public class UIDefaults extends Hashtable private String printArgs(Object[] array) { String s = "{"; - if (array !=null) { + if (array != null) { for (int i = 0 ; i < array.length-1; i++) { s = s.concat(array[i] + ","); } diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboPopup.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboPopup.java index 38798d4c94f..42791772c2d 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboPopup.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboPopup.java @@ -1070,11 +1070,11 @@ public class BasicComboPopup extends JPopupMenu implements ComboPopup { ComponentOrientation o =(ComponentOrientation)e.getNewValue(); JList list = getList(); - if (list!=null && list.getComponentOrientation()!=o) { + if (list != null && list.getComponentOrientation()!=o) { list.setComponentOrientation(o); } - if (scroller!=null && scroller.getComponentOrientation()!=o) { + if (scroller != null && scroller.getComponentOrientation()!=o) { scroller.setComponentOrientation(o); } diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java index 0a4aa03dce9..ab5fdb12c5a 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java @@ -876,7 +876,7 @@ public class BasicListUI extends ListUI } Long l = (Long)UIManager.get("List.timeFactor"); - timeFactor = (l!=null) ? l.longValue() : 1000L; + timeFactor = (l != null) ? l.longValue() : 1000L; updateIsFileList(); } diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuBarUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuBarUI.java index 2079aafd3a2..b7310b56aef 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuBarUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuBarUI.java @@ -125,7 +125,7 @@ public class BasicMenuBarUI extends MenuBarUI { for (int i = 0; i < menuBar.getMenuCount(); i++) { JMenu menu = menuBar.getMenu(i); - if (menu!=null) + if (menu != null) menu.getModel().addChangeListener(changeListener); } menuBar.addContainerListener(containerListener); @@ -167,7 +167,7 @@ public class BasicMenuBarUI extends MenuBarUI { * Uninstalls default properties. */ protected void uninstallDefaults() { - if (menuBar!=null) { + if (menuBar != null) { LookAndFeel.uninstallBorder(menuBar); } } @@ -180,7 +180,7 @@ public class BasicMenuBarUI extends MenuBarUI { for (int i = 0; i < menuBar.getMenuCount(); i++) { JMenu menu = menuBar.getMenu(i); - if (menu !=null) + if (menu != null) menu.getModel().removeChangeListener(changeListener); } @@ -240,7 +240,7 @@ public class BasicMenuBarUI extends MenuBarUI { int i,c; for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) { JMenu menu = menuBar.getMenu(i); - if(menu !=null && menu.isSelected()) { + if(menu != null && menu.isSelected()) { menuBar.getSelectionModel().setSelectedIndex(i); break; } @@ -277,7 +277,7 @@ public class BasicMenuBarUI extends MenuBarUI { MenuElement[] me; MenuElement[] subElements; JMenu menu = menuBar.getMenu(0); - if (menu!=null) { + if (menu != null) { me = new MenuElement[3]; me[0] = (MenuElement) menuBar; me[1] = (MenuElement) menu; diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java index 8df27d3e3cb..b72a2d8a140 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java @@ -908,7 +908,7 @@ public class BasicPopupMenuUI extends PopupMenuUI { } boolean isInPopup(Component src) { - for (Component c=src; c!=null; c=c.getParent()) { + for (Component c=src; c != null; c=c.getParent()) { if (c instanceof Window) { break; } else if (c instanceof JPopupMenu) { diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java index 2b871343697..7080fa3aac1 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java @@ -688,7 +688,7 @@ public class BasicSpinnerUI extends SpinnerUI arrowButton = (JButton)e.getSource(); } } else { - if (arrowButton!=null && !arrowButton.getModel().isPressed() + if (arrowButton != null && !arrowButton.getModel().isPressed() && autoRepeatTimer.isRunning()) { autoRepeatTimer.stop(); spinner = null; diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSplitPaneUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSplitPaneUI.java index 270181f4600..1d8c471e9ec 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSplitPaneUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSplitPaneUI.java @@ -2273,7 +2273,7 @@ public class BasicSplitPaneUI extends SplitPaneUI JSplitPane parentSplitPane = (JSplitPane)SwingUtilities.getAncestorOfClass( JSplitPane.class, splitPane); - if (parentSplitPane!=null) { + if (parentSplitPane != null) { parentSplitPane.requestFocus(); } } @@ -2307,7 +2307,7 @@ public class BasicSplitPaneUI extends SplitPaneUI } while (splitPane.isAncestorOf(focusOn) && !focusFrom.contains(focusOn)); } - if ( focusOn!=null && !splitPane.isAncestorOf(focusOn) ) { + if ( focusOn != null && !splitPane.isAncestorOf(focusOn) ) { focusOn.requestFocus(); } } @@ -2323,7 +2323,7 @@ public class BasicSplitPaneUI extends SplitPaneUI if (focusOn != null) { // don't change the focus if the new focused component belongs // to the same splitpane and the same side - if ( focus!=null && + if ( focus != null && ( (SwingUtilities.isDescendingFrom(focus, left) && SwingUtilities.isDescendingFrom(focusOn, left)) || (SwingUtilities.isDescendingFrom(focus, right) && @@ -2338,15 +2338,15 @@ public class BasicSplitPaneUI extends SplitPaneUI Component left = splitPane.getLeftComponent(); Component right = splitPane.getRightComponent(); Component next; - if (focus!=null && SwingUtilities.isDescendingFrom(focus, left) && - right!=null) { + if (focus != null && SwingUtilities.isDescendingFrom(focus, left) && + right != null) { next = getFirstAvailableComponent(right); if (next != null) { return next; } } JSplitPane parentSplitPane = (JSplitPane)SwingUtilities.getAncestorOfClass(JSplitPane.class, splitPane); - if (parentSplitPane!=null) { + if (parentSplitPane != null) { // focus next side of the parent split pane next = getNextSide(parentSplitPane, focus); } else { diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java index 842e8892c76..6e0d0101b9c 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java @@ -528,7 +528,7 @@ public class BasicTabbedPaneUI extends TabbedPaneUI implements SwingConstants { } tabPane.removeContainerListener(getHandler()); - if (htmlViews!=null) { + if (htmlViews != null) { htmlViews.removeAllElements(); htmlViews = null; } @@ -4090,7 +4090,7 @@ public class BasicTabbedPaneUI extends TabbedPaneUI implements SwingConstants { setHtmlView(v, inserted, index); } } else { // Not HTML - if (htmlViews!=null) { // Add placeholder + if (htmlViews != null) { // Add placeholder setHtmlView(null, inserted, index); } // else nada! } diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolBarUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolBarUI.java index 41ab6137976..1bcd4a9be8d 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolBarUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolBarUI.java @@ -980,7 +980,7 @@ public class BasicToolBarUI extends ToolBarUI implements SwingConstants { toolBar.setOrientation( orientation ); - if (dragWindow !=null) + if (dragWindow != null) dragWindow.setOrientation(orientation); } @@ -1616,7 +1616,7 @@ public class BasicToolBarUI extends ToolBarUI implements SwingConstants this.orientation = o; Dimension size = getSize(); setSize(new Dimension(size.height, size.width)); - if (offset!=null) { + if (offset != null) { if( BasicGraphicsUtils.isLeftToRight(toolBar) ) { setOffset(new Point(offset.y, offset.x)); } else if( o == JToolBar.HORIZONTAL ) { diff --git a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java index 28beaee6929..096fe7cc5f7 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java @@ -943,7 +943,7 @@ public class BasicTreeUI extends TreeUI lineTypeDashed = UIManager.getBoolean("Tree.lineTypeDashed"); Long l = (Long)UIManager.get("Tree.timeFactor"); - timeFactor = (l!=null) ? l.longValue() : 1000L; + timeFactor = (l != null) ? l.longValue() : 1000L; Object showsRootHandles = UIManager.get("Tree.showsRootHandles"); if (showsRootHandles != null) { diff --git a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java index 1bdff19fa2d..cfcc014940a 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java @@ -660,7 +660,7 @@ public abstract class AbstractRegionPainter implements Painter { ImageScalingHelper.paint(g, 0, 0, w, h, img, insets, dstInsets, ImageScalingHelper.PaintType.PAINT9_STRETCH, ImageScalingHelper.PAINT_ALL); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - oldScalingHints!=null?oldScalingHints:RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); + oldScalingHints != null ? oldScalingHints:RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else { // render directly paint0(g, c, w, h, extendedCacheKeys); diff --git a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java index c9340a62368..05fa3bbc9b6 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java @@ -531,7 +531,7 @@ public class NimbusLookAndFeel extends SynthLookAndFeel { public Object createValue(UIDefaults table) { Object obj = null; // check specified state - if (state!=null){ + if (state != null){ obj = uiDefaults.get(prefix+"["+state+"]."+suffix); } // check enabled state diff --git a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/SynthPainterImpl.java b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/SynthPainterImpl.java index ca19a74b6ac..1842073588b 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/nimbus/SynthPainterImpl.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/nimbus/SynthPainterImpl.java @@ -63,13 +63,13 @@ class SynthPainterImpl extends SynthPainter { if (p != null) { if (g instanceof Graphics2D){ Graphics2D gfx = (Graphics2D)g; - if (transform!=null){ + if (transform != null) { gfx.transform(transform); } gfx.translate(x, y); p.paint(gfx, ctx.getComponent(), w, h); gfx.translate(-x, -y); - if (transform!=null){ + if (transform != null){ try { gfx.transform(transform.createInverse()); } catch (NoninvertibleTransformException e) { @@ -85,7 +85,7 @@ class SynthPainterImpl extends SynthPainter { BufferedImage img = new BufferedImage(w,h, BufferedImage.TYPE_INT_ARGB); Graphics2D gfx = img.createGraphics(); - if (transform!=null){ + if (transform != null){ gfx.transform(transform); } p.paint(gfx, ctx.getComponent(), w, h); diff --git a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java index 77ca2a1fc05..ad10e70a837 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthComboBoxUI.java @@ -814,7 +814,7 @@ public class SynthComboBoxUI extends BasicComboBoxUI implements public void propertyChange(PropertyChangeEvent evt) { ComboBoxEditor newEditor = comboBox.getEditor(); if (editor != newEditor){ - if (editorComponent!=null){ + if (editorComponent != null) { editorComponent.removeFocusListener(this); } editor = newEditor; diff --git a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthScrollPaneUI.java b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthScrollPaneUI.java index 96541d51f67..0c3a17fbdbb 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthScrollPaneUI.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthScrollPaneUI.java @@ -225,7 +225,7 @@ public class SynthScrollPaneUI extends BasicScrollPaneUI private int getComponentState(JComponent c) { int baseState = SynthLookAndFeel.getComponentState(c); - if (viewportViewFocusHandler!=null && viewportViewHasFocus){ + if (viewportViewFocusHandler != null && viewportViewHasFocus) { baseState = baseState | FOCUSED; } return baseState; diff --git a/src/java.desktop/share/classes/javax/swing/text/JTextComponent.java b/src/java.desktop/share/classes/javax/swing/text/JTextComponent.java index 59cee1e12ee..f81ba9d66c2 100644 --- a/src/java.desktop/share/classes/javax/swing/text/JTextComponent.java +++ b/src/java.desktop/share/classes/javax/swing/text/JTextComponent.java @@ -1181,7 +1181,7 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A Hashtable h = new Hashtable(); for (Action a : actions) { String value = (String)a.getValue(Action.NAME); - h.put((value!=null ? value:""), a); + h.put((value != null ? value : ""), a); } for (KeyBinding binding : bindings) { Action a = h.get(binding.actionName); diff --git a/src/java.desktop/share/classes/javax/swing/text/TextAction.java b/src/java.desktop/share/classes/javax/swing/text/TextAction.java index 5c11c994ab2..d05f7e24a6b 100644 --- a/src/java.desktop/share/classes/javax/swing/text/TextAction.java +++ b/src/java.desktop/share/classes/javax/swing/text/TextAction.java @@ -107,11 +107,11 @@ public abstract class TextAction extends AbstractAction { Hashtable h = new Hashtable(); for (Action a : list1) { String value = (String)a.getValue(Action.NAME); - h.put((value!=null ? value:""), a); + h.put((value != null ? value : ""), a); } for (Action a : list2) { String value = (String)a.getValue(Action.NAME); - h.put((value!=null ? value:""), a); + h.put((value != null ? value : ""), a); } Action[] actions = new Action[h.size()]; int index = 0; diff --git a/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellEditor.java b/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellEditor.java index 4662c95c01f..3f3721dd30a 100644 --- a/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellEditor.java +++ b/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellEditor.java @@ -259,7 +259,7 @@ public class DefaultTreeCellEditor implements ActionListener, TreeCellEditor, ((MouseEvent)event).getY()); editable = (lastPath != null && path != null && lastPath.equals(path)); - if (path!=null) { + if (path != null) { lastRow = tree.getRowForPath(path); Object value = path.getLastPathComponent(); boolean isSelected = tree.isRowSelected(lastRow); diff --git a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java index 28b93ed1e2a..399ef3e531d 100644 --- a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java +++ b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java @@ -1646,17 +1646,17 @@ public class SwingUtilities2 { if (container.isFocusCycleRoot()) { FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); Component comp = policy.getDefaultComponent(container); - if (comp!=null) { + if (comp != null) { comp.requestFocus(FocusEvent.Cause.TRAVERSAL); return comp; } } Container rootAncestor = container.getFocusCycleRootAncestor(); - if (rootAncestor!=null) { + if (rootAncestor != null) { FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component comp = policy.getComponentAfter(rootAncestor, container); - if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { + if (comp != null && SwingUtilities.isDescendingFrom(comp, container)) { comp.requestFocus(FocusEvent.Cause.TRAVERSAL); return comp; } From d52e5bd0357a074f74757f7a8256ed14a2e0eaee Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Wed, 18 Mar 2026 03:36:00 +0000 Subject: [PATCH 18/22] 8378727: [macOS] Missing dispatch_release for semaphores in CDesktopPeer Reviewed-by: honkar, kizune, azvegint, dmarkov --- .../macosx/native/libawt_lwawt/awt/CDesktopPeer.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/CDesktopPeer.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/CDesktopPeer.m index 460749c363d..faacef5adea 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CDesktopPeer.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CDesktopPeer.m @@ -70,6 +70,7 @@ JNI_COCOA_ENTER(env); dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC)); // 1 second timeout // Asynchronous call to openURL + dispatch_retain(semaphore); [[NSWorkspace sharedWorkspace] openURLs:urls withApplicationAtURL:appURI configuration:configuration @@ -78,9 +79,11 @@ JNI_COCOA_ENTER(env); status = (OSStatus) error.code; } dispatch_semaphore_signal(semaphore); + dispatch_release(semaphore); }]; dispatch_semaphore_wait(semaphore, timeout); + dispatch_release(semaphore); JNI_COCOA_EXIT(env); return status; @@ -146,6 +149,7 @@ JNI_COCOA_ENTER(env); dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC)); // 1 second timeout // Asynchronous call - openURLs:withApplicationAtURL + dispatch_retain(semaphore); [[NSWorkspace sharedWorkspace] openURLs:urls withApplicationAtURL:appURI configuration:configuration @@ -154,9 +158,11 @@ JNI_COCOA_ENTER(env); status = (OSStatus) error.code; } dispatch_semaphore_signal(semaphore); + dispatch_release(semaphore); }]; dispatch_semaphore_wait(semaphore, timeout); + dispatch_release(semaphore); [urlToOpen release]; JNI_COCOA_EXIT(env); From 31de288c0c67448487eabd46348bef97a1a60846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Galder=20Zamarre=C3=B1o?= Date: Wed, 18 Mar 2026 06:49:20 +0000 Subject: [PATCH 19/22] 8367399: C2 SuperWord: add IR rules for MemorySegment cases from JDK-8329077 Reviewed-by: fyang, epeter --- .../TestCompatibleUseDefTypeSize.java | 204 +++++++++--------- 1 file changed, 96 insertions(+), 108 deletions(-) diff --git a/test/hotspot/jtreg/compiler/loopopts/superword/TestCompatibleUseDefTypeSize.java b/test/hotspot/jtreg/compiler/loopopts/superword/TestCompatibleUseDefTypeSize.java index 873533aaba8..e6edda9085a 100644 --- a/test/hotspot/jtreg/compiler/loopopts/superword/TestCompatibleUseDefTypeSize.java +++ b/test/hotspot/jtreg/compiler/loopopts/superword/TestCompatibleUseDefTypeSize.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2026 IBM Corporation. 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 @@ -24,13 +25,15 @@ package compiler.loopopts.superword; import compiler.lib.ir_framework.*; +import compiler.lib.verify.Verify; import jdk.test.lib.Utils; import jdk.test.whitebox.WhiteBox; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; import java.lang.reflect.Array; import java.util.Map; import java.util.HashMap; import java.util.Random; -import java.nio.ByteOrder; /* * @test @@ -61,6 +64,8 @@ public class TestCompatibleUseDefTypeSize { float[] bF; double[] aD; double[] bD; + MemorySegment aMSF; + MemorySegment aMSD; // List of tests Map tests = new HashMap(); @@ -92,6 +97,8 @@ public class TestCompatibleUseDefTypeSize { bF = generateF(); aD = generateD(); bD = generateD(); + aMSF = generateMemorySegmentF(); + aMSD = generateMemorySegmentD(); // Add all tests to list tests.put("test0", () -> { return test0(aB.clone(), bC.clone()); }); @@ -122,6 +129,10 @@ public class TestCompatibleUseDefTypeSize { tests.put("testLongToShort", () -> { return testLongToShort(aL.clone(), bS.clone()); }); tests.put("testLongToChar", () -> { return testLongToChar(aL.clone(), bC.clone()); }); tests.put("testLongToInt", () -> { return testLongToInt(aL.clone(), bI.clone()); }); + tests.put("testFloatToIntMemorySegment", () -> { return testFloatToIntMemorySegment(copyF(aMSF), bF.clone()); }); + tests.put("testDoubleToLongMemorySegment", () -> { return testDoubleToLongMemorySegment(copyD(aMSD), bD.clone()); }); + tests.put("testIntToFloatMemorySegment", () -> { return testIntToFloatMemorySegment(copyF(aMSF), bF.clone()); }); + tests.put("testLongToDoubleMemorySegment", () -> { return testLongToDoubleMemorySegment(copyD(aMSD), bD.clone()); }); // Compute gold value for all test methods before compilation for (Map.Entry entry : tests.entrySet()) { @@ -160,7 +171,11 @@ public class TestCompatibleUseDefTypeSize { "testLongToByte", "testLongToShort", "testLongToChar", - "testLongToInt"}) + "testLongToInt", + "testFloatToIntMemorySegment", + "testDoubleToLongMemorySegment", + "testIntToFloatMemorySegment", + "testLongToDoubleMemorySegment"}) public void runTests() { for (Map.Entry entry : tests.entrySet()) { String name = entry.getKey(); @@ -170,7 +185,7 @@ public class TestCompatibleUseDefTypeSize { // Compute new result Object[] result = test.run(); // Compare gold and new result - verify(name, gold, result); + Verify.checkEQ(gold, result); } } @@ -230,119 +245,32 @@ public class TestCompatibleUseDefTypeSize { return a; } - static void verify(String name, Object[] gold, Object[] result) { - if (gold.length != result.length) { - throw new RuntimeException("verify " + name + ": not the same number of outputs: gold.length = " + - gold.length + ", result.length = " + result.length); - } - for (int i = 0; i < gold.length; i++) { - Object g = gold[i]; - Object r = result[i]; - if (g.getClass() != r.getClass() || !g.getClass().isArray() || !r.getClass().isArray()) { - throw new RuntimeException("verify " + name + ": must both be array of same type:" + - " gold[" + i + "].getClass() = " + g.getClass().getSimpleName() + - " result[" + i + "].getClass() = " + r.getClass().getSimpleName()); - } - if (g == r) { - throw new RuntimeException("verify " + name + ": should be two separate arrays (with identical content):" + - " gold[" + i + "] == result[" + i + "]"); - } - if (Array.getLength(g) != Array.getLength(r)) { - throw new RuntimeException("verify " + name + ": arrays must have same length:" + - " gold[" + i + "].length = " + Array.getLength(g) + - " result[" + i + "].length = " + Array.getLength(r)); - } - Class c = g.getClass().getComponentType(); - if (c == byte.class) { - verifyB(name, i, (byte[])g, (byte[])r); - } else if (c == short.class) { - verifyS(name, i, (short[])g, (short[])r); - } else if (c == char.class) { - verifyC(name, i, (char[])g, (char[])r); - } else if (c == int.class) { - verifyI(name, i, (int[])g, (int[])r); - } else if (c == long.class) { - verifyL(name, i, (long[])g, (long[])r); - } else if (c == float.class) { - verifyF(name, i, (float[])g, (float[])r); - } else if (c == double.class) { - verifyD(name, i, (double[])g, (double[])r); - } else { - throw new RuntimeException("verify " + name + ": array type not supported for verify:" + - " gold[" + i + "].getClass() = " + g.getClass().getSimpleName() + - " result[" + i + "].getClass() = " + r.getClass().getSimpleName()); - } + static MemorySegment generateMemorySegmentF() { + MemorySegment a = MemorySegment.ofArray(new float[RANGE]); + for (int i = 0; i < (int) a.byteSize(); i += 8) { + a.set(ValueLayout.JAVA_LONG_UNALIGNED, i, RANDOM.nextLong()); } + return a; } - static void verifyB(String name, int i, byte[] g, byte[] r) { - for (int j = 0; j < g.length; j++) { - if (g[j] != r[j]) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } + MemorySegment copyF(MemorySegment src) { + MemorySegment dst = generateMemorySegmentF(); + MemorySegment.copy(src, 0, dst, 0, src.byteSize()); + return dst; } - static void verifyS(String name, int i, short[] g, short[] r) { - for (int j = 0; j < g.length; j++) { - if (g[j] != r[j]) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } + static MemorySegment generateMemorySegmentD() { + MemorySegment a = MemorySegment.ofArray(new double[RANGE]); + for (int i = 0; i < (int) a.byteSize(); i += 8) { + a.set(ValueLayout.JAVA_LONG_UNALIGNED, i, RANDOM.nextLong()); } + return a; } - static void verifyC(String name, int i, char[] g, char[] r) { - for (int j = 0; j < g.length; j++) { - if (g[j] != r[j]) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } - } - - static void verifyI(String name, int i, int[] g, int[] r) { - for (int j = 0; j < g.length; j++) { - if (g[j] != r[j]) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } - } - - static void verifyL(String name, int i, long[] g, long[] r) { - for (int j = 0; j < g.length; j++) { - if (g[j] != r[j]) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } - } - - static void verifyF(String name, int i, float[] g, float[] r) { - for (int j = 0; j < g.length; j++) { - if (Float.floatToIntBits(g[j]) != Float.floatToIntBits(r[j])) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } - } - - static void verifyD(String name, int i, double[] g, double[] r) { - for (int j = 0; j < g.length; j++) { - if (Double.doubleToLongBits(g[j]) != Double.doubleToLongBits(r[j])) { - throw new RuntimeException("verify " + name + ": arrays must have same content:" + - " gold[" + i + "][" + j + "] = " + g[j] + - " result[" + i + "][" + j + "] = " + r[j]); - } - } + MemorySegment copyD(MemorySegment src) { + MemorySegment dst = generateMemorySegmentD(); + MemorySegment.copy(src, 0, dst, 0, src.byteSize()); + return dst; } @Test @@ -707,4 +635,64 @@ public class TestCompatibleUseDefTypeSize { return new Object[] { ints, res }; } + + @Test + @IR(counts = {IRNode.LOAD_VECTOR_F, IRNode.VECTOR_SIZE + "min(max_int, max_float)", "> 0", + IRNode.STORE_VECTOR, "> 0", + IRNode.VECTOR_REINTERPRET, "> 0"}, + applyIf = {"AlignVector", "false"}, + applyIfPlatform = {"64-bit", "true"}, + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true", "rvv", "true"}) + static Object[] testFloatToIntMemorySegment(MemorySegment a, float[] b) { + for (int i = 0; i < RANGE; i++) { + a.set(ValueLayout.JAVA_FLOAT_UNALIGNED, 4L * i, b[i]); + } + + return new Object[]{ a, b }; + } + + @Test + @IR(counts = {IRNode.LOAD_VECTOR_D, IRNode.VECTOR_SIZE + "min(max_long, max_double)", "> 0", + IRNode.STORE_VECTOR, "> 0", + IRNode.VECTOR_REINTERPRET, "> 0"}, + applyIf = {"AlignVector", "false"}, + applyIfPlatform = {"64-bit", "true"}, + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true", "rvv", "true"}) + static Object[] testDoubleToLongMemorySegment(MemorySegment a, double[] b) { + for (int i = 0; i < RANGE; i++) { + a.set(ValueLayout.JAVA_DOUBLE_UNALIGNED, 8L * i, b[i]); + } + + return new Object[]{ a, b }; + } + + @Test + @IR(counts = {IRNode.LOAD_VECTOR_I, "> 0", + IRNode.STORE_VECTOR, "> 0", + IRNode.VECTOR_REINTERPRET, "> 0"}, + applyIf = {"AlignVector", "false"}, + applyIfPlatform = {"64-bit", "true"}, + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true", "rvv", "true"}) + static Object[] testIntToFloatMemorySegment(MemorySegment a, float[] b) { + for (int i = 0; i < RANGE; i++) { + b[i] = a.get(ValueLayout.JAVA_FLOAT_UNALIGNED, 4L * i); + } + + return new Object[]{ a, b }; + } + + @Test + @IR(counts = {IRNode.LOAD_VECTOR_L, "> 0", + IRNode.STORE_VECTOR, "> 0", + IRNode.VECTOR_REINTERPRET, "> 0"}, + applyIf = {"AlignVector", "false"}, + applyIfPlatform = {"64-bit", "true"}, + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true", "rvv", "true"}) + static Object[] testLongToDoubleMemorySegment(MemorySegment a, double[] b) { + for (int i = 0; i < RANGE; i++) { + b[i] = a.get(ValueLayout.JAVA_DOUBLE_UNALIGNED, 8L * i); + } + + return new Object[]{ a, b }; + } } From d8f19bf961f1f25a029943b6390f8b9c95f31304 Mon Sep 17 00:00:00 2001 From: Dusan Balek Date: Wed, 18 Mar 2026 07:04:25 +0000 Subject: [PATCH 20/22] 8347418: ConsoleIOContext.countTrailintBackslashes causes NullPointerException Reviewed-by: jlahoda --- .../jshell/tool/ConsoleIOContext.java | 2 + .../jshell/ConcurrentHistoryLoadingTest.java | 80 +++++++++++++++++++ .../langtools/jdk/jshell/ReplToolTesting.java | 14 ++-- .../jdk/jshell/TerminalNoExecTest.java | 8 +- 4 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 test/langtools/jdk/jshell/ConcurrentHistoryLoadingTest.java diff --git a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java index 32664504d20..eba81b4be07 100644 --- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java +++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java @@ -49,6 +49,7 @@ import java.util.List; import java.util.ListIterator; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -202,6 +203,7 @@ class ConsoleIOContext extends IOContext { .filter(key -> key.startsWith(HISTORY_LINE_PREFIX)) .sorted() .map(key -> repl.prefs.get(key)) + .filter(Objects::nonNull) .forEach(loadHistory::add); for (ListIterator it = loadHistory.listIterator(); it.hasNext(); ) { diff --git a/test/langtools/jdk/jshell/ConcurrentHistoryLoadingTest.java b/test/langtools/jdk/jshell/ConcurrentHistoryLoadingTest.java new file mode 100644 index 00000000000..926dbbf50b4 --- /dev/null +++ b/test/langtools/jdk/jshell/ConcurrentHistoryLoadingTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2026, 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. + */ + +import java.io.ByteArrayInputStream; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.prefs.Preferences; + +import jdk.jshell.tool.JavaShellToolBuilder; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/* + * @test + * @bug 8347418 + * @summary Verify that loading of JShell history doesn't lead to a + * NullPointerException when the Preferences are modified concurrently. + * @run junit ConcurrentHistoryLoadingTest + */ +public class ConcurrentHistoryLoadingTest { + + private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_"; + + @Test + public void testConcurrentHistoryLoading() throws Throwable { + AtomicBoolean removeOnAccess = new AtomicBoolean(); + Preferences testPrefs = new ReplToolTesting.MemoryPreferences() { + @Override + protected String getSpi(String key) { + String result = super.getSpi(key); + if (key.startsWith(HISTORY_LINE_PREFIX) && removeOnAccess.getAndSet(false)) { + for (String key2Remote : keysSpi()) { + remove(key2Remote); + } + } + return result; + } + }; + StringBuilder input = new StringBuilder(); + int max = 10; + for (int j = 0; j < max; j++) { + input.append("int x").append(j).append(" = 42\n"); + } + JavaShellToolBuilder + .builder() + .persistence(testPrefs) + .in(new ByteArrayInputStream(input.toString().getBytes()), null) + .start(); + Assertions.assertEquals(10, Arrays.stream(testPrefs.keys()) + .filter(key -> key.startsWith(HISTORY_LINE_PREFIX)) + .count()); + removeOnAccess.set(true); + JavaShellToolBuilder + .builder() + .persistence(testPrefs) + .in(new ByteArrayInputStream(input.toString().getBytes()), null) + .start(); + + } +} diff --git a/test/langtools/jdk/jshell/ReplToolTesting.java b/test/langtools/jdk/jshell/ReplToolTesting.java index 429a0a7ce02..2dabf29e1f9 100644 --- a/test/langtools/jdk/jshell/ReplToolTesting.java +++ b/test/langtools/jdk/jshell/ReplToolTesting.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2026, 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 @@ -914,7 +914,7 @@ public class ReplToolTesting { } } - public static final class MemoryPreferences extends AbstractPreferences { + public static class MemoryPreferences extends AbstractPreferences { private final Map values = new HashMap<>(); private final Map nodes = new HashMap<>(); @@ -943,17 +943,17 @@ public class ReplToolTesting { } @Override - protected void removeNodeSpi() throws BackingStoreException { + protected void removeNodeSpi() { ((MemoryPreferences) parent()).nodes.remove(name()); } @Override - protected String[] keysSpi() throws BackingStoreException { + protected String[] keysSpi() { return values.keySet().toArray(new String[0]); } @Override - protected String[] childrenNamesSpi() throws BackingStoreException { + protected String[] childrenNamesSpi() { return nodes.keySet().toArray(new String[0]); } @@ -963,11 +963,11 @@ public class ReplToolTesting { } @Override - protected void syncSpi() throws BackingStoreException { + protected void syncSpi() { } @Override - protected void flushSpi() throws BackingStoreException { + protected void flushSpi() { } } diff --git a/test/langtools/jdk/jshell/TerminalNoExecTest.java b/test/langtools/jdk/jshell/TerminalNoExecTest.java index 3d76157fd26..d7cd20046af 100644 --- a/test/langtools/jdk/jshell/TerminalNoExecTest.java +++ b/test/langtools/jdk/jshell/TerminalNoExecTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, 2026, 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 @@ -39,7 +39,7 @@ import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.concurrent.TimeUnit; +import java.util.HashMap; import java.util.concurrent.atomic.AtomicBoolean; import jdk.jfr.consumer.RecordingStream; import jdk.jshell.tool.JavaShellToolBuilder; @@ -58,7 +58,9 @@ public class TerminalNoExecTest { spawnedNewProcess.set(true); }); rs.startAsync(); - JavaShellToolBuilder.builder().run("--execution=local", "--no-startup"); + JavaShellToolBuilder.builder() + .persistence(new HashMap<>()) + .run("--execution=local", "--no-startup"); rs.stop(); } if (spawnedNewProcess.get()) { From 706fbb3044ef56cad14cf4e413b24042a3ea3f13 Mon Sep 17 00:00:00 2001 From: Volkan Yazici Date: Wed, 18 Mar 2026 08:25:29 +0000 Subject: [PATCH 21/22] 8378431: Move input validation checks to Java for java.lang.StringUTF16 intrinsics Reviewed-by: dfenacci, rgiulietti, rriggs --- src/hotspot/share/classfile/vmIntrinsics.hpp | 4 +- src/hotspot/share/opto/library_call.cpp | 214 ++++++++---------- src/hotspot/share/opto/library_call.hpp | 4 +- .../share/classes/java/lang/StringUTF16.java | 116 +++++++--- .../TestStringIntrinsicRangeChecks.java | 10 +- 5 files changed, 200 insertions(+), 148 deletions(-) diff --git a/src/hotspot/share/classfile/vmIntrinsics.hpp b/src/hotspot/share/classfile/vmIntrinsics.hpp index 67817682ced..e84acd62284 100644 --- a/src/hotspot/share/classfile/vmIntrinsics.hpp +++ b/src/hotspot/share/classfile/vmIntrinsics.hpp @@ -368,10 +368,10 @@ class methodHandle; do_intrinsic(_inflateStringB, java_lang_StringLatin1, inflate_name, inflateB_signature, F_S) \ do_signature(inflateB_signature, "([BI[BII)V") \ do_intrinsic(_toBytesStringU, java_lang_StringUTF16, toBytes_name, toBytesU_signature, F_S) \ - do_name( toBytes_name, "toBytes") \ + do_name( toBytes_name, "toBytes0") \ do_signature(toBytesU_signature, "([CII)[B") \ do_intrinsic(_getCharsStringU, java_lang_StringUTF16, getCharsU_name, getCharsU_signature, F_S) \ - do_name( getCharsU_name, "getChars") \ + do_name( getCharsU_name, "getChars0") \ do_signature(getCharsU_signature, "([BII[CI)V") \ do_intrinsic(_getCharStringU, java_lang_StringUTF16, getChar_name, getCharStringU_signature, F_S) \ do_signature(getCharStringU_signature, "([BI)C") \ diff --git a/src/hotspot/share/opto/library_call.cpp b/src/hotspot/share/opto/library_call.cpp index 501e35ae5cb..ffc798019b4 100644 --- a/src/hotspot/share/opto/library_call.cpp +++ b/src/hotspot/share/opto/library_call.cpp @@ -843,6 +843,22 @@ void LibraryCallKit::set_result(RegionNode* region, PhiNode* value) { assert(value->type()->basic_type() == result()->bottom_type()->basic_type(), "sanity"); } +RegionNode* LibraryCallKit::create_bailout() { + RegionNode* bailout = new RegionNode(1); + record_for_igvn(bailout); + return bailout; +} + +bool LibraryCallKit::check_bailout(RegionNode* bailout) { + if (bailout->req() > 1) { + bailout = _gvn.transform(bailout)->as_Region(); + Node* frame = _gvn.transform(new ParmNode(C->start(), TypeFunc::FramePtr)); + Node* halt = _gvn.transform(new HaltNode(bailout, frame, "unexpected guard failure in intrinsic")); + C->root()->add_req(halt); + } + return stopped(); +} + //------------------------------generate_guard--------------------------- // Helper function for generating guarded fast-slow graph structures. // The given 'test', if true, guards a slow path. If the test fails @@ -951,36 +967,19 @@ void LibraryCallKit::generate_string_range_check(Node* array, Node* offset, Node* count, bool char_count, - bool halt_on_oob) { + RegionNode* region) { if (stopped()) { return; // already stopped } - RegionNode* bailout = new RegionNode(1); - record_for_igvn(bailout); if (char_count) { // Convert char count to byte count count = _gvn.transform(new LShiftINode(count, intcon(1))); } - // Offset and count must not be negative - generate_negative_guard(offset, bailout, nullptr, halt_on_oob); - generate_negative_guard(count, bailout, nullptr, halt_on_oob); + generate_negative_guard(offset, region, nullptr, true); + generate_negative_guard(count, region, nullptr, true); // Offset + count must not exceed length of array - generate_limit_guard(offset, count, load_array_length(array), bailout, halt_on_oob); - - if (bailout->req() > 1) { - if (halt_on_oob) { - bailout = _gvn.transform(bailout)->as_Region(); - Node* frame = _gvn.transform(new ParmNode(C->start(), TypeFunc::FramePtr)); - Node* halt = _gvn.transform(new HaltNode(bailout, frame, "unexpected guard failure in intrinsic")); - C->root()->add_req(halt); - } else { - PreserveJVMState pjvms(this); - set_control(_gvn.transform(bailout)); - uncommon_trap(Deoptimization::Reason_intrinsic, - Deoptimization::Action_maybe_recompile); - } - } + generate_limit_guard(offset, count, load_array_length(array), region, true); } Node* LibraryCallKit::current_thread_helper(Node*& tls_output, ByteSize handle_offset, @@ -1139,10 +1138,6 @@ bool LibraryCallKit::inline_array_equals(StrIntrinsicNode::ArgEnc ae) { //------------------------------inline_countPositives------------------------------ // int java.lang.StringCoding#countPositives0(byte[] ba, int off, int len) bool LibraryCallKit::inline_countPositives() { - if (too_many_traps(Deoptimization::Reason_intrinsic)) { - return false; - } - assert(callee()->signature()->size() == 3, "countPositives has 3 parameters"); // no receiver since it is static method Node* ba = argument(0); @@ -1150,8 +1145,9 @@ bool LibraryCallKit::inline_countPositives() { Node* len = argument(2); ba = must_be_not_null(ba, true); - generate_string_range_check(ba, offset, len, false, true); - if (stopped()) { + RegionNode* bailout = create_bailout(); + generate_string_range_check(ba, offset, len, false, bailout); + if (check_bailout(bailout)) { return true; } @@ -1283,9 +1279,6 @@ bool LibraryCallKit::inline_string_indexOf(StrIntrinsicNode::ArgEnc ae) { //-----------------------------inline_string_indexOfI----------------------- bool LibraryCallKit::inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae) { - if (too_many_traps(Deoptimization::Reason_intrinsic)) { - return false; - } if (!Matcher::match_rule_supported(Op_StrIndexOf)) { return false; } @@ -1307,9 +1300,10 @@ bool LibraryCallKit::inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae) { Node* tgt_start = array_element_address(tgt, intcon(0), T_BYTE); // Range checks - generate_string_range_check(src, src_offset, src_count, ae != StrIntrinsicNode::LL, true); - generate_string_range_check(tgt, intcon(0), tgt_count, ae == StrIntrinsicNode::UU, true); - if (stopped()) { + RegionNode* bailout = create_bailout(); + generate_string_range_check(src, src_offset, src_count, ae != StrIntrinsicNode::LL, bailout); + generate_string_range_check(tgt, intcon(0), tgt_count, ae == StrIntrinsicNode::UU, bailout); + if (check_bailout(bailout)) { return true; } @@ -1404,7 +1398,11 @@ bool LibraryCallKit::inline_string_indexOfChar(StrIntrinsicNode::ArgEnc ae) { Node* src_count = _gvn.transform(new SubINode(max, from_index)); // Range checks - generate_string_range_check(src, src_offset, src_count, ae == StrIntrinsicNode::U, true); + RegionNode* bailout = create_bailout(); + generate_string_range_check(src, src_offset, src_count, ae == StrIntrinsicNode::U, bailout); + if (check_bailout(bailout)) { + return true; + } // Check for int_ch >= 0 Node* int_ch_cmp = _gvn.transform(new CmpINode(int_ch, intcon(0))); @@ -1454,9 +1452,6 @@ bool LibraryCallKit::inline_string_indexOfChar(StrIntrinsicNode::ArgEnc ae) { // void StringLatin1.inflate0(byte[] src, int srcOff, char[] dst, int dstOff, int len) // void StringLatin1.inflate0(byte[] src, int srcOff, byte[] dst, int dstOff, int len) bool LibraryCallKit::inline_string_copy(bool compress) { - if (too_many_traps(Deoptimization::Reason_intrinsic)) { - return false; - } int nargs = 5; // 2 oops, 3 ints assert(callee()->signature()->size() == nargs, "string copy has 5 arguments"); @@ -1495,9 +1490,10 @@ bool LibraryCallKit::inline_string_copy(bool compress) { } // Range checks - generate_string_range_check(src, src_offset, length, convert_src, true); - generate_string_range_check(dst, dst_offset, length, convert_dst, true); - if (stopped()) { + RegionNode* bailout = create_bailout(); + generate_string_range_check(src, src_offset, length, convert_src, bailout); + generate_string_range_check(dst, dst_offset, length, convert_dst, bailout); + if (check_bailout(bailout)) { return true; } @@ -1545,12 +1541,10 @@ bool LibraryCallKit::inline_string_copy(bool compress) { #endif //_LP64 //------------------------inline_string_toBytesU-------------------------- -// public static byte[] StringUTF16.toBytes(char[] value, int off, int len) +// public static byte[] StringUTF16.toBytes0(char[] value, int off, int len) bool LibraryCallKit::inline_string_toBytesU() { - if (too_many_traps(Deoptimization::Reason_intrinsic)) { - return false; - } // Get the arguments. + assert(callee()->signature()->size() == 3, "character array encoder requires 3 arguments"); Node* value = argument(0); Node* offset = argument(1); Node* length = argument(2); @@ -1558,30 +1552,18 @@ bool LibraryCallKit::inline_string_toBytesU() { Node* newcopy = nullptr; // Set the original stack and the reexecute bit for the interpreter to reexecute - // the bytecode that invokes StringUTF16.toBytes() if deoptimization happens. + // the bytecode that invokes StringUTF16.toBytes0() if deoptimization happens. { PreserveReexecuteState preexecs(this); jvms()->set_should_reexecute(true); - // Check if a null path was taken unconditionally. - value = null_check(value); - - RegionNode* bailout = new RegionNode(1); - record_for_igvn(bailout); - - // Range checks - generate_negative_guard(offset, bailout); - generate_negative_guard(length, bailout); - generate_limit_guard(offset, length, load_array_length(value), bailout); + value = must_be_not_null(value, true); + RegionNode* bailout = create_bailout(); + generate_negative_guard(offset, bailout, nullptr, true); + generate_negative_guard(length, bailout, nullptr, true); + generate_limit_guard(offset, length, load_array_length(value), bailout, true); // Make sure that resulting byte[] length does not overflow Integer.MAX_VALUE - generate_limit_guard(length, intcon(0), intcon(max_jint/2), bailout); - - if (bailout->req() > 1) { - PreserveJVMState pjvms(this); - set_control(_gvn.transform(bailout)); - uncommon_trap(Deoptimization::Reason_intrinsic, - Deoptimization::Action_maybe_recompile); - } - if (stopped()) { + generate_limit_guard(length, intcon(0), intcon(max_jint/2), bailout, true); + if (check_bailout(bailout)) { return true; } @@ -1640,12 +1622,9 @@ bool LibraryCallKit::inline_string_toBytesU() { } //------------------------inline_string_getCharsU-------------------------- -// public void StringUTF16.getChars(byte[] src, int srcBegin, int srcEnd, char dst[], int dstBegin) +// public void StringUTF16.getChars0(byte[] src, int srcBegin, int srcEnd, char dst[], int dstBegin) bool LibraryCallKit::inline_string_getCharsU() { - if (too_many_traps(Deoptimization::Reason_intrinsic)) { - return false; - } - + assert(callee()->signature()->size() == 5, "StringUTF16.getChars0() has 5 arguments"); // Get the arguments. Node* src = argument(0); Node* src_begin = argument(1); @@ -1658,8 +1637,8 @@ bool LibraryCallKit::inline_string_getCharsU() { AllocateArrayNode* alloc = tightly_coupled_allocation(dst); // Check if a null path was taken unconditionally. - src = null_check(src); - dst = null_check(dst); + src = must_be_not_null(src, true); + dst = must_be_not_null(dst, true); if (stopped()) { return true; } @@ -1669,51 +1648,50 @@ bool LibraryCallKit::inline_string_getCharsU() { src_begin = _gvn.transform(new LShiftINode(src_begin, intcon(1))); // Range checks - generate_string_range_check(src, src_begin, length, true); - generate_string_range_check(dst, dst_begin, length, false); - if (stopped()) { + RegionNode* bailout = create_bailout(); + generate_string_range_check(src, src_begin, length, true, bailout); + generate_string_range_check(dst, dst_begin, length, false, bailout); + if (check_bailout(bailout)) { return true; } - if (!stopped()) { - // Calculate starting addresses. - Node* src_start = array_element_address(src, src_begin, T_BYTE); - Node* dst_start = array_element_address(dst, dst_begin, T_CHAR); + // Calculate starting addresses. + Node* src_start = array_element_address(src, src_begin, T_BYTE); + Node* dst_start = array_element_address(dst, dst_begin, T_CHAR); - // Check if array addresses are aligned to HeapWordSize - const TypeInt* tsrc = gvn().type(src_begin)->is_int(); - const TypeInt* tdst = gvn().type(dst_begin)->is_int(); - bool aligned = tsrc->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_BYTE) + tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) && - tdst->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_CHAR) + tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); + // Check if array addresses are aligned to HeapWordSize + const TypeInt* tsrc = gvn().type(src_begin)->is_int(); + const TypeInt* tdst = gvn().type(dst_begin)->is_int(); + bool aligned = tsrc->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_BYTE) + tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) && + tdst->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_CHAR) + tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); - // Figure out which arraycopy runtime method to call (disjoint, uninitialized). - const char* copyfunc_name = "arraycopy"; - address copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true); - Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, - OptoRuntime::fast_arraycopy_Type(), - copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM, - src_start, dst_start, ConvI2X(length) XTOP); - // Do not let reads from the cloned object float above the arraycopy. - if (alloc != nullptr) { - if (alloc->maybe_set_complete(&_gvn)) { - // "You break it, you buy it." - InitializeNode* init = alloc->initialization(); - assert(init->is_complete(), "we just did this"); - init->set_complete_with_arraycopy(); - assert(dst->is_CheckCastPP(), "sanity"); - assert(dst->in(0)->in(0) == init, "dest pinned"); - } - // Do not let stores that initialize this object be reordered with - // a subsequent store that would make this object accessible by - // other threads. - // Record what AllocateNode this StoreStore protects so that - // escape analysis can go from the MemBarStoreStoreNode to the - // AllocateNode and eliminate the MemBarStoreStoreNode if possible - // based on the escape status of the AllocateNode. - insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress)); - } else { - insert_mem_bar(Op_MemBarCPUOrder); + // Figure out which arraycopy runtime method to call (disjoint, uninitialized). + const char* copyfunc_name = "arraycopy"; + address copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true); + Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, + OptoRuntime::fast_arraycopy_Type(), + copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM, + src_start, dst_start, ConvI2X(length) XTOP); + // Do not let reads from the cloned object float above the arraycopy. + if (alloc != nullptr) { + if (alloc->maybe_set_complete(&_gvn)) { + // "You break it, you buy it." + InitializeNode* init = alloc->initialization(); + assert(init->is_complete(), "we just did this"); + init->set_complete_with_arraycopy(); + assert(dst->is_CheckCastPP(), "sanity"); + assert(dst->in(0)->in(0) == init, "dest pinned"); } + // Do not let stores that initialize this object be reordered with + // a subsequent store that would make this object accessible by + // other threads. + // Record what AllocateNode this StoreStore protects so that + // escape analysis can go from the MemBarStoreStoreNode to the + // AllocateNode and eliminate the MemBarStoreStoreNode if possible + // based on the escape status of the AllocateNode. + insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress)); + } else { + insert_mem_bar(Op_MemBarCPUOrder); } C->set_has_split_ifs(true); // Has chance for split-if optimization @@ -1725,9 +1703,16 @@ bool LibraryCallKit::inline_string_getCharsU() { // static void StringUTF16.putChar(byte[] val, int index, int c) // static char StringUTF16.getChar(byte[] val, int index) bool LibraryCallKit::inline_string_char_access(bool is_store) { + Node* ch; + if (is_store) { + assert(callee()->signature()->size() == 3, "StringUTF16.putChar() has 3 arguments"); + ch = argument(2); + } else { + assert(callee()->signature()->size() == 2, "StringUTF16.getChar() has 2 arguments"); + ch = nullptr; + } Node* value = argument(0); Node* index = argument(1); - Node* ch = is_store ? argument(2) : nullptr; // This intrinsic accesses byte[] array as char[] array. Computing the offsets // correctly requires matched array shapes. @@ -6185,9 +6170,10 @@ bool LibraryCallKit::inline_encodeISOArray(bool ascii) { } // Check source & target bounds - generate_string_range_check(src, src_offset, length, src_elem == T_BYTE, true); - generate_string_range_check(dst, dst_offset, length, false, true); - if (stopped()) { + RegionNode* bailout = create_bailout(); + generate_string_range_check(src, src_offset, length, src_elem == T_BYTE, bailout); + generate_string_range_check(dst, dst_offset, length, false, bailout); + if (check_bailout(bailout)) { return true; } diff --git a/src/hotspot/share/opto/library_call.hpp b/src/hotspot/share/opto/library_call.hpp index bd4b753d1cb..9b87df645e1 100644 --- a/src/hotspot/share/opto/library_call.hpp +++ b/src/hotspot/share/opto/library_call.hpp @@ -130,6 +130,8 @@ class LibraryCallKit : public GraphKit { virtual int reexecute_sp() { return _reexecute_sp; } // Helper functions to inline natives + RegionNode* create_bailout(); + bool check_bailout(RegionNode* bailout); Node* generate_guard(Node* test, RegionNode* region, float true_prob); Node* generate_slow_guard(Node* test, RegionNode* region); Node* generate_fair_guard(Node* test, RegionNode* region); @@ -143,7 +145,7 @@ class LibraryCallKit : public GraphKit { bool with_opaque = false); void generate_string_range_check(Node* array, Node* offset, Node* length, bool char_count, - bool halt_on_oob = false); + RegionNode* region); Node* current_thread_helper(Node* &tls_output, ByteSize handle_offset, bool is_immutable); Node* generate_current_thread(Node* &tls_output); diff --git a/src/java.base/share/classes/java/lang/StringUTF16.java b/src/java.base/share/classes/java/lang/StringUTF16.java index 27b9ae54a8a..23de31a61b7 100644 --- a/src/java.base/share/classes/java/lang/StringUTF16.java +++ b/src/java.base/share/classes/java/lang/StringUTF16.java @@ -67,30 +67,61 @@ final class StringUTF16 { // Check the size of a UTF16-coded string // Throw an exception if out of range - static int newBytesLength(int len) { - if (len < 0) { - throw new NegativeArraySizeException(); - } - if (len >= MAX_LENGTH) { - throw new OutOfMemoryError("UTF16 String size is " + len + - ", should be less than " + MAX_LENGTH); - } + private static int newBytesLength(int len) { + checkBytesLength(len); return len << 1; } + /** + * Checks if the provided length is a valid UTF-16 string byte array length. + * + * @param length a UTF-16 string byte array length + * + * @throws NegativeArraySizeException if {@code length < 0} + * @throws OutOfMemoryError if {@code length > (Integer.MAX_VALUE / 2)} + */ + private static void checkBytesLength(int length) { + if (length < 0) { + throw new NegativeArraySizeException(); + } + if (length >= MAX_LENGTH) { + throw new OutOfMemoryError("UTF16 String size is " + length + + ", should be less than " + MAX_LENGTH); + } + } + + /** + * Writes the given code point to the specified position of the provided + * UTF-16 string byte array. + *

+ * WARNING: This method does not perform any input validations. + * + * @param val a UTF-16 string byte array + * @param index the index of the character to write the code point to + * @param c a code point + */ + // vmIntrinsics::_putCharStringU @IntrinsicCandidate - // intrinsic performs no bounds checks static void putChar(byte[] val, int index, int c) { - assert index >= 0 && index < length(val) : "Trusted caller missed bounds check"; + assert val != null && index >= 0 && index < length(val) : "Trusted caller violated input constraints"; index <<= 1; val[index++] = (byte)(c >> HI_BYTE_SHIFT); val[index] = (byte)(c >> LO_BYTE_SHIFT); } + /** + * {@return the code point at the the specified position of the provided + * UTF-16 string byte array} + *

+ * WARNING: This method does not perform any input validations. + * + * @param val a UTF-16 string byte array + * @param index the index of the character to get the code point from + */ + // vmIntrinsics::_getCharStringU @IntrinsicCandidate - // intrinsic performs no bounds checks static char getChar(byte[] val, int index) { - assert index >= 0 && index < length(val) : "Trusted caller missed bounds check"; + assert val != null && index >= 0 && index < length(val) : "Trusted caller violated input constraints"; index <<= 1; return (char)(((val[index++] & 0xff) << HI_BYTE_SHIFT) | ((val[index] & 0xff) << LO_BYTE_SHIFT)); @@ -173,14 +204,27 @@ final class StringUTF16 { } /** - * {@return an encoded byte[] for the UTF16 characters in char[]} - * No checking is done on the characters, some may or may not be latin1. - * @param value a char array - * @param off an offset - * @param len a length + * {@return a UTF-16 string byte array produced by encoding the characters + * in the provided character array sub-range} + * + * @param value a character array to encode + * @param off the index of the character to start encoding from + * @param len the number of characters to encode + * + * @throws NegativeArraySizeException if {@code len < 0} + * @throws NullPointerException if {@code value} is null + * @throws OutOfMemoryError if {@code len > (Integer.MAX_VALUE / 2)} + * @throws StringIndexOutOfBoundsException if the sub-range is out of bounds */ - @IntrinsicCandidate static byte[] toBytes(char[] value, int off, int len) { + checkBytesLength(len); + String.checkBoundsOffCount(off, len, value.length); // Implicit null check on `value` + return toBytes0(value, off, len); + } + + // vmIntrinsics::_toBytesStringU + @IntrinsicCandidate + private static byte[] toBytes0(char[] value, int off, int len) { byte[] val = newBytesFor(len); for (int i = 0; i < len; i++) { putChar(val, i, value[off]); @@ -495,12 +539,28 @@ final class StringUTF16 { return result; } - @IntrinsicCandidate + /** + * Copies the specified sub-range of characters from a UTF-16 string byte + * array to the specified character array sub-range. + * + * @param value the source UTF-16 string byte array to copy from + * @param srcBegin the index (inclusive) of the first character in the source sub-range + * @param srcEnd the index (exclusive) of the last character in the source sub-range + * @param dst the target character array to copy to + * @param dstBegin the index (inclusive) of the first character in the target sub-range + * + * @throws NullPointerException if {@code value} or {@code dst} is null + * @throws StringIndexOutOfBoundsException if the sub-ranges are out of bounds + */ static void getChars(byte[] value, int srcBegin, int srcEnd, char[] dst, int dstBegin) { - // We need a range check here because 'getChar' has no checks - if (srcBegin < srcEnd) { - String.checkBoundsOffCount(srcBegin, srcEnd - srcBegin, length(value)); - } + checkBoundsBeginEnd(srcBegin, srcEnd, value); // Implicit null check on `value` via `checkBoundsBeginEnd()` + String.checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length); // Implicit null check on `dst` + getChars0(value, srcBegin, srcEnd, dst, dstBegin); + } + + // vmIntrinsics::_getCharsStringU + @IntrinsicCandidate + private static void getChars0(byte[] value, int srcBegin, int srcEnd, char[] dst, int dstBegin) { for (int i = srcBegin; i < srcEnd; i++) { dst[dstBegin++] = getChar(value, i); } @@ -721,7 +781,7 @@ final class StringUTF16 { return -StringLatin1.compareToCI_UTF16(other, value); } - public static int compareToFC_Latin1(byte[] value, byte[] other) { + static int compareToFC_Latin1(byte[] value, byte[] other) { return -StringLatin1.compareToFC_UTF16(other, value); } @@ -769,7 +829,7 @@ final class StringUTF16 { return 0; } - public static int compareToFC(byte[] value, byte[] other) { + static int compareToFC(byte[] value, byte[] other) { int tlast = length(value); int olast = length(other); int lim = Math.min(tlast, olast); @@ -1970,13 +2030,13 @@ final class StringUTF16 { } } - static final int MAX_LENGTH = Integer.MAX_VALUE >> 1; + private static final int MAX_LENGTH = Integer.MAX_VALUE >> 1; - static void checkIndex(int off, byte[] val) { + private static void checkIndex(int off, byte[] val) { String.checkIndex(off, length(val)); } - static void checkOffset(int off, byte[] val) { + private static void checkOffset(int off, byte[] val) { String.checkOffset(off, length(val)); } diff --git a/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java b/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java index a7d2cfe7fa7..ec99bba19d2 100644 --- a/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java +++ b/test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsicRangeChecks.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2026, 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 @@ -89,12 +89,16 @@ public class TestStringIntrinsicRangeChecks { for (int srcOff = 0; srcOff < SIZE; ++srcOff) { for (int dstOff = 0; dstOff < SIZE; ++dstOff) { for (int len = 0; len < SIZE; ++len) { + int srcEnd = srcOff + len; + int dstEnd = dstOff + len; // Check for potential overlows in source or destination array boolean srcOverflow = (srcOff + len) > SIZE; boolean srcOverflowB = (2*srcOff + 2*len) > SIZE; boolean dstOverflow = (dstOff + len) > SIZE; boolean dstOverflowB = (2*dstOff + 2*len) > SIZE; - boolean getCharsOver = (srcOff < len) && ((2*(len-1) >= SIZE) || ((dstOff + len - srcOff) > SIZE)); + boolean getCharsOver = srcOff > srcEnd || (2*srcEnd) > SIZE || // src + (2*len) > SIZE || // len + dstOff > dstEnd || dstEnd > SIZE; // dst // Check if an exception is thrown and bail out if result is inconsistent with above // assumptions (for example, an exception was not thrown although an overflow happened). check(compressByte, srcOverflowB || dstOverflow, byteArray, srcOff, SIZE, dstOff, len); @@ -102,7 +106,7 @@ public class TestStringIntrinsicRangeChecks { check(inflateByte, srcOverflow || dstOverflowB, byteArray, srcOff, SIZE, dstOff, len); check(inflateChar, srcOverflow || dstOverflow, byteArray, srcOff, SIZE, dstOff, len); check(toBytes, srcOverflow, charArray, srcOff, len); - check(getChars, getCharsOver, byteArray, srcOff, len, SIZE, dstOff); + check(getChars, getCharsOver, byteArray, srcOff, srcEnd, SIZE, dstOff); } } } From 3a93daf189213d4c8af5d57d7af1e832401331b6 Mon Sep 17 00:00:00 2001 From: Albert Mingkun Yang Date: Wed, 18 Mar 2026 09:07:14 +0000 Subject: [PATCH 22/22] 8373369: [REDO] Remove ThreadLocalAllocBuffer::_reserve_for_allocation_prefetch Reviewed-by: mdoerr, kvn, tschatzl --- src/hotspot/cpu/ppc/ppc.ad | 29 ------- .../gc/shared/threadLocalAllocBuffer.cpp | 28 +------ .../gc/shared/threadLocalAllocBuffer.hpp | 1 - src/hotspot/share/opto/macro.cpp | 3 +- src/hotspot/share/runtime/vmStructs.cpp | 1 - .../runtime/ThreadLocalAllocBuffer.java | 3 +- .../classes/sun/jvm/hotspot/runtime/VM.java | 7 -- test/hotspot/jtreg/ProblemList.txt | 3 +- .../TestAllocatePrefetchStyleLargeFlags.java | 79 +++++++++++++++++++ 9 files changed, 84 insertions(+), 70 deletions(-) create mode 100644 test/hotspot/jtreg/compiler/c2/TestAllocatePrefetchStyleLargeFlags.java diff --git a/src/hotspot/cpu/ppc/ppc.ad b/src/hotspot/cpu/ppc/ppc.ad index 21fc12742ef..01c290f9b04 100644 --- a/src/hotspot/cpu/ppc/ppc.ad +++ b/src/hotspot/cpu/ppc/ppc.ad @@ -6327,36 +6327,8 @@ instruct loadConD_Ex(regD dst, immD src) %{ // Prefetch instructions. // Must be safe to execute with invalid address (cannot fault). -// Special prefetch versions which use the dcbz instruction. -instruct prefetch_alloc_zero(indirectMemory mem, iRegLsrc src) %{ - match(PrefetchAllocation (AddP mem src)); - predicate(AllocatePrefetchStyle == 3); - ins_cost(MEMORY_REF_COST); - - format %{ "PREFETCH $mem, 2, $src \t// Prefetch write-many with zero" %} - size(4); - ins_encode %{ - __ dcbz($src$$Register, $mem$$base$$Register); - %} - ins_pipe(pipe_class_memory); -%} - -instruct prefetch_alloc_zero_no_offset(indirectMemory mem) %{ - match(PrefetchAllocation mem); - predicate(AllocatePrefetchStyle == 3); - ins_cost(MEMORY_REF_COST); - - format %{ "PREFETCH $mem, 2 \t// Prefetch write-many with zero" %} - size(4); - ins_encode %{ - __ dcbz($mem$$base$$Register); - %} - ins_pipe(pipe_class_memory); -%} - instruct prefetch_alloc(indirectMemory mem, iRegLsrc src) %{ match(PrefetchAllocation (AddP mem src)); - predicate(AllocatePrefetchStyle != 3); ins_cost(MEMORY_REF_COST); format %{ "PREFETCH $mem, 2, $src \t// Prefetch write-many" %} @@ -6369,7 +6341,6 @@ instruct prefetch_alloc(indirectMemory mem, iRegLsrc src) %{ instruct prefetch_alloc_no_offset(indirectMemory mem) %{ match(PrefetchAllocation mem); - predicate(AllocatePrefetchStyle != 3); ins_cost(MEMORY_REF_COST); format %{ "PREFETCH $mem, 2 \t// Prefetch write-many" %} diff --git a/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp b/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp index 61cf73fe04a..4c160929f5a 100644 --- a/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp +++ b/src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp @@ -37,7 +37,6 @@ #include "utilities/copy.hpp" size_t ThreadLocalAllocBuffer::_max_size = 0; -int ThreadLocalAllocBuffer::_reserve_for_allocation_prefetch = 0; unsigned int ThreadLocalAllocBuffer::_target_refills = 0; ThreadLocalAllocBuffer::ThreadLocalAllocBuffer() : @@ -225,30 +224,6 @@ void ThreadLocalAllocBuffer::startup_initialization() { // abort during VM initialization. _target_refills = MAX2(_target_refills, 2U); -#ifdef COMPILER2 - // If the C2 compiler is present, extra space is needed at the end of - // TLABs, otherwise prefetching instructions generated by the C2 - // compiler will fault (due to accessing memory outside of heap). - // The amount of space is the max of the number of lines to - // prefetch for array and for instance allocations. (Extra space must be - // reserved to accommodate both types of allocations.) - // - // Only SPARC-specific BIS instructions are known to fault. (Those - // instructions are generated if AllocatePrefetchStyle==3 and - // AllocatePrefetchInstr==1). To be on the safe side, however, - // extra space is reserved for all combinations of - // AllocatePrefetchStyle and AllocatePrefetchInstr. - // - // If the C2 compiler is not present, no space is reserved. - - // +1 for rounding up to next cache line, +1 to be safe - if (CompilerConfig::is_c2_or_jvmci_compiler_enabled()) { - int lines = MAX2(AllocatePrefetchLines, AllocateInstancePrefetchLines) + 2; - _reserve_for_allocation_prefetch = (AllocatePrefetchDistance + AllocatePrefetchStepSize * lines) / - (int)HeapWordSize; - } -#endif - // During jvm startup, the main thread is initialized // before the heap is initialized. So reinitialize it now. guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread"); @@ -454,8 +429,7 @@ void ThreadLocalAllocStats::publish() { } size_t ThreadLocalAllocBuffer::end_reserve() { - size_t reserve_size = CollectedHeap::lab_alignment_reserve(); - return MAX2(reserve_size, (size_t)_reserve_for_allocation_prefetch); + return CollectedHeap::lab_alignment_reserve(); } size_t ThreadLocalAllocBuffer::estimated_used_bytes() const { diff --git a/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp b/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp index 25d9bf00eac..eb664d13961 100644 --- a/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp +++ b/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp @@ -57,7 +57,6 @@ private: uint64_t _allocated_before_last_gc; // total bytes allocated up until the last gc static size_t _max_size; // maximum size of any TLAB - static int _reserve_for_allocation_prefetch; // Reserve at the end of the TLAB static unsigned _target_refills; // expected number of refills between GCs unsigned _number_of_refills; diff --git a/src/hotspot/share/opto/macro.cpp b/src/hotspot/share/opto/macro.cpp index b9045ddcf4c..6995cacd1b0 100644 --- a/src/hotspot/share/opto/macro.cpp +++ b/src/hotspot/share/opto/macro.cpp @@ -1917,8 +1917,7 @@ Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false, transform_later(cache_adr); cache_adr = new CastP2XNode(needgc_false, cache_adr); transform_later(cache_adr); - // Address is aligned to execute prefetch to the beginning of cache line size - // (it is important when BIS instruction is used on SPARC as prefetch). + // Address is aligned to execute prefetch to the beginning of cache line size. Node* mask = _igvn.MakeConX(~(intptr_t)(step_size-1)); cache_adr = new AndXNode(cache_adr, mask); transform_later(cache_adr); diff --git a/src/hotspot/share/runtime/vmStructs.cpp b/src/hotspot/share/runtime/vmStructs.cpp index 9796b990b2a..93e0ff2f3b6 100644 --- a/src/hotspot/share/runtime/vmStructs.cpp +++ b/src/hotspot/share/runtime/vmStructs.cpp @@ -335,7 +335,6 @@ nonstatic_field(ThreadLocalAllocBuffer, _pf_top, HeapWord*) \ nonstatic_field(ThreadLocalAllocBuffer, _desired_size, size_t) \ nonstatic_field(ThreadLocalAllocBuffer, _refill_waste_limit, size_t) \ - static_field(ThreadLocalAllocBuffer, _reserve_for_allocation_prefetch, int) \ static_field(ThreadLocalAllocBuffer, _target_refills, unsigned) \ nonstatic_field(ThreadLocalAllocBuffer, _number_of_refills, unsigned) \ nonstatic_field(ThreadLocalAllocBuffer, _refill_waste, unsigned) \ diff --git a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ThreadLocalAllocBuffer.java b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ThreadLocalAllocBuffer.java index 11f03a6003e..683e4b67935 100644 --- a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ThreadLocalAllocBuffer.java +++ b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ThreadLocalAllocBuffer.java @@ -76,10 +76,9 @@ public class ThreadLocalAllocBuffer extends VMObject { private long endReserve() { long labAlignmentReserve = VM.getVM().getLabAlignmentReserve(); - long reserveForAllocationPrefetch = VM.getVM().getReserveForAllocationPrefetch(); long heapWordSize = VM.getVM().getHeapWordSize(); - return Math.max(labAlignmentReserve, reserveForAllocationPrefetch) * heapWordSize; + return labAlignmentReserve * heapWordSize; } /** Support for iteration over heap -- not sure how this will diff --git a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/VM.java b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/VM.java index dc27a4fc59e..1607563150a 100644 --- a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/VM.java +++ b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/VM.java @@ -123,7 +123,6 @@ public class VM { private int invocationEntryBCI; private ReversePtrs revPtrs; private VMRegImpl vmregImpl; - private int reserveForAllocationPrefetch; private int labAlignmentReserve; // System.getProperties from debuggee VM @@ -447,8 +446,6 @@ public class VM { boolType = (CIntegerType) db.lookupType("bool"); Type threadLocalAllocBuffer = db.lookupType("ThreadLocalAllocBuffer"); - CIntegerField reserveForAllocationPrefetchField = threadLocalAllocBuffer.getCIntegerField("_reserve_for_allocation_prefetch"); - reserveForAllocationPrefetch = (int)reserveForAllocationPrefetchField.getCInteger(intType); Type collectedHeap = db.lookupType("CollectedHeap"); CIntegerField labAlignmentReserveField = collectedHeap.getCIntegerField("_lab_alignment_reserve"); @@ -915,10 +912,6 @@ public class VM { return vmInternalInfo; } - public int getReserveForAllocationPrefetch() { - return reserveForAllocationPrefetch; - } - public int getLabAlignmentReserve() { return labAlignmentReserve; } diff --git a/test/hotspot/jtreg/ProblemList.txt b/test/hotspot/jtreg/ProblemList.txt index ba63f775223..60d2ffa722f 100644 --- a/test/hotspot/jtreg/ProblemList.txt +++ b/test/hotspot/jtreg/ProblemList.txt @@ -83,6 +83,8 @@ compiler/c2/aarch64/TestStaticCallStub.java 8359963 linux-aarch64,macosx-aarch64 compiler/longcountedloops/TestLoopNestTooManyTraps.java 8376591 generic-all +compiler/unsafe/AlignmentGapAccess.java 8373487 generic-all + ############################################################################# # :hotspot_gc @@ -196,4 +198,3 @@ vmTestbase/nsk/monitoring/ThreadMXBean/findMonitorDeadlockedThreads/find006/Test # in either implementation or test code. ############################################################################# - diff --git a/test/hotspot/jtreg/compiler/c2/TestAllocatePrefetchStyleLargeFlags.java b/test/hotspot/jtreg/compiler/c2/TestAllocatePrefetchStyleLargeFlags.java new file mode 100644 index 00000000000..b455107d5f6 --- /dev/null +++ b/test/hotspot/jtreg/compiler/c2/TestAllocatePrefetchStyleLargeFlags.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2026, 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. + */ + +/* + * @test + * @summary Stress allocation prefetch with large legal AllocatePrefetch* values + * @requires vm.compiler2.enabled + * + * @run main/othervm -Xbatch -XX:-TieredCompilation -XX:+UseTLAB + * -XX:AllocatePrefetchStyle=1 + * -XX:AllocatePrefetchDistance=512 + * -XX:AllocatePrefetchStepSize=512 + * -XX:AllocatePrefetchLines=64 + * -XX:AllocateInstancePrefetchLines=64 + * compiler.c2.TestAllocatePrefetchStyleLargeFlags + * @run main/othervm -Xbatch -XX:-TieredCompilation -XX:+UseTLAB + * -XX:AllocatePrefetchStyle=2 + * -XX:AllocatePrefetchDistance=512 + * -XX:AllocatePrefetchStepSize=512 + * -XX:AllocatePrefetchLines=64 + * -XX:AllocateInstancePrefetchLines=64 + * compiler.c2.TestAllocatePrefetchStyleLargeFlags + * @run main/othervm -Xbatch -XX:-TieredCompilation -XX:+UseTLAB + * -XX:AllocatePrefetchStyle=3 + * -XX:AllocatePrefetchDistance=512 + * -XX:AllocatePrefetchStepSize=512 + * -XX:AllocatePrefetchLines=64 + * -XX:AllocateInstancePrefetchLines=64 + * compiler.c2.TestAllocatePrefetchStyleLargeFlags + */ + +package compiler.c2; + +public class TestAllocatePrefetchStyleLargeFlags { + private static volatile Object sink; + + private static final class Payload { + private final int value; + + private Payload(int value) { + this.value = value; + } + } + + private static Object allocateInstance(int value) { + return new Payload(value); + } + + private static Object allocateArray(int value) { + return new int[value & 31]; + } + + public static void main(String[] args) { + for (int i = 0; i < 50_000; i++) { + sink = allocateInstance(i); + sink = allocateArray(i); + } + } +}