You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2008/06/25 06:25:10 UTC

svn commit: r671417 - in /myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src: main/java/org/apache/myfaces/trinidad/validator/ test/java/org/apache/myfaces/trinidad/validator/

Author: matzew
Date: Tue Jun 24 21:25:10 2008
New Revision: 671417

URL: http://svn.apache.org/viewvc?rev=671417&view=rev
Log:
TRINIDAD-1129 - Server-side validation does not work when using Sun JSF implementation

Added:
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java
Modified:
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
    myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java

Modified: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java?rev=671417&r1=671416&r2=671417&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java (original)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java Tue Jun 24 21:25:10 2008
@@ -30,6 +30,7 @@
 import org.apache.myfaces.trinidad.bean.FacesBean;
 import org.apache.myfaces.trinidad.bean.PropertyKey;
 import org.apache.myfaces.trinidad.util.ComponentUtils;
+import org.apache.myfaces.trinidad.util.IntegerUtils;
 import org.apache.myfaces.trinidad.util.MessageFactory;
 
 /**
@@ -75,7 +76,13 @@
   public static final String NOT_IN_RANGE_MESSAGE_ID =
       "org.apache.myfaces.trinidad.validator.DoubleRangeValidator.NOT_IN_RANGE";
 
-  
+  /**
+   * <p>The message identifier of the FacesMessage to be created if
+   * the value cannot be converted
+   */
+  public static final String CONVERT_MESSAGE_ID =
+      "org.apache.myfaces.trinidad.convert.DoubleConverter.CONVERT";
+
   /**
    * Construct a {@link Validator} with no preconfigured limits.
    */
@@ -118,7 +125,7 @@
   {
     Object maxDouble = _facesBean.getProperty(_MAXIMUM_KEY);
     if(maxDouble == null)
-      maxDouble = Double.MAX_VALUE;
+      maxDouble = _MAXIMUM_KEY.getDefault();
     return ComponentUtils.resolveDouble(maxDouble);
   }
 
@@ -131,7 +138,6 @@
   @Override
   public void setMaximum(double maximum)
   {
-    super.setMaximum(maximum);
     _facesBean.setProperty(_MAXIMUM_KEY, Double.valueOf(maximum));
   }
 
@@ -146,7 +152,7 @@
   {
     Object minDouble = _facesBean.getProperty(_MINIMUM_KEY);
     if(minDouble == null)
-      minDouble = Double.MIN_VALUE;
+      minDouble = _MINIMUM_KEY.getDefault();
     return ComponentUtils.resolveDouble(minDouble);
   }
 
@@ -159,7 +165,6 @@
   @Override
   public void setMinimum(double minimum)
   {
-    super.setMinimum(minimum);
     _facesBean.setProperty(_MINIMUM_KEY, Double.valueOf(minimum));
   }
 
@@ -305,53 +310,53 @@
     Object value
     ) throws ValidatorException
   {
-    try
+    if ((context == null) || (component == null))
+    {
+      throw new NullPointerException();
+    }
+
+    if (value == null)
     {
-      super.validate(context, component, value);
+      return;
     }
-    catch (ValidatorException ve)
+    if (value instanceof Number)
     {
-         
-      if (value != null && value instanceof Number)
+      double doubleValue = ((Number)value).doubleValue(); 
+      double min = getMinimum();
+      double max = getMaximum();
+
+      if(isMaximumSet() && isMinimumSet())
       {
-        double doubleValue = ((Number)value).doubleValue(); 
-        
-        double min = getMinimum();
-        double max = getMaximum();
-        
-        if (doubleValue > max)
+        if(doubleValue < min || doubleValue > max)
         {
-          if (min != Double.MIN_VALUE)//the default...
-          {
-             throw new ValidatorException
-                        (_getNotInRangeMessage(context, component, value, min, max));
-          }
-          else
-          {
-             throw new ValidatorException
-                        (_getMaximumMessage(context, component, value, max));
-          }
+          throw new ValidatorException(
+            _getNotInRangeMessage(context, component, value, min, max));
         }
-
-        if (doubleValue < min)
+      }
+      
+      if(isMaximumSet())
+      {
+        if (doubleValue > max)
         {
-          if (max != Double.MAX_VALUE)//the default...
-          {
-            throw new ValidatorException
-                        (_getNotInRangeMessage(context, component, value, min, max));
-          }
-          else
-          {
-            FacesMessage msg = _getMinimumMessage(context, component, value, min);
-            throw new ValidatorException(msg);
-          }
+          throw new ValidatorException(
+            _getMaximumMessage(context, component, value, max));
         }
       }
-      else
+      
+      if(isMinimumSet())
       {
-        throw ve;
+        if (doubleValue < min)
+        {
+          throw new ValidatorException(
+            _getMinimumMessage(context, component, value, min));
+        }
       }
-    }     
+    }
+    else
+    {
+      FacesMessage msg = _getNotCorrectType(context);
+      throw new ValidatorException(msg);
+    }
   }
 
   //  StateHolder Methods
