Updated Branches:
  refs/heads/wicket-1.5.x a78de25b3 -> 777dc4cdf

WICKET-4356
StringValueConversionException thrown when calling toInt(final int 
defaultValue) on StringValue. Why not to return the default value?


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/777dc4cd
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/777dc4cd
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/777dc4cd

Branch: refs/heads/wicket-1.5.x
Commit: 777dc4cdff42934b2c7d4b8c348543c081f3e23e
Parents: a78de25
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Tue Jan 31 13:24:47 2012 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Tue Jan 31 13:24:47 2012 +0200

----------------------------------------------------------------------
 .../org/apache/wicket/util/string/StringValue.java |  130 ++++++++++++++-
 .../apache/wicket/util/string/StringValueTest.java |   23 +++
 2 files changed, 146 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/777dc4cd/wicket-util/src/main/java/org/apache/wicket/util/string/StringValue.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/main/java/org/apache/wicket/util/string/StringValue.java 
b/wicket-util/src/main/java/org/apache/wicket/util/string/StringValue.java
index fa5ba54..2f9865c 100755
--- a/wicket-util/src/main/java/org/apache/wicket/util/string/StringValue.java
+++ b/wicket-util/src/main/java/org/apache/wicket/util/string/StringValue.java
@@ -26,6 +26,8 @@ import org.apache.wicket.IClusterable;
 import org.apache.wicket.util.lang.Objects;
 import org.apache.wicket.util.time.Duration;
 import org.apache.wicket.util.time.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -48,6 +50,8 @@ public class StringValue implements IClusterable
 {
        private static final long serialVersionUID = 1L;
 
+       private static final Logger LOG = 
LoggerFactory.getLogger(StringValue.class);
+
        /** Locale to be used for formatting and parsing. */
        private final Locale locale;
 
@@ -372,7 +376,23 @@ public class StringValue implements IClusterable
        public final boolean toBoolean(final boolean defaultValue)
                throws StringValueConversionException
        {
-               return (text == null) ? defaultValue : toBoolean();
+               boolean result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toBoolean();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                                       "An error occurred 
while converting '%s' to a boolean: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -407,7 +427,23 @@ public class StringValue implements IClusterable
         */
        public final char toChar(final char defaultValue) throws 
StringValueConversionException
        {
-               return (text == null) ? defaultValue : toChar();
+               char result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toChar();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                               "An error occurred while 
converting '%s' to a character: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -450,7 +486,23 @@ public class StringValue implements IClusterable
         */
        public final double toDouble(final double defaultValue) throws 
StringValueConversionException
        {
-               return (text == null) ? defaultValue : toDouble();
+               double result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toDouble();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                                       "An error occurred 
while converting '%s' to a double: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -486,7 +538,23 @@ public class StringValue implements IClusterable
        public final Duration toDuration(final Duration defaultValue)
                throws StringValueConversionException
        {
-               return (text == null) ? defaultValue : toDuration();
+               Duration result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toDuration();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                                       "An error occurred 
while converting '%s' to a Duration: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -518,7 +586,23 @@ public class StringValue implements IClusterable
         */
        public final int toInt(final int defaultValue) throws 
StringValueConversionException
        {
-               return (text == null) ? defaultValue : toInt();
+               int result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toInt();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                               "An error occurred while 
converting '%s' to an integer: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -569,7 +653,23 @@ public class StringValue implements IClusterable
         */
        public final long toLong(final long defaultValue) throws 
StringValueConversionException
        {
-               return (text == null) ? defaultValue : toLong();
+               long result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toLong();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                                       "An error occurred 
while converting '%s' to a long: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**
@@ -728,7 +828,23 @@ public class StringValue implements IClusterable
         */
        public final Time toTime(final Time defaultValue) throws 
StringValueConversionException
        {
-               return (text == null) ? defaultValue : toTime();
+               Time result = defaultValue;
+               if (text != null)
+               {
+                       try
+                       {
+                               result = toTime();
+                       } catch (Exception x)
+                       {
+                               if (LOG.isDebugEnabled())
+                               {
+                                       LOG.debug(String.format(
+                                               "An error occurred while 
converting '%s' to a Time: %s", text, x.getMessage()), x);
+                               }
+                       }
+               }
+
+               return result;
        }
 
        /**

http://git-wip-us.apache.org/repos/asf/wicket/blob/777dc4cd/wicket-util/src/test/java/org/apache/wicket/util/string/StringValueTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-util/src/test/java/org/apache/wicket/util/string/StringValueTest.java 
b/wicket-util/src/test/java/org/apache/wicket/util/string/StringValueTest.java
index 37ed2f2..0b3d4f5 100644
--- 
a/wicket-util/src/test/java/org/apache/wicket/util/string/StringValueTest.java
+++ 
b/wicket-util/src/test/java/org/apache/wicket/util/string/StringValueTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.wicket.util.string;
 
+import org.apache.wicket.util.time.Duration;
+import org.apache.wicket.util.time.Time;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -57,4 +59,25 @@ public class StringValueTest extends Assert
                assertNull(sv.toOptionalString());
                assertNull(sv.toOptionalTime());
        }
+
+       /**
+        * https://issues.apache.org/jira/browse/WICKET-4356
+        */
+       @Test
+       public void defaultValues()
+       {
+               StringValue sv = new StringValue("unknown");
+               
+               assertTrue(sv.toBoolean(true));
+               assertFalse(sv.toBoolean(false));
+               
+               assertEquals(4, sv.toInt(4));
+               assertEquals(4.0, sv.toDouble(4.0), 0.005);
+               assertEquals('c', sv.toChar('c'));
+               assertEquals(Duration.seconds(3), 
sv.toDuration(Duration.seconds(3)));
+               assertEquals(Time.millis(5), sv.toTime(Time.millis(5)));
+               assertEquals(40L, sv.toLong(40));
+
+               assertEquals("unknown", sv.toString("def"));
+       }
 }

Reply via email to