You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2010/05/17 02:30:25 UTC

svn commit: r944946 - in /myfaces/extensions/validator/trunk/test-modules: base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/

Author: gpetracek
Date: Mon May 17 00:30:25 2010
New Revision: 944946

URL: http://svn.apache.org/viewvc?rev=944946&view=rev
Log:
EXTVAL-90 thx to Rudy De Busscher

Added:
    myfaces/extensions/validator/trunk/test-modules/core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/SupportForMapPropertyTest.java
Modified:
    myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ExtValMockValueExpression.java

Modified: myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ExtValMockValueExpression.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ExtValMockValueExpression.java?rev=944946&r1=944945&r2=944946&view=diff
==============================================================================
--- myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ExtValMockValueExpression.java (original)
+++ myfaces/extensions/validator/trunk/test-modules/base-test-infrastructure/src/test/java/org/apache/myfaces/extensions/validator/test/base/mock/ExtValMockValueExpression.java Mon May 17 00:30:25 2010
@@ -20,12 +20,17 @@ package org.apache.myfaces.extensions.va
 
 import org.apache.myfaces.extensions.validator.core.el.ExtValELResolver;
 import org.apache.shale.test.el.MockValueExpression;
+import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
 
 import javax.el.ELContext;
 import javax.el.ELResolver;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Gerhard Petracek
@@ -103,6 +108,11 @@ public class ExtValMockValueExpression e
         }
         else
         {
+            if (base instanceof Map)
+            {
+                return determineMapValueType(context, base);
+            }
+
             String getter = "get" + createPropertyString();
 
             Method getterMethod;
@@ -128,6 +138,59 @@ public class ExtValMockValueExpression e
         }
     }
 
+    private Class determineMapValueType(ELContext context, Object base)
+    {
+        Object parent = getParentOfBase(context, context.getELResolver());
+        if (parent == null)
+        {
+            return fallbackMapValueTypeDetermination(base);
+        }
+        else
+        {
+            try
+            {
+                Field field = parent.getClass().getDeclaredField(elements[elements.length - 2]);
+                Type type = field.getGenericType();
+                if (type instanceof ParameterizedTypeImpl)
+                {
+                    Type[] types = ((ParameterizedTypeImpl) type).getActualTypeArguments();
+                    return types[0].getClass();
+                }
+                return fallbackMapValueTypeDetermination(base);
+            }
+            catch (SecurityException e)
+            {
+                return fallbackMapValueTypeDetermination(base);
+            }
+            catch (NoSuchFieldException e)
+            {
+                return fallbackMapValueTypeDetermination(base);
+            }
+
+        }
+    }
+
+    private Class fallbackMapValueTypeDetermination(Object base)
+    {
+        // We can only determine the type by looking at the first value. 
+        Iterator iter = ((Map) base).values().iterator();
+        if (!iter.hasNext())
+        {
+            throw new IllegalStateException();
+        }
+        return iter.next().getClass();
+    }
+
+    public Object getParentOfBase(ELContext context, ELResolver resolver)
+    {
+        Object base = null;
+        for (int i = 0; i < elements.length - 2; i++)
+        {
+            base = resolver.getValue(context, base, elements[i]);
+        }
+        return base;
+    }
+
     private void parse()
     {
 
@@ -211,4 +274,4 @@ public class ExtValMockValueExpression e
         String property = elements[elements.length - 1];
         return property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
     }
-}
\ No newline at end of file
+}

Added: myfaces/extensions/validator/trunk/test-modules/core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/SupportForMapPropertyTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/trunk/test-modules/core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/SupportForMapPropertyTest.java?rev=944946&view=auto
==============================================================================
--- myfaces/extensions/validator/trunk/test-modules/core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/SupportForMapPropertyTest.java (added)
+++ myfaces/extensions/validator/trunk/test-modules/core-tests/src/test/java/org/apache/myfaces/extensions/validator/test/SupportForMapPropertyTest.java Mon May 17 00:30:25 2010
@@ -0,0 +1,111 @@
+/*
+ * 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.extensions.validator.test;
+
+import org.apache.myfaces.extensions.validator.test.base.AbstractExValTestCase;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.faces.component.UIViewRoot;
+import javax.faces.component.html.HtmlForm;
+import javax.faces.component.html.HtmlInputText;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+//just a test for the mock impl.
+public class SupportForMapPropertyTest extends AbstractExValTestCase
+{
+    HtmlInputText inputComponent1 = null;
+
+    UIViewRoot rootComponent = null;
+
+    public SupportForMapPropertyTest(String name)
+    {
+        super(name);
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite(SupportForMapPropertyTest.class);
+    }
+
+    @Override
+    protected void invokeStartupListeners()
+    {
+        //do nothing
+    }
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        TestBean bean = new TestBean();
+        createValueBinding(null, "value", "#{testBean}");
+        facesContext.getExternalContext().getRequestMap().put("testBean", bean);
+
+        rootComponent = new UIViewRoot();
+        HtmlForm form = new HtmlForm();
+        form.setId("form");
+        rootComponent.getChildren().add(form);
+        inputComponent1 = new HtmlInputText();
+        form.getChildren().add(inputComponent1);
+        inputComponent1.setId("input1");
+
+    }
+
+    @Override
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    public void testMapPropertyFromScreen()
+    {
+        createValueBinding(inputComponent1, "value", "#{testBean.mapProperty['Key']}");
+
+        //decode
+        inputComponent1.setSubmittedValue("value1");
+
+        //validate
+        inputComponent1.validate(facesContext);
+    }
+
+    public class TestBean
+    {
+        private Map<String, String> mapProperty;
+
+        public TestBean()
+        {
+            mapProperty = new HashMap<String, String>();
+            mapProperty.put("model", "ModelValue");
+        }
+
+        public void setMapProperty(Map<String, String> mapProperty)
+        {
+            this.mapProperty = mapProperty;
+        }
+
+        public Map<String, String> getMapProperty()
+        {
+            return mapProperty;
+        }
+    }
+}