@@ -455,13 +460,29 @@
     _transientValue = transientValue;
   }
 
+  protected boolean isMaximumSet()
+  {
+    return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+  }
+
+  protected boolean isMinimumSet()
+  {
+    return _facesBean.getProperty(_MINIMUM_KEY) != null;
+  }
+
+  private FacesMessage _getNotCorrectType(
+    FacesContext context)
+  {
+    return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
+  }
+  
   private FacesMessage _getNotInRangeMessage(
-      FacesContext context,
-      UIComponent component,
-      Object value,
-      Object min,
-      Object max)
-    {
+    FacesContext context,
+    UIComponent component,
+    Object value,
+    Object min,
+    Object max)
+  {
       Object msg   = _getRawNotInRangeMessageDetail();
       Object label = ValidatorUtils.getComponentLabel(component);
 
@@ -469,7 +490,7 @@
 
       return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID,
                                         msg, params, component);
-    }
+  }
 
 
     
@@ -526,10 +547,14 @@
   private static final FacesBean.Type _TYPE = new FacesBean.Type();
 
   private static final PropertyKey _MINIMUM_KEY =
-    _TYPE.registerKey("minimum", Double.class);
+    _TYPE.registerKey("minimum", Double.class,
+                                 // Don't rely on autoboxing: there's a method overload
+                                 Double.valueOf(Double.MIN_VALUE));
 
   private static final PropertyKey _MAXIMUM_KEY =
-    _TYPE.registerKey("maximum", Double.class);
+    _TYPE.registerKey("maximum", Double.class,
+                                 // Don't rely on autoboxing: there's a method overload
+                                 Double.valueOf(Double.MAX_VALUE));
 
   private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
     _TYPE.registerKey("messageDetailMaximum", String.class);

Modified: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java?rev=671417&r1=671416&r2=671417&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java (original)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java Tue Jun 24 21:25:10 2008
@@ -31,7 +31,6 @@
 import org.apache.myfaces.trinidad.bean.FacesBean;
 import org.apache.myfaces.trinidad.bean.PropertyKey;
 import org.apache.myfaces.trinidad.util.ComponentUtils;
