tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/coerce ValueConverterImpl.java
Date Sat, 20 Nov 2004 21:21:29 GMT
hlship      2004/11/20 13:21:29

  Modified:    src/documentation/content/xdocs/UsersGuide spec.xml
                        upgrade.xml index.xml
               framework/src/test/org/apache/tapestry/junit/parse
                        PropertySpecifications.page Property.page
                        TestSpecificationParser.java
               framework/src/java/org/apache/tapestry/parse
                        SpecificationParser.java
               framework/src/java/org/apache/tapestry/pageload
                        PageLoader.java
               framework/src/test/org/apache/tapestry/coerce
                        TestValueConverter.java
               framework/src/java/org/apache/tapestry/coerce
                        ValueConverterImpl.java
  Added:       framework/src/test/org/apache/tapestry/pageload
                        TestPropertyBindingInitializer.java
                        ComponentFixture.java
                        TestPropertyReinitializer.java
               framework/src/java/org/apache/tapestry/pageload
                        PropertyReinitializer.java
                        PropertyBindingInitializer.java
  Removed:     framework/src/java/org/apache/tapestry/pageload
                        PropertyInitializer.java
  Log:
  Change the initial-value attribute of the <property> element to be a binding reference
(rather than an OGNL expression).
  
  Revision  Changes    Path
  1.9       +3 -2      jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/spec.xml
  
  Index: spec.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/spec.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- spec.xml	19 Nov 2004 20:50:28 -0000	1.8
  +++ spec.xml	20 Nov 2004 21:21:29 -0000	1.9
  @@ -126,6 +126,8 @@
     <li>The &spec.inject; element has been added.</li>
     <li>The &spec.parameter; element has been simplified; the type and direction
attributes have been dropped. The
       default-value attribute is now a <em>binding reference</em>, as with the
&spec.binding; element's value attribute.</li>
  +  <li>The &spec.property; element's initial-value attribute (or enclosed character
data) is now a binding reference. In release 3.0
  +    it was always an OGNL expression, but now it must be prefixed.</li>
   </ul>
   
   <p>
  @@ -1376,8 +1378,7 @@
   	<td>no</td>
   	<td/>
   	<td>
  -	An optional OGNL expression used to initialize the property.  The expression is evaluated
  -	only when the page is first constructed.
  +	An optional binding reference used to initialize the property.
   	</td>
   </tr>
   
  
  
  
  1.3       +11 -0     jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/upgrade.xml
  
  Index: upgrade.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/upgrade.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- upgrade.xml	19 Nov 2004 20:50:28 -0000	1.2
  +++ upgrade.xml	20 Nov 2004 21:21:29 -0000	1.3
  @@ -289,6 +289,17 @@
   matching &spec.parameter; element.  Typically, the &spec.property; element is only
used when the property is either
   not referenced inside Java code (such as a listener method), or when when the property
must be persistent.
   </p>
  +
  +<p>
  +In the 3.0 DTDs, properties could have an initial value set. The initial-value attribute
was an OGNL expression used to
  +initial the property when the page is first constructed, and when the page is detached
(at the end of a request cycle). The initial value
  +may instead be specified as the enclosed character data of the &lt;property-specification&gt;
element.
  +</p>
  +
  +<p>
  +Using the 3.1 DTDs, this is still true, but the initial-value attribute (or the enclosed
character data) is a binding reference. To get
  +the same behavior as the 3.0 DTD, it must be prefixed with "ognl:".
  +</p>
     
   </section> <!-- upgrade.property -->
   
  
  
  
  1.6       +4 -4      jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/index.xml
  
  Index: index.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/src/documentation/content/xdocs/UsersGuide/index.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- index.xml	2 Nov 2004 03:32:42 -0000	1.5
  +++ index.xml	20 Nov 2004 21:21:29 -0000	1.6
  @@ -294,13 +294,13 @@
    &HiveMind; microkernel.  This new backbone will provide the support necessary to meet
the needs of Tapestry's
    much larger community ... including support for <em>prettier</em> URLs, integration
of Tapestry and the Java Portlet API,
    and modularization of applications (allowing the use of folders, and thus, J2EE declarative
security). In addition,
  - a more sophisticated approach to implementing connected parameter properties will be introduced,
and more flexibility for
  - storing session-specific state as HTTP cookies or query parameters will be provided.
  + a more sophisticated approach to implementing connected parameter properties has already
been introduced, and more flexibility for
  + storing session-specific state as HTTP cookies or query parameters will likely be provided.
   </p>
   
  -<note>
  +<warning>
   The exact feature set for Tapestry 3.1, and its schedule, are most assuredly up in the
