You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by se...@apache.org on 2007/05/16 04:46:02 UTC

svn commit: r538410 - in /jakarta/turbine/core/branches/TURBINE_2_3_BRANCH: conf/test/ src/java/org/apache/turbine/services/intake/model/ src/test/org/apache/turbine/services/intake/

Author: seade
Date: Tue May 15 19:46:01 2007
New Revision: 538410

URL: http://svn.apache.org/viewvc?view=rev&rev=538410
Log:
Ensure that required fields that are not set are still validated.  Added test case for this and a couple of other potential problems.

Added:
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/RequiredFalseGroupTestObject.java
Modified:
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/conf/test/intake.xml
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/intake/model/Group.java
    jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/IntakeServiceTest.java

Modified: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/conf/test/intake.xml
URL: http://svn.apache.org/viewvc/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/conf/test/intake.xml?view=diff&rev=538410&r1=538409&r2=538410
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/conf/test/intake.xml (original)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/conf/test/intake.xml Tue May 15 19:46:01 2007
@@ -53,12 +53,20 @@
             <rule value="IntMin" name="less-than-or-equal">Value must be less than or equal to IntMin</rule>
         </field>
     </group>
-    <group name="RequiredFalseTest" key="rft">
+    <group name="RequiredFalseTest" key="rft" mapToObject="org.apache.turbine.services.intake.RequiredFalseGroupTestObject">
         <field name="StringRF" key="stringrf" type="String">
             <rule name="required" value="false">Please enter a value.</rule>
             <rule name="minLength" value="3">Please enter 3 characters only.</rule>
             <rule name="maxLength" value="3">Please enter 3 characters only.</rule>
         </field>
+        <!-- An Integer object -->
+        <field name="IntegerRF" key="integerrf" type="int">
+            <rule name="required" value="false">Please enter the a value.</rule>
+            <rule name="minValue" value="1">The value for this field must be between 1 and 250 inclusive.</rule>
+            <rule name="maxValue" value="250">The value for this field must be between 1 and 250 inclusive.</rule>
+            <rule name="invalidNumber" value="true">The value entered must be a number.</rule>
+        </field>
+        <!-- An int primative -->
         <field name="IntRF" key="intrf" type="int">
             <rule name="required" value="false">Please enter the a value.</rule>
             <rule name="minValue" value="1">The value for this field must be between 1 and 250 inclusive.</rule>
@@ -69,6 +77,13 @@
             <rule name="required" value="false">Please enter a value.</rule>
             <rule name="format" value="d/MM/yyyy">Please enter a sensible date ("dd/mm/yyyy").</rule>
             <rule name="format1" value="d/MM/yy">Please enter a sensible date ("dd/mm/yyyy").</rule>
+        </field>
+    </group>
+    <group name="RequiredTrueTest" key="rtt">
+        <field name="StringRT" key="stringrt" type="String">
+            <rule name="required" value="true">Please enter a value.</rule>
+            <rule name="minLength" value="3">Please enter 3 characters only.</rule>
+            <rule name="maxLength" value="3">Please enter 3 characters only.</rule>
         </field>
     </group>
 </input-data>