-import org.apache.myfaces.trinidad.util.IntegerUtils;
 import org.apache.myfaces.trinidad.util.MessageFactory;
 
 /**
@@ -132,8 +131,6 @@
   public int getMaximum()
   {
     Object maxInt = _facesBean.getProperty(_MAXIMUM_KEY);
-    if (maxInt == null)
-      maxInt = _MAXIMUM_KEY.getDefault();
     return ComponentUtils.resolveInteger(maxInt);
   }
 
@@ -146,7 +143,6 @@
   @Override
   public void setMaximum(int maximum)
   {
-    super.setMaximum(maximum);
     _facesBean.setProperty(_MAXIMUM_KEY, Integer.valueOf(maximum));
   }
 
@@ -160,8 +156,6 @@
   public int getMinimum()
   {
     Object minInt = _facesBean.getProperty(_MINIMUM_KEY);
-    if (minInt == null)
-      minInt = _MINIMUM_KEY.getDefault();
     return ComponentUtils.resolveInteger(minInt);
   }
 
@@ -174,7 +168,6 @@
   @Override
   public void setMinimum(int minimum)
   {
-    super.setMinimum(minimum);
     _facesBean.setProperty(_MINIMUM_KEY, Integer.valueOf(minimum));
   }
 
@@ -370,39 +363,46 @@
     Object value
     ) throws ValidatorException
   {
-    try
+    if ((context == null) || (component == null))
     {
-      super.validate(context, component, value);
+      throw new NullPointerException();
     }
-    catch (ValidatorException ve)
+
+    if(value != null)
     {
-      // We don't really care about the value.  It
-      // failed validation in the superclass, so we just
-      // care about what sort of message to show
-      int min = getMinimum();
       int max = getMaximum();
-      
-      // FIXME: the default of max should be zero, not MAX_VALUE,
-      // at least according to the superclass
-      if (max != Integer.MAX_VALUE)
+      int min = getMinimum();
+      int length = value instanceof String ?
+        ((String)value).length() : value.toString().length();
+
+      // range validation
+      if(isMaximumSet() && isMinimumSet())
       {
-        if (min > 0)
+        if(length<min || length>max)
         {
           throw new ValidatorException(
             _getNotInRangeMessage(context, component, value, min, max));
         }
-        else
+      }
+      // too short
+      if(isMinimumSet())
+      {
+        if (length < min)
         {
           throw new ValidatorException(
-            _getMaximumMessage(context, component, value, max));
+            _getMinimumMessage(context, component, value, max));
         }
       }
-      else
+      // too long
+      if(isMaximumSet())
       {
-        throw new ValidatorException(
-          _getMinimumMessage(context, component, value, min));
+        if (length > max)
+        {
+          throw new ValidatorException(
+            _getMaximumMessage(context, component, value, max));
+        }
       }
-    }     
+    }
   }
 
   //  StateHolder Methods
@@ -506,6 +506,15 @@
     _transientValue = transientValue;
   }
 
+  protected boolean isMaximumSet()
+  {
+    return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+  }
+
+  protected boolean isMinimumSet()
+  {
+    return _facesBean.getProperty(_MINIMUM_KEY) != null;
+  }
 
   private FacesMessage _getNotInRangeMessage(
       FacesContext context,
@@ -612,7 +621,7 @@
   private static final PropertyKey _MAXIMUM_KEY =
     _TYPE.registerKey("maximum", Integer.class,
                       // Don't rely on autoboxing: there's a method overload
-                      Integer.valueOf(Integer.MAX_VALUE));
+                      Integer.valueOf(0));
 
   private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
     _TYPE.registerKey("messageDetailMaximum", String.class);

Modified: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java?rev=671417&r1=671416&r2=671417&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java (original)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java Tue Jun 24 21:25:10 2008
@@ -78,7 +78,14 @@
   public static final String NOT_IN_RANGE_MESSAGE_ID =
       "org.apache.myfaces.trinidad.validator.LongRangeValidator.NOT_IN_RANGE";
 
-  
+  /**
+   * <p>The message identifier of the FacesMessage to be created if
+   * the value cannot be converted to an integer
+   */
+  public static final String CONVERT_MESSAGE_ID =
+      "org.apache.myfaces.trinidad.convert.LongConverter.CONVERT";
+
+
   /**
    * Construct a {@link Validator} with no preconfigured limits.
    */
@@ -121,7 +128,7 @@
   {
     Object maxLong = _facesBean.getProperty(_MAXIMUM_KEY);
     if(maxLong == null)
-      maxLong = Long.MAX_VALUE;
+      maxLong = _MAXIMUM_KEY.getDefault();
     return ComponentUtils.resolveLong(maxLong);
   }
 
