mirror of
https://github.com/openjdk/jdk.git
synced 2026-07-28 03:43:21 +00:00
8353332: Test jdk/jshell/ToolProviderTest.java failed in relation to enable-preview
Reviewed-by: jpai, asotona
This commit is contained in:
parent
fda5eecd67
commit
9088afc4f1
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 2025, 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
|
||||
@ -31,6 +31,7 @@ import java.io.PrintStream;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.tools.Tool;
|
||||
import jdk.jshell.tool.JavaShellToolBuilder;
|
||||
@ -40,6 +41,10 @@ import jdk.jshell.tool.JavaShellToolBuilder;
|
||||
*/
|
||||
public class JShellToolProvider implements Tool {
|
||||
|
||||
//for tests, so that they can tweak the builder (typically set persistence):
|
||||
private static Function<JavaShellToolBuilder, JavaShellToolBuilder> augmentToolBuilder =
|
||||
builder -> builder;
|
||||
|
||||
/**
|
||||
* Returns the name of this Java shell tool provider.
|
||||
*
|
||||
@ -86,11 +91,12 @@ public class JShellToolProvider implements Tool {
|
||||
? (PrintStream) err
|
||||
: new PrintStream(err);
|
||||
try {
|
||||
return JavaShellToolBuilder
|
||||
.builder()
|
||||
.in(xin, null)
|
||||
.out(xout)
|
||||
.err(xerr)
|
||||
return augmentToolBuilder.apply(
|
||||
JavaShellToolBuilder
|
||||
.builder()
|
||||
.in(xin, null)
|
||||
.out(xout)
|
||||
.err(xerr))
|
||||
.start(arguments);
|
||||
} catch (Throwable ex) {
|
||||
xerr.println(ex.getMessage());
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 2025, 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,20 +21,25 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import javax.tools.Tool;
|
||||
import jdk.internal.jshell.tool.JShellToolProvider;
|
||||
import jdk.jshell.tool.JavaShellToolBuilder;
|
||||
import org.testng.annotations.Test;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8170044 8171343 8179856 8185840 8190383
|
||||
* @bug 8170044 8171343 8179856 8185840 8190383 8353332
|
||||
* @summary Test ServiceLoader launching of jshell tool
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
* jdk.compiler/com.sun.tools.javac.main
|
||||
* jdk.jdeps/com.sun.tools.javap
|
||||
* jdk.jshell/jdk.internal.jshell.tool
|
||||
* jdk.jshell/jdk.internal.jshell.tool:+open
|
||||
* @library /tools/lib
|
||||
* @build Compiler toolbox.ToolBox
|
||||
* @run testng ToolProviderTest
|
||||
@ -68,13 +73,20 @@ public class ToolProviderTest extends StartOptionTest {
|
||||
|
||||
@Override
|
||||
protected int runShell(String... args) {
|
||||
ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
|
||||
for (Tool provider : sl) {
|
||||
if (provider.name().equals("jshell")) {
|
||||
return provider.run(cmdInStream, cmdout, cmderr, args);
|
||||
//make sure the JShell running during the test is not using persisted preferences from the machine:
|
||||
Function<JavaShellToolBuilder, JavaShellToolBuilder> prevAugmentedToolBuilder =
|
||||
getAndSetAugmentedToolBuilder(builder -> builder.persistence(new HashMap<>()));
|
||||
try {
|
||||
ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
|
||||
for (Tool provider : sl) {
|
||||
if (provider.name().equals("jshell")) {
|
||||
return provider.run(cmdInStream, cmdout, cmderr, args);
|
||||
}
|
||||
}
|
||||
throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
|
||||
} finally {
|
||||
getAndSetAugmentedToolBuilder(prevAugmentedToolBuilder);
|
||||
}
|
||||
throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
|
||||
}
|
||||
|
||||
// Test --show-version
|
||||
@ -87,4 +99,22 @@ public class ToolProviderTest extends StartOptionTest {
|
||||
},
|
||||
"--show-version");
|
||||
}
|
||||
|
||||
private Function<JavaShellToolBuilder, JavaShellToolBuilder> getAndSetAugmentedToolBuilder
|
||||
(Function<JavaShellToolBuilder, JavaShellToolBuilder> augmentToolBuilder) {
|
||||
try {
|
||||
Field f = JShellToolProvider.class.getDeclaredField("augmentToolBuilder");
|
||||
|
||||
f.setAccessible(true);
|
||||
|
||||
Function<JavaShellToolBuilder, JavaShellToolBuilder> prev =
|
||||
(Function<JavaShellToolBuilder, JavaShellToolBuilder>) f.get(null);
|
||||
|
||||
f.set(null, augmentToolBuilder);
|
||||
|
||||
return prev;
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user