Skip to content

8353332: Test jdk/jshell/ToolProviderTest.java failed in relation to enable-preview #24492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,7 @@
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;
Expand All @@ -40,6 +41,10 @@
*/
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.
*
Expand Down Expand Up @@ -86,11 +91,12 @@ public int run(InputStream in, OutputStream out, OutputStream err, String... arg
? (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());
Expand Down
46 changes: 38 additions & 8 deletions test/langtools/jdk/jshell/ToolProviderTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -68,13 +73,20 @@ protected void startCheckUserOutput(Consumer<String> checkUserOutput, String...

@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
Expand All @@ -87,4 +99,22 @@ public void testShowVersion() {
},
"--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);
}
}
}