1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary Test custom id generators 27 * @build KullaTesting TestingInputStream 28 * @run testng IdGeneratorTest 29 */ 30 31 import java.io.ByteArrayOutputStream; 32 import java.io.PrintStream; 33 import java.util.List; 34 import java.util.function.Supplier; 35 36 import jdk.jshell.EvalException; 37 import jdk.jshell.JShell; 38 import jdk.jshell.SnippetEvent; 39 import jdk.jshell.UnresolvedReferenceException; 40 import jdk.jshell.VarSnippet; 41 import org.testng.annotations.Test; 42 43 import static org.testng.Assert.assertEquals; 44 import static org.testng.Assert.assertTrue; 45 46 @Test 47 public class IdGeneratorTest { 48 getBuilder()49 public JShell.Builder getBuilder() { 50 TestingInputStream inStream = new TestingInputStream(); 51 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 52 ByteArrayOutputStream errStream = new ByteArrayOutputStream(); 53 return JShell.builder() 54 .in(inStream) 55 .out(new PrintStream(outStream)) 56 .err(new PrintStream(errStream)); 57 } 58 testTempNameGenerator()59 public void testTempNameGenerator() { 60 JShell.Builder builder = getBuilder().tempVariableNameGenerator(new Supplier<String>() { 61 int count = 0; 62 63 @Override 64 public String get() { 65 return "temp" + ++count; 66 } 67 }); 68 try (JShell jShell = builder.build()) { 69 for (int i = 0; i < 3; ++i) { 70 VarSnippet v = (VarSnippet) jShell.eval("2 + " + (i + 1)).get(0).snippet(); 71 assertEquals("temp" + (i + 1), v.name(), "Custom id: "); 72 } 73 } 74 } 75 testResetTempNameGenerator()76 public void testResetTempNameGenerator() { 77 JShell.Builder builder = getBuilder().tempVariableNameGenerator(() -> { 78 throw new AssertionError("Should not be called"); 79 }); 80 try (JShell jShell = builder.tempVariableNameGenerator(null).build()) { 81 jShell.eval("2 + 2"); 82 } 83 } 84 testIdGenerator()85 public void testIdGenerator() { 86 JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id)); 87 try (JShell jShell = builder.build()) { 88 List<SnippetEvent> eval = jShell.eval("int a, b;"); 89 checkIds(eval); 90 checkIds(jShell.drop(eval.get(0).snippet())); 91 } 92 } 93 checkIds(List<SnippetEvent> events)94 private void checkIds(List<SnippetEvent> events) { 95 for (SnippetEvent event : events) { 96 assertTrue(event.snippet().id().startsWith("custom"), "Not started with \"custom\": " 97 + event.snippet().id()); 98 } 99 } 100 testIdInException()101 public void testIdInException() { 102 JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id)); 103 try (JShell jShell = builder.build()) { 104 EvalException evalException = (EvalException) jShell.eval("throw new Error();").get(0).exception(); 105 for (StackTraceElement ste : evalException.getStackTrace()) { 106 assertTrue(ste.getFileName().startsWith("#custom"), "Not started with \"#custom\": " 107 + ste.getFileName()); 108 } 109 jShell.eval("void f() { g(); }"); 110 UnresolvedReferenceException unresolvedException = (UnresolvedReferenceException) jShell.eval("f();").get(0).exception(); 111 for (StackTraceElement ste : unresolvedException.getStackTrace()) { 112 assertTrue(ste.getFileName().startsWith("#custom"), "Not started with \"#custom\": " 113 + ste.getFileName()); 114 } 115 } 116 } 117 testResetIdGenerator()118 public void testResetIdGenerator() { 119 JShell.Builder builder = getBuilder().idGenerator((sn, id) -> { 120 throw new AssertionError("Should not be called"); 121 }); 122 try (JShell jShell = builder.idGenerator(null).build()) { 123 jShell.eval("2 + 2"); 124 } 125 } 126 } 127