You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by jk...@apache.org on 2006/07/30 22:12:22 UTC

svn commit: r426933 - in /tapestry/tapestry4/trunk: src/site/xdoc/components/ tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/ tapestry-framework/src/java/org/apache/tapestry/dojo/form/ tapestry-framework/src/test/org/apache...

Author: jkuhnert
Date: Sun Jul 30 13:12:22 2006
New Revision: 426933

URL: http://svn.apache.org/viewvc?rev=426933&view=rev
Log:
Took out non existant "direct" parameter of Form component. 

Implemented new IAutocompleteModel / DefaultAutocompleteModel for Autocomplete component that use the IPrimaryKeyConverter interface
for efficient filtering/generation of selection lists. Fixes TAPESTRY-1029.

Added:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/DefaultAutocompleteModel.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/IAutocompleteModel.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/DefaultAutocompleteModelTest.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/SimpleBean.java
Modified:
    tapestry/tapestry4/trunk/src/site/xdoc/components/Form.xml
    tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/TaskEntryPage.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/Autocompleter.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java

Modified: tapestry/tapestry4/trunk/src/site/xdoc/components/Form.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/src/site/xdoc/components/Form.xml?rev=426933&r1=426932&r2=426933&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/src/site/xdoc/components/Form.xml (original)
+++ tapestry/tapestry4/trunk/src/site/xdoc/components/Form.xml Sun Jul 30 13:12:22 2006
@@ -153,18 +153,6 @@
                         </td>
                     </tr>
                     <tr>
-                        <td>direct</td>
-                        <td>boolean</td>
-                        <td>in</td>
-                        <td>no</td>
-                        <td>true</td>
-                        <td>
-                            If true (the default), then the more efficient direct service is used.
-                            If false, then the action service is used. The action service requires
-                            rewinding of the entire page, and is rarely (if ever) used.
-                        </td>
-                    </tr>
-                    <tr>
                         <td>delegate</td>
                         <td>
                             <a

Modified: tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/TaskEntryPage.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/TaskEntryPage.java?rev=426933&r1=426932&r2=426933&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/TaskEntryPage.java (original)
+++ tapestry/tapestry4/trunk/tapestry-examples/TimeTracker/src/java/org/apache/tapestry/timetracker/page/TaskEntryPage.java Sun Jul 30 13:12:22 2006
@@ -21,10 +21,10 @@
 import org.apache.tapestry.annotations.InjectObject;
 import org.apache.tapestry.annotations.Persist;
 import org.apache.tapestry.dojo.form.Autocompleter;
+import org.apache.tapestry.dojo.form.DefaultAutocompleteModel;
 import org.apache.tapestry.dojo.form.DropdownDatePicker;
 import org.apache.tapestry.dojo.form.DropdownTimePicker;
-import org.apache.tapestry.form.BeanPropertySelectionModel;
-import org.apache.tapestry.form.IPropertySelectionModel;
+import org.apache.tapestry.dojo.form.IAutocompleteModel;
 import org.apache.tapestry.form.TextField;
 import org.apache.tapestry.html.BasePage;
 import org.apache.tapestry.timetracker.dao.ProjectDao;
@@ -89,9 +89,9 @@
      * Selection model for projects.
      * @return
      */
-    public IPropertySelectionModel getProjectModel()
+    public IAutocompleteModel getProjectModel()
     {
-        return new BeanPropertySelectionModel(getProjectDao().listProjects(), "name");
+        return new DefaultAutocompleteModel(getProjectDao().listProjects(), "id", "name");
     }
     
     /**

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/Autocompleter.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/Autocompleter.java?rev=426933&r1=426932&r2=426933&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/Autocompleter.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/Autocompleter.java Sun Jul 30 13:12:22 2006
@@ -16,6 +16,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -30,7 +31,6 @@
 import org.apache.tapestry.engine.DirectServiceParameter;
 import org.apache.tapestry.engine.IEngineService;
 import org.apache.tapestry.engine.ILink;
-import org.apache.tapestry.form.IPropertySelectionModel;
 import org.apache.tapestry.form.ValidatableField;
 import org.apache.tapestry.form.ValidatableFieldSupport;
 import org.apache.tapestry.json.IJSONWriter;
@@ -41,6 +41,9 @@
  * An html field similar to a <code>select</code> input field that 
  * is wrapped by a dojo ComboBox widget.
  * 
+ * This component uses the {@link IAutocompleteModel} to retrieve and match against
+ * selected values.
+ * 
  * @author jkuhnert
  */
 public abstract class Autocompleter extends AbstractFormWidget 
