Skip to content

Commit

Permalink
fix: prevent NPE in ShortcutRegistration.toString (#17218) (#17225)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Collovati <marco@vaadin.com>
  • Loading branch information
vaadin-bot and mcollovati committed Jul 14, 2023
1 parent f0ea667 commit c4ef634
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -886,9 +886,11 @@ public List<String> getKeys() {
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[");
builder.append(Arrays.stream(listenOnComponents)
.map(component -> component.getClass().getSimpleName())
.collect(Collectors.joining(",")));
if (listenOnComponents != null) {
builder.append(Arrays.stream(listenOnComponents)
.map(component -> component.getClass().getSimpleName())
.collect(Collectors.joining(",")));
}
builder.append("]");
return String.format(
"%s [key = %s, modifiers = %s, owner = %s, listenOn = %s, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,18 @@ public void reattachComponent_detachListenerIsAddedOnEveryAttach_listenOnUIIsClo
Assert.assertTrue(hasKeyAInKeyDownExpression(newUI));
}

@Test
public void toString_listenOnComponentsNotInitialized_doesNotFail() {
ShortcutRegistration registration = new ShortcutRegistration(
lifecycleOwner, () -> listenOn, event -> {
}, Key.KEY_A);
Assert.assertTrue(registration.toString().contains("listenOn = []"));

clientResponse();
Assert.assertTrue(
registration.toString().matches(".*listenOn = \\[[^]]+],.*"));
}

private Element mockLifecycle(boolean visible) {
Mockito.when(lifecycleOwner.isVisible()).thenReturn(visible);
Element element = ElementFactory.createAnchor();
Expand Down

0 comments on commit c4ef634

Please sign in to comment.