@@ -134,7 +141,6 @@
   @Override
   public void setMaximum(long maximum)
   {
-    super.setMaximum(maximum);
     _facesBean.setProperty(_MAXIMUM_KEY, Long.valueOf(maximum));
   }
 
@@ -149,7 +155,7 @@
   {
     Object minLong = _facesBean.getProperty(_MINIMUM_KEY);
     if(minLong == null)
-      minLong = Long.MIN_VALUE;
+      minLong = _MINIMUM_KEY.getDefault();
     return ComponentUtils.resolveLong(minLong);
   }
 
@@ -162,7 +168,6 @@
   @Override
   public void setMinimum(long minimum)
   {
-    super.setMinimum(minimum);
     _facesBean.setProperty(_MINIMUM_KEY, Long.valueOf(minimum));
   }
 
@@ -308,53 +313,54 @@
     Object value
     ) throws ValidatorException
   {
-    try
+    if ((context == null) || (component == null))
     {
-      super.validate(context, component, value);
+      throw new NullPointerException();
     }
-    catch (ValidatorException ve)
+
+    if (value == null)
     {
-         
-      if (value != null && value instanceof Number)
+      return;
+    }
+
+    if (value instanceof Number)
+    {
+      long longValue = ((Number)value).longValue();
+      long min = getMinimum();
+      long max = getMaximum();
+      
+      if(isMaximumSet() && isMinimumSet())
       {
-        long longValue = ((Number)value).longValue(); 
-        
-        long min = getMinimum();
-        long max = getMaximum();
-        
-        if (longValue > max)
+        if(longValue<min || longValue>max)
         {
-          if (min != Long.MIN_VALUE)//the default...
-          {
-             throw new ValidatorException
-                        (_getNotInRangeMessage(context, component, value, IntegerUtils.getString(min), IntegerUtils.getString(max)));
-          }
-          else
-          {
-             throw new ValidatorException
-                        (_getMaximumMessage(context, component, value, IntegerUtils.getString(max)));
-          }
+          throw new ValidatorException(
+            _getNotInRangeMessage(context, component, value, min, max));
         }
-
-        if (longValue < min)
+      }
+      
+      if(isMaximumSet())
+      {
+        if (longValue > max)
         {
-          if (max != Long.MAX_VALUE)//the default...
-          {
-            throw new ValidatorException
-                        (_getNotInRangeMessage(context, component, value, IntegerUtils.getString(min), IntegerUtils.getString(max)));
-          }
-          else
-          {
-            FacesMessage msg = _getMinimumMessage(context, component, value, IntegerUtils.getString(min));
-            throw new ValidatorException(msg);
-          }
+          throw new ValidatorException(
+            _getMaximumMessage(context, component, value, IntegerUtils.getString(max)));
         }
       }
-      else
+      
+      if(isMinimumSet())
       {
-        throw ve;
+        if (longValue < min)
+        {
+            throw new ValidatorException(
+              _getMinimumMessage(context, component, value, IntegerUtils.getString(min)));
+        }
       }
-    }     
+    }
+    else
+    {
+      FacesMessage msg = _getNotCorrectType(context);
+      throw new ValidatorException(msg);
+    }
   }
 
   //  StateHolder Methods
@@ -458,6 +464,22 @@
     _transientValue = transientValue;
   }
 
