33362
|
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.PersistentSnippet;
|
|
39 |
import jdk.jshell.SnippetEvent;
|
|
40 |
import jdk.jshell.UnresolvedReferenceException;
|
|
41 |
import jdk.jshell.VarSnippet;
|
|
42 |
import org.testng.annotations.Test;
|
|
43 |
|
|
44 |
import static org.testng.Assert.assertEquals;
|
|
45 |
import static org.testng.Assert.assertTrue;
|
|
46 |
|
|
47 |
@Test
|
|
48 |
public class IdGeneratorTest {
|
|
49 |
|
|
50 |
public JShell.Builder getBuilder() {
|
|
51 |
TestingInputStream inStream = new TestingInputStream();
|
|
52 |
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
53 |
ByteArrayOutputStream errStream = new ByteArrayOutputStream();
|
|
54 |
return JShell.builder()
|
|
55 |
.in(inStream)
|
|
56 |
.out(new PrintStream(outStream))
|
|
57 |
.err(new PrintStream(errStream));
|
|
58 |
}
|
|
59 |
|
|
60 |
public void testTempNameGenerator() {
|
|
61 |
JShell.Builder builder = getBuilder().tempVariableNameGenerator(new Supplier<String>() {
|
|
62 |
int count = 0;
|
|
63 |
|
|
64 |
@Override
|
|
65 |
public String get() {
|
|
66 |
return "temp" + ++count;
|
|
67 |
}
|
|
68 |
});
|
|
69 |
try (JShell jShell = builder.build()) {
|
|
70 |
for (int i = 0; i < 3; ++i) {
|
|
71 |
VarSnippet v = (VarSnippet) jShell.eval("2 + " + (i + 1)).get(0).snippet();
|
|
72 |
assertEquals("temp" + (i + 1), v.name(), "Custom id: ");
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
public void testResetTempNameGenerator() {
|
|
78 |
JShell.Builder builder = getBuilder().tempVariableNameGenerator(() -> {
|
|
79 |
throw new AssertionError("Should not be called");
|
|
80 |
});
|
|
81 |
try (JShell jShell = builder.tempVariableNameGenerator(null).build()) {
|
|
82 |
jShell.eval("2 + 2");
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
public void testIdGenerator() {
|
|
87 |
JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id));
|
|
88 |
try (JShell jShell = builder.build()) {
|
|
89 |
List<SnippetEvent> eval = jShell.eval("int a, b;");
|
|
90 |
checkIds(eval);
|
|
91 |
checkIds(jShell.drop((PersistentSnippet) eval.get(0).snippet()));
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
private void checkIds(List<SnippetEvent> events) {
|
|
96 |
for (SnippetEvent event : events) {
|
|
97 |
assertTrue(event.snippet().id().startsWith("custom"), "Not started with \"custom\": "
|
|
98 |
+ event.snippet().id());
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
@Test(enabled = false) // TODO 8133507
|
|
103 |
public void testIdInException() {
|
|
104 |
JShell.Builder builder = getBuilder().idGenerator(((snippet, id) -> "custom" + id));
|
|
105 |
try (JShell jShell = builder.build()) {
|
|
106 |
EvalException evalException = (EvalException) jShell.eval("throw new Error();").get(0).exception();
|
|
107 |
for (StackTraceElement ste : evalException.getStackTrace()) {
|
|
108 |
assertTrue(ste.getFileName().startsWith("custom"), "Not started with \"custom\": "
|
|
109 |
+ ste.getFileName());
|
|
110 |
}
|
|
111 |
jShell.eval("void f() { g(); }");
|
|
112 |
UnresolvedReferenceException unresolvedException = (UnresolvedReferenceException) jShell.eval("f();").get(0).exception();
|
|
113 |
for (StackTraceElement ste : unresolvedException.getStackTrace()) {
|
|
114 |
assertTrue(ste.getFileName().startsWith("custom"), "Not started with \"custom\": "
|
|
115 |
+ ste.getFileName());
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
public void testResetIdGenerator() {
|
|
121 |
JShell.Builder builder = getBuilder().idGenerator((sn, id) -> {
|
|
122 |
throw new AssertionError("Should not be called");
|
|
123 |
});
|
|
124 |
try (JShell jShell = builder.idGenerator(null).build()) {
|
|
125 |
jShell.eval("2 + 2");
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|