Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: throw when setting value that is not present in key mapper #6273

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ private void handleDataChange(DataChangeEvent<T> dataChangeEvent) {
item -> Objects.equals(getItemId(item.item), otherItemId))
.findFirst().ifPresent(this::updateCheckbox);
} else {
keyMapper.removeAll();
selectionPreservationHandler.handleDataChange(dataChangeEvent);
keyMapper.removeAll();
rebuild();
}
}
Expand Down Expand Up @@ -424,6 +424,12 @@ public void setValue(Set<T> value) {
Objects.requireNonNull(value,
"Cannot set a null value to checkbox group. "
+ "Use the clear-method to reset the component's value to an empty set.");

if (value.stream().anyMatch(item -> !keyMapper.has(item))) {
throw new IllegalArgumentException(
"Value must be one of the items in CheckboxGroup");
}

super.setValue(value);
refreshCheckboxes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ public void withoutSettingIdentifierProvider_setItemWithNullId_shouldSelectCorre
Assert.assertArrayEquals(new long[] { 2L }, selectedIds);
}

@Test
public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExistingItemById() {
@Test(expected = IllegalArgumentException.class)
public void setIdentifierProviderOnId_setItemWithNullId_throws() {
CustomItem first = new CustomItem(1L, "First");
CustomItem second = new CustomItem(2L, "Second");
CustomItem third = new CustomItem(3L, "Third");
Expand All @@ -540,8 +540,6 @@ public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExisti

checkboxGroup
.setValue(Collections.singleton(new CustomItem(null, "First")));
Assert.assertNull(checkboxGroup.getSelectedItems().stream().findFirst()
.get().getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class RadioButtonGroup<T>
HasThemeVariant<RadioGroupVariant>, HasValidationProperties,
HasValidator<T>, SingleSelect<RadioButtonGroup<T>, T> {

private final KeyMapper<T> keyMapper = new KeyMapper<>();
private final KeyMapper<T> keyMapper = new KeyMapper<>(this::getItemId);

private final AtomicReference<DataProvider<T, ?>> dataProvider = new AtomicReference<>(
DataProvider.ofItems());
Expand Down Expand Up @@ -121,6 +121,9 @@ private static <T> T presentationToModel(

private static <T> String modelToPresentation(
RadioButtonGroup<T> radioButtonGroup, T model) {
if (model == null) {
return "";
}
if (!radioButtonGroup.keyMapper.has(model)) {
return null;
}
Expand Down Expand Up @@ -374,6 +377,11 @@ public ItemLabelGenerator<T> getItemLabelGenerator() {

@Override
public void setValue(T value) {
if (value != null && !keyMapper.has(value)) {
throw new IllegalArgumentException(
"Value must be one of the items in RadioButtonGroup");
}

super.setValue(value);
if (value == null) {
getRadioButtons().forEach(rb -> rb.setChecked(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ public void withoutSettingIdentifierProvider_setItemWithNullId_shouldSelectCorre
radioButtonGroup.getValue().getId());
}

@Test
public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExistingItemById() {
@Test(expected = IllegalArgumentException.class)
public void setIdentifierProviderOnId_setItemWithNullId_throws() {
CustomItem first = new CustomItem(1L, "First");
CustomItem second = new CustomItem(2L, "Second");
CustomItem third = new CustomItem(3L, "Third");
Expand All @@ -438,7 +438,6 @@ public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExisti
listDataView.setIdentifierProvider(CustomItem::getId);

radioButtonGroup.setValue(new CustomItem(null, "First"));
Assert.assertNull(radioButtonGroup.getValue().getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,16 @@ protected boolean valueEquals(T value1, T value2) {
return getItemId(value1).equals(getItemId(value2));
}

@Override
public void setValue(T value) {
if (value != null && !keyMapper.has(value)) {
throw new IllegalArgumentException(
"Value must be one of the items in Select");
}

super.setValue(value);
}

private void initConnector() {
runBeforeClientResponse(ui -> {
ui.getPage().executeJs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,8 @@ public void withoutSettingIdentifierProvider_setItemWithNullId_shouldSelectCorre
Assert.assertEquals(Long.valueOf(2L), select.getValue().getId());
}

@Test
public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExistingItemById() {
@Test(expected = IllegalArgumentException.class)
public void setIdentifierProviderOnId_setItemWithNullId_throws() {
CustomItem first = new CustomItem(1L, "First");
CustomItem second = new CustomItem(2L, "Second");
CustomItem third = new CustomItem(3L, "Third");
Expand All @@ -787,7 +787,6 @@ public void setIdentifierProviderOnId_setItemWithNullId_shouldFailToSelectExisti
listDataView.setIdentifierProvider(CustomItem::getId);

select.setValue(new CustomItem(null, "First"));
Assert.assertNull(select.getValue().getId());
}

@Test
Expand Down