+  protected boolean isMaximumSet()
+  {
+    return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+  }
+
+  protected boolean isMinimumSet()
+  {
+    return _facesBean.getProperty(_MINIMUM_KEY) != null;
+  }
+
+  private FacesMessage _getNotCorrectType(
+    FacesContext context)
+  {
+    return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
+  }
+
   private FacesMessage _getNotInRangeMessage(
       FacesContext context,
       UIComponent component,
@@ -529,10 +551,14 @@
   private static final FacesBean.Type _TYPE = new FacesBean.Type();
 
   private static final PropertyKey _MINIMUM_KEY =
-    _TYPE.registerKey("minimum", Long.class);
+    _TYPE.registerKey("minimum", Long.class,
+                                 // Don't rely on autoboxing: there's a method overload
+                                 Long.valueOf(Long.MIN_VALUE));
 
   private static final PropertyKey _MAXIMUM_KEY =
-    _TYPE.registerKey("maximum", Long.class);
+    _TYPE.registerKey("maximum", Long.class,
+                                 // Don't rely on autoboxing: there's a method overload
+                                 Long.valueOf(Long.MAX_VALUE));
 
   private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
     _TYPE.registerKey("messageDetailMaximum", String.class);

Added: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java?rev=671417&view=auto
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java (added)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java Tue Jun 24 21:25:10 2008
@@ -0,0 +1,207 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for DoubleRangeValidator
+ *
+ */
+public class DoubleRangeValidatorTest extends ValidatorTestCase
+{
+  public DoubleRangeValidatorTest(String testName)
+  {
+    super(testName);
+  }
+  
+  @Override
+  protected void setUp() throws Exception
+  {
+    super.setUp();
+  }
+  
+  @Override
+  protected void tearDown() throws Exception
+  {
+    super.tearDown();
+  }
+  
+  public static Test suite()
+  {
+    return new TestSuite(DoubleRangeValidatorTest.class);
+  }
+
+  /**
+   * Tests that null returns immediately.
+   *
+   * @throws ValidatorException  when test fails
+   */
+  public void testNull() throws ValidatorException
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    DoubleRangeValidator validator = new DoubleRangeValidator();
+
+    doTestNull(facesContext, wrapper, validator);
+  }
+
+  /**
+   * Test when context is set to null
+   */
+  public void testNullContext()
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    DoubleRangeValidator validator = new DoubleRangeValidator();
+
+    doTestNullContext(wrapper, validator);
+  }
+
+  public void testNullComponent()
+  {
+    DoubleRangeValidator validator = new DoubleRangeValidator();
+
+    doTestNullComponent(facesContext, validator);
+  }
+
+  public void testTooLarge()
+  {
+    // since the pattern has not been set it will be null
+    // let us push some arbitary value
+    Mock mock = mock(UIComponent.class); 
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      DoubleRangeValidator validator = new DoubleRangeValidator();
+      validator.setMaximum(100);
+      validator.validate(facesContext, component, 1000);
+      // test fails if it is here
+
+      fail("Expected Null pointer exception");
+    }
+    catch (ValidatorException ve)
+    {
+      // suppress it - this is as expected
+    }
+    mock.verify();
+  }
+
+  public void testWrongType()
+  {
+    // since the pattern has not been set it will be null
+    // let us push some arbitary value
+    Mock mock = mock(UIComponent.class); 
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      DoubleRangeValidator validator = new DoubleRangeValidator();
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, "thisShouldFail");
+      // test fails if it is here
+
+      fail("Expected Null pointer exception");
+    }
+    catch (ValidatorException ve)
+    {
+      // suppress it - this is as expected
+    }
+    mock.verify();
+  }
+
+  public void testExactFailure()
+  {
+    // some very basic sanity test
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      DoubleRangeValidator validator = new DoubleRangeValidator();
+      double value = 20d;
+      validator.setMinimum(2);
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, value);
+      fail("Expected ValidatorException for exact");
+    }
+    catch (ValidatorException ve)
+    {
+      // if exception then fine.
+    }
+
+    mock.verify();
+  }
+
+  public void testSanitySuccess()
+  {
+    //some very basic sanity test
+    //
+    DoubleRangeValidator validator = new DoubleRangeValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    Double values[]   = {200d,500d};
+    validator.setMinimum(2);
+    for (int i = 0; i < values.length ; i++)
+    {
+      doTestValidate(validator, facesContext, wrapper, values[i]);
+    }
+  }
+
+  public void testStateHolderSaveRestore()
+  {
+    DoubleRangeValidator validator = new DoubleRangeValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    validator.setMaximum(5);
+    validator.setMessageDetailMaximum("Validation failed");
+    DoubleRangeValidator restoreValidator = new  DoubleRangeValidator();
+
+    doTestStateHolderSaveRestore(validator, restoreValidator,
+                                 facesContext, wrapper);
+  }
+}
+  //////////////////////////////////////////////////////////////////////////////
+  //                             MOCK OBJECTS
+  // 1. Get a MockControl for the interface we would like to simulate
+  // 2. get the MockObject from MockControl
+  // 3. specify the behaviour of the Mock Object (record state)
+  // 4. activate the MockObject via the control  (replay state)
+  //
+  //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file

