Repository: wicket Updated Branches: refs/heads/wicket-6.x cc9d3d7b2 -> 273814a95
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/273814a9 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/273814a9 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/273814a9 Branch: refs/heads/wicket-6.x Commit: 273814a957d2a7c742ff2ced3de4372f9a955eb7 Parents: cc9d3d7 Author: Alexander Morozov <[email protected]> Authored: Tue Apr 14 13:39:31 2015 +0600 Committer: Sven Meier <[email protected]> Committed: Tue Apr 14 10:20:31 2015 +0200 ---------------------------------------------------------------------- .../wicket/markup/html/form/FormComponent.java | 9 ++- .../html/form/CollectionFormComponentTest.java | 69 +++++++++++++++++--- 2 files changed, 63 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/273814a9/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 051317d..f0c656d 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 @@ -1616,6 +1616,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) @@ -1632,10 +1635,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) @@ -1645,7 +1645,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<S>(convertedInput); } http://git-wip-us.apache.org/repos/asf/wicket/blob/273814a9/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 7451daf..1b2ed5d 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; @@ -91,7 +93,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = new ArrayList<String>(); - + 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); } }; @@ -119,7 +121,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = new ArrayList<String>(); - + public Collection<String> getStrings() { return internal; @@ -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()); } @@ -144,7 +146,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = new ArrayList<String>(); - + public Collection<String> getStrings() { return Collections.unmodifiableCollection(internal); @@ -153,7 +155,7 @@ public class CollectionFormComponentTest extends WicketTestCase public void setStrings(Collection<String> strings) { this.internal = strings; - + setCalled.set(true); } }; @@ -172,7 +174,7 @@ public class CollectionFormComponentTest extends WicketTestCase Object object = new Object() { private Collection<String> internal = new ArrayList<String>(); - + public Collection<String> getStrings() { return Collections.unmodifiableCollection(internal); @@ -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")); } } + }
