myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r1296394 - in /myfaces/core/trunk/api/src: main/java/javax/faces/component/UISelectOne.java test/java/javax/faces/component/UISelectOneTest.java
Date Fri, 02 Mar 2012 19:13:48 GMT
Author: lu4242
Date: Fri Mar  2 19:13:47 2012
New Revision: 1296394

URL: http://svn.apache.org/viewvc?rev=1296394&view=rev
Log:
MYFACES-3448 UISelectOne.validateValue(...) optimised & refactored (Thanks to David Law
for provide this patch)

Added:
    myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java   (with
props)
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UISelectOne.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UISelectOne.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UISelectOne.java?rev=1296394&r1=1296393&r2=1296394&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UISelectOne.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UISelectOne.java Fri Mar  2
19:13:47 2012
@@ -74,15 +74,19 @@ public class UISelectOne extends UIInput
         // selected value must match to one of the available options
         // and if required is true it must not match an option with noSelectionOption set
to true (since 2.0)
         Converter converter = getConverter();
-        if (!(_SelectItemsUtil.matchValue(context, this, value, new _SelectItemsIterator(this,
context), converter)
-              && (!this.isRequired() 
-                  || (this.isRequired() 
-                      && !_SelectItemsUtil.isNoSelectionOption(context, this, value,
-                                                      new _SelectItemsIterator(this, context),
converter)))))
+
+        if (_SelectItemsUtil.matchValue(context, this, value, new _SelectItemsIterator(this,
context), converter))
         {
-            _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, new Object[]
{ _MessageUtils.getLabel(
-                context, this) });
-            setValid(false);
+            if (! this.isRequired())
+            {
+                return; // Matched & Required false, so return ok.
+            }
+            if (! _SelectItemsUtil.isNoSelectionOption(context, this, value, new _SelectItemsIterator(this,
context), converter))
+            {
+                return; // Matched & Required true & No-selection did NOT match,
so return ok.
+            }
         }
+        _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, new Object[] {_MessageUtils.getLabel(context,
this) });
+        setValid(false);
     }
 }

Added: myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java?rev=1296394&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java (added)
+++ myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java Fri Mar
 2 19:13:47 2012
@@ -0,0 +1,185 @@
+/*
+ * 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 javax.faces.component;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class UISelectOneTest extends AbstractJsfTestCase
+{
+    
+    @Test
+    public void testValidateRequiredNull()
+    {
+
+        facesContext.getViewRoot().setLocale(_TEST_LOCALE);
+
+        UISelectOne selectOne = new UISelectOne();
+        selectOne.setId("selectOne");
+        selectOne.setRendererType(null);
+        selectOne.setRequired(true);
+        List<UIComponent> children = selectOne.getChildren();
+
+        UISelectItem one = new UISelectItem();
+        one.setItemValue(new Integer(1));
+        children.add(one);
+
+        UISelectItem two = new UISelectItem();
+        two.setItemValue(new Integer(2));
+        children.add(two);
+
+        UISelectItem three = new UISelectItem();
+        three.setItemValue(new Integer(3));
+        children.add(three);
+
+        selectOne.validateValue(facesContext, null);
+
+        Assert.assertFalse(selectOne.isValid());
+        Assert.assertEquals(1, facesContext.getMessageList().size());
+    }
+    
+    @Test
+    public void testValidateNotRequiredValid()
+    {
+
+        facesContext.getViewRoot().setLocale(_TEST_LOCALE);
+
+        UISelectOne selectOne = new UISelectOne();
+        selectOne.setId("selectOne");
+        selectOne.setRendererType(null);
+        selectOne.setRequired(false);
+        List<UIComponent> children = selectOne.getChildren();
+
+        UISelectItem one = new UISelectItem();
+        one.setItemValue(new Integer(1));
+        children.add(one);
+
+        UISelectItem two = new UISelectItem();
+        two.setItemValue(new Integer(2));
+        children.add(two);
+
+        UISelectItem three = new UISelectItem();
+        three.setItemValue(new Integer(3));
+        children.add(three);
+
+        selectOne.validateValue(facesContext, 1);
+
+        Assert.assertTrue(selectOne.isValid());
+        Assert.assertEquals(0, facesContext.getMessageList().size());
+    }
+    
+    @Test
+    public void testValidateNotRequiredNotValid()
+    {
+
+        facesContext.getViewRoot().setLocale(_TEST_LOCALE);
+
+        UISelectOne selectOne = new UISelectOne();
+        selectOne.setId("selectOne");
+        selectOne.setRendererType(null);
+        selectOne.setRequired(false);
+        List<UIComponent> children = selectOne.getChildren();
+
+        UISelectItem one = new UISelectItem();
+        one.setItemValue(new Integer(1));
+        children.add(one);
+
+        UISelectItem two = new UISelectItem();
+        two.setItemValue(new Integer(2));
+        children.add(two);
+
+        UISelectItem three = new UISelectItem();
+        three.setItemValue(new Integer(3));
+        children.add(three);
+
+        selectOne.validateValue(facesContext, 4);
+
+        Assert.assertFalse(selectOne.isValid());
+        Assert.assertEquals(1, facesContext.getMessageList().size());
+    }
+    
+    @Test
+    public void testValidateRequiredNotSelectOption()
+    {
+
+        facesContext.getViewRoot().setLocale(_TEST_LOCALE);
+
+        UISelectOne selectOne = new UISelectOne();
+        selectOne.setId("selectOne");
+        selectOne.setRendererType(null);
+        selectOne.setRequired(true);
+        List<UIComponent> children = selectOne.getChildren();
+
+        UISelectItem one = new UISelectItem();
+        one.setItemValue(new Integer(1));
+        children.add(one);
+
+        UISelectItem two = new UISelectItem();
+        two.setItemValue(new Integer(2));
+        children.add(two);
+
+        UISelectItem three = new UISelectItem();
+        three.setItemValue(new Integer(3));
+        three.setNoSelectionOption(true);
+        children.add(three);
+
+        selectOne.validateValue(facesContext, 3);
+
+        Assert.assertFalse(selectOne.isValid());
+        Assert.assertEquals(1, facesContext.getMessageList().size());
+    }
+    
+    @Test
+    public void testValidateNotRequiredNotSelectOption()
+    {
+
+        facesContext.getViewRoot().setLocale(_TEST_LOCALE);
+
+        UISelectOne selectOne = new UISelectOne();
+        selectOne.setId("selectOne");
+        selectOne.setRendererType(null);
+        selectOne.setRequired(false);
+        List<UIComponent> children = selectOne.getChildren();
+
+        UISelectItem one = new UISelectItem();
+        one.setItemValue(new Integer(1));
+        children.add(one);
+
+        UISelectItem two = new UISelectItem();
+        two.setItemValue(new Integer(2));
+        children.add(two);
+
+        UISelectItem three = new UISelectItem();
+        three.setItemValue(new Integer(3));
+        three.setNoSelectionOption(true);
+        children.add(three);
+
+        selectOne.validateValue(facesContext, 3);
+
+        Assert.assertTrue(selectOne.isValid());
+        Assert.assertEquals(0, facesContext.getMessageList().size());
+    }
+    
+    static private final Locale _TEST_LOCALE = new Locale("xx", "TEST");
+
+}

Propchange: myfaces/core/trunk/api/src/test/java/javax/faces/component/UISelectOneTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message