Repository: wicket Updated Branches: refs/heads/master c076c07fc -> 8c83c5c50
WICKET-5881 FormComponent#updateCollectionModel must check convertedInput for null before wrapping into ArrayListFormComponent#updateCollectionModel must check convertedInput for null before wrapping into ArrayList Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/8c83c5c5 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/8c83c5c5 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/8c83c5c5 Branch: refs/heads/master Commit: 8c83c5c50161b040fab0b748232a4fb5e7855e68 Parents: c076c07 Author: Alexander Morozov <[email protected]> Authored: Tue Apr 14 13:39:31 2015 +0600 Committer: Sven Meier <[email protected]> Committed: Tue Apr 14 12:25:17 2015 +0200 ---------------------------------------------------------------------- .../wicket/markup/html/form/FormComponent.java | 9 ++- .../html/form/CollectionFormComponentTest.java | 61 ++++++++++++++++++-- 2 files changed, 59 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/8c83c5c5/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java index 88c5350..c0f4f10 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java @@ -1613,6 +1613,9 @@ public abstract class FormComponent<T> extends LabeledWebMarkupContainer impleme public static <S> void updateCollectionModel(FormComponent<Collection<S>> formComponent) { Collection<S> convertedInput = formComponent.getConvertedInput(); + if (convertedInput == null) { + convertedInput = Collections.emptyList(); + } Collection<S> collection = formComponent.getModelObject(); if (collection == null) @@ -1629,10 +1632,7 @@ public abstract class FormComponent<T> extends LabeledWebMarkupContainer impleme try { collection.clear(); - if (convertedInput != null) - { - collection.addAll(convertedInput); - } + collection.addAll(convertedInput); modified = true; } catch (UnsupportedOperationException unmodifiable) @@ -1642,7 +1642,6 @@ public abstract class FormComponent<T> extends LabeledWebMarkupContainer impleme logger.debug("An error occurred while trying to modify the collection attached to " + formComponent, unmodifiable); } - collection = new ArrayList<>(convertedInput); } http://git-wip-us.apache.org/repos/asf/wicket/blob/8c83c5c5/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CollectionFormComponentTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CollectionFormComponentTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CollectionFormComponentTest.java index 81f4e80..80849df 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CollectionFormComponentTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CollectionFormComponentTest.java @@ -24,7 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.wicket.WicketRuntimeException; import org.apache.wicket.WicketTestCase; +import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.model.PropertyModel; +import org.junit.Assert; import org.junit.Test; /** @@ -43,7 +45,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = null; - + public Collection<String> getStrings() { return internal; @@ -52,7 +54,7 @@ public class CollectionFormComponentTest extends WicketTestCase public void setStrings(Collection<String> strings) { this.internal = strings; - + setCalled.set(true); } }; @@ -71,7 +73,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = null; - + public Collection<String> getStrings() { return internal; @@ -100,7 +102,7 @@ public class CollectionFormComponentTest extends WicketTestCase public void setStrings(Collection<String> strings) { this.internal = strings; - + setCalled.set(true); } }; @@ -129,7 +131,7 @@ public class CollectionFormComponentTest extends WicketTestCase Choice choice = new Choice(object); choice.setConvertedInput(Arrays.asList("A", "B")); FormComponent.updateCollectionModel(choice); - + assertEquals("[A, B]", choice.getDefaultModelObjectAsString()); } @@ -153,7 +155,7 @@ public class CollectionFormComponentTest extends WicketTestCase public void setStrings(Collection<String> strings) { this.internal = strings; - + setCalled.set(true); } }; @@ -184,6 +186,52 @@ public class CollectionFormComponentTest extends WicketTestCase FormComponent.updateCollectionModel(choice); } + @Test + public void getUnmodifiableInCaseOfNoConvertedInput() + { + LoadableDetachableModel<Collection<String>> model = new LoadableDetachableModel<Collection<String>>() + { + + @Override + protected Collection<String> load() + { + return Collections.unmodifiableList(Arrays.asList("1", "2")); + } + + }; + FormComponent<Collection<String>> formComponent = new FormComponent<Collection<String>>( + "formComponent", model) + { + + }; + formComponent.setConvertedInput(null); + FormComponent.updateCollectionModel(formComponent); + Assert.assertTrue(formComponent.getModelObject().isEmpty()); + } + + @Test + public void getModelCollectionIsNullInCaseOfNoConvertedInput() + { + LoadableDetachableModel<Collection<String>> model = new LoadableDetachableModel<Collection<String>>() + { + + @Override + protected Collection<String> load() + { + return null; + } + + }; + FormComponent<Collection<String>> formComponent = new FormComponent<Collection<String>>( + "formComponent", model) + { + + }; + formComponent.setConvertedInput(null); + FormComponent.updateCollectionModel(formComponent); + Assert.assertTrue(formComponent.getModelObject().isEmpty()); + } + private class Choice extends FormComponent<Collection<String>> { @@ -192,4 +240,5 @@ public class CollectionFormComponentTest extends WicketTestCase super("choice", new PropertyModel(object, "strings")); } } + }
