Path: blob/master/test/jdk/java/foreign/TestUpcallException.java
66643 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"26* @library /test/lib27* @modules jdk.incubator.foreign/jdk.internal.foreign28* @build ThrowingUpcall TestUpcallException29*30* @run testng/othervm/native31* --enable-native-access=ALL-UNNAMED32* TestUpcallException33*/3435import jdk.incubator.foreign.CLinker;36import jdk.incubator.foreign.FunctionDescriptor;37import jdk.incubator.foreign.ResourceScope;38import jdk.test.lib.Utils;39import org.testng.annotations.Test;4041import java.io.BufferedReader;42import java.io.IOException;43import java.io.InputStream;44import java.io.InputStreamReader;45import java.nio.file.Paths;46import java.util.List;4748import static org.testng.Assert.assertFalse;49import static org.testng.Assert.assertNotEquals;50import static org.testng.Assert.assertTrue;5152public class TestUpcallException {5354@Test55public void testExceptionInterpreted() throws InterruptedException, IOException {56boolean useSpec = false;57run(useSpec);58}5960@Test61public void testExceptionSpecialized() throws IOException, InterruptedException {62boolean useSpec = true;63run(useSpec);64}6566private void run(boolean useSpec) throws IOException, InterruptedException {67Process process = new ProcessBuilder()68.command(69Paths.get(Utils.TEST_JDK)70.resolve("bin")71.resolve("java")72.toAbsolutePath()73.toString(),74"--add-modules", "jdk.incubator.foreign",75"--enable-native-access=ALL-UNNAMED",76"-Djava.library.path=" + System.getProperty("java.library.path"),77"-Djdk.internal.foreign.ProgrammableUpcallHandler.USE_SPEC=" + useSpec,78"-cp", Utils.TEST_CLASS_PATH,79"ThrowingUpcall")80.start();8182int result = process.waitFor();83assertNotEquals(result, 0);8485List<String> outLines = linesFromStream(process.getInputStream());86outLines.forEach(System.out::println);87List<String> errLines = linesFromStream(process.getErrorStream());88errLines.forEach(System.err::println);8990// Exception message would be found in stack trace91String shouldInclude = "Testing upcall exceptions";92assertTrue(linesContain(errLines, shouldInclude), "Did not find '" + shouldInclude + "' in stderr");93}9495private boolean linesContain(List<String> errLines, String shouldInclude) {96return errLines.stream().anyMatch(line -> line.contains(shouldInclude));97}9899private static List<String> linesFromStream(InputStream stream) throws IOException {100try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {101return reader.lines().toList();102}103}104}105106107