tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r501291 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/services/ main/java/org/apache/tapestry/translator/ main/resources/org/apache/tapestry/internal/ test/java/org/apache/tapestry/translator/
Date Tue, 30 Jan 2007 02:08:00 GMT
Author: hlship
Date: Mon Jan 29 18:07:59 2007
New Revision: 501291

URL: http://svn.apache.org/viewvc?view=rev&rev=501291
Log:
Add in translators for double and long.

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/DoubleTranslator.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/LongTranslator.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/DoubleTranslatorTest.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/LongTranslatorTest.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/IntegerTranslator.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ValidationMessages.properties

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?view=diff&rev=501291&r1=501290&r2=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
(original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
Mon Jan 29 18:07:59 2007
@@ -156,7 +156,9 @@
 import org.apache.tapestry.ioc.util.StrategyRegistry;
 import org.apache.tapestry.runtime.Component;
 import org.apache.tapestry.runtime.RenderCommand;
+import org.apache.tapestry.translator.DoubleTranslator;
 import org.apache.tapestry.translator.IntegerTranslator;
+import org.apache.tapestry.translator.LongTranslator;
 import org.apache.tapestry.translator.StringTranslator;
 import org.apache.tapestry.validator.Max;
 import org.apache.tapestry.validator.MaxLength;
@@ -961,6 +963,8 @@
      * <ul>
      * <li>integer</li>
      * <li>string</li>
+     * <li>long</li>
+     * <li>double</li>
      * </ul>
      */
     public static void contributeTranslatorSource(
@@ -973,6 +977,8 @@
 
         configuration.add("integer", new IntegerTranslator());
         configuration.add("string", new StringTranslator());
+        configuration.add("long", new LongTranslator());
+        configuration.add("double", new DoubleTranslator());
     }
 
     /**
@@ -980,6 +986,8 @@
      * <ul>
      * <li>Integer</li>
      * <li>String</li>
+     * <li>Long</li>
+     * <li>Double</li>
      * </li>
      */
     public static void contributeTranslatorDefaultSource(
@@ -987,6 +995,8 @@
     {
         configuration.add(Integer.class, new IntegerTranslator());
         configuration.add(String.class, new StringTranslator());
+        configuration.add(Long.class, new LongTranslator());
+        configuration.add(Double.class, new DoubleTranslator());
     }
 
     /**

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/DoubleTranslator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/DoubleTranslator.java?view=auto&rev=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/DoubleTranslator.java
(added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/DoubleTranslator.java
Mon Jan 29 18:07:59 2007
@@ -0,0 +1,52 @@
+// Copyright 2007 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.translator;
+
+import org.apache.tapestry.Translator;
+import org.apache.tapestry.ValidationException;
+import org.apache.tapestry.ioc.Messages;
+import org.apache.tapestry.ioc.internal.util.InternalUtils;
+
+public class DoubleTranslator implements Translator<Double>
+{
+    /**
+     * Parses blank values to null, otherwise parses the client value to a long
+     * 
+     * @throws ValidationException
+     *             if the clientValue can not be parsed
+     */
+    public Double parseClient(String clientValue, Messages messages) throws ValidationException
+    {
+        if (InternalUtils.isBlank(clientValue))
+            return null;
+
+        try
+        {
+            return new Double(clientValue.trim());
+        }
+        catch (NumberFormatException ex)
+        {
+            throw new ValidationException(messages.format("number-format-exception", clientValue));
+        }
+    }
+
+    /**
+     * Converts null to the blank string, non-null to a string representation.
+     */
+    public String toClient(Double value)
+    {
+        return value == null ? "" : value.toString();
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/IntegerTranslator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/IntegerTranslator.java?view=diff&rev=501291&r1=501290&r2=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/IntegerTranslator.java
(original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/IntegerTranslator.java
Mon Jan 29 18:07:59 2007
@@ -37,7 +37,7 @@
 
         try
         {
-            return Integer.parseInt(clientValue.trim());
+            return new Integer(clientValue.trim());
         }
         catch (NumberFormatException ex)
         {

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/LongTranslator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/LongTranslator.java?view=auto&rev=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/LongTranslator.java
(added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/translator/LongTranslator.java
Mon Jan 29 18:07:59 2007
@@ -0,0 +1,53 @@
+// Copyright 2007 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.translator;
+
+import org.apache.tapestry.Translator;
+import org.apache.tapestry.ValidationException;
+import org.apache.tapestry.ioc.Messages;
+import org.apache.tapestry.ioc.internal.util.InternalUtils;
+
+/** A translator for type long. */
+public class LongTranslator implements Translator<Long>
+{
+    /**
+     * Parses blank values to null, otherwise parses the client value to a long
+     * 
+     * @throws ValidationException
+     *             if the clientValue can not be parsed
+     */
+    public Long parseClient(String clientValue, Messages messages) throws ValidationException
+    {
+        if (InternalUtils.isBlank(clientValue))
+            return null;
+
+        try
+        {
+            return new Long(clientValue.trim());
+        }
+        catch (NumberFormatException ex)
+        {
+            throw new ValidationException(messages.format("integer-format-exception", clientValue));
+        }
+    }
+
+    /**
+     * Converts null to the blank string, non-null to a string representation.
+     */
+    public String toClient(Long value)
+    {
+        return value == null ? "" : value.toString();
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ValidationMessages.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ValidationMessages.properties?view=diff&rev=501291&r1=501290&r2=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ValidationMessages.properties
(original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ValidationMessages.properties
Mon Jan 29 18:07:59 2007
@@ -1,4 +1,4 @@
-# Copyright 2006 The Apache Software Foundation
+# Copyright 2006, 2007 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.
@@ -16,7 +16,11 @@
 # as the first parameter, and the field's label as the second parameter.
 
 required=You must provide a value for %s.
-integer-format-exception=The input value '%s' is not parseable as an integer value. 
 minimum-string-length=You must provide at least %d characters for %s.
 maximum-string-length=You may provide at most %d characters for %s.
-min-integer=%2$s requires a value of at least %1$d. 
\ No newline at end of file
+min-integer=%2$s requires a value of at least %1$d. 
+
+# This is where the translator messages go.
+
+integer-format-exception=The input value '%s' is not parseable as an integer value.
+number-format-exception=The input value '%s' is not parseable as a numeric value. 
\ No newline at end of file

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/DoubleTranslatorTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/DoubleTranslatorTest.java?view=auto&rev=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/DoubleTranslatorTest.java
(added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/DoubleTranslatorTest.java
Mon Jan 29 18:07:59 2007
@@ -0,0 +1,92 @@
+// Copyright 2007 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.translator;
+
+import org.apache.tapestry.Translator;
+import org.apache.tapestry.ValidationException;
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.Messages;
+import org.testng.annotations.Test;
+
+public class DoubleTranslatorTest extends InternalBaseTestCase
+{
+    @Test
+    public void parse_invalid_format()
+    {
+        Messages messages = validationMessages();
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        try
+        {
+            translator.parseClient("xyz", messages);
+            unreachable();
+        }
+        catch (ValidationException ex)
+        {
+            assertEquals(
+                    ex.getMessage(),
+                    "The input value 'xyz' is not parseable as a numeric value. ");
+        }
+    }
+
+    @Test
+    public void blank_parses_to_null() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        assertNull(translator.parseClient("", messages));
+    }
+
+    @Test
+    public void null_converts_to_client_as_blank()
+    {
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        assertEquals(translator.toClient(null), "");
+    }
+
+    @Test
+    public void convert_non_null()
+    {
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        assertEquals(translator.toClient(37.0d), "37.0");
+    }
+
+    @Test
+    public void successful_parse_from_client() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        assertEquals(translator.parseClient("-23823", messages), new Double(-23823));
+    }
+
+    @Test
+    public void parse_ignores_trimmed_whitespace() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Double> translator = new DoubleTranslator();
+
+        assertEquals(translator.parseClient(" -123 ", messages), new Double(-123));
+    }
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/LongTranslatorTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/LongTranslatorTest.java?view=auto&rev=501291
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/LongTranslatorTest.java
(added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/translator/LongTranslatorTest.java
Mon Jan 29 18:07:59 2007
@@ -0,0 +1,95 @@
+// Copyright 2007 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.translator;
+
+import org.apache.tapestry.Translator;
+import org.apache.tapestry.ValidationException;
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.Messages;
+import org.testng.annotations.Test;
+
+public class LongTranslatorTest extends InternalBaseTestCase
+{
+    @Test
+    public void parse_invalid_format()
+    {
+        Messages messages = validationMessages();
+
+        Translator<Long> translator = new LongTranslator();
+
+        try
+        {
+            translator.parseClient("xyz", messages);
+            unreachable();
+        }
+        catch (ValidationException ex)
+        {
+            // It's okay that it says 'integer' (not 'long') here because users don't
+            // care about the distinction.
+
+            assertEquals(
+                    ex.getMessage(),
+                    "The input value 'xyz' is not parseable as an integer value. ");
+        }
+    }
+
+    @Test
+    public void blank_parses_to_null() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Long> translator = new LongTranslator();
+
+        assertNull(translator.parseClient("", messages));
+    }
+
+    @Test
+    public void null_converts_to_client_as_blank()
+    {
+
+        Translator<Long> translator = new LongTranslator();
+
+        assertEquals(translator.toClient(null), "");
+    }
+
+    @Test
+    public void convert_non_null()
+    {
+
+        Translator<Long> translator = new LongTranslator();
+
+        assertEquals(translator.toClient(37l), "37");
+    }
+
+    @Test
+    public void successful_parse_from_client() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Long> translator = new LongTranslator();
+
+        assertEquals(translator.parseClient("-23823", messages), new Long(-23823));
+    }
+
+    @Test
+    public void parse_ignores_trimmed_whitespace() throws Exception
+    {
+        Messages messages = validationMessages();
+
+        Translator<Long> translator = new LongTranslator();
+
+        assertEquals(translator.parseClient(" -123 ", messages), new Long(-123));
+    }
+}



Mime
View raw message