IdGeneratorTest.java revision 3695:40468274ff3b
1178476Sjb/* 2178476Sjb * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3178476Sjb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4178476Sjb * 5178476Sjb * This code is free software; you can redistribute it and/or modify it 6178476Sjb * under the terms of the GNU General Public License version 2 only, as 7178476Sjb * published by the Free Software Foundation. 8178476Sjb * 9178476Sjb * This code is distributed in the hope that it will be useful, but WITHOUT 10178476Sjb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11178476Sjb * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12178476Sjb * version 2 for more details (a copy is included in the LICENSE file that 13178476Sjb * accompanied this code). 14178476Sjb * 15178476Sjb * You should have received a copy of the GNU General Public License version 16178476Sjb * 2 along with this work; if not, write to the Free Software Foundation, 17178476Sjb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18178476Sjb * 19178476Sjb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20178476Sjb * or visit www.oracle.com if you need additional information or have any 21178476Sjb * questions. 22178476Sjb */ 23178476Sjb 24178476Sjb/* 25178476Sjb * @test 26178476Sjb * @summary Test custom id generators 27178476Sjb * @build KullaTesting TestingInputStream 28178476Sjb * @run testng IdGeneratorTest 29178476Sjb */ 30178476Sjb 31178476Sjbimport java.io.ByteArrayOutputStream; 32178476Sjbimport java.io.PrintStream; 33178476Sjbimport java.util.List; 34178476Sjbimport java.util.function.Supplier; 35178476Sjb 36178476Sjbimport jdk.jshell.EvalException; 37178476Sjbimport jdk.jshell.JShell; 38178476Sjbimport jdk.jshell.SnippetEvent; 39178476Sjbimport jdk.jshell.UnresolvedReferenceException; 40178476Sjbimport jdk.jshell.VarSnippet; 41178476Sjbimport org.testng.annotations.Test; 42178476Sjb 43178476Sjbimport static org.testng.Assert.assertEquals; 44178476Sjbimport static org.testng.Assert.assertTrue; 45178476Sjb 46178476Sjb@Test 47178476Sjbpublic class IdGeneratorTest { 48178476Sjb 49178476Sjb public JShell.Builder getBuilder() { 50178476Sjb TestingInputStream inStream = new TestingInputStream(); 51178476Sjb ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 52178476Sjb ByteArrayOutputStream errStream = new ByteArrayOutputStream(); 53178476Sjb return JShell.builder() 54178476Sjb .in(inStream) 55178476Sjb .out(new PrintStream(outStream)) 56178476Sjb .err(new PrintStream(errStream)); 57178476Sjb } 58178476Sjb 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 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 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 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 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 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