author | jlahoda |
Tue, 18 Oct 2016 16:00:32 +0200 | |
changeset 41628 | 664e7664343d |
parent 41514 | a75c2b869d8d |
child 41858 | 5843b57ce3a6 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2014, 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 |
||
41628 | 24 |
import java.io.ByteArrayInputStream; |
33362 | 25 |
import java.io.ByteArrayOutputStream; |
41628 | 26 |
import java.io.IOException; |
27 |
import java.io.InputStream; |
|
33362 | 28 |
import java.io.PrintStream; |
29 |
import java.io.StringWriter; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
30 |
import java.lang.reflect.Method; |
33362 | 31 |
import java.nio.file.Path; |
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Collection; |
|
35 |
import java.util.Collections; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.util.LinkedHashMap; |
|
38 |
import java.util.LinkedHashSet; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.Set; |
|
42 |
import java.util.TreeMap; |
|
39369 | 43 |
import java.util.function.Consumer; |
33362 | 44 |
import java.util.function.Predicate; |
45 |
import java.util.function.Supplier; |
|
46 |
import java.util.stream.Collectors; |
|
47 |
import java.util.stream.Stream; |
|
48 |
||
49 |
import javax.tools.Diagnostic; |
|
50 |
||
51 |
import jdk.jshell.EvalException; |
|
52 |
import jdk.jshell.JShell; |
|
53 |
import jdk.jshell.JShell.Subscription; |
|
54 |
import jdk.jshell.Snippet; |
|
55 |
import jdk.jshell.DeclarationSnippet; |
|
56 |
import jdk.jshell.ExpressionSnippet; |
|
57 |
import jdk.jshell.ImportSnippet; |
|
58 |
import jdk.jshell.Snippet.Kind; |
|
59 |
import jdk.jshell.MethodSnippet; |
|
60 |
import jdk.jshell.Snippet.Status; |
|
61 |
import jdk.jshell.Snippet.SubKind; |
|
62 |
import jdk.jshell.TypeDeclSnippet; |
|
63 |
import jdk.jshell.VarSnippet; |
|
64 |
import jdk.jshell.SnippetEvent; |
|
65 |
import jdk.jshell.SourceCodeAnalysis; |
|
66 |
import jdk.jshell.SourceCodeAnalysis.CompletionInfo; |
|
67 |
import jdk.jshell.SourceCodeAnalysis.Completeness; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
68 |
import jdk.jshell.SourceCodeAnalysis.QualifiedNames; |
33362 | 69 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
70 |
import jdk.jshell.UnresolvedReferenceException; |
|
71 |
import org.testng.annotations.AfterMethod; |
|
72 |
import org.testng.annotations.BeforeMethod; |
|
73 |
||
74 |
import jdk.jshell.Diag; |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
75 |
import static java.util.stream.Collectors.toList; |
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
76 |
import static java.util.stream.Collectors.toSet; |
33362 | 77 |
import static jdk.jshell.Snippet.Status.*; |
78 |
import static org.testng.Assert.*; |
|
79 |
import static jdk.jshell.Snippet.SubKind.METHOD_SUBKIND; |
|
80 |
||
81 |
public class KullaTesting { |
|
82 |
||
83 |
public static final String IGNORE_VALUE = "<ignore-value>"; |
|
84 |
public static final Class<? extends Throwable> IGNORE_EXCEPTION = (new Throwable() {}).getClass(); |
|
85 |
public static final Snippet MAIN_SNIPPET; |
|
86 |
||
87 |
private SourceCodeAnalysis analysis = null; |
|
88 |
private JShell state = null; |
|
41628 | 89 |
private InputStream inStream = null; |
33362 | 90 |
private ByteArrayOutputStream outStream = null; |
91 |
private ByteArrayOutputStream errStream = null; |
|
92 |
||
93 |
private Map<String, Snippet> idToSnippet = new LinkedHashMap<>(); |
|
94 |
private Set<Snippet> allSnippets = new LinkedHashSet<>(); |
|
95 |
private List<String> classpath; |
|
96 |
||
97 |
static { |
|
98 |
JShell js = JShell.create(); |
|
99 |
MAIN_SNIPPET = js.eval("MAIN_SNIPPET").get(0).snippet(); |
|
100 |
js.close(); |
|
101 |
assertTrue(MAIN_SNIPPET != null, "Bad MAIN_SNIPPET set-up -- must not be null"); |
|
102 |
} |
|
103 |
||
104 |
public enum DiagCheck { |
|
105 |
DIAG_OK, |
|
106 |
DIAG_WARNING, |
|
107 |
DIAG_ERROR, |
|
108 |
DIAG_IGNORE |
|
109 |
} |
|
110 |
||
111 |
public void setInput(String s) { |
|
41628 | 112 |
setInput(new ByteArrayInputStream(s.getBytes())); |
113 |
} |
|
114 |
||
115 |
public void setInput(InputStream in) { |
|
116 |
inStream = in; |
|
33362 | 117 |
} |
118 |
||
119 |
public String getOutput() { |
|
120 |
String s = outStream.toString(); |
|
121 |
outStream.reset(); |
|
122 |
return s; |
|
123 |
} |
|
124 |
||
125 |
public String getErrorOutput() { |
|
126 |
String s = errStream.toString(); |
|
127 |
errStream.reset(); |
|
128 |
return s; |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* @return the analysis |
|
133 |
*/ |
|
134 |
public SourceCodeAnalysis getAnalysis() { |
|
135 |
if (analysis == null) { |
|
136 |
analysis = state.sourceCodeAnalysis(); |
|
137 |
} |
|
138 |
return analysis; |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* @return the state |
|
143 |
*/ |
|
144 |
public JShell getState() { |
|
145 |
return state; |
|
146 |
} |
|
147 |
||
148 |
public List<Snippet> getActiveKeys() { |
|
149 |
return allSnippets.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
150 |
.filter(k -> getState().status(k).isActive()) |
33362 | 151 |
.collect(Collectors.toList()); |
152 |
} |
|
153 |
||
154 |
public void addToClasspath(String path) { |
|
155 |
classpath.add(path); |
|
156 |
getState().addToClasspath(path); |
|
157 |
} |
|
158 |
||
159 |
public void addToClasspath(Path path) { |
|
160 |
addToClasspath(path.toString()); |
|
161 |
} |
|
162 |
||
163 |
@BeforeMethod |
|
164 |
public void setUp() { |
|
39369 | 165 |
setUp(b -> {}); |
166 |
} |
|
167 |
||
168 |
public void setUp(Consumer<JShell.Builder> bc) { |
|
41628 | 169 |
InputStream in = new InputStream() { |
170 |
@Override |
|
171 |
public int read() throws IOException { |
|
172 |
assertNotNull(inStream); |
|
173 |
return inStream.read(); |
|
174 |
} |
|
175 |
@Override |
|
176 |
public int read(byte[] b) throws IOException { |
|
177 |
assertNotNull(inStream); |
|
178 |
return inStream.read(b); |
|
179 |
} |
|
180 |
@Override |
|
181 |
public int read(byte[] b, int off, int len) throws IOException { |
|
182 |
assertNotNull(inStream); |
|
183 |
return inStream.read(b, off, len); |
|
184 |
} |
|
185 |
}; |
|
33362 | 186 |
outStream = new ByteArrayOutputStream(); |
187 |
errStream = new ByteArrayOutputStream(); |
|
39369 | 188 |
JShell.Builder builder = JShell.builder() |
41628 | 189 |
.in(in) |
33362 | 190 |
.out(new PrintStream(outStream)) |
39369 | 191 |
.err(new PrintStream(errStream)); |
192 |
bc.accept(builder); |
|
193 |
state = builder.build(); |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
194 |
allSnippets = new LinkedHashSet<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
195 |
idToSnippet = new LinkedHashMap<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
196 |
classpath = new ArrayList<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
197 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37644
diff
changeset
|
198 |
|
33362 | 199 |
@AfterMethod |
200 |
public void tearDown() { |
|
201 |
if (state != null) state.close(); |
|
202 |
state = null; |
|
203 |
analysis = null; |
|
204 |
allSnippets = null; |
|
205 |
idToSnippet = null; |
|
206 |
classpath = null; |
|
207 |
} |
|
208 |
||
209 |
public List<String> assertUnresolvedDependencies(DeclarationSnippet key, int unresolvedSize) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
210 |
List<String> unresolved = getState().unresolvedDependencies(key).collect(toList()); |
33362 | 211 |
assertEquals(unresolved.size(), unresolvedSize, "Input: " + key.source() + ", checking unresolved: "); |
212 |
return unresolved; |
|
213 |
} |
|
214 |
||
215 |
public DeclarationSnippet assertUnresolvedDependencies1(DeclarationSnippet key, Status status, String name) { |
|
216 |
List<String> unresolved = assertUnresolvedDependencies(key, 1); |
|
217 |
String input = key.source(); |
|
218 |
assertEquals(unresolved.size(), 1, "Given input: " + input + ", checking unresolved"); |
|
219 |
assertEquals(unresolved.get(0), name, "Given input: " + input + ", checking unresolved: "); |
|
220 |
assertEquals(getState().status(key), status, "Given input: " + input + ", checking status: "); |
|
221 |
return key; |
|
222 |
} |
|
223 |
||
36499 | 224 |
public DeclarationSnippet assertEvalUnresolvedException(String input, String name, int unresolvedSize, int diagnosticsSize) { |
33362 | 225 |
List<SnippetEvent> events = assertEval(input, null, UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
226 |
SnippetEvent ste = events.get(0); |
|
36499 | 227 |
DeclarationSnippet sn = ((UnresolvedReferenceException) ste.exception()).getSnippet(); |
228 |
assertEquals(sn.name(), name, "Given input: " + input + ", checking name"); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
229 |
assertEquals(getState().unresolvedDependencies(sn).count(), unresolvedSize, "Given input: " + input + ", checking unresolved"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
230 |
assertEquals(getState().diagnostics(sn).count(), (long) diagnosticsSize, "Given input: " + input + ", checking diagnostics"); |
36499 | 231 |
return sn; |
33362 | 232 |
} |
233 |
||
234 |
public Snippet assertKeyMatch(String input, boolean isExecutable, SubKind expectedSubKind, STEInfo mainInfo, STEInfo... updates) { |
|
235 |
Snippet key = key(assertEval(input, IGNORE_VALUE, mainInfo, updates)); |
|
236 |
String source = key.source(); |
|
237 |
assertEquals(source, input, "Key \"" + input + "\" source mismatch, got: " + source + ", expected: " + input); |
|
238 |
SubKind subkind = key.subKind(); |
|
239 |
assertEquals(subkind, expectedSubKind, "Key \"" + input + "\" subkind mismatch, got: " |
|
240 |
+ subkind + ", expected: " + expectedSubKind); |
|
241 |
assertEquals(subkind.isExecutable(), isExecutable, "Key \"" + input + "\", expected isExecutable: " |
|
242 |
+ isExecutable + ", got: " + subkind.isExecutable()); |
|
243 |
Snippet.Kind expectedKind = getKind(key); |
|
244 |
assertEquals(key.kind(), expectedKind, "Checking kind: "); |
|
245 |
assertEquals(expectedSubKind.kind(), expectedKind, "Checking kind: "); |
|
246 |
return key; |
|
247 |
} |
|
248 |
||
249 |
private Kind getKind(Snippet key) { |
|
250 |
SubKind expectedSubKind = key.subKind(); |
|
251 |
Kind expectedKind; |
|
252 |
switch (expectedSubKind) { |
|
253 |
case SINGLE_TYPE_IMPORT_SUBKIND: |
|
254 |
case SINGLE_STATIC_IMPORT_SUBKIND: |
|
255 |
case TYPE_IMPORT_ON_DEMAND_SUBKIND: |
|
256 |
case STATIC_IMPORT_ON_DEMAND_SUBKIND: |
|
257 |
expectedKind = Kind.IMPORT; |
|
258 |
break; |
|
259 |
case CLASS_SUBKIND: |
|
260 |
case INTERFACE_SUBKIND: |
|
261 |
case ENUM_SUBKIND: |
|
262 |
case ANNOTATION_TYPE_SUBKIND: |
|
263 |
expectedKind = Kind.TYPE_DECL; |
|
264 |
break; |
|
265 |
case METHOD_SUBKIND: |
|
266 |
expectedKind = Kind.METHOD; |
|
267 |
break; |
|
268 |
case VAR_DECLARATION_SUBKIND: |
|
269 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
270 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: |
|
271 |
expectedKind = Kind.VAR; |
|
272 |
break; |
|
273 |
case VAR_VALUE_SUBKIND: |
|
274 |
case ASSIGNMENT_SUBKIND: |
|
275 |
expectedKind = Kind.EXPRESSION; |
|
276 |
break; |
|
277 |
case STATEMENT_SUBKIND: |
|
278 |
expectedKind = Kind.STATEMENT; |
|
279 |
break; |
|
280 |
case UNKNOWN_SUBKIND: |
|
281 |
expectedKind = Kind.ERRONEOUS; |
|
282 |
break; |
|
283 |
default: |
|
284 |
throw new AssertionError("Unsupported key: " + key.getClass().getCanonicalName()); |
|
285 |
} |
|
286 |
return expectedKind; |
|
287 |
} |
|
288 |
||
289 |
public ImportSnippet assertImportKeyMatch(String input, String name, SubKind subkind, STEInfo mainInfo, STEInfo... updates) { |
|
290 |
Snippet key = assertKeyMatch(input, false, subkind, mainInfo, updates); |
|
291 |
||
292 |
assertTrue(key instanceof ImportSnippet, "Expected an ImportKey, got: " + key.getClass().getName()); |
|
293 |
ImportSnippet importKey = (ImportSnippet) key; |
|
294 |
assertEquals(importKey.name(), name, "Input \"" + input + |
|
295 |
"\" name mismatch, got: " + importKey.name() + ", expected: " + name); |
|
296 |
assertEquals(importKey.kind(), Kind.IMPORT, "Checking kind: "); |
|
297 |
return importKey; |
|
298 |
} |
|
299 |
||
300 |
public DeclarationSnippet assertDeclarationKeyMatch(String input, boolean isExecutable, String name, SubKind subkind, STEInfo mainInfo, STEInfo... updates) { |
|
301 |
Snippet key = assertKeyMatch(input, isExecutable, subkind, mainInfo, updates); |
|
302 |
||
303 |
assertTrue(key instanceof DeclarationSnippet, "Expected a DeclarationKey, got: " + key.getClass().getName()); |
|
304 |
DeclarationSnippet declKey = (DeclarationSnippet) key; |
|
305 |
assertEquals(declKey.name(), name, "Input \"" + input + |
|
306 |
"\" name mismatch, got: " + declKey.name() + ", expected: " + name); |
|
307 |
return declKey; |
|
308 |
} |
|
309 |
||
310 |
public VarSnippet assertVarKeyMatch(String input, boolean isExecutable, String name, SubKind kind, String typeName, STEInfo mainInfo, STEInfo... updates) { |
|
311 |
Snippet sn = assertDeclarationKeyMatch(input, isExecutable, name, kind, mainInfo, updates); |
|
312 |
assertTrue(sn instanceof VarSnippet, "Expected a VarKey, got: " + sn.getClass().getName()); |
|
313 |
VarSnippet variableKey = (VarSnippet) sn; |
|
314 |
String signature = variableKey.typeName(); |
|
315 |
assertEquals(signature, typeName, "Key \"" + input + |
|
316 |
"\" typeName mismatch, got: " + signature + ", expected: " + typeName); |
|
317 |
assertEquals(variableKey.kind(), Kind.VAR, "Checking kind: "); |
|
318 |
return variableKey; |
|
319 |
} |
|
320 |
||
321 |
public void assertExpressionKeyMatch(String input, String name, SubKind kind, String typeName) { |
|
322 |
Snippet key = assertKeyMatch(input, true, kind, added(VALID)); |
|
323 |
assertTrue(key instanceof ExpressionSnippet, "Expected a ExpressionKey, got: " + key.getClass().getName()); |
|
324 |
ExpressionSnippet exprKey = (ExpressionSnippet) key; |
|
325 |
assertEquals(exprKey.name(), name, "Input \"" + input + |
|
326 |
"\" name mismatch, got: " + exprKey.name() + ", expected: " + name); |
|
327 |
assertEquals(exprKey.typeName(), typeName, "Key \"" + input + |
|
328 |
"\" typeName mismatch, got: " + exprKey.typeName() + ", expected: " + typeName); |
|
329 |
assertEquals(exprKey.kind(), Kind.EXPRESSION, "Checking kind: "); |
|
330 |
} |
|
331 |
||
332 |
// For expressions throwing an EvalException |
|
333 |
public SnippetEvent assertEvalException(String input) { |
|
334 |
List<SnippetEvent> events = assertEval(input, null, EvalException.class, |
|
335 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
|
336 |
return events.get(0); |
|
337 |
} |
|
338 |
||
339 |
||
340 |
public List<SnippetEvent> assertEvalFail(String input) { |
|
341 |
return assertEval(input, null, null, |
|
342 |
DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, added(REJECTED)); |
|
343 |
} |
|
344 |
||
345 |
public List<SnippetEvent> assertEval(String input) { |
|
346 |
return assertEval(input, IGNORE_VALUE, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, added(VALID)); |
|
347 |
} |
|
348 |
||
349 |
public List<SnippetEvent> assertEval(String input, String value) { |
|
350 |
return assertEval(input, value, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, added(VALID)); |
|
351 |
} |
|
352 |
||
353 |
public List<SnippetEvent> assertEval(String input, STEInfo mainInfo, STEInfo... updates) { |
|
354 |
return assertEval(input, IGNORE_VALUE, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
|
355 |
} |
|
356 |
||
357 |
public List<SnippetEvent> assertEval(String input, String value, |
|
358 |
STEInfo mainInfo, STEInfo... updates) { |
|
359 |
return assertEval(input, value, null, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
|
360 |
} |
|
361 |
||
362 |
public List<SnippetEvent> assertEval(String input, DiagCheck diagMain, DiagCheck diagUpdates) { |
|
363 |
return assertEval(input, IGNORE_VALUE, null, diagMain, diagUpdates, added(VALID)); |
|
364 |
} |
|
365 |
||
366 |
public List<SnippetEvent> assertEval(String input, DiagCheck diagMain, DiagCheck diagUpdates, |
|
367 |
STEInfo mainInfo, STEInfo... updates) { |
|
368 |
return assertEval(input, IGNORE_VALUE, null, diagMain, diagUpdates, mainInfo, updates); |
|
369 |
} |
|
370 |
||
371 |
public List<SnippetEvent> assertEval(String input, |
|
372 |
String value, Class<? extends Throwable> exceptionClass, |
|
373 |
DiagCheck diagMain, DiagCheck diagUpdates, |
|
374 |
STEInfo mainInfo, STEInfo... updates) { |
|
375 |
return assertEval(input, diagMain, diagUpdates, new EventChain(mainInfo, value, exceptionClass, updates)); |
|
376 |
} |
|
377 |
||
378 |
// Use this directly or usually indirectly for all non-empty calls to eval() |
|
379 |
public List<SnippetEvent> assertEval(String input, |
|
380 |
DiagCheck diagMain, DiagCheck diagUpdates, EventChain... eventChains) { |
|
381 |
return checkEvents(() -> getState().eval(input), "eval(" + input + ")", diagMain, diagUpdates, eventChains); |
|
382 |
} |
|
383 |
||
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
384 |
<T> void assertStreamMatch(Stream<T> result, T... expected) { |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
385 |
Set<T> sns = result.collect(toSet()); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
386 |
Set<T> exp = Stream.of(expected).collect(toSet()); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
387 |
assertEquals(sns, exp); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
388 |
} |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
40304
diff
changeset
|
389 |
|
33362 | 390 |
private Map<Snippet, Snippet> closure(List<SnippetEvent> events) { |
391 |
Map<Snippet, Snippet> transitions = new HashMap<>(); |
|
392 |
for (SnippetEvent event : events) { |
|
393 |
transitions.put(event.snippet(), event.causeSnippet()); |
|
394 |
} |
|
395 |
Map<Snippet, Snippet> causeSnippets = new HashMap<>(); |
|
396 |
for (Map.Entry<Snippet, Snippet> entry : transitions.entrySet()) { |
|
397 |
Snippet snippet = entry.getKey(); |
|
398 |
Snippet cause = getInitialCause(transitions, entry.getValue()); |
|
399 |
causeSnippets.put(snippet, cause); |
|
400 |
} |
|
401 |
return causeSnippets; |
|
402 |
} |
|
403 |
||
404 |
private Snippet getInitialCause(Map<Snippet, Snippet> transitions, Snippet snippet) { |
|
405 |
Snippet result; |
|
406 |
while ((result = transitions.get(snippet)) != null) { |
|
407 |
snippet = result; |
|
408 |
} |
|
409 |
return snippet; |
|
410 |
} |
|
411 |
||
412 |
private Map<Snippet, List<SnippetEvent>> groupByCauseSnippet(List<SnippetEvent> events) { |
|
413 |
Map<Snippet, List<SnippetEvent>> map = new TreeMap<>((a, b) -> a.id().compareTo(b.id())); |
|
414 |
for (SnippetEvent event : events) { |
|
415 |
if (event == null) { |
|
416 |
throw new InternalError("null event found in " + events); |
|
417 |
} |
|
418 |
if (event.snippet() == null) { |
|
419 |
throw new InternalError("null event Snippet found in " + events); |
|
420 |
} |
|
421 |
if (event.snippet().id() == null) { |
|
422 |
throw new InternalError("null event Snippet id() found in " + events); |
|
423 |
} |
|
424 |
} |
|
425 |
for (SnippetEvent event : events) { |
|
426 |
if (event.causeSnippet() == null) { |
|
427 |
map.computeIfAbsent(event.snippet(), ($) -> new ArrayList<>()).add(event); |
|
428 |
} |
|
429 |
} |
|
430 |
Map<Snippet, Snippet> causeSnippets = closure(events); |
|
431 |
for (SnippetEvent event : events) { |
|
432 |
Snippet causeSnippet = causeSnippets.get(event.snippet()); |
|
433 |
if (causeSnippet != null) { |
|
434 |
map.get(causeSnippet).add(event); |
|
435 |
} |
|
436 |
} |
|
437 |
for (Map.Entry<Snippet, List<SnippetEvent>> entry : map.entrySet()) { |
|
438 |
Collections.sort(entry.getValue(), |
|
439 |
(a, b) -> a.causeSnippet() == null |
|
440 |
? -1 : b.causeSnippet() == null |
|
441 |
? 1 : a.snippet().id().compareTo(b.snippet().id())); |
|
442 |
} |
|
443 |
return map; |
|
444 |
} |
|
445 |
||
446 |
private List<STEInfo> getInfos(EventChain... eventChains) { |
|
447 |
List<STEInfo> list = new ArrayList<>(); |
|
448 |
for (EventChain i : eventChains) { |
|
449 |
list.add(i.mainInfo); |
|
450 |
Collections.addAll(list, i.updates); |
|
451 |
} |
|
452 |
return list; |
|
453 |
} |
|
454 |
||
455 |
private List<SnippetEvent> checkEvents(Supplier<List<SnippetEvent>> toTest, |
|
456 |
String descriptor, |
|
457 |
DiagCheck diagMain, DiagCheck diagUpdates, |
|
458 |
EventChain... eventChains) { |
|
459 |
List<SnippetEvent> dispatched = new ArrayList<>(); |
|
460 |
Subscription token = getState().onSnippetEvent(kse -> { |
|
461 |
if (dispatched.size() > 0 && dispatched.get(dispatched.size() - 1) == null) { |
|
462 |
throw new RuntimeException("dispatch event after done"); |
|
463 |
} |
|
464 |
dispatched.add(kse); |
|
465 |
}); |
|
466 |
List<SnippetEvent> events = toTest.get(); |
|
467 |
getState().unsubscribe(token); |
|
468 |
assertEquals(dispatched.size(), events.size(), "dispatched event size not the same as event size"); |
|
469 |
for (int i = events.size() - 1; i >= 0; --i) { |
|
470 |
assertEquals(dispatched.get(i), events.get(i), "Event element " + i + " does not match"); |
|
471 |
} |
|
472 |
dispatched.add(null); // mark end of dispatchs |
|
473 |
||
474 |
for (SnippetEvent evt : events) { |
|
475 |
assertTrue(evt.snippet() != null, "key must never be null, but it was for: " + descriptor); |
|
476 |
assertTrue(evt.previousStatus() != null, "previousStatus must never be null, but it was for: " + descriptor); |
|
477 |
assertTrue(evt.status() != null, "status must never be null, but it was for: " + descriptor); |
|
478 |
assertTrue(evt.status() != NONEXISTENT, "status must not be NONEXISTENT: " + descriptor); |
|
479 |
if (evt.previousStatus() != NONEXISTENT) { |
|
480 |
Snippet old = idToSnippet.get(evt.snippet().id()); |
|
481 |
if (old != null) { |
|
482 |
switch (evt.status()) { |
|
483 |
case DROPPED: |
|
484 |
assertEquals(old, evt.snippet(), |
|
485 |
"Drop: Old snippet must be what is dropped -- input: " + descriptor); |
|
486 |
break; |
|
487 |
case OVERWRITTEN: |
|
488 |
assertEquals(old, evt.snippet(), |
|
489 |
"Overwrite: Old snippet (" + old |
|
490 |
+ ") must be what is overwritten -- input: " |
|
491 |
+ descriptor + " -- " + evt); |
|
492 |
break; |
|
493 |
default: |
|
494 |
if (evt.causeSnippet() == null) { |
|
495 |
// New source |
|
496 |
assertNotEquals(old, evt.snippet(), |
|
497 |
"New source: Old snippet must be different from the replacing -- input: " |
|
498 |
+ descriptor); |
|
499 |
} else { |
|
500 |
// An update (key Overwrite??) |
|
501 |
assertEquals(old, evt.snippet(), |
|
502 |
"Update: Old snippet must be equal to the replacing -- input: " |
|
503 |
+ descriptor); |
|
504 |
} |
|
505 |
break; |
|
506 |
} |
|
507 |
} |
|
508 |
} |
|
509 |
} |
|
510 |
for (SnippetEvent evt : events) { |
|
511 |
if (evt.causeSnippet() == null && evt.status() != DROPPED) { |
|
512 |
allSnippets.add(evt.snippet()); |
|
513 |
idToSnippet.put(evt.snippet().id(), evt.snippet()); |
|
514 |
} |
|
515 |
} |
|
516 |
assertTrue(events.size() >= 1, "Expected at least one event, got none."); |
|
517 |
List<STEInfo> all = getInfos(eventChains); |
|
518 |
if (events.size() != all.size()) { |
|
519 |
StringBuilder sb = new StringBuilder(); |
|
520 |
sb.append("Got events --\n"); |
|
521 |
for (SnippetEvent evt : events) { |
|
522 |
sb.append(" key: ").append(evt.snippet()); |
|
523 |
sb.append(" before: ").append(evt.previousStatus()); |
|
524 |
sb.append(" status: ").append(evt.status()); |
|
525 |
sb.append(" isSignatureChange: ").append(evt.isSignatureChange()); |
|
526 |
sb.append(" cause: "); |
|
527 |
if (evt.causeSnippet() == null) { |
|
528 |
sb.append("direct"); |
|
529 |
} else { |
|
530 |
sb.append(evt.causeSnippet()); |
|
531 |
} |
|
532 |
sb.append("\n"); |
|
533 |
} |
|
534 |
sb.append("Expected ").append(all.size()); |
|
535 |
sb.append(" events, got: ").append(events.size()); |
|
536 |
fail(sb.toString()); |
|
537 |
} |
|
538 |
||
539 |
int impactId = 0; |
|
540 |
Map<Snippet, List<SnippetEvent>> groupedEvents = groupByCauseSnippet(events); |
|
541 |
assertEquals(groupedEvents.size(), eventChains.length, "Number of main events"); |
|
542 |
for (Map.Entry<Snippet, List<SnippetEvent>> entry : groupedEvents.entrySet()) { |
|
543 |
EventChain eventChain = eventChains[impactId++]; |
|
544 |
SnippetEvent main = entry.getValue().get(0); |
|
545 |
Snippet mainKey = main.snippet(); |
|
546 |
if (eventChain.mainInfo != null) { |
|
547 |
eventChain.mainInfo.assertMatch(entry.getValue().get(0), mainKey); |
|
548 |
if (eventChain.updates.length > 0) { |
|
549 |
if (eventChain.updates.length == 1) { |
|
550 |
eventChain.updates[0].assertMatch(entry.getValue().get(1), mainKey); |
|
551 |
} else { |
|
552 |
Arrays.sort(eventChain.updates, (a, b) -> ((a.snippet() == MAIN_SNIPPET) |
|
553 |
? mainKey |
|
554 |
: a.snippet()).id().compareTo(b.snippet().id())); |
|
555 |
List<SnippetEvent> updateEvents = new ArrayList<>(entry.getValue().subList(1, entry.getValue().size())); |
|
556 |
int idx = 0; |
|
557 |
for (SnippetEvent ste : updateEvents) { |
|
558 |
eventChain.updates[idx++].assertMatch(ste, mainKey); |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
if (((Object) eventChain.value) != IGNORE_VALUE) { |
|
564 |
assertEquals(main.value(), eventChain.value, "Expected execution value of: " + eventChain.value + |
|
565 |
", but got: " + main.value()); |
|
566 |
} |
|
567 |
if (eventChain.exceptionClass != IGNORE_EXCEPTION) { |
|
568 |
if (main.exception() == null) { |
|
569 |
assertEquals(eventChain.exceptionClass, null, "Expected an exception of class " |
|
570 |
+ eventChain.exceptionClass + " got no exception"); |
|
571 |
} else if (eventChain.exceptionClass == null) { |
|
572 |
fail("Expected no exception but got " + main.exception().toString()); |
|
573 |
} else { |
|
574 |
assertTrue(eventChain.exceptionClass.isInstance(main.exception()), |
|
575 |
"Expected an exception of class " + eventChain.exceptionClass + |
|
576 |
" got: " + main.exception().toString()); |
|
577 |
} |
|
578 |
} |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
579 |
List<Diag> diagnostics = getState().diagnostics(mainKey).collect(toList()); |
33362 | 580 |
switch (diagMain) { |
581 |
case DIAG_OK: |
|
582 |
assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics)); |
|
583 |
break; |
|
584 |
case DIAG_WARNING: |
|
585 |
assertFalse(hasFatalError(diagnostics), "Expected no errors, got: " + diagnosticsToString(diagnostics)); |
|
586 |
break; |
|
587 |
case DIAG_ERROR: |
|
588 |
assertTrue(hasFatalError(diagnostics), "Expected errors, got: " + diagnosticsToString(diagnostics)); |
|
589 |
break; |
|
590 |
} |
|
591 |
if (eventChain.mainInfo != null) { |
|
592 |
for (STEInfo ste : eventChain.updates) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
593 |
diagnostics = getState().diagnostics(ste.snippet()).collect(toList()); |
33362 | 594 |
switch (diagUpdates) { |
595 |
case DIAG_OK: |
|
596 |
assertEquals(diagnostics.size(), 0, "Expected no diagnostics, got: " + diagnosticsToString(diagnostics)); |
|
597 |
break; |
|
598 |
case DIAG_WARNING: |
|
599 |
assertFalse(hasFatalError(diagnostics), "Expected no errors, got: " + diagnosticsToString(diagnostics)); |
|
600 |
break; |
|
601 |
} |
|
602 |
} |
|
603 |
} |
|
604 |
} |
|
605 |
return events; |
|
606 |
} |
|
607 |
||
608 |
// Use this for all EMPTY calls to eval() |
|
609 |
public void assertEvalEmpty(String input) { |
|
610 |
List<SnippetEvent> events = getState().eval(input); |
|
611 |
assertEquals(events.size(), 0, "Expected no events, got: " + events.size()); |
|
612 |
} |
|
613 |
||
614 |
public VarSnippet varKey(List<SnippetEvent> events) { |
|
615 |
Snippet key = key(events); |
|
616 |
assertTrue(key instanceof VarSnippet, "Expected a VariableKey, got: " + key); |
|
617 |
return (VarSnippet) key; |
|
618 |
} |
|
619 |
||
620 |
public MethodSnippet methodKey(List<SnippetEvent> events) { |
|
621 |
Snippet key = key(events); |
|
622 |
assertTrue(key instanceof MethodSnippet, "Expected a MethodKey, got: " + key); |
|
623 |
return (MethodSnippet) key; |
|
624 |
} |
|
625 |
||
626 |
public TypeDeclSnippet classKey(List<SnippetEvent> events) { |
|
627 |
Snippet key = key(events); |
|
628 |
assertTrue(key instanceof TypeDeclSnippet, "Expected a ClassKey, got: " + key); |
|
629 |
return (TypeDeclSnippet) key; |
|
630 |
} |
|
631 |
||
632 |
public ImportSnippet importKey(List<SnippetEvent> events) { |
|
633 |
Snippet key = key(events); |
|
634 |
assertTrue(key instanceof ImportSnippet, "Expected a ImportKey, got: " + key); |
|
635 |
return (ImportSnippet) key; |
|
636 |
} |
|
637 |
||
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
638 |
public Snippet key(List<SnippetEvent> events) { |
33362 | 639 |
assertTrue(events.size() >= 1, "Expected at least one event, got none."); |
640 |
return events.get(0).snippet(); |
|
641 |
} |
|
642 |
||
643 |
public void assertVarValue(Snippet key, String expected) { |
|
644 |
String value = state.varValue((VarSnippet) key); |
|
645 |
assertEquals(value, expected, "Expected var value of: " + expected + ", but got: " + value); |
|
646 |
} |
|
647 |
||
648 |
public Snippet assertDeclareFail(String input, String expectedErrorCode) { |
|
649 |
return assertDeclareFail(input, expectedErrorCode, added(REJECTED)); |
|
650 |
} |
|
651 |
||
652 |
public Snippet assertDeclareFail(String input, String expectedErrorCode, |
|
653 |
STEInfo mainInfo, STEInfo... updates) { |
|
654 |
return assertDeclareFail(input, |
|
655 |
new ExpectedDiagnostic(expectedErrorCode, -1, -1, -1, -1, -1, Diagnostic.Kind.ERROR), |
|
656 |
mainInfo, updates); |
|
657 |
} |
|
658 |
||
659 |
public Snippet assertDeclareFail(String input, ExpectedDiagnostic expectedDiagnostic) { |
|
660 |
return assertDeclareFail(input, expectedDiagnostic, added(REJECTED)); |
|
661 |
} |
|
662 |
||
663 |
public Snippet assertDeclareFail(String input, ExpectedDiagnostic expectedDiagnostic, |
|
664 |
STEInfo mainInfo, STEInfo... updates) { |
|
665 |
List<SnippetEvent> events = assertEval(input, null, null, |
|
666 |
DiagCheck.DIAG_ERROR, DiagCheck.DIAG_IGNORE, mainInfo, updates); |
|
667 |
SnippetEvent e = events.get(0); |
|
668 |
Snippet key = e.snippet(); |
|
669 |
assertEquals(getState().status(key), REJECTED); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
670 |
List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList()); |
33362 | 671 |
assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none"); |
672 |
assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic); |
|
673 |
assertTrue(key != null, "key must never be null, but it was for: " + input); |
|
674 |
return key; |
|
675 |
} |
|
676 |
||
677 |
public Snippet assertDeclareWarn1(String input, String expectedErrorCode) { |
|
678 |
return assertDeclareWarn1(input, new ExpectedDiagnostic(expectedErrorCode, -1, -1, -1, -1, -1, Diagnostic.Kind.WARNING)); |
|
679 |
} |
|
680 |
||
681 |
public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic) { |
|
682 |
return assertDeclareWarn1(input, expectedDiagnostic, added(VALID)); |
|
683 |
} |
|
684 |
||
685 |
public Snippet assertDeclareWarn1(String input, ExpectedDiagnostic expectedDiagnostic, STEInfo mainInfo, STEInfo... updates) { |
|
686 |
List<SnippetEvent> events = assertEval(input, IGNORE_VALUE, null, |
|
687 |
DiagCheck.DIAG_WARNING, DiagCheck.DIAG_IGNORE, mainInfo, updates); |
|
688 |
SnippetEvent e = events.get(0); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
689 |
List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList()); |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36499
diff
changeset
|
690 |
if (expectedDiagnostic != null) assertDiagnostic(input, diagnostics.get(0), expectedDiagnostic); |
33362 | 691 |
return e.snippet(); |
692 |
} |
|
693 |
||
694 |
private void assertDiagnostic(String input, Diag diagnostic, ExpectedDiagnostic expectedDiagnostic) { |
|
695 |
if (expectedDiagnostic != null) expectedDiagnostic.assertDiagnostic(diagnostic); |
|
696 |
// assertEquals(diagnostic.getSource(), input, "Diagnostic source"); |
|
697 |
} |
|
698 |
||
699 |
public void assertTypeDeclSnippet(TypeDeclSnippet type, String expectedName, |
|
700 |
Status expectedStatus, SubKind expectedSubKind, |
|
701 |
int unressz, int othersz) { |
|
702 |
assertDeclarationSnippet(type, expectedName, expectedStatus, |
|
703 |
expectedSubKind, unressz, othersz); |
|
704 |
} |
|
705 |
||
706 |
public void assertMethodDeclSnippet(MethodSnippet method, |
|
707 |
String expectedName, String expectedSignature, |
|
708 |
Status expectedStatus, int unressz, int othersz) { |
|
709 |
assertDeclarationSnippet(method, expectedName, expectedStatus, |
|
710 |
METHOD_SUBKIND, unressz, othersz); |
|
711 |
String signature = method.signature(); |
|
712 |
assertEquals(signature, expectedSignature, |
|
713 |
"Expected " + method.source() + " to have the name: " + |
|
714 |
expectedSignature + ", got: " + signature); |
|
715 |
} |
|
716 |
||
717 |
public void assertVariableDeclSnippet(VarSnippet var, |
|
718 |
String expectedName, String expectedTypeName, |
|
719 |
Status expectedStatus, SubKind expectedSubKind, |
|
720 |
int unressz, int othersz) { |
|
721 |
assertDeclarationSnippet(var, expectedName, expectedStatus, |
|
722 |
expectedSubKind, unressz, othersz); |
|
723 |
String signature = var.typeName(); |
|
724 |
assertEquals(signature, expectedTypeName, |
|
725 |
"Expected " + var.source() + " to have the name: " + |
|
726 |
expectedTypeName + ", got: " + signature); |
|
727 |
} |
|
728 |
||
729 |
public void assertDeclarationSnippet(DeclarationSnippet declarationKey, |
|
730 |
String expectedName, |
|
731 |
Status expectedStatus, SubKind expectedSubKind, |
|
732 |
int unressz, int othersz) { |
|
733 |
assertKey(declarationKey, expectedStatus, expectedSubKind); |
|
734 |
String source = declarationKey.source(); |
|
735 |
assertEquals(declarationKey.name(), expectedName, |
|
736 |
"Expected " + source + " to have the name: " + expectedName + ", got: " + declarationKey.name()); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
737 |
long unresolved = getState().unresolvedDependencies(declarationKey).count(); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
738 |
assertEquals(unresolved, unressz, "Expected " + source + " to have " + unressz |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
739 |
+ " unresolved symbols, got: " + unresolved); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
740 |
long otherCorralledErrorsCount = getState().diagnostics(declarationKey).count(); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
741 |
assertEquals(otherCorralledErrorsCount, othersz, "Expected " + source + " to have " + othersz |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
742 |
+ " other errors, got: " + otherCorralledErrorsCount); |
33362 | 743 |
} |
744 |
||
745 |
public void assertKey(Snippet key, Status expectedStatus, SubKind expectedSubKind) { |
|
746 |
String source = key.source(); |
|
747 |
SubKind actualSubKind = key.subKind(); |
|
748 |
assertEquals(actualSubKind, expectedSubKind, |
|
749 |
"Expected " + source + " to have the subkind: " + expectedSubKind + ", got: " + actualSubKind); |
|
750 |
Status status = getState().status(key); |
|
751 |
assertEquals(status, expectedStatus, "Expected " + source + " to be " |
|
752 |
+ expectedStatus + ", but it is " + status); |
|
753 |
Snippet.Kind expectedKind = getKind(key); |
|
754 |
assertEquals(key.kind(), expectedKind, "Checking kind: "); |
|
755 |
assertEquals(expectedSubKind.kind(), expectedKind, "Checking kind: "); |
|
756 |
} |
|
757 |
||
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40515
diff
changeset
|
758 |
public void assertDrop(Snippet key, STEInfo mainInfo, STEInfo... updates) { |
33362 | 759 |
assertDrop(key, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, mainInfo, updates); |
760 |
} |
|
761 |
||
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40515
diff
changeset
|
762 |
public void assertDrop(Snippet key, DiagCheck diagMain, DiagCheck diagUpdates, STEInfo mainInfo, STEInfo... updates) { |
33362 | 763 |
assertDrop(key, diagMain, diagUpdates, new EventChain(mainInfo, null, null, updates)); |
764 |
} |
|
765 |
||
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40515
diff
changeset
|
766 |
public void assertDrop(Snippet key, DiagCheck diagMain, DiagCheck diagUpdates, EventChain... eventChains) { |
33362 | 767 |
checkEvents(() -> getState().drop(key), "drop(" + key + ")", diagMain, diagUpdates, eventChains); |
768 |
} |
|
769 |
||
770 |
public void assertAnalyze(String input, String source, String remaining, boolean isComplete) { |
|
771 |
assertAnalyze(input, null, source, remaining, isComplete); |
|
772 |
} |
|
773 |
||
774 |
public void assertAnalyze(String input, Completeness status, String source) { |
|
775 |
assertAnalyze(input, status, source, null, null); |
|
776 |
} |
|
777 |
||
778 |
public void assertAnalyze(String input, Completeness status, String source, String remaining, Boolean isComplete) { |
|
779 |
CompletionInfo ci = getAnalysis().analyzeCompletion(input); |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
780 |
if (status != null) assertEquals(ci.completeness(), status, "Input : " + input + ", status: "); |
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
781 |
if (source != null) assertEquals(ci.source(), source, "Input : " + input + ", source: "); |
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
782 |
if (remaining != null) assertEquals(ci.remaining(), remaining, "Input : " + input + ", remaining: "); |
33362 | 783 |
if (isComplete != null) { |
784 |
boolean isExpectedComplete = isComplete; |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
785 |
assertEquals(ci.completeness().isComplete(), isExpectedComplete, "Input : " + input + ", isComplete: "); |
33362 | 786 |
} |
787 |
} |
|
788 |
||
789 |
public void assertNumberOfActiveVariables(int cnt) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
790 |
assertEquals(getState().variables().count(), cnt, "Variables : " + getState().variables().collect(toList())); |
33362 | 791 |
} |
792 |
||
793 |
public void assertNumberOfActiveMethods(int cnt) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
794 |
assertEquals(getState().methods().count(), cnt, "Methods : " + getState().methods().collect(toList())); |
33362 | 795 |
} |
796 |
||
797 |
public void assertNumberOfActiveClasses(int cnt) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
798 |
assertEquals(getState().types().count(), cnt, "Types : " + getState().types().collect(toList())); |
33362 | 799 |
} |
800 |
||
801 |
public void assertKeys(MemberInfo... expected) { |
|
802 |
int index = 0; |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
803 |
List<Snippet> snippets = getState().snippets().collect(toList()); |
33362 | 804 |
assertEquals(allSnippets.size(), snippets.size()); |
805 |
for (Snippet sn : snippets) { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
806 |
if (sn.kind().isPersistent() && getState().status(sn).isActive()) { |
33362 | 807 |
MemberInfo actual = getMemberInfo(sn); |
808 |
MemberInfo exp = expected[index]; |
|
809 |
assertEquals(actual, exp, String.format("Difference in #%d. Expected: %s, actual: %s", |
|
810 |
index, exp, actual)); |
|
811 |
++index; |
|
812 |
} |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
public void assertActiveKeys() { |
|
817 |
Collection<Snippet> expected = getActiveKeys(); |
|
818 |
assertActiveKeys(expected.toArray(new Snippet[expected.size()])); |
|
819 |
} |
|
820 |
||
821 |
public void assertActiveKeys(Snippet... expected) { |
|
822 |
int index = 0; |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
823 |
for (Snippet key : getState().snippets().collect(toList())) { |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
824 |
if (state.status(key).isActive()) { |
33362 | 825 |
assertEquals(expected[index], key, String.format("Difference in #%d. Expected: %s, actual: %s", index, key, expected[index])); |
826 |
++index; |
|
827 |
} |
|
828 |
} |
|
829 |
} |
|
830 |
||
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
831 |
private void assertActiveSnippets(Stream<? extends Snippet> snippets, Predicate<Snippet> p, String label) { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
832 |
Set<Snippet> active = getActiveKeys().stream() |
33362 | 833 |
.filter(p) |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
834 |
.collect(Collectors.toSet()); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
835 |
Set<Snippet> got = snippets |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
836 |
.collect(Collectors.toSet()); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
837 |
assertEquals(active, got, label); |
33362 | 838 |
} |
839 |
||
840 |
public void assertVariables() { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
841 |
assertActiveSnippets(getState().variables(), (key) -> key instanceof VarSnippet, "Variables"); |
33362 | 842 |
} |
843 |
||
844 |
public void assertMethods() { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
845 |
assertActiveSnippets(getState().methods(), (key) -> key instanceof MethodSnippet, "Methods"); |
33362 | 846 |
} |
847 |
||
848 |
public void assertClasses() { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
849 |
assertActiveSnippets(getState().types(), (key) -> key instanceof TypeDeclSnippet, "Classes"); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
850 |
} |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
851 |
|
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
852 |
public void assertMembers(Stream<? extends Snippet> members, MemberInfo...expectedInfos) { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
853 |
Set<MemberInfo> expected = Stream.of(expectedInfos).collect(Collectors.toSet()); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
854 |
Set<MemberInfo> got = members |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
855 |
.map(this::getMemberInfo) |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
856 |
.collect(Collectors.toSet()); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
857 |
assertEquals(got.size(), expected.size(), "Expected : " + expected + ", actual : " + members); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
858 |
assertEquals(got, expected); |
33362 | 859 |
} |
860 |
||
861 |
public void assertVariables(MemberInfo...expected) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
862 |
assertMembers(getState().variables(), expected); |
33362 | 863 |
} |
864 |
||
865 |
public void assertMethods(MemberInfo...expected) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
866 |
assertMembers(getState().methods(), expected); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
867 |
getState().methods().forEach(methodKey -> { |
33362 | 868 |
MemberInfo expectedInfo = null; |
869 |
for (MemberInfo info : expected) { |
|
870 |
if (info.name.equals(methodKey.name()) && info.type.equals(methodKey.signature())) { |
|
871 |
expectedInfo = getMemberInfo(methodKey); |
|
872 |
} |
|
873 |
} |
|
874 |
assertNotNull(expectedInfo, "Not found method: " + methodKey.name()); |
|
875 |
int lastIndexOf = expectedInfo.type.lastIndexOf(')'); |
|
876 |
assertEquals(methodKey.parameterTypes(), expectedInfo.type.substring(1, lastIndexOf), "Parameter types"); |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
877 |
}); |
33362 | 878 |
} |
879 |
||
880 |
public void assertClasses(MemberInfo...expected) { |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
881 |
assertMembers(getState().types(), expected); |
33362 | 882 |
} |
883 |
||
884 |
public void assertCompletion(String code, String... expected) { |
|
885 |
assertCompletion(code, null, expected); |
|
886 |
} |
|
887 |
||
888 |
public void assertCompletion(String code, Boolean isSmart, String... expected) { |
|
889 |
List<String> completions = computeCompletions(code, isSmart); |
|
890 |
assertEquals(completions, Arrays.asList(expected), "Input: " + code + ", " + completions.toString()); |
|
891 |
} |
|
892 |
||
893 |
public void assertCompletionIncludesExcludes(String code, Set<String> expected, Set<String> notExpected) { |
|
894 |
assertCompletionIncludesExcludes(code, null, expected, notExpected); |
|
895 |
} |
|
896 |
||
897 |
public void assertCompletionIncludesExcludes(String code, Boolean isSmart, Set<String> expected, Set<String> notExpected) { |
|
898 |
List<String> completions = computeCompletions(code, isSmart); |
|
899 |
assertTrue(completions.containsAll(expected), String.valueOf(completions)); |
|
900 |
assertTrue(Collections.disjoint(completions, notExpected), String.valueOf(completions)); |
|
901 |
} |
|
902 |
||
903 |
private List<String> computeCompletions(String code, Boolean isSmart) { |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
904 |
waitIndexingFinished(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
905 |
|
33362 | 906 |
int cursor = code.indexOf('|'); |
907 |
code = code.replace("|", ""); |
|
908 |
assertTrue(cursor > -1, "'|' expected, but not found in: " + code); |
|
909 |
List<Suggestion> completions = |
|
910 |
getAnalysis().completionSuggestions(code, cursor, new int[1]); //XXX: ignoring anchor for now |
|
911 |
return completions.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
912 |
.filter(s -> isSmart == null || isSmart == s.matchesType()) |
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
913 |
.map(s -> s.continuation()) |
33362 | 914 |
.distinct() |
915 |
.collect(Collectors.toList()); |
|
916 |
} |
|
917 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
918 |
public void assertInferredType(String code, String expectedType) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
919 |
String inferredType = getAnalysis().analyzeType(code, code.length()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
920 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
921 |
assertEquals(inferredType, expectedType, "Input: " + code + ", " + inferredType); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
922 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
923 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
924 |
public void assertInferredFQNs(String code, String... fqns) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
925 |
assertInferredFQNs(code, code.length(), false, fqns); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
926 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
927 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
928 |
public void assertInferredFQNs(String code, int simpleNameLen, boolean resolvable, String... fqns) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
929 |
waitIndexingFinished(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
930 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
931 |
QualifiedNames candidates = getAnalysis().listQualifiedNames(code, code.length()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
932 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
933 |
assertEquals(candidates.getNames(), Arrays.asList(fqns), "Input: " + code + ", candidates=" + candidates.getNames()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
934 |
assertEquals(candidates.getSimpleNameLength(), simpleNameLen, "Input: " + code + ", simpleNameLen=" + candidates.getSimpleNameLength()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
935 |
assertEquals(candidates.isResolvable(), resolvable, "Input: " + code + ", resolvable=" + candidates.isResolvable()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
936 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
937 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
938 |
protected void waitIndexingFinished() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
939 |
try { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
940 |
Method waitBackgroundTaskFinished = getAnalysis().getClass().getDeclaredMethod("waitBackgroundTaskFinished"); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
941 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
942 |
waitBackgroundTaskFinished.setAccessible(true); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
943 |
waitBackgroundTaskFinished.invoke(getAnalysis()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
944 |
} catch (Exception ex) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
945 |
throw new AssertionError("Cannot wait for indexing end.", ex); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
946 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
947 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
948 |
|
33362 | 949 |
public void assertDocumentation(String code, String... expected) { |
950 |
int cursor = code.indexOf('|'); |
|
951 |
code = code.replace("|", ""); |
|
952 |
assertTrue(cursor > -1, "'|' expected, but not found in: " + code); |
|
953 |
String documentation = getAnalysis().documentation(code, cursor); |
|
954 |
Set<String> docSet = Stream.of(documentation.split("\r?\n")).collect(Collectors.toSet()); |
|
955 |
Set<String> expectedSet = Stream.of(expected).collect(Collectors.toSet()); |
|
956 |
assertEquals(docSet, expectedSet, "Input: " + code); |
|
957 |
} |
|
958 |
||
959 |
public enum ClassType { |
|
960 |
CLASS("CLASS_SUBKIND") { |
|
961 |
@Override |
|
962 |
public String toString() { |
|
963 |
return "class"; |
|
964 |
} |
|
965 |
}, |
|
966 |
ENUM("ENUM_SUBKIND") { |
|
967 |
@Override |
|
968 |
public String toString() { |
|
969 |
return "enum"; |
|
970 |
} |
|
971 |
}, |
|
972 |
INTERFACE("INTERFACE_SUBKIND") { |
|
973 |
@Override |
|
974 |
public String toString() { |
|
975 |
return "interface"; |
|
976 |
} |
|
977 |
}, |
|
978 |
ANNOTATION("ANNOTATION_TYPE_SUBKIND") { |
|
979 |
@Override |
|
980 |
public String toString() { |
|
981 |
return "@interface"; |
|
982 |
} |
|
983 |
}; |
|
984 |
||
985 |
private final String classType; |
|
986 |
||
987 |
ClassType(String classType) { |
|
988 |
this.classType = classType; |
|
989 |
} |
|
990 |
||
991 |
public String getClassType() { |
|
992 |
return classType; |
|
993 |
} |
|
994 |
||
995 |
@Override |
|
996 |
public abstract String toString(); |
|
997 |
} |
|
998 |
||
999 |
public static MemberInfo variable(String type, String name) { |
|
1000 |
return new MemberInfo(type, name); |
|
1001 |
} |
|
1002 |
||
1003 |
public static MemberInfo method(String signature, String name) { |
|
1004 |
return new MemberInfo(signature, name); |
|
1005 |
} |
|
1006 |
||
1007 |
public static MemberInfo clazz(ClassType classType, String className) { |
|
1008 |
return new MemberInfo(classType.getClassType(), className); |
|
1009 |
} |
|
1010 |
||
1011 |
public static class MemberInfo { |
|
1012 |
public final String type; |
|
1013 |
public final String name; |
|
1014 |
||
1015 |
public MemberInfo(String type, String name) { |
|
1016 |
this.type = type; |
|
1017 |
this.name = name; |
|
1018 |
} |
|
1019 |
||
1020 |
@Override |
|
1021 |
public int hashCode() { |
|
1022 |
return type.hashCode() + 3 * name.hashCode(); |
|
1023 |
} |
|
1024 |
||
1025 |
@Override |
|
1026 |
public boolean equals(Object o) { |
|
1027 |
if (o instanceof MemberInfo) { |
|
1028 |
MemberInfo other = (MemberInfo) o; |
|
1029 |
return type.equals(other.type) && name.equals(other.name); |
|
1030 |
} |
|
1031 |
return false; |
|
1032 |
} |
|
1033 |
||
1034 |
@Override |
|
1035 |
public String toString() { |
|
1036 |
return String.format("%s %s", type, name); |
|
1037 |
} |
|
1038 |
} |
|
1039 |
||
1040 |
public MemberInfo getMemberInfo(Snippet key) { |
|
1041 |
SubKind subkind = key.subKind(); |
|
1042 |
switch (subkind) { |
|
1043 |
case CLASS_SUBKIND: |
|
1044 |
case INTERFACE_SUBKIND: |
|
1045 |
case ENUM_SUBKIND: |
|
1046 |
case ANNOTATION_TYPE_SUBKIND: |
|
1047 |
return new MemberInfo(subkind.name(), ((DeclarationSnippet) key).name()); |
|
1048 |
case METHOD_SUBKIND: |
|
1049 |
MethodSnippet method = (MethodSnippet) key; |
|
1050 |
return new MemberInfo(method.signature(), method.name()); |
|
1051 |
case VAR_DECLARATION_SUBKIND: |
|
1052 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: |
|
1053 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
1054 |
VarSnippet var = (VarSnippet) key; |
|
1055 |
return new MemberInfo(var.typeName(), var.name()); |
|
1056 |
default: |
|
1057 |
throw new AssertionError("Unknown snippet : " + key.kind() + " in expression " + key.toString()); |
|
1058 |
} |
|
1059 |
} |
|
1060 |
||
1061 |
public String diagnosticsToString(List<Diag> diagnostics) { |
|
1062 |
StringWriter writer = new StringWriter(); |
|
1063 |
for (Diag diag : diagnostics) { |
|
1064 |
writer.write("Error --\n"); |
|
1065 |
for (String line : diag.getMessage(null).split("\\r?\\n")) { |
|
1066 |
writer.write(String.format("%s\n", line)); |
|
1067 |
} |
|
1068 |
} |
|
1069 |
return writer.toString().replace("\n", System.lineSeparator()); |
|
1070 |
} |
|
1071 |
||
1072 |
public boolean hasFatalError(List<Diag> diagnostics) { |
|
1073 |
for (Diag diag : diagnostics) { |
|
1074 |
if (diag.isError()) { |
|
1075 |
return true; |
|
1076 |
} |
|
1077 |
} |
|
1078 |
return false; |
|
1079 |
} |
|
1080 |
||
1081 |
public static EventChain chain(STEInfo mainInfo, STEInfo... updates) { |
|
1082 |
return chain(mainInfo, IGNORE_VALUE, null, updates); |
|
1083 |
} |
|
1084 |
||
1085 |
public static EventChain chain(STEInfo mainInfo, String value, Class<? extends Throwable> exceptionClass, STEInfo... updates) { |
|
1086 |
return new EventChain(mainInfo, value, exceptionClass, updates); |
|
1087 |
} |
|
1088 |
||
1089 |
public static STEInfo ste(Snippet key, Status previousStatus, Status status, |
|
1090 |
Boolean isSignatureChange, Snippet causeKey) { |
|
1091 |
return new STEInfo(key, previousStatus, status, isSignatureChange, causeKey); |
|
1092 |
} |
|
1093 |
||
1094 |
public static STEInfo added(Status status) { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
1095 |
return new STEInfo(MAIN_SNIPPET, NONEXISTENT, status, status.isDefined(), null); |
33362 | 1096 |
} |
1097 |
||
1098 |
public static class EventChain { |
|
1099 |
public final STEInfo mainInfo; |
|
1100 |
public final STEInfo[] updates; |
|
1101 |
public final String value; |
|
1102 |
public final Class<? extends Throwable> exceptionClass; |
|
1103 |
||
1104 |
public EventChain(STEInfo mainInfo, String value, Class<? extends Throwable> exceptionClass, STEInfo... updates) { |
|
1105 |
this.mainInfo = mainInfo; |
|
1106 |
this.updates = updates; |
|
1107 |
this.value = value; |
|
1108 |
this.exceptionClass = exceptionClass; |
|
1109 |
} |
|
1110 |
} |
|
1111 |
||
1112 |
public static class STEInfo { |
|
1113 |
||
1114 |
STEInfo(Snippet snippet, Status previousStatus, Status status, |
|
1115 |
Boolean isSignatureChange, Snippet causeSnippet) { |
|
1116 |
this.snippet = snippet; |
|
1117 |
this.previousStatus = previousStatus; |
|
1118 |
this.status = status; |
|
1119 |
this.checkIsSignatureChange = isSignatureChange != null; |
|
1120 |
this.isSignatureChange = checkIsSignatureChange ? isSignatureChange : false; |
|
1121 |
this.causeSnippet = causeSnippet; |
|
1122 |
assertTrue(snippet != null, "Bad test set-up. The match snippet must not be null"); |
|
1123 |
} |
|
1124 |
||
1125 |
final Snippet snippet; |
|
1126 |
final Status previousStatus; |
|
1127 |
final Status status; |
|
1128 |
final boolean isSignatureChange; |
|
1129 |
final Snippet causeSnippet; |
|
1130 |
||
1131 |
final boolean checkIsSignatureChange; |
|
1132 |
public Snippet snippet() { |
|
1133 |
return snippet; |
|
1134 |
} |
|
1135 |
public Status previousStatus() { |
|
1136 |
return previousStatus; |
|
1137 |
} |
|
1138 |
public Status status() { |
|
1139 |
return status; |
|
1140 |
} |
|
1141 |
public boolean isSignatureChange() { |
|
1142 |
if (!checkIsSignatureChange) { |
|
1143 |
throw new IllegalStateException("isSignatureChange value is undefined"); |
|
1144 |
} |
|
1145 |
return isSignatureChange; |
|
1146 |
} |
|
1147 |
public Snippet causeSnippet() { |
|
1148 |
return causeSnippet; |
|
1149 |
} |
|
1150 |
public String value() { |
|
1151 |
return null; |
|
1152 |
} |
|
1153 |
public Exception exception() { |
|
1154 |
return null; |
|
1155 |
} |
|
1156 |
||
1157 |
public void assertMatch(SnippetEvent ste, Snippet mainSnippet) { |
|
1158 |
assertKeyMatch(ste, ste.snippet(), snippet(), mainSnippet); |
|
1159 |
assertStatusMatch(ste, ste.previousStatus(), previousStatus()); |
|
1160 |
assertStatusMatch(ste, ste.status(), status()); |
|
1161 |
if (checkIsSignatureChange) { |
|
1162 |
assertEquals(ste.isSignatureChange(), isSignatureChange(), |
|
1163 |
"Expected " + |
|
1164 |
(isSignatureChange()? "" : "no ") + |
|
1165 |
"signature-change, got: " + |
|
1166 |
(ste.isSignatureChange()? "" : "no ") + |
|
1167 |
"signature-change" + |
|
1168 |
"\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1169 |
} |
|
1170 |
assertKeyMatch(ste, ste.causeSnippet(), causeSnippet(), mainSnippet); |
|
1171 |
} |
|
1172 |
||
1173 |
private void assertKeyMatch(SnippetEvent ste, Snippet sn, Snippet expected, Snippet mainSnippet) { |
|
1174 |
Snippet testKey = expected; |
|
1175 |
if (testKey != null) { |
|
1176 |
if (expected == MAIN_SNIPPET) { |
|
1177 |
assertNotNull(mainSnippet, "MAIN_SNIPPET used, test must pass value to assertMatch"); |
|
1178 |
testKey = mainSnippet; |
|
1179 |
} |
|
1180 |
if (ste.causeSnippet() == null && ste.status() != DROPPED && expected != MAIN_SNIPPET) { |
|
1181 |
// Source change, always new snippet -- only match id() |
|
1182 |
assertTrue(sn != testKey, |
|
1183 |
"Main-event: Expected new snippet to be != : " + testKey |
|
1184 |
+ "\n got-event: " + toString(ste)); |
|
1185 |
assertEquals(sn.id(), testKey.id(), "Expected IDs to match: " + testKey + ", got: " + sn |
|
1186 |
+ "\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1187 |
} else { |
|
1188 |
assertEquals(sn, testKey, "Expected key to be: " + testKey + ", got: " + sn |
|
1189 |
+ "\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1190 |
} |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
private void assertStatusMatch(SnippetEvent ste, Status status, Status expected) { |
|
1195 |
if (expected != null) { |
|
1196 |
assertEquals(status, expected, "Expected status to be: " + expected + ", got: " + status + |
|
1197 |
"\n expected-event: " + this + "\n got-event: " + toString(ste)); |
|
1198 |
} |
|
1199 |
} |
|
1200 |
||
1201 |
@Override |
|
1202 |
public String toString() { |
|
1203 |
return "STEInfo key: " + |
|
1204 |
(snippet()==MAIN_SNIPPET? "MAIN_SNIPPET" : (snippet()==null? "ignore" : snippet().id())) + |
|
1205 |
" before: " + previousStatus() + |
|
1206 |
" status: " + status() + " sig: " + isSignatureChange() + |
|
1207 |
" cause: " + (causeSnippet()==null? "null" : causeSnippet().id()); |
|
1208 |
} |
|
1209 |
||
1210 |
private String toString(SnippetEvent ste) { |
|
1211 |
return "key: " + (ste.snippet()==MAIN_SNIPPET? "MAIN_SNIPPET" : ste.snippet().id()) + " before: " + ste.previousStatus() |
|
1212 |
+ " status: " + ste.status() + " sig: " + ste.isSignatureChange() |
|
1213 |
+ " cause: " + ste.causeSnippet(); |
|
1214 |
} |
|
1215 |
} |
|
1216 |
} |