Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerMethodInvokerImpl.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerMethodInvokerImpl.java?rev=399861&r1=399860&r2=399861&view=diff ============================================================================== --- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerMethodInvokerImpl.java (original) +++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerMethodInvokerImpl.java Thu May 4 14:34:07 2006 @@ -25,19 +25,20 @@ import org.apache.tapestry.engine.ILink; /** - * Logic for mapping a listener method name to an actual method invocation; this may require a - * little searching to find the correct version of the method, based on the number of parameters to - * the method (there's a lot of flexibility in terms of what methods may be considered a listener - * method). + * Logic for mapping a listener method name to an actual method invocation; this + * may require a little searching to find the correct version of the method, + * based on the number of parameters to the method (there's a lot of flexibility + * in terms of what methods may be considered a listener method). * * @author Howard M. Lewis Ship * @since 4.0 */ public class ListenerMethodInvokerImpl implements ListenerMethodInvoker { + /** - * Methods with a name appropriate for this class, sorted into descending order by number of - * parameters. + * Methods with a name appropriate for this class, sorted into descending + * order by number of parameters. */ private final Method[] _methods; @@ -77,22 +78,22 @@ if (searchAndInvoke(target, true, false, cycle, listenerParameters)) return; - throw new ApplicationRuntimeException(ListenerMessages.noListenerMethodFound( - _name, - listenerParameters, - target), target, null, null); + throw new ApplicationRuntimeException(ListenerMessages + .noListenerMethodFound(_name, listenerParameters, target), + target, null, null); } - private boolean searchAndInvoke(Object target, boolean includeCycle, boolean includeParameters, - IRequestCycle cycle, Object[] listenerParameters) + private boolean searchAndInvoke(Object target, boolean includeCycle, + boolean includeParameters, IRequestCycle cycle, + Object[] listenerParameters) { int listenerParameterCount = Tapestry.size(listenerParameters); - int methodParameterCount = includeParameters ? listenerParameterCount : 0; + int methodParameterCount = includeParameters ? listenerParameterCount + : 0; - if (includeCycle) - methodParameterCount++; + if (includeCycle) methodParameterCount++; - for (int i = 0; i < _methods.length; i++) + for(int i = 0; i < _methods.length; i++) { Method m = _methods[i]; @@ -101,11 +102,9 @@ Class[] parameterTypes = m.getParameterTypes(); - if (parameterTypes.length < methodParameterCount) - break; + if (parameterTypes.length < methodParameterCount) break; - if (parameterTypes.length != methodParameterCount) - continue; + if (parameterTypes.length != methodParameterCount) continue; boolean firstIsCycle = parameterTypes.length > 0 && parameterTypes[0] == IRequestCycle.class; @@ -118,16 +117,10 @@ // any methods whose first parameter is the request cycle // (we'll catch those in a later search). - if (includeCycle != firstIsCycle) - continue; + if (includeCycle != firstIsCycle) continue; - invokeListenerMethod( - m, - target, - includeCycle, - includeParameters, - cycle, - listenerParameters); + invokeListenerMethod(m, target, includeCycle, includeParameters, + cycle, listenerParameters); return true; } @@ -135,24 +128,25 @@ return false; } - private void invokeListenerMethod(Method listenerMethod, Object target, boolean includeCycle, - boolean includeParameters, IRequestCycle cycle, Object[] listenerParameters) + private void invokeListenerMethod(Method listenerMethod, Object target, + boolean includeCycle, boolean includeParameters, + IRequestCycle cycle, Object[] listenerParameters) { Object[] parameters = new Object[listenerMethod.getParameterTypes().length]; int cursor = 0; - if (includeCycle) - parameters[cursor++] = cycle; + if (includeCycle) parameters[cursor++] = cycle; if (includeParameters) - for (int i = 0; i < Tapestry.size(listenerParameters); i++) + for(int i = 0; i < Tapestry.size(listenerParameters); i++) parameters[cursor++] = listenerParameters[i]; Object methodResult = null; try { - methodResult = invokeTargetMethod(target, listenerMethod, parameters); + methodResult = invokeTargetMethod(target, listenerMethod, + parameters); } catch (InvocationTargetException ex) { @@ -161,24 +155,21 @@ if (targetException instanceof ApplicationRuntimeException) throw (ApplicationRuntimeException) targetException; - throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure( - listenerMethod, - target, - targetException), target, null, targetException); + throw new ApplicationRuntimeException(ListenerMessages + .listenerMethodFailure(listenerMethod, target, + targetException), target, null, targetException); } catch (Exception ex) { - throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure( - listenerMethod, - target, - ex), target, null, ex); + throw new ApplicationRuntimeException(ListenerMessages + .listenerMethodFailure(listenerMethod, target, ex), target, + null, ex); } // void methods return null - if (methodResult == null) - return; + if (methodResult == null) return; // The method scanner, inside ListenerMapSourceImpl, // ensures that only methods that return void, String, @@ -204,13 +195,14 @@ } /** - * Provided as a hook so that subclasses can perform any additional work before or after - * invoking the listener method. + * Provided as a hook so that subclasses can perform any additional work + * before or after invoking the listener method. */ - protected Object invokeTargetMethod(Object target, Method listenerMethod, Object[] parameters) - throws IllegalAccessException, InvocationTargetException + protected Object invokeTargetMethod(Object target, Method listenerMethod, + Object[] parameters) + throws IllegalAccessException, InvocationTargetException { return listenerMethod.invoke(target, parameters); } -} \ No newline at end of file +}
Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerStrings.properties URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerStrings.properties?rev=399861&r1=399860&r2=399861&view=diff ============================================================================== --- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerStrings.properties (original) +++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/ListenerStrings.properties Thu May 4 14:34:07 2006 @@ -15,4 +15,4 @@ object-missing-method=Object {0} does not implement a listener method named ''{1}''. unable-to-invoke-method=Unable to invoke method {0} on {1}: {2} listener-method-failure=Failure invoking listener method ''{0}'' on {1}: {2} -no-listener-method-found=No listener method named ''{0}'' suitable for {1,choice,0#no listener parameters|1#one listener parameter|1<{1,number} listener parameters} found in {2}. \ No newline at end of file +no-listener-method-found=No listener method named ''{0}'' suitable for {1,choice,0#no listener parameters|1#one listener parameter|1<{1,number} listener parameters} found in {2}. Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/SyntheticListener.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/SyntheticListener.java?rev=399861&r1=399860&r2=399861&view=diff ============================================================================== --- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/SyntheticListener.java (original) +++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/listener/SyntheticListener.java Thu May 4 14:34:07 2006 @@ -21,18 +21,20 @@ /** * Adapter class that combines a target object (typically, a component) with a - * [EMAIL PROTECTED] org.apache.tapestry.listener.ListenerMethodInvoker}. This is the bridge from listener - * method names to listener method invocations. + * [EMAIL PROTECTED] org.apache.tapestry.listener.ListenerMethodInvoker}. This is the + * bridge from listener method names to listener method invocations. * <p> - * TODO: It would really be nice if we could get the location of the listener binding into thrown - * exceptions. As implemented, as best, it will be the location of the <page-specification> - * (or <component>) of the page (or component) containing the listener method. + * TODO: It would really be nice if we could get the location of the listener + * binding into thrown exceptions. As implemented, as best, it will be the + * location of the <page-specification> (or <component>) of the page + * (or component) containing the listener method. * * @author Howard M. Lewis Ship * @since 4.0 */ public class SyntheticListener implements IActionListener { + private final Object _target; private final ListenerMethodInvoker _invoker; @@ -50,4 +52,4 @@ { _invoker.invokeListenerMethod(_target, cycle); } -} \ No newline at end of file +} Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/BaseComponentTestCase.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/BaseComponentTestCase.java?rev=399861&r1=399860&r2=399861&view=diff ============================================================================== --- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/BaseComponentTestCase.java (original) +++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/BaseComponentTestCase.java Thu May 4 14:34:07 2006 @@ -29,7 +29,9 @@ import org.apache.tapestry.engine.IEngineService; import org.apache.tapestry.engine.ILink; import org.apache.tapestry.engine.NullWriter; +import org.apache.tapestry.json.IJSONWriter; import org.apache.tapestry.markup.AsciiMarkupFilter; +import org.apache.tapestry.markup.JSONWriterImpl; import org.apache.tapestry.markup.MarkupWriterImpl; import org.apache.tapestry.services.ResponseBuilder; import org.apache.tapestry.services.impl.DefaultResponseBuilder; @@ -69,7 +71,15 @@ return new MarkupWriterImpl("text/html", pw, new AsciiMarkupFilter()); } - + + protected IJSONWriter newBufferJSONWriter() + { + _charArrayWriter = new CharArrayWriter(); + PrintWriter pw = new PrintWriter(_charArrayWriter); + + return new JSONWriterImpl(pw); + } + protected void assertBuffer(String expected) { String actual = _charArrayWriter.toString(); Added: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java?rev=399861&view=auto ============================================================================== --- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java (added) +++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java Thu May 4 14:34:07 2006 @@ -0,0 +1,246 @@ +// Copyright 2005 The Apache Software Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.tapestry.dojo.form; + +import java.util.HashMap; + +import org.apache.hivemind.test.AggregateArgumentsMatcher; +import org.apache.hivemind.test.ArgumentMatcher; +import org.apache.tapestry.IForm; +import org.apache.tapestry.IMarkupWriter; +import org.apache.tapestry.IRequestCycle; +import org.apache.tapestry.IScript; +import org.apache.tapestry.IgnoreMatcher; +import org.apache.tapestry.PageRenderSupport; +import org.apache.tapestry.engine.DirectServiceParameter; +import org.apache.tapestry.engine.IEngineService; +import org.apache.tapestry.engine.ILink; +import org.apache.tapestry.form.BaseFormComponentTestCase; +import org.apache.tapestry.form.MockDelegate; +import org.apache.tapestry.form.StringPropertySelectionModel; +import org.apache.tapestry.form.ValidatableFieldSupport; +import org.apache.tapestry.json.IJSONWriter; +import org.apache.tapestry.valid.IValidationDelegate; +import org.apache.tapestry.valid.ValidatorException; +import org.easymock.MockControl; + +/** + * Tests for [EMAIL PROTECTED] org.apache.tapestry.form.TextField}. + * + * @author Howard M. Lewis Ship + * @since 4.0 + */ +public class TestAutocompleter extends BaseFormComponentTestCase +{ + public void testRewind() + { + String[] values = { "red", "green", "blue" }; + StringPropertySelectionModel model = new StringPropertySelectionModel(values); + + MockControl vfsc = newControl(ValidatableFieldSupport.class); + ValidatableFieldSupport vfs = (ValidatableFieldSupport) vfsc.getMock(); + + Autocompleter component = (Autocompleter) newInstance(Autocompleter.class, new Object[] + { "model", model, "validatableFieldSupport", vfs }); + + MockControl cyclec = newControl(IRequestCycle.class); + IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); + + MockControl formc = newControl(IForm.class); + IForm form = (IForm) formc.getMock(); + + IMarkupWriter writer = newWriter(); + + IValidationDelegate delegate = newDelegate(); + + trainGetForm(cycle, form); + trainWasPrerendered(form, writer, component, false); + trainGetDelegate(form, delegate); + + delegate.setFormComponent(component); + + trainGetElementId(form, component, "barney"); + trainIsRewinding(form, true); + + String key = "0"; + String value = values[0]; + + trainGetParameter(cycle, "barney_selected", key); + + try + { + vfs.validate(component, writer, cycle, value); + } + catch (ValidatorException e) + { + unreachable(); + } + + replayControls(); + + component.render(writer, cycle); + + verifyControls(); + + assertEquals(values[0], component.getValue()); + } + + public void testRewindNotForm() + { + Autocompleter component = (Autocompleter) newInstance(Autocompleter.class); + + MockControl cyclec = newControl(IRequestCycle.class); + IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); + + MockControl formc = newControl(IForm.class); + IForm form = (IForm) formc.getMock(); + + IMarkupWriter writer = newWriter(); + + IValidationDelegate delegate = newDelegate(); + + trainGetForm(cycle, form); + trainWasPrerendered(form, writer, component, false); + trainGetDelegate(form, delegate); + + delegate.setFormComponent(component); + + trainGetElementId(form, component, "barney"); + trainIsRewinding(form, false); + trainIsRewinding(cycle, true); + + replayControls(); + + component.render(writer, cycle); + + verifyControls(); + } + + public void testRender() + { + String[] values = { "red", "green", "blue" }; + StringPropertySelectionModel model = new StringPropertySelectionModel(values); + + ValidatableFieldSupport vfs = (ValidatableFieldSupport) newMock(ValidatableFieldSupport.class); + + MockControl cyclec = newControl(IRequestCycle.class); + IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); + + MockControl formc = newControl(IForm.class); + IForm form = (IForm) formc.getMock(); + + IMarkupWriter writer = newBufferWriter(); + + MockDelegate delegate = new MockDelegate(); + + IEngineService engine = newEngineService(); + ILink link = newLink(); + + IScript script = (IScript)newMock(IScript.class); + + Autocompleter component = (Autocompleter) newInstance(Autocompleter.class, new Object[] + { "name", "fred", "model", model, + "directService", engine, + "script", script, + "validatableFieldSupport", vfs, + "value", values[1] }); + + DirectServiceParameter dsp = + new DirectServiceParameter(component, new Object[]{}, + new String[]{"fred"}, true); + + trainGetForm(cycle, form); + trainWasPrerendered(form, writer, component, false); + trainGetDelegate(form, delegate); + + delegate.setFormComponent(component); + + trainGetElementId(form, component, "fred"); + trainIsRewinding(form, false); + trainIsRewinding(cycle, false); + + delegate.setFormComponent(component); + + trainGetDelegate(form, delegate); + trainGetDelegate(form, delegate); + trainGetDelegate(form, delegate); + + vfs.renderContributions(component, writer, cycle); + + trainGetLinkCheckIgnoreParameter(engine, cycle, true, dsp, link); + + trainGetURL(link, "urlstring"); + + PageRenderSupport prs = newPageRenderSupport(); + trainGetPageRenderSupport(cycle, prs); + + script.execute(cycle, prs, new HashMap()); + + ArgumentMatcher ignore = new IgnoreMatcher(); + getControl(script).setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] + { null, null, ignore })); + + replayControls(); + + component.render(writer, cycle); + + verifyControls(); + + assertBuffer("<span class=\"prefix\"><select name=\"fred\" class=\"validation-delegate\"></select></span>"); + } + + public void testRenderJSON() + { + String[] values = { "red", "green", "blue", "yellow" }; + StringPropertySelectionModel model = new StringPropertySelectionModel(values); + + MockControl cyclec = newControl(IRequestCycle.class); + IRequestCycle cycle = (IRequestCycle) cyclec.getMock(); + + IJSONWriter json = newBufferJSONWriter(); + + Autocompleter component = (Autocompleter) newInstance(Autocompleter.class, new Object[] + { "name", "fred", "model", model, + "filter", "l" }); + + replayControls(); + + component.renderComponent(json, cycle); + + verifyControls(); + + assertEquals(json.length(), 2); + assertEquals(json.get("3"), "yellow"); + assertEquals(json.get("2"), "blue"); + } + + public void testIsRequired() + { + MockControl supportc = newControl(ValidatableFieldSupport.class); + ValidatableFieldSupport support = (ValidatableFieldSupport) supportc.getMock(); + + Autocompleter field = (Autocompleter) newInstance(Autocompleter.class, new Object[] + { "validatableFieldSupport", support, }); + + support.isRequired(field); + supportc.setReturnValue(true); + + replayControls(); + + assertEquals(true, field.isRequired()); + + verifyControls(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]