Repository: tapestry-5 Updated Branches: refs/heads/master 2b6debc3c -> 0619d96b4
TAP-2490: use the value encoder from the EventContext to convert client-side context values back Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/0619d96b Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/0619d96b Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/0619d96b Branch: refs/heads/master Commit: 0619d96b4006cb2108299163aaf8c958891408e0 Parents: 2b6debc Author: Jochen Kemnade <[email protected]> Authored: Fri Nov 6 09:11:36 2015 +0100 Committer: Jochen Kemnade <[email protected]> Committed: Fri Nov 6 09:36:06 2015 +0100 ---------------------------------------------------------------------- .../tapestry5/corelib/components/Select.java | 27 ++++-- .../tapestry5/corelib/mixins/Autocomplete.java | 33 +++++--- .../corelib/components/SelectTest.java | 87 ++++++++++++++++++++ 3 files changed, 126 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0619d96b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java index 3f5a7f4..9be23ab 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java @@ -18,6 +18,7 @@ import org.apache.tapestry5.corelib.base.AbstractField; import org.apache.tapestry5.corelib.data.BlankOption; import org.apache.tapestry5.corelib.data.SecureOption; import org.apache.tapestry5.corelib.mixins.RenderDisabled; +import org.apache.tapestry5.internal.AbstractEventContext; import org.apache.tapestry5.internal.InternalComponentResources; import org.apache.tapestry5.internal.TapestryInternalUtils; import org.apache.tapestry5.internal.util.CaptureResultCallback; @@ -263,7 +264,7 @@ public class Select extends AbstractField } } - Object onChange(final List<Object> context, + Object onChange(final EventContext context, @RequestParameter(value = "t:selectvalue", allowBlank = true) final String selectValue) throws ValidationException { @@ -271,15 +272,25 @@ public class Select extends AbstractField CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>(); - Object[] newContext = new Object[context.size() + 1]; - newContext[0] = newValue; - for (int i = 1; i < newContext.length; i++) - { - newContext[i] = context.get(i - 1); - } + EventContext newContext = new AbstractEventContext() { + + @Override + public int getCount() { + return context.getCount() + 1; + } + + @Override + public <T> T get(Class<T> desiredType, int index) { + if (index == 0) + { + return typeCoercer.coerce(newValue, desiredType); + } + return context.get(desiredType, index-1); + } + }; - this.resources.triggerEvent(EventConstants.VALUE_CHANGED, newContext, callback); + this.resources.triggerContextEvent(EventConstants.VALUE_CHANGED, newContext, callback); this.value = newValue; http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0619d96b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/Autocomplete.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/Autocomplete.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/Autocomplete.java index 1dfcd3d..6284632 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/Autocomplete.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/Autocomplete.java @@ -14,6 +14,7 @@ package org.apache.tapestry5.corelib.mixins; import org.apache.tapestry5.*; import org.apache.tapestry5.annotations.*; +import org.apache.tapestry5.internal.AbstractEventContext; import org.apache.tapestry5.internal.util.Holder; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.ioc.services.TypeCoercer; @@ -130,8 +131,8 @@ public class Autocomplete jsSupport.require("t5/core/autocomplete").with(spec); } - Object onAutocomplete(List<String> context, @RequestParameter("t:input") - String input) + Object onAutocomplete(final EventContext context, @RequestParameter("t:input") + final String input) { final Holder<List> matchesHolder = Holder.create(); @@ -151,19 +152,25 @@ public class Autocomplete } }; - Object[] newContext; - if (context.size() == 0) { - newContext = new Object[] {input}; - } - else { - newContext = new Object[context.size() + 1]; - newContext[0] = input; - for (int i = 1; i < newContext.length; i++) { - newContext[i] = context.get(i - 1); + EventContext newContext = new AbstractEventContext() { + + @Override + public int getCount() { + return context.getCount() + 1; + } + + @Override + public <T> T get(Class<T> desiredType, int index) { + if (index == 0) + { + return coercer.coerce(input, desiredType); } - } + return context.get(desiredType, index-1); + } + }; + - resources.triggerEvent(EventConstants.PROVIDE_COMPLETIONS, newContext, callback); + resources.triggerContextEvent(EventConstants.PROVIDE_COMPLETIONS, newContext, callback); JSONObject reply = new JSONObject(); http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/0619d96b/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java index f96e957..6456384 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java @@ -13,6 +13,7 @@ package org.apache.tapestry5.corelib.components; import org.apache.tapestry5.*; +import org.apache.tapestry5.corelib.components.SelectTest.Platform; import org.apache.tapestry5.corelib.data.BlankOption; import org.apache.tapestry5.corelib.data.SecureOption; import org.apache.tapestry5.dom.XMLMarkupModel; @@ -21,6 +22,8 @@ import org.apache.tapestry5.internal.OptionGroupModelImpl; import org.apache.tapestry5.internal.OptionModelImpl; import org.apache.tapestry5.internal.SelectModelImpl; import org.apache.tapestry5.internal.TapestryInternalUtils; +import org.apache.tapestry5.internal.URLEventContext; +import org.apache.tapestry5.internal.services.ContextValueEncoderImpl; import org.apache.tapestry5.internal.services.MarkupWriterImpl; import org.apache.tapestry5.internal.services.StringValueEncoder; import org.apache.tapestry5.internal.test.InternalBaseTestCase; @@ -28,11 +31,14 @@ import org.apache.tapestry5.internal.util.Holder; import org.apache.tapestry5.ioc.Messages; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.services.TypeCoercer; +import org.apache.tapestry5.services.ContextValueEncoder; import org.apache.tapestry5.services.Request; import org.apache.tapestry5.services.ValueEncoderSource; import org.apache.tapestry5.util.EnumSelectModel; +import org.apache.tapestry5.util.EnumValueEncoder; import org.easymock.EasyMock; import org.easymock.IAnswer; +import org.easymock.IArgumentMatcher; import org.testng.annotations.Test; import java.io.BufferedInputStream; @@ -644,4 +650,85 @@ public class SelectTest extends InternalBaseTestCase } + @Test + public void context_that_needs_to_be_encoded() throws Exception + { + + ValueEncoderSource valueEncoderSource = mockValueEncoderSource(); + + TypeCoercer typeCoercer = getService(TypeCoercer.class); + + ContextValueEncoder contextValueEncoder = new ContextValueEncoderImpl(valueEncoderSource); + + ValueEncoder<Platform> platformEncoder = new ValueEncoder<SelectTest.Platform>() { + + @Override + public Platform toValue(String clientValue) { + return Platform.valueOf(clientValue.substring(10)); + } + + @Override + public String toClient(Platform value) { + return "Platform: "+value.name(); + } + }; + + InternalComponentResources resources = mockInternalComponentResources(); + expect(valueEncoderSource.getValueEncoder(Platform.class)).andReturn(platformEncoder).anyTimes(); + expect(valueEncoderSource.getValueEncoder(String.class)).andReturn(new StringValueEncoder()).anyTimes(); + + expect(resources.triggerContextEvent(EasyMock.eq(EventConstants.VALUE_CHANGED), eqEventContext(null, Platform.LINUX), EasyMock.isA(ComponentEventCallback.class))).andReturn(true); + + + Select select = new Select(); + + set(select, "resources", resources); + set(select, "encoder", new StringValueEncoder()); + set(select, "typeCoercer", typeCoercer); + + replay(); + + select.onChange(new URLEventContext(contextValueEncoder, new String[]{platformEncoder.toClient(Platform.LINUX)}), null); + + verify(); + } + + private static EventContext eqEventContext(final Object ... expectedContext) + { + EasyMock.reportMatcher(new IArgumentMatcher() { + + @Override + public boolean matches(Object argument) + { + EventContext context = (EventContext) argument; + for (int i = 0; i < expectedContext.length; i++) + { + Object expected = expectedContext[i]; + Class expectedClass = expected == null ? Object.class : expected.getClass(); + + if (!TapestryInternalUtils.isEqual(context.get(expectedClass, i), expected)) + { + return false; + } + } + return true; + } + + @Override + public void appendTo(StringBuffer buffer) + { + buffer.append("expected event context ["); + for (int i = 0; i < expectedContext.length; i++) + { + if (i != 0) + { + buffer.append(", "); + } + buffer.append(expectedContext[i]); + } + buffer.append("]"); + } + }); + return null; + } }
