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