Modified: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/intake/model/Group.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/intake/model/Group.java?view=diff&rev=538410&r1=538409&r2=538410
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/intake/model/Group.java (original)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/services/intake/model/Group.java Tue May 15 19:46:01 2007
@@ -187,7 +187,8 @@
         }
         for (int i = fieldsArray.length - 1; i >= 0; i--)
         {
-            if (fieldsArray[i].isSet() && !fieldsArray[i].isValidated())
+            if ((fieldsArray[i].isSet() || fieldsArray[i].isRequired())
+                    && !fieldsArray[i].isValidated())
             {
                 fieldsArray[i].validate();
             }

Modified: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/IntakeServiceTest.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/IntakeServiceTest.java?view=diff&rev=538410&r1=538409&r2=538410
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/IntakeServiceTest.java (original)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/IntakeServiceTest.java Tue May 15 19:46:01 2007
@@ -19,6 +19,9 @@
  * under the License.
  */
 
+import java.util.Calendar;
+import java.util.Date;
+
 import junit.framework.TestSuite;
 
 import org.apache.turbine.services.ServiceManager;
@@ -40,6 +43,7 @@
     Group rangeTestGroup = null;
     Group integerRangeTestGroup = null;
     Group requiredFalseTestGroup = null;
+    Group requiredTrueTestGroup = null;
 
     public IntakeServiceTest(String name) throws Exception
     {
@@ -51,9 +55,9 @@
         rangeTestGroup = intakeService.getGroup("DateRangeTest");
         integerRangeTestGroup = intakeService.getGroup("IntRangeTest");
         requiredFalseTestGroup = intakeService.getGroup("RequiredFalseTest");
+        requiredTrueTestGroup = intakeService.getGroup("RequiredTrueTest");
     }
 
-
     public void testEmptyBooleanField() throws IntakeException
     {
         Field booleanField = booleanTestGroup.get("EmptyBooleanTestField");
@@ -107,29 +111,150 @@
         assertFalse("The integer range should not be valid", imax2.isValid());
     }
 
+    /**
+     * This test attempts to verify that an intake field returns false for
+     * isSet() when an empty field is submitted (IMHO this was a bug prior to
+     * revision 538232 (i.e. release 2.3.3).
+     *
+     * @throws IntakeException
+     */
     public void testRequiredFalse() throws IntakeException
     {
         ParameterParser pp = new DefaultParameterParser();
         pp.add("rft_0stringrf", "");
+        pp.add("rft_0integerrf", "");
         pp.add("rft_0intrf", "");
         pp.add("rft_0daterf", "");
         requiredFalseTestGroup.init(Group.NEW, pp);
-        
+
         Field stringRF = requiredFalseTestGroup.get("StringRF");
+        Field integerRF = requiredFalseTestGroup.get("IntegerRF");
         Field intRF = requiredFalseTestGroup.get("IntRF");
         Field dateRF = requiredFalseTestGroup.get("DateRF");
-        
+
         assertFalse("StringRF should not be set", stringRF.isSet());
         assertTrue("StringRF should be valid", stringRF.isValid());
         assertNull(stringRF.getValue());
+        assertFalse("IntegerRF should not be set", integerRF.isSet());
+        assertTrue("IntegerRF should be valid", integerRF.isValid());
+        assertNull(integerRF.getValue());
         assertFalse("IntRF should not be set", intRF.isSet());
         assertTrue("IntRF should be valid", intRF.isValid());
-        assertNull(intRF.getValue());
+        assertNull(intRF.getValue()); // zero?
         assertFalse("DateRF should not be set", dateRF.isSet());
         assertTrue("DateRF should be valid", dateRF.isValid());
         assertNull(dateRF.getValue());
     }
-    
+
+    /**
+     * This test attempts to verify that with the isSet() fix applied to Field
+     * (see testRequiredFalse()) an empty field will still clear existing
+     * values.
+     *
+     * @throws IntakeException
+     */
+    public void testClearValues() throws IntakeException
+    {
+        RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
+        rfgto.setStringRF("originalString");
+        rfgto.setIntegerRF(new Integer(5));
+        rfgto.setIntRF(6);
+        Date testDate = new Date();
+        rfgto.setDateRF(testDate);
+
+        ParameterParser pp = new DefaultParameterParser();
+        pp.add("rft_0stringrf", "");
+        pp.add("rft_0integerrf", "");
+        pp.add("rft_0intrf", "");
+        pp.add("rft_0daterf", "");
+        requiredFalseTestGroup.init(Group.NEW, pp);
+
+        requiredFalseTestGroup.setProperties(rfgto);
+        assertNull("String value should have been cleared.", rfgto.getStringRF());
+        assertNull("Date value should have been cleared.", rfgto.getDateRF());
+        assertEquals("int value should have been cleared to zero.", 0, rfgto.getIntRF());
+
+        // The following commented out test fails.
+        // The trouble is that the use of reflection forces Intake to use Integer rather than int
+        // when invoking the setter method on the object to which the group is being mapped.
+
+        //assertNull("Integer value should have been cleared to null, but instead it is "
+        //        + rfgto.getIntegerRF(), rfgto.getIntegerRF());
+
+        // The net result is that Intake is currently not well suited to validating
+        // Integer fields where a null value needs to be distinguished from a zero.
+    }
+
+    /**
+     * This test attempts to verify that with that valid values coming from
+     * intake map through to an object correctly.
+     *
+     * @throws IntakeException
+     */
+    public void testSetValues() throws IntakeException
+    {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(new Date());
+        cal.set(Calendar.HOUR_OF_DAY, 0);
+        cal.set(Calendar.MINUTE, 0);
+        cal.set(Calendar.SECOND, 0);
+        cal.set(Calendar.MILLISECOND, 0);
+        Date testDate = cal.getTime();
+        // This is in dd/mm/yyyy format, as defined in the intake.xml for this group.
+        String testDateString = cal.get(Calendar.DAY_OF_MONTH) + "/"
+                + (cal.get(Calendar.MONTH) + 1) + "/" + (cal.get(Calendar.YEAR));
+
+        ParameterParser pp = new DefaultParameterParser();
+        pp.add("rft_0stringrf", "ABC"); // rules require 3 characters.
+        pp.add("rft_0integerrf", new Integer(10));
+        pp.add("rft_0intrf", 11);
+        pp.add("rft_0daterf", testDateString);
+        requiredFalseTestGroup.init(Group.NEW, pp);
+
+        Field stringRF = requiredFalseTestGroup.get("StringRF");
+        Field integerRF = requiredFalseTestGroup.get("IntegerRF");
+        Field intRF = requiredFalseTestGroup.get("IntRF");
+        Field dateRF = requiredFalseTestGroup.get("DateRF");
+
+        assertTrue("StringRF should be set", stringRF.isSet());
+        assertTrue("StringRF should be valid", stringRF.isValid());
+        assertEquals("ABC", stringRF.getValue());
+        assertTrue("IntegerRF should be set", integerRF.isSet());
+        assertTrue("IntegerRF should be valid", integerRF.isValid());
+        assertEquals(new Integer(10), integerRF.getValue());
+        assertTrue("IntRF should be set", intRF.isSet());
+        assertTrue("IntRF should be valid", intRF.isValid());
+        assertEquals(11, ((Integer) intRF.getValue()).intValue());
+        assertTrue("DateRF should be set", dateRF.isSet());
+        assertTrue("DateRF should be valid", dateRF.isValid());
+        assertEquals(testDate, dateRF.getValue());
+
+        RequiredFalseGroupTestObject rfgto = new RequiredFalseGroupTestObject();
+        requiredFalseTestGroup.setProperties(rfgto);
+        assertEquals("ABC", rfgto.getStringRF());
+        assertEquals(new Integer(10), rfgto.getIntegerRF());
+        assertEquals(11, rfgto.getIntRF());
+        assertEquals(testDate, rfgto.getDateRF());
+    }
+
+    /**
+     * Test that a required field with no value assigned is invalid.
+     *
+     * @throws IntakeException
+     */
+    public void testRequiredTrue() throws IntakeException
+    {
+        ParameterParser pp = new DefaultParameterParser();
+        pp.add("rft_0stringrf", "");
+        requiredTrueTestGroup.init(Group.NEW, pp);
+
+        Field stringRT = requiredTrueTestGroup.get("StringRT");
+
+        assertFalse("StringRT should not be set", stringRT.isSet());
+        assertFalse("StringRT should not be valid", stringRT.isValid());
+        assertNull(stringRT.getValue());
+    }
+
     /**
      * Factory method for creating a TestSuite for this class.
      *

Added: jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/RequiredFalseGroupTestObject.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/RequiredFalseGroupTestObject.java?view=auto&rev=538410
==============================================================================
--- jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/RequiredFalseGroupTestObject.java (added)
+++ jakarta/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/services/intake/RequiredFalseGroupTestObject.java Tue May 15 19:46:01 2007
@@ -0,0 +1,94 @@
+package org.apache.turbine.services.intake;
+
+/*
+ * 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.
+ */
+
+import java.util.Date;
+
+public class RequiredFalseGroupTestObject
+{
+    private String stringRF;
+    private Integer integerRF = null;
+    private int intRF;
+    private Date dateRF;
+
+    /**
+     * @return the dateRF
+     */
+    public Date getDateRF()
+    {
+        return dateRF;
+    }
+
+    /**
+     * @param dateRF the dateRF to set
+     */
+    public void setDateRF(Date dateRF)
+    {
+        this.dateRF = dateRF;
+    }
+
+    /**
+     * @return the integerRF
+     */
+    public Integer getIntegerRF()
+    {
+        return integerRF;
+    }
+
+    /**
+     * @param integerRF the integerRF to set
+     */
+    public void setIntegerRF(Integer integerRF)
+    {
+        this.integerRF = integerRF;
+    }
+
+    /**
+     * @return the intRF
+     */
+    public int getIntRF()
+    {
+        return intRF;
+    }
+    
+    /**
+     * @param intRF the intRF to set
+     */
+    public void setIntRF(int intRF)
+    {
+        this.intRF = intRF;
+    }
+    
+    /**
+     * @return the stringRF
+     */
+    public String getStringRF()
+    {
+        return stringRF;
+    }
+
+    /**
+     * @param stringRF the stringRF to set
+     */
+    public void setStringRF(String stringRF)
+    {
+        this.stringRF = stringRF;
+    }
+}



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