Path: blob/master/test/langtools/jdk/jshell/IdGeneratorTest.java
40931 views
/*1* Copyright (c) 2015, 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* @summary Test custom id generators26* @build KullaTesting TestingInputStream27* @run testng IdGeneratorTest28*/2930import java.io.ByteArrayOutputStream;31import java.io.PrintStream;32import java.util.List;33import java.util.function.Supplier;3435import jdk.jshell.EvalException;36import jdk.jshell.JShell;37import jdk.jshell.SnippetEvent;38import jdk.jshell.UnresolvedReferenceException;39import jdk.jshell.VarSnippet;40import org.testng.annotations.Test;4142import static org.testng.Assert.assertEquals;43import static org.testng.Assert.assertTrue;4445@Test46public class IdGeneratorTest {4748public JShell.Builder getBuilder() {49TestingInputStream inStream = new TestingInputStream();50ByteArrayOutputStream outStream = new ByteArrayOutputStream();51ByteArrayOutputStream errStream = new ByteArrayOutputStream();52return JShell.builder()53.in(inStream)54.out(new PrintStream(outStream))55.err(new PrintStream(errStream));56}5758public void testTempNameGenerator() {59JShell.Builder builder = getBuilder().tempVariableNameGenerator(new Supplier<String>() {60int count = 0;6162@Override63public String get() {64return "temp" + ++count;65}66});67try (JShell jShell = builder.build()) {68for (int i = 0; i < 3; ++i) {69VarSnippet v = (VarSnippet) jShell.eval("2 + " + (i + 1)).get(0).snippet();70assertEquals("temp" + (i + 1), v.name(), "Custom id: ");71}72}73}7475public void testResetTempNameGenerator() {76JShell.Builder builder = getBuilder().tempVariableNameGenerator(() -> {77throw new AssertionError("Should not be called");78});79try (JShell jShell = builder.tempVariableNameGenerator(null).build()) {80jShell.eval("2 + 2");81}82}8384public void testIdGenerator() {85JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id));86try (JShell jShell = builder.build()) {87List<SnippetEvent> eval = jShell.eval("int a, b;");88checkIds(eval);89checkIds(jShell.drop(eval.get(0).snippet()));90}91}9293private void checkIds(List<SnippetEvent> events) {94for (SnippetEvent event : events) {95assertTrue(event.snippet().id().startsWith("custom"), "Not started with \"custom\": "96+ event.snippet().id());97}98}99100public void testIdInException() {101JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id));102try (JShell jShell = builder.build()) {103EvalException evalException = (EvalException) jShell.eval("throw new Error();").get(0).exception();104for (StackTraceElement ste : evalException.getStackTrace()) {105assertTrue(ste.getFileName().startsWith("#custom"), "Not started with \"#custom\": "106+ ste.getFileName());107}108jShell.eval("void f() { g(); }");109UnresolvedReferenceException unresolvedException = (UnresolvedReferenceException) jShell.eval("f();").get(0).exception();110for (StackTraceElement ste : unresolvedException.getStackTrace()) {111assertTrue(ste.getFileName().startsWith("#custom"), "Not started with \"#custom\": "112+ ste.getFileName());113}114}115}116117public void testResetIdGenerator() {118JShell.Builder builder = getBuilder().idGenerator((sn, id) -> {119throw new AssertionError("Should not be called");120});121try (JShell jShell = builder.idGenerator(null).build()) {122jShell.eval("2 + 2");123}124}125}126127128