Added: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java?rev=671417&view=auto
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java (added)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java Tue Jun 24 21:25:10 2008
@@ -0,0 +1,182 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for LengthValidator
+ *
+ */
+public class LengthValidatorTest extends ValidatorTestCase
+{
+  public LengthValidatorTest(String testName)
+  {
+    super(testName);
+  }
+  
+  @Override
+  protected void setUp() throws Exception
+  {
+    super.setUp();
+  }
+  
+  @Override
+  protected void tearDown() throws Exception
+  {
+    super.tearDown();
+  }
+  
+  public static Test suite()
+  {
+    return new TestSuite(LengthValidatorTest.class);
+  }
+
+  /**
+   * Tests that null returns immediately.
+   *
+   * @throws ValidatorException  when test fails
+   */
+  public void testNull() throws ValidatorException
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    LengthValidator validator = new LengthValidator();
+
+    doTestNull(facesContext, wrapper, validator);
+  }
+
+  /**
+   * Test when context is set to null
+   */
+  public void testNullContext()
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    LengthValidator validator = new LengthValidator();
+
+    doTestNullContext(wrapper, validator);
+  }
+
+  public void testNullComponent()
+  {
+    LengthValidator validator = new LengthValidator();
+
+    doTestNullComponent(facesContext, validator);
+  }
+
+  public void testTooLargeLength()
+  {
+    // since the pattern has not been set it will be null
+    // let us push some arbitary value
+    Mock mock = mock(UIComponent.class); 
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      LengthValidator validator = new LengthValidator();
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, "someValue");
+      // test fails if it is here
+
+      fail("Expected Null pointer exception");
+    }
+    catch (ValidatorException ve)
+    {
+      // suppress it - this is as expected
+    }
+    mock.verify();
+  }
+
+  public void testExactFailure()
+  {
+    // some very basic sanity test
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      LengthValidator validator = new LengthValidator();
+      String value = "999999";
+      validator.setMinimum(2);
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, value);
+      fail("Expected ValidatorException for exact");
+    }
+    catch (ValidatorException ve)
+    {
+      // if exception then fine.
+    }
+
+    mock.verify();
+  }
+
+  public void testSanitySuccess()
+  {
+    //some very basic sanity test
+    //
+    LengthValidator validator = new LengthValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    String values[]   = {"Hallo","world"};
+    validator.setMinimum(2);
+    for (int i = 0; i < values.length ; i++)
+    {
+      doTestValidate(validator, facesContext, wrapper, values[i]);
+    }
+  }
+
+  public void testStateHolderSaveRestore()
+  {
+    LengthValidator validator = new LengthValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    validator.setMaximum(5);
+    validator.setMessageDetailMaximum("Validation failed");
+    LengthValidator restoreValidator = new  LengthValidator();
+
+    doTestStateHolderSaveRestore(validator, restoreValidator,
+                                 facesContext, wrapper);
+  }
+}
+  //////////////////////////////////////////////////////////////////////////////
+  //                             MOCK OBJECTS
+  // 1. Get a MockControl for the interface we would like to simulate
+  // 2. get the MockObject from MockControl
+  // 3. specify the behaviour of the Mock Object (record state)
+  // 4. activate the MockObject via the control  (replay state)
+  //
+  //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file

