Skip to content

Commit

Permalink
fix: handle invalid widget index (#6584)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin committed Oct 10, 2024
1 parent 6f73b21 commit 4656bf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ public void addWidgetAtIndex(int index, DashboardWidget widget) {
throw new IllegalArgumentException(
"Cannot add a widget with a negative index.");
}
// The case when the index is bigger than the children count is handled
// inside the method below
if (index > widgets.size()) {
throw new IllegalArgumentException(String.format(
"Cannot add a widget with index %d when there are %d widgets",
index, widgets.size()));
}
doAddWidget(index, widget);
updateClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ public void addWidgetAtIndex_widgetIsCorrectlyAdded() {
assertWidgets(dashboard, widget1, widget3, widget2);
}

@Test
public void addWidgetAtInvalidIndex_exceptionIsThrown() {
DashboardWidget widget1 = new DashboardWidget();
DashboardWidget widget2 = new DashboardWidget();
dashboard.add(widget1);
fakeClientCommunication();
Assert.assertThrows(IllegalArgumentException.class,
() -> dashboard.addWidgetAtIndex(2, widget2));
fakeClientCommunication();
assertWidgets(dashboard, widget1);
}

@Test
public void addWidgetAtNegativeIndex_exceptionIsThrown() {
DashboardWidget widget = new DashboardWidget();
Assert.assertThrows(IllegalArgumentException.class,
() -> dashboard.addWidgetAtIndex(-1, widget));
fakeClientCommunication();
assertWidgets(dashboard);
}

@Test
public void addNullWidgetAtIndex_exceptionIsThrown() {
Assert.assertThrows(NullPointerException.class,
Expand Down

0 comments on commit 4656bf0

Please sign in to comment.