@@ -86,21 +89,17 @@
         json.put("widgetId", getName());
         json.put("name", getName());
         
-        IPropertySelectionModel model = getModel();
+        IAutocompleteModel model = getModel();
         if (model == null)
             throw Tapestry.createRequiredParameterException(this, "model");
         
-        int count = model.getOptionCount();
         Object value = getValue();
+        Object key = value != null ? model.getPrimaryKey(value) : null;
         
-        for (int i = 0; i < count; i++) {
-            Object option = model.getOption(i);
+        if (value != null && key != null) {
             
-            if (isEqual(option, value)) {
-                json.put("value", model.getValue(i));
-                json.put("label", model.getLabel(i));
-                break;
-            }
+            json.put("value", key);
+            json.put("label", model.getLabelFor(value));
         }
         
         parms.put("props", json.toString());
@@ -115,30 +114,26 @@
      */
     public void renderComponent(IJSONWriter writer, IRequestCycle cycle)
     {
-        IPropertySelectionModel model = getModel();
+        IAutocompleteModel model = getModel();
         
         if (model == null)
             throw Tapestry.createRequiredParameterException(this, "model");
         
-        int count = model.getOptionCount();
+        Map filteredValues = model.filterValues(getFilter());
         
-        for (int i = 0; i < count; i++)
-        {
-            String value = model.getValue(i);
-            String label = model.getLabel(i);
+        if (filteredValues == null)
+            return;
+        
+        Iterator it = filteredValues.keySet().iterator();
+        Object key = null;
+        
+        while (it.hasNext()) {
             
-            if (getFilter() == null || getFilter().trim().length() <= 0) {
-                writer.put(value, label);
-                continue;
-            }
+            key = it.next();
             
-            // primitive filter, for now
-            // TODO: Create filter interface in IPropertySelectionModel
-            if (getFilter() != null 
-                    && label.toLowerCase().indexOf(getFilter().toLowerCase()) > -1) {
-                writer.put(value, label);
-            }
+            writer.put(key.toString(), filteredValues.get(key));
         }
+        
     }
     
     /**
@@ -148,7 +143,7 @@
     {
         String value = cycle.getParameter(getName());
         
-        Object object = getModel().translateValue(value);
+        Object object = getModel().getValue(value);
         
         try
         {
@@ -162,23 +157,6 @@
         }
     }
     
-    private boolean isEqual(Object left, Object right)
-    {
-        // Both null, or same object, then are equal
-        
-        if (left == right)
-            return true;
-        
-        // If one is null, the other isn't, then not equal.
-        
-        if (left == null || right == null)
-            return false;
-        
-        // Both non-null; use standard comparison.
-        
-        return left.equals(right);
-    }
-    
     /** 
      * {@inheritDoc}
      */
@@ -197,7 +175,7 @@
         setFilter(cycle.getParameter("filter"));
     }
     
-    public abstract IPropertySelectionModel getModel();
+    public abstract IAutocompleteModel getModel();
     
     /** @since 4.1 */
     public abstract boolean isFilterOnChange();

Added: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/DefaultAutocompleteModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/DefaultAutocompleteModel.java?rev=426933&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/DefaultAutocompleteModel.java (added)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/DefaultAutocompleteModel.java Sun Jul 30 13:12:22 2006
@@ -0,0 +1,144 @@
+// Copyright Jul 30, 2006 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 java.util.List;
+import java.util.Map;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.util.Defense;
+
+
+/**
+ * Default simple implementation of {@link IAutocompleteModel}. This class relies
+ * on the java beans specification to resolve key fields of an incoming 
+ * {@link List}. 
+ * 
+ * <p>
+ *  If you had an object type of <code>User</code>, with the primary/unique id of 
+ *  each <code>User</code> object stored as a member with a name of <code>id</code> 
+ *  you would pass something like this into the model(don't forget that javabeans syntax
+ *  requires a corresponding getId() for members):
+ * </p>
+ * 
+ * <pre>
+ *  IAutocompleteModel model = new DefaultAutocompleteModel(List users, "id", "name");
+ * </pre>
+ * 
+ * @see {@linkplain http://jakarta.apache.org/commons/beanutils/commons-beanutils-1.6.1/docs/api/org/apache/commons/beanutils/PropertyUtils.html}
+ * @author jkuhnert
+ */
+public class DefaultAutocompleteModel implements IAutocompleteModel
+{
+
+    private List _values;
+    
+    private String _keyExpression;
+    
+    private String _labelExpression;
+    
+    /**
+     * Create a new model using java beans syntax to access the key/label
+     * for the list using the specified bean expressions.
+     * 
+     * @param values 
+     *          The list of values to manage.
+     * @param keyField 
+     *          The java beans expression for getting the primary key of each object
+     *          in the list. {@link #getPrimaryKey(Object)}.
+     * @param labelField
+     *          The java beans expression for getting the label of each object
+     *          in the list. {@link #getLabelFor(Object)}.
+     */
+    public DefaultAutocompleteModel(List values, String keyField, String labelField) 
+    {
+        Defense.notNull(values, "Value list can't be null.");
+        Defense.notNull(keyField, "Model keyField java beans expression can't be null.");
+        Defense.notNull(labelField, "Model labelField java beans expression can't be null.");
+        
+        _values = values;
+        _keyExpression = keyField;
+        _labelExpression = labelField;
+    }
+    
+    /** 
+     * {@inheritDoc}
+     */
+    public Map filterValues(String match)
+    {
+        Map ret = new HashMap();
+        
+        if (match == null)
+            return ret;
+        
+        String filter = match.trim().toLowerCase();
+        
+        for (int i = 0; i < _values.size(); i++) {
+            
+            Object value = _values.get(i);
+            String label = getLabelFor(value);
+            
+            if (label.toLowerCase().indexOf(filter) > -1)
+                ret.put(getPrimaryKey(value), label);
+        }
+        
+        return ret;
+    }
+    
+    /** 
+     * {@inheritDoc}
+     */
+    public String getLabelFor(Object value)
+    {
+        try {
+            
+            return PropertyUtils.getProperty(value, _labelExpression).toString();
+            
+        } catch (Exception e) {
+            throw new ApplicationRuntimeException(e);
+        }
+    }
+
+    /** 
+     * {@inheritDoc}
+     */
+    public Object getPrimaryKey(Object value)
+    {
+        try {
+            
+            return PropertyUtils.getProperty(value, _keyExpression);
+            
+        } catch (Exception e) {
+            throw new ApplicationRuntimeException(e);
+        }
+    }
+
+    /** 
+     * {@inheritDoc}
+     */
+    public Object getValue(Object primaryKey)
+    {
+        for (int i = 0; i < _values.size(); i++) {
+            
+            Object value = _values.get(i);
+            if (getPrimaryKey(value).toString().equals(primaryKey.toString()))
+                return value;
+        }
+        
+        return null;
+    }
+
+}

Added: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/IAutocompleteModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/IAutocompleteModel.java?rev=426933&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/IAutocompleteModel.java (added)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/dojo/form/IAutocompleteModel.java Sun Jul 30 13:12:22 2006
@@ -0,0 +1,60 @@
+// Copyright Jul 30, 2006 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.Map;
+
+import org.apache.tapestry.components.IPrimaryKeyConverter;
+
+
+/**
+ * Defines the interface used by the {@link Autocompleter} component to filter
+ * and match values from a potentially large data set. 
+ * 
+ * <p>
+ *  The roots of this model come from the {@link IPropertySelectionModel} interface, adding
+ *  additional logic for filtering where the normal semantics of {@link IPropertySelectionModel} 
+ *  would be prohibitively expensive.
+ * </p>
+ * 
+ * @author jkuhnert
+ */
+public interface IAutocompleteModel extends IPrimaryKeyConverter
+{
+
+    /**
+     * For the given value, provide a user friendly label that will
+     * be presented in a drop down selection list in the browser ui.
+     * 
+     * @param value
+     *          The object to retrieve a label for.
+     * @return
+     *          The label to use for the given value.
+     */
+    String getLabelFor(Object value);
+    
+    /**
+     * Used to filter a potentially large list of objects.
+     * 
+     * @param match
+     *          The given partial string that should be matched against object
+     *          <i>labels</i> in the model being managed.
+     * @return
+     *        A {@link Map} containing key/value pairs matching the given input label string. 
+     *        The map should contain a key compatible with {@link IPrimaryKeyConverter#getPrimaryKey(Object)} 
+     *        and value compatible with {@link #getLabelFor(Object)}.
+     */
+    Map filterValues(String match);
+    
+}

Added: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/DefaultAutocompleteModelTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/DefaultAutocompleteModelTest.java?rev=426933&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/DefaultAutocompleteModelTest.java (added)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/DefaultAutocompleteModelTest.java Sun Jul 30 13:12:22 2006
@@ -0,0 +1,100 @@
+// Copyright Jul 30, 2006 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.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tapestry.BaseComponentTestCase;
+import org.testng.annotations.Test;
+
+
+/**
+ * Tests functionality of {@link DefaultAutocompelteModel}.
+ * 
+ * @author jkuhnert
+ */
+@Test
+public class DefaultAutocompleteModelTest extends BaseComponentTestCase
+{
+
+    public void test_Basic_Model()
+    {
+        List values = new ArrayList();
+        
+        SimpleBean s1 = new SimpleBean(new Integer(1), "Simple 1", 100);
+        SimpleBean s2 = new SimpleBean(new Integer(2), "Simple 2", 200);
+        SimpleBean s3 = new SimpleBean(new Integer(3), "Simple 3", 300);
+        
+        values.add(s1);
+        values.add(s2);
+        values.add(s3);
+        
+        IAutocompleteModel model = new DefaultAutocompleteModel(values, "id", "label");
+        
+        // basic checks
+        assert s2.getLabel().equals(model.getLabelFor(s2));
+        assert model.getPrimaryKey(s3).equals(3);
+        assert model.getValue(1) == s1;
+    }
+    
+    public void test_Filtering_Match()
+    {
+        List values = new ArrayList();
+        
+        SimpleBean s1 = new SimpleBean(new Integer(1), "Simple 1", 100);
+        SimpleBean s2 = new SimpleBean(new Integer(2), "Simple 2", 200);
+        SimpleBean s3 = new SimpleBean(new Integer(3), "Simple 3", 300);
+        
+        values.add(s1);
+        values.add(s2);
+        values.add(s3);
+        
+        IAutocompleteModel model = new DefaultAutocompleteModel(values, "id", "label");
+        
+        Map results = model.filterValues("sim");
+        
+        assert results != null;
+        assert results.size() == 3;
+        
+        assert results.containsKey(2);
+        assert results.get(2).equals(s2.getLabel());
+        
+        results = model.filterValues("simple 1");
+        
+        assert results.size() == 1;
+        assert results.get(1) == s1.getLabel();
+    }
+    
+    public void test_Filtering_Null()
+    {
+        List values = new ArrayList();
+        
+        SimpleBean s1 = new SimpleBean(new Integer(1), "Simple 1", 100);
+        SimpleBean s2 = new SimpleBean(new Integer(2), "Simple 2", 200);
+        SimpleBean s3 = new SimpleBean(new Integer(3), "Simple 3", 300);
+        
+        values.add(s1);
+        values.add(s2);
+        values.add(s3);
+        
+        IAutocompleteModel model = new DefaultAutocompleteModel(values, "id", "label");
+        
+        Map results = model.filterValues(null);
+        
+        assert results != null;
+        assert results.size() == 0;
+    }
+}

Added: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/SimpleBean.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/SimpleBean.java?rev=426933&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/SimpleBean.java (added)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/SimpleBean.java Sun Jul 30 13:12:22 2006
@@ -0,0 +1,91 @@
+// Copyright Jul 30, 2006 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;
+
+
+/**
+ * Simple bean style class to test {@link DefaultAutocompleteModel}.
+ * 
+ * @author jkuhnert
+ */
+public class SimpleBean
+{
+
+    private Integer _id;
+    
+    private String _label;
+    
+    private int _value;
+    
+    public SimpleBean(Integer id, String label, int value)
+    {
+        _id = id;
+        _label = label;
+        _value = value;
+    }
+    
+    /**
+     * @return the id
+     */
+    public Integer getId()
+    {
+        return _id;
+    }
+    
+    /**
+     * @return the label
+     */
+    public String getLabel()
+    {
+        return _label;
+    }
+    
+    /**
+     * Returns the value stored.
+     * 
+     * @return
+     */
+    public int getValue()
+    {
+        return _value;
+    }
+
+    /** 
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + ((_id == null) ? 0 : _id.hashCode());
+        return result;
+    }
+    
+    /** 
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        final SimpleBean other = (SimpleBean) obj;
+        if (_id == null) {
+            if (other._id != null) return false;
+        } else if (!_id.equals(other._id)) return false;
+        return true;
+    }
+}

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java?rev=426933&r1=426932&r2=426933&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/dojo/form/TestAutocompleter.java Sun Jul 30 13:12:22 2006
@@ -20,6 +20,8 @@
 import static org.easymock.EasyMock.isA;
 import static org.testng.AssertJUnit.assertEquals;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.tapestry.IForm;
@@ -32,7 +34,6 @@
 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;
@@ -48,10 +49,24 @@
 @Test
 public class TestAutocompleter extends BaseFormComponentTestCase
 {
+    private IAutocompleteModel createModel()
+    {
+        List values = new ArrayList();
+        
+        SimpleBean s1 = new SimpleBean(new Integer(1), "Simple 1", 100);
+        SimpleBean s2 = new SimpleBean(new Integer(2), "Simple 2", 200);
+        SimpleBean s3 = new SimpleBean(new Integer(3), "Simple 3", 300);
+        
+        values.add(s1);
+        values.add(s2);
+        values.add(s3);
+        
+        return new DefaultAutocompleteModel(values, "id", "label");
+    }
+    
     public void testRewind()
     {
-        String[] values = { "red", "green", "blue" };
-        StringPropertySelectionModel model = new StringPropertySelectionModel(values);
+        IAutocompleteModel model = createModel();
         ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class);
         
         Autocompleter component = newInstance(Autocompleter.class, 
@@ -73,14 +88,13 @@
         trainGetElementId(form, component, "barney");
         trainIsRewinding(form, true);
         
-        String key = "0";
-        String value = values[0];
+        trainGetParameter(cycle, "barney", "1");
         
-        trainGetParameter(cycle, "barney", key);
+        SimpleBean match = new SimpleBean(new Integer(1), null, -1);
         
         try
         {
-            vfs.validate(component, writer, cycle, value);
+            vfs.validate(component, writer, cycle, match);
         }
         catch (ValidatorException e)
         {
@@ -93,7 +107,7 @@
         
         verify();
         
-        assertEquals(values[0], component.getValue());
+        assertEquals(match, component.getValue());
     }
     
     public void testRewindNotForm()
@@ -126,9 +140,7 @@
     
     public void testRender()
     {
-        String[] values = { "red", "green", "blue" };
-        StringPropertySelectionModel model = new StringPropertySelectionModel(values);
-        
+        IAutocompleteModel model = createModel();
         ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class);
         
         IRequestCycle cycle = newMock(IRequestCycle.class);
@@ -148,13 +160,15 @@
         
         IScript script = newMock(IScript.class);
         
+        SimpleBean match = new SimpleBean(new Integer(2), "Simple 2", 200);
+        
         Autocompleter component = newInstance(Autocompleter.class, 
                 new Object[] { 
             "name", "fred", "model", model, 
             "directService", engine,
             "script", script,
             "validatableFieldSupport", vfs, 
-            "value", values[1]
+            "value", match
         });
         
         DirectServiceParameter dsp = 
@@ -195,9 +209,7 @@
     
     public void testRenderJSON()
     {
-        String[] values = { "red", "green", "blue", "yellow" };
-        StringPropertySelectionModel model = new StringPropertySelectionModel(values);
-        
+        IAutocompleteModel model = createModel();
         IRequestCycle cycle = newMock(IRequestCycle.class);
         
         IJSONWriter json = newBufferJSONWriter();
@@ -212,9 +224,10 @@
         
         verify();
         
-        assertEquals(json.length(), 2);
-        assertEquals(json.get("3"), "yellow");
-        assertEquals(json.get("2"), "blue");
+        assertEquals(json.length(), 3);
+        assertEquals(json.get("1"), "Simple 1");
+        assertEquals(json.get("2"), "Simple 2");
+        assertEquals(json.get("3"), "Simple 3");
     }
     
     public void testIsRequired()