Added: myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java?rev=671417&view=auto
==============================================================================
--- myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java (added)
+++ myfaces/trinidad/branches/1.2.8.1-branch/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java Tue Jun 24 21:25:10 2008
@@ -0,0 +1,207 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for LongRangeValidator
+ *
+ */
+public class LongRangeValidatorTest extends ValidatorTestCase
+{
+  public LongRangeValidatorTest(String testName)
+  {
+    super(testName);
+  }
+  
+  @Override
+  protected void setUp() throws Exception
+  {
+    super.setUp();
+  }
+  
+  @Override
+  protected void tearDown() throws Exception
+  {
+    super.tearDown();
+  }
+  
+  public static Test suite()
+  {
+    return new TestSuite(LongRangeValidatorTest.class);
+  }
+
+  /**
+   * Tests that null returns immediately.
+   *
+   * @throws ValidatorException  when test fails
+   */
+  public void testNull() throws ValidatorException
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    LongRangeValidator validator = new LongRangeValidator();
+
+    doTestNull(facesContext, wrapper, validator);
+  }
+
+  /**
+   * Test when context is set to null
+   */
+  public void testNullContext()
+  {
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    LongRangeValidator validator = new LongRangeValidator();
+
+    doTestNullContext(wrapper, validator);
+  }
+
+  public void testNullComponent()
+  {
+    LongRangeValidator validator = new LongRangeValidator();
+
+    doTestNullComponent(facesContext, validator);
+  }
+
+  public void testTooLarge()
+  {
+    // since the pattern has not been set it will be null
+    // let us push some arbitary value
+    Mock mock = mock(UIComponent.class); 
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      LongRangeValidator validator = new LongRangeValidator();
+      validator.setMaximum(100);
+      validator.validate(facesContext, component, 1000);
+      // test fails if it is here
+
+      fail("Expected Null pointer exception");
+    }
+    catch (ValidatorException ve)
+    {
+      // suppress it - this is as expected
+    }
+    mock.verify();
+  }
+
+  public void testWrongType()
+  {
+    // since the pattern has not been set it will be null
+    // let us push some arbitary value
+    Mock mock = mock(UIComponent.class); 
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      LongRangeValidator validator = new LongRangeValidator();
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, "thisShouldFail");
+      // test fails if it is here
+
+      fail("Expected Null pointer exception");
+    }
+    catch (ValidatorException ve)
+    {
+      // suppress it - this is as expected
+    }
+    mock.verify();
+  }
+
+  public void testExactFailure()
+  {
+    // some very basic sanity test
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+    setMockLabelForComponent(wrapper);
+
+    try
+    {
+      LongRangeValidator validator = new LongRangeValidator();
+      long value = 20;
+      validator.setMinimum(2);
+      validator.setMaximum(2);
+      validator.validate(facesContext, component, value);
+      fail("Expected ValidatorException for exact");
+    }
+    catch (ValidatorException ve)
+    {
+      // if exception then fine.
+    }
+
+    mock.verify();
+  }
+
+  public void testSanitySuccess()
+  {
+    //some very basic sanity test
+    //
+    LongRangeValidator validator = new LongRangeValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    Long values[]   = {200l,500l};
+    validator.setMinimum(2);
+    for (int i = 0; i < values.length ; i++)
+    {
+      doTestValidate(validator, facesContext, wrapper, values[i]);
+    }
+  }
+
+  public void testStateHolderSaveRestore()
+  {
+    LongRangeValidator validator = new LongRangeValidator();
+    Mock mock = buildMockUIComponent();
+    UIComponent component = (UIComponent) mock.proxy();
+    MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+    validator.setMaximum(5);
+    validator.setMessageDetailMaximum("Validation failed");
+    LongRangeValidator restoreValidator = new  LongRangeValidator();
+
+    doTestStateHolderSaveRestore(validator, restoreValidator,
+                                 facesContext, wrapper);
+  }
+}
+  //////////////////////////////////////////////////////////////////////////////
+  //                             MOCK OBJECTS
+  // 1. Get a MockControl for the interface we would like to simulate
+  // 2. get the MockObject from MockControl
+  // 3. specify the behaviour of the Mock Object (record state)
+  // 4. activate the MockObject via the control  (replay state)
+  //
+  //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file