air!
  -</note>
  +</warning>
   
   <p>
     A further future direction, in the Tapestry 4.0 timeframe (the far future), is to rethink
the component object model
  
  
  
  1.1                  jakarta-tapestry/framework/src/test/org/apache/tapestry/pageload/TestPropertyBindingInitializer.java
  
  Index: TestPropertyBindingInitializer.java
  ===================================================================
  // Copyright 2004 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.pageload;
  
  import org.apache.hivemind.ApplicationRuntimeException;
  import org.apache.hivemind.Location;
  import org.apache.hivemind.test.HiveMindTestCase;
  import org.apache.tapestry.IBinding;
  import org.easymock.MockControl;
  
  /**
   * Tests {@link org.apache.tapestry.pageload.PropertyBindingInitializer}.
   * 
   * @author Howard M. Lewis Ship
   * @since 3.1
   */
  public class TestPropertyBindingInitializer extends HiveMindTestCase
  {
      public void testSuccess()
      {
          ComponentFixture c = new ComponentFixture();
  
          MockControl bc = newControl(IBinding.class);
          IBinding b = (IBinding) bc.getMock();
  
          replayControls();
  
          PropertyBindingInitializer i = new PropertyBindingInitializer(c, "stringProperty",
b);
  
          verifyControls();
  
          b.getObject(String.class);
          bc.setReturnValue("fred");
  
          replayControls();
  
          i.pageDetached(null);
  
          verifyControls();
  
          assertEquals("fred", c.getStringProperty());
      }
  
      public void testFailure()
      {
          Location l = fabricateLocation(99);
  
          ComponentFixture c = new ComponentFixture();
  
          MockControl bc = newControl(IBinding.class);
          IBinding b = (IBinding) bc.getMock();
  
          Throwable npe = new NullPointerException();
  
          replayControls();
  
          PropertyBindingInitializer i = new PropertyBindingInitializer(c, "stringProperty",
b);
  
          verifyControls();
  
          b.getObject(String.class);
          bc.setThrowable(npe);
  
          b.getLocation();
          bc.setReturnValue(l);
  
          replayControls();
  
          try
          {
              i.pageDetached(null);
              unreachable();
          }
          catch (ApplicationRuntimeException ex)
          {
              assertSame(l, ex.getLocation());
              assertSame(c, ex.getComponent());
              assertSame(npe, ex.getRootCause());
              assertExceptionSubstring(ex, "Unable to initialize property stringProperty");
          }
  
          verifyControls();
  
          assertNull(c.getStringProperty());
      }
  }
  
  
  1.1                  jakarta-tapestry/framework/src/test/org/apache/tapestry/pageload/ComponentFixture.java
  
  Index: ComponentFixture.java
  ===================================================================
  // Copyright 2004 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.pageload;
  
  import org.apache.tapestry.AbstractComponent;
  import org.apache.tapestry.IMarkupWriter;
  import org.apache.tapestry.IRequestCycle;
  
  /**
   * Used by test cases for property initializing / re-initializing.
   * 
   * @author Howard Lewis Ship
   * @since 3.1
   */
  public class ComponentFixture extends AbstractComponent
  {
      private String _stringProperty;
  
      public String getStringProperty()
      {
          return _stringProperty;
      }
  
      public void setStringProperty(String stringProperty)
      {
          _stringProperty = stringProperty;
      }
  
      protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
      {
          //
      }
  }
  
  
  1.1                  jakarta-tapestry/framework/src/test/org/apache/tapestry/pageload/TestPropertyReinitializer.java
  
  Index: TestPropertyReinitializer.java
  ===================================================================
  // Copyright 2004 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.pageload;
  
  import org.apache.hivemind.test.HiveMindTestCase;
  
  /**
   * Tests {@link org.apache.tapestry.pageload.PropertyReinitializer}.
   * 
   * @author Howard M. Lewis Ship
   */
  public class TestPropertyReinitializer extends HiveMindTestCase
  {
      public void testSuccess()
      {
          ComponentFixture c = new ComponentFixture();
  
          c.setStringProperty("initial-value");
  
          PropertyReinitializer i = new PropertyReinitializer(c, "stringProperty");
  
          i.pageDetached(null);
  
          assertEquals("initial-value", c.getStringProperty());
  
          c.setStringProperty("updated-value");
  
          i.pageDetached(null);
  
          assertEquals("initial-value", c.getStringProperty());
      }
  }
  
  
  1.2       +3 -0      jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/PropertySpecifications.page
  
  Index: PropertySpecifications.page
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/PropertySpecifications.page,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertySpecifications.page	29 Oct 2004 17:13:42 -0000	1.1
  +++ PropertySpecifications.page	20 Nov 2004 21:21:29 -0000	1.2
  @@ -24,6 +24,9 @@
   <property-specification name="bool" type="boolean"/>
   <property-specification name="persist" persistent="yes"/>
   <property-specification name="init" initial-value="pageName"/>
  +<property-specification name="longInitialValue">
  +    long.initial.value
  +</property-specification>
   
   </page-specification>
   
  
  
  
  1.3       +4 -1      jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/Property.page
  
  Index: Property.page
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/Property.page,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Property.page	3 Nov 2004 13:53:43 -0000	1.2
  +++ Property.page	20 Nov 2004 21:21:29 -0000	1.3
  @@ -23,7 +23,10 @@
   
     <property name="bool"/>
     <property name="persist" persistent="yes"/>
  -  <property name="init" initial-value="pageName"/>
  +  <property name="init" initial-value="ognl:pageName"/>
  +  <property name="longInit">
  +      message:long-init-key
  +  </property>
   
   </page-specification>
   
  
  
  
  1.6       +18 -4     jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/TestSpecificationParser.java
  
  Index: TestSpecificationParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/junit/parse/TestSpecificationParser.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestSpecificationParser.java	19 Nov 2004 16:44:08 -0000	1.5
  +++ TestSpecificationParser.java	20 Nov 2004 21:21:29 -0000	1.6
  @@ -504,7 +504,7 @@
           IComponentSpecification spec = parsePage("PropertySpecifications.page");
   
           checkList("propertySpecificationNames", new String[]
  -        { "bool", "init", "persist" }, spec.getPropertySpecificationNames());
  +        { "bool", "init", "longInitialValue", "persist" }, spec.getPropertySpecificationNames());
   
           IPropertySpecification ps = spec.getPropertySpecification("bool");
           assertEquals("name", "bool", ps.getName());
  @@ -517,7 +517,11 @@
           assertEquals("name", "init", ps.getName());
           assertEquals("persistent", false, ps.isPersistent());
           assertNull("type", ps.getType());
  -        assertEquals("initialValue", "pageName", ps.getInitialValue());
  +
  +        // Starting with release 3.1, the initial value is a binding reference
  +        // with an appropriate prefix. In 3.0 it was always an OGNL expression.
  +
  +        assertEquals("initialValue", "ognl:pageName", ps.getInitialValue());
           checkLine(ps, 26);
   
           ps = spec.getPropertySpecification("persist");
  @@ -527,6 +531,9 @@
           assertNull("initialValue", ps.getInitialValue());
           checkLine(ps, 25);
   
  +        ps = spec.getPropertySpecification("longInitialValue");
  +        assertEquals("ognl:long.initial.value", ps.getInitialValue());
  +
           ps = spec.getPropertySpecification("unknown");
   
           assertNull("Unknown PropertySpecification", ps);
  @@ -860,7 +867,7 @@
           IComponentSpecification spec = parsePage("Property.page");
   
           checkList("propertySpecificationNames", new String[]
  -        { "bool", "init", "persist" }, spec.getPropertySpecificationNames());
  +        { "bool", "init", "longInit", "persist" }, spec.getPropertySpecificationNames());
   
           IPropertySpecification ps = spec.getPropertySpecification("bool");
           assertEquals("name", "bool", ps.getName());
  @@ -868,6 +875,10 @@
   
           // In a 3.1 DTD, type is always null.
           assertNull("type", ps.getType());
  +
  +        // Note that no prefix is added. Initial value will be a string literal,
  +        // or have a prefix and be something else.
  +
           assertNull("initialValue", ps.getInitialValue());
           checkLine(ps, 24);
   
  @@ -875,7 +886,7 @@
           assertEquals("name", "init", ps.getName());
           assertEquals("persistent", false, ps.isPersistent());
   
  -        assertEquals("initialValue", "pageName", ps.getInitialValue());
  +        assertEquals("initialValue", "ognl:pageName", ps.getInitialValue());
           checkLine(ps, 26);
   
           ps = spec.getPropertySpecification("persist");
  @@ -883,6 +894,9 @@
           assertEquals("persistent", true, ps.isPersistent());
           assertNull("initialValue", ps.getInitialValue());
           checkLine(ps, 25);
  +
  +        ps = spec.getPropertySpecification("longInit");
  +        assertEquals("message:long-init-key", ps.getInitialValue());
   
           ps = spec.getPropertySpecification("unknown");
   
  
  
  
  1.12      +7 -1      jakarta-tapestry/framework/src/java/org/apache/tapestry/parse/SpecificationParser.java
  
  Index: SpecificationParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/parse/SpecificationParser.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SpecificationParser.java	19 Nov 2004 16:44:11 -0000	1.11
  +++ SpecificationParser.java	20 Nov 2004 21:21:29 -0000	1.12
  @@ -902,6 +902,12 @@
   
           String initialValue = getExtendedValue(ps.getInitialValue(), "initial-value", false);
   
  +        // In the 3.0 DTD, the initial value was always an OGNL expression.
  +        // In the 3.1 DTD, it is a binding reference, qualified with a prefix.
  +
  +        if (initialValue != null && !_DTD_3_1)
  +            initialValue = "ognl:" + initialValue;
  +
           ps.setInitialValue(initialValue);
       }
   
  @@ -1234,7 +1240,7 @@
               defaultValue = "ognl:" + defaultValue;
   
           ps.setDefaultValue(defaultValue);
  -        
  +
           // type will only be specified in a 3.0 DTD.
   
           String type = getAttribute("type");
  
  
  
  1.8       +21 -3     jakarta-tapestry/framework/src/java/org/apache/tapestry/pageload/PageLoader.java
  
  Index: PageLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/pageload/PageLoader.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PageLoader.java	17 Nov 2004 01:34:58 -0000	1.7
  +++ PageLoader.java	20 Nov 2004 21:21:29 -0000	1.8
  @@ -584,6 +584,11 @@
               // parameters are bound
               _verifyRequiredParametersWalker.walkComponentTree(page);
   
  +            // Note that we defer this until last, because
  +            // we want to ensure that the page is attached to the engine,
  +            // and that all sub-components of components are created
  +            // because everything is free to reference everthing else!
  +
               establishDefaultPropertyValues();
           }
           finally
  @@ -672,14 +677,27 @@
           List names = spec.getPropertySpecificationNames();
           int count = names.size();
   
  +        PageDetachListener initializer = null;
  +
           for (int i = 0; i < count; i++)
           {
               String name = (String) names.get(i);
               IPropertySpecification ps = spec.getPropertySpecification(name);
  -            String expression = ps.getInitialValue();
   
  -            PageDetachListener initializer = new PropertyInitializer(component, name, expression,
  -                    ps.getLocation(), _expressionEvaluator);
  +            String initialValue = ps.getInitialValue();
  +
  +            if (initialValue == null)
  +                initializer = new PropertyReinitializer(component, name);
  +            else
  +            {
  +                IBinding initialValueBinding = _bindingSource.createBinding(
  +                        component,
  +                        name,
  +                        initialValue,
  +                        ps.getLocation());
  +
  +                initializer = new PropertyBindingInitializer(component, name, initialValueBinding);
  +            }
   
               _propertyInitializers.add(initializer);
               page.addPageDetachListener(initializer);
  
  
  
  1.1                  jakarta-tapestry/framework/src/java/org/apache/tapestry/pageload/PropertyReinitializer.java
  
  Index: PropertyReinitializer.java
  ===================================================================
  // Copyright 2004 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.pageload;
  
  import org.apache.hivemind.Defense;
  import org.apache.hivemind.util.PropertyUtils;
  import org.apache.tapestry.IComponent;
  import org.apache.tapestry.event.PageDetachListener;
  import org.apache.tapestry.event.PageEvent;
  
  /**
   * Re-initializes a page property from its initial value.
   * 
   * @author Howard M. Lewis Ship
   * @since 3.1
   */
  public class PropertyReinitializer implements PageDetachListener
  {
      private IComponent _component;
  
      private String _propertyName;
  
      private Object _initialValue;
  
      private boolean _initialize = true;
  
      public PropertyReinitializer(IComponent component, String propertyName)
      {
          Defense.notNull(component, "component");
          Defense.notNull(propertyName, "propertyName");
  
          _component = component;
          _propertyName = propertyName;
      }
  
      public void pageDetached(PageEvent event)
      {
          // On the first call, which originates with the PageLoader,
          // we read the value (which may have been set inside
          // a finishLoad() method).
  
          if (_initialize)
          {
              _initialValue = PropertyUtils.read(_component, _propertyName);
              _initialize = false;
              return;
          }
  
          PropertyUtils.write(_component, _propertyName, _initialValue);
      }
  
  }
  
  
  1.1                  jakarta-tapestry/framework/src/java/org/apache/tapestry/pageload/PropertyBindingInitializer.java
  
  Index: PropertyBindingInitializer.java
  ===================================================================
  // Copyright 2004 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.pageload;
  
  import org.apache.hivemind.ApplicationRuntimeException;
  import org.apache.hivemind.Defense;
  import org.apache.hivemind.util.PropertyUtils;
  import org.apache.tapestry.IBinding;
  import org.apache.tapestry.IComponent;
  import org.apache.tapestry.event.PageDetachListener;
  import org.apache.tapestry.event.PageEvent;
  
  /**
   * Initializes (at finishLoad()) and re-initializes (on page detach) a property of a component
using
   * a {@link org.apache.tapestry.IBinding}as the value source.
   * 
   * @author Howard Lewis Ship
   * @since 3.1
   */
  
  public class PropertyBindingInitializer implements PageDetachListener
  {
      private IComponent _component;
  
      private String _propertyName;
  
      private IBinding _binding;
  
      private Class _propertyType;
  
      public PropertyBindingInitializer(IComponent component, String propertyName, IBinding
binding)
      {
          Defense.notNull(component, "component");
          Defense.notNull(propertyName, "propertyName");
          Defense.notNull(binding, "binding");
  
          _component = component;
          _propertyName = propertyName;
          _binding = binding;
  
          _propertyType = PropertyUtils.getPropertyType(component, _propertyName);
      }
  
      public void pageDetached(PageEvent event)
      {
  
          try
          {
              Object value = _binding.getObject(_propertyType);
  
              PropertyUtils.write(_component, _propertyName, value);
          }
          catch (Exception ex)
          {
              throw new ApplicationRuntimeException(PageloadMessages.unableToInitializeProperty(
                      _propertyName,
                      _component,
                      ex), _component, _binding.getLocation(), ex);
          }
      }
  }
  
  
  1.2       +10 -1     jakarta-tapestry/framework/src/test/org/apache/tapestry/coerce/TestValueConverter.java
  
  Index: TestValueConverter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/coerce/TestValueConverter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestValueConverter.java	17 Nov 2004 01:34:58 -0000	1.1
  +++ TestValueConverter.java	20 Nov 2004 21:21:29 -0000	1.2
  @@ -187,7 +187,7 @@
           }
   
       }
  -    
  +
       public void testStringToNonNumericPrimitive()
       {
           MockControl tcc = newControl(TypeConverter.class);
  @@ -215,5 +215,14 @@
           assertSame(output, v.coerceValue(input, Boolean.class));
   
           verifyControls();
  +    }
  +
  +    public void testNumberToNumber()
  +    {
  +        ValueConverterImpl v = new ValueConverterImpl();
  +
  +        Object result = v.coerceValue(new Integer(123), long.class);
  +
  +        assertEquals(new Long(123), result);
       }
   }
  
  
  
  1.2       +22 -4     jakarta-tapestry/framework/src/java/org/apache/tapestry/coerce/ValueConverterImpl.java
  
  Index: ValueConverterImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/coerce/ValueConverterImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ValueConverterImpl.java	17 Nov 2004 01:34:56 -0000	1.1
  +++ ValueConverterImpl.java	20 Nov 2004 21:21:29 -0000	1.2
  @@ -23,6 +23,7 @@
   
   import org.apache.hivemind.ApplicationRuntimeException;
   import org.apache.hivemind.Defense;
  +import org.apache.hivemind.util.ConstructorUtils;
   
   /**
    * Implementation of {@link org.apache.tapestry.coerce.ValueConverter}. Selects an appropriate
type
  @@ -78,15 +79,20 @@
   
           Class effectiveType = convertType(desiredType);
   
  -        Object result = convertUsingPropertyEditor(value, effectiveType);
  +        // Already the correct type? Go no further!
  +
  +        if (value != null && effectiveType.isAssignableFrom(value.getClass()))
  +            return value;
  +        
  +        Object result = convertNumberToNumber(value, effectiveType);
   
           if (result != null)
               return result;
   
  -        // Already the correct type? Go no further!
  +        result = convertUsingPropertyEditor(value, effectiveType);
   
  -        if (value != null && effectiveType.isAssignableFrom(value.getClass()))
  -            return value;
  +        if (result != null)
  +            return result;
   
           TypeConverter converter = (TypeConverter) _converterMap.get(effectiveType);
   
  @@ -154,6 +160,18 @@
                       ex), ex);
           }
   
  +    }
  +
  +    private Number convertNumberToNumber(Object value, Class targetType)
  +    {
  +        if (value == null || !Number.class.isAssignableFrom(value.getClass())
  +                || !Number.class.isAssignableFrom(targetType))
  +            return null;
  +
  +        String valueAsString = value.toString();
  +
  +        return (Number) ConstructorUtils.invokeConstructor(targetType, new Object[]
  +        { valueAsString });
       }
   
       private Class convertType(Class possiblePrimitiveType)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message