You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2020/02/28 15:32:34 UTC

[myfaces] branch 3.0.x updated: fix tests

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch 3.0.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/3.0.x by this push:
     new 88e3ab8  fix tests
88e3ab8 is described below

commit 88e3ab8f031a2c1756284b2b68f19980569f8698
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Fri Feb 28 16:32:26 2020 +0100

    fix tests
---
 impl-test/pom.xml                                  |   6 -
 impl/pom.xml                                       |   6 -
 .../apache/myfaces/test/mock/MockApplication.java  |  40 ++-
 .../myfaces/test/mock/MockMethodBinding.java       | 278 ++++++++++++++
 .../apache/myfaces/test/mock/MockValueBinding.java | 399 +++++++++++++++++++++
 5 files changed, 709 insertions(+), 20 deletions(-)

diff --git a/impl-test/pom.xml b/impl-test/pom.xml
index 5bb9432..16749af 100644
--- a/impl-test/pom.xml
+++ b/impl-test/pom.xml
@@ -197,12 +197,6 @@
         </dependency>
         <dependency>
             <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-jsf</artifactId>
-            <version>${openwebbeans.version}</version>
-            <optional>true</optional>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
             <artifactId>openwebbeans-web</artifactId>
             <version>${openwebbeans.version}</version>
             <optional>true</optional>
diff --git a/impl/pom.xml b/impl/pom.xml
index 256c7a4..736a1af 100644
--- a/impl/pom.xml
+++ b/impl/pom.xml
@@ -1315,12 +1315,6 @@
         </dependency>
         <dependency>
             <groupId>org.apache.openwebbeans</groupId>
-            <artifactId>openwebbeans-jsf</artifactId>
-            <version>${openwebbeans.version}</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.openwebbeans</groupId>
             <artifactId>openwebbeans-web</artifactId>
             <version>${openwebbeans.version}</version>
             <scope>test</scope>
diff --git a/test/src/main/java/org/apache/myfaces/test/mock/MockApplication.java b/test/src/main/java/org/apache/myfaces/test/mock/MockApplication.java
index be10ec8..2e44d3e 100644
--- a/test/src/main/java/org/apache/myfaces/test/mock/MockApplication.java
+++ b/test/src/main/java/org/apache/myfaces/test/mock/MockApplication.java
@@ -38,6 +38,14 @@ import jakarta.faces.el.VariableResolver;
  */
 public class MockApplication extends MockApplication22
 {
+    private VariableResolver variableResolver;
+    private PropertyResolver propertyResolver;
+    
+    public MockApplication()
+    {
+        setVariableResolver(new MockVariableResolver());
+        setPropertyResolver(new MockPropertyResolver());
+    }
 
     @Override
     public UIComponent createComponent(ValueBinding componentBinding, FacesContext context, String componentType)
@@ -49,36 +57,52 @@ public class MockApplication extends MockApplication22
     @Override
     public MethodBinding createMethodBinding(String ref, Class<?>[] params) throws ReferenceSyntaxException
     {
-        return null;
+        if (ref == null)
+        {
+            throw new NullPointerException();
+        }
+        else
+        {
+            return (new MockMethodBinding(this, ref, params));
+        }
     }
 
     @Override
     public ValueBinding createValueBinding(String ref) throws ReferenceSyntaxException
     {
-        return null;
+        if (ref == null)
+        {
+            throw new NullPointerException();
+        }
+        else
+        {
+            return (new MockValueBinding(this, ref));
+        }
     }
 
     @Override
     public PropertyResolver getPropertyResolver()
     {
-        return null;
+        return this.propertyResolver;
     }
 
     @Override
     public VariableResolver getVariableResolver()
     {
-        return null;
+
+        return this.variableResolver;
     }
 
     @Override
-    public void setPropertyResolver(PropertyResolver resolver)
+    public void setVariableResolver(VariableResolver variableResolver)
     {
-
+        this.variableResolver = variableResolver;
     }
 
     @Override
-    public void setVariableResolver(VariableResolver resolver)
+    public void setPropertyResolver(PropertyResolver resolver)
     {
-
+        this.propertyResolver = resolver;
     }
+
 }
diff --git a/test/src/main/java/org/apache/myfaces/test/mock/MockMethodBinding.java b/test/src/main/java/org/apache/myfaces/test/mock/MockMethodBinding.java
new file mode 100644
index 0000000..81edcc9
--- /dev/null
+++ b/test/src/main/java/org/apache/myfaces/test/mock/MockMethodBinding.java
@@ -0,0 +1,278 @@
+/*
+ * 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.test.mock;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import jakarta.faces.application.Application;
+import jakarta.faces.component.StateHolder;
+import jakarta.faces.component.UIComponentBase;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.el.EvaluationException;
+import jakarta.faces.el.MethodBinding;
+import jakarta.faces.el.MethodNotFoundException;
+import jakarta.faces.el.ReferenceSyntaxException;
+import jakarta.faces.el.ValueBinding;
+
+/**
+ * <p>Mock implementation of <code>MethodBinding</code>.</p>
+ *
+ * <p>This implementation is subject to the following restrictions:</p>
+ * <ul>
+ * <li>The portion of the method reference expression before the final
+ *     "." must conform to the limitations of {@link MockValueBinding}.</li>
+ * <li>The name of the method to be executed cannot be delimited by "[]".</li>
+ * </ul>
+ * 
+ * @since 1.0.0
+ */
+
+public class MockMethodBinding extends MethodBinding implements StateHolder
+{
+
+    // ------------------------------------------------------------ Constructors
+
+    /**
+     * <p>Construct a default instance.</p>
+     */
+    public MockMethodBinding()
+    {
+    }
+
+    /**
+     * <p>Construct a configured instance.</p>
+     *
+     * @param application Application instance for this application
+     * @param ref Method binding expression to be parsed
+     * @param args Signature of this method
+     */
+    public MockMethodBinding(Application application, String ref, Class[] args)
+    {
+
+        this.application = application;
+        this.args = args;
+        if (ref.startsWith("#{") && ref.endsWith("}"))
+        {
+            ref = ref.substring(2, ref.length() - 1);
+        }
+        this.ref = ref;
+        int period = ref.lastIndexOf(".");
+        if (period < 0)
+        {
+            throw new ReferenceSyntaxException(ref);
+        }
+        vb = application.createValueBinding(ref.substring(0, period));
+        name = ref.substring(period + 1);
+        if (name.length() < 1)
+        {
+            throw new ReferenceSyntaxException(ref);
+        }
+
+    }
+
+    // ------------------------------------------------------ Instance Variables
+
+    private Application application;
+    private Class args[];
+    private String name;
+    private String ref;
+    private ValueBinding vb;
+
+    // --------------------------------------------------- MethodBinding Methods
+
+    /** {@inheritDoc} */
+    public Object invoke(FacesContext context, Object[] params)
+            throws EvaluationException, MethodNotFoundException
+    {
+
+        if (context == null)
+        {
+            throw new NullPointerException();
+        }
+        Object base = vb.getValue(context);
+        if (base == null)
+        {
+            throw new EvaluationException(
+                    "Cannot find object via expression \""
+                            + vb.getExpressionString() + "\"");
+        }
+        Method method = method(base);
+        try
+        {
+            return (method.invoke(base, params));
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new EvaluationException(e);
+        }
+        catch (InvocationTargetException e)
+        {
+            throw new EvaluationException(e.getTargetException());
+        }
+
+    }
+
+    /** {@inheritDoc} */
+    public Class getType(FacesContext context)
+    {
+
+        Object base = vb.getValue(context);
+        Method method = method(base);
+        Class returnType = method.getReturnType();
+        if ("void".equals(returnType.getName()))
+        {
+            return (null);
+        }
+        else
+        {
+            return (returnType);
+        }
+
+    }
+
+    /** {@inheritDoc} */
+    public String getExpressionString()
+    {
+        return "#{" + ref + "}";
+    }
+
+    // ----------------------------------------------------- StateHolder Methods
+
+    /** {@inheritDoc} */
+    public Object saveState(FacesContext context)
+    {
+        Object values[] = new Object[4];
+        values[0] = name;
+        values[1] = ref;
+        values[2] = UIComponentBase.saveAttachedState(context, vb);
+        values[3] = args;
+        return (values);
+    }
+
+    /** {@inheritDoc} */
+    public void restoreState(FacesContext context, Object state)
+    {
+        Object values[] = (Object[]) state;
+        name = (String) values[0];
+        ref = (String) values[1];
+        vb = (ValueBinding) UIComponentBase.restoreAttachedState(context,
+                values[2]);
+        args = (Class[]) values[3];
+    }
+
+    /**
+     * <p>Flag indicating this is a transient instance.</p>
+     */
+    private boolean transientFlag = false;
+
+    /** {@inheritDoc} */
+    public boolean isTransient()
+    {
+        return (this.transientFlag);
+    }
+
+    /** {@inheritDoc} */
+    public void setTransient(boolean transientFlag)
+    {
+        this.transientFlag = transientFlag;
+    }
+
+    /** {@inheritDoc} */
+    public int hashCode()
+    {
+        if (ref == null)
+        {
+            return 0;
+        }
+        else
+        {
+            return ref.hashCode();
+        }
+    }
+
+    /** {@inheritDoc} */
+    public boolean equals(Object otherObj)
+    {
+        MockMethodBinding other = null;
+
+        if (!(otherObj instanceof MockMethodBinding))
+        {
+            return false;
+        }
+        other = (MockMethodBinding) otherObj;
+        // test object reference equality
+        if (this.ref != other.ref)
+        {
+            // test object equality
+            if (null != this.ref && null != other.ref)
+            {
+                if (!this.ref.equals(other.ref))
+                {
+                    return false;
+                }
+            }
+            return false;
+        }
+        // no need to test name, since it flows from ref.
+        // test our args array
+        if (this.args != other.args)
+        {
+            if (this.args.length != other.args.length)
+            {
+                return false;
+            }
+            for (int i = 0, len = this.args.length; i < len; i++)
+            {
+                if (this.args[i] != other.args[i])
+                {
+                    if (!this.ref.equals(other.ref))
+                    {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    // --------------------------------------------------------- Private Methods
+
+    /**
+     * <p>Return the <code>Method</code> to be called.</p>
+     *
+     * @param base Base object from which to extract the method reference
+     */
+    Method method(Object base)
+    {
+
+        Class clazz = base.getClass();
+        try
+        {
+            return (clazz.getMethod(name, args));
+        }
+        catch (NoSuchMethodException e)
+        {
+            throw new MethodNotFoundException(ref + ": " + e.getMessage());
+        }
+
+    }
+
+}
diff --git a/test/src/main/java/org/apache/myfaces/test/mock/MockValueBinding.java b/test/src/main/java/org/apache/myfaces/test/mock/MockValueBinding.java
new file mode 100644
index 0000000..16489dc
--- /dev/null
+++ b/test/src/main/java/org/apache/myfaces/test/mock/MockValueBinding.java
@@ -0,0 +1,399 @@
+/*
+ * 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.test.mock;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import jakarta.faces.application.Application;
+import jakarta.faces.component.StateHolder;
+import jakarta.faces.context.ExternalContext;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.el.EvaluationException;
+import jakarta.faces.el.PropertyNotFoundException;
+import jakarta.faces.el.PropertyResolver;
+import jakarta.faces.el.ReferenceSyntaxException;
+import jakarta.faces.el.ValueBinding;
+import jakarta.faces.el.VariableResolver;
+
+/**
+ * <p>Mock implementation of <code>ValueBinding</code>.</p>
+ * 
+ * @since 1.0.0
+ */
+public class MockValueBinding extends ValueBinding implements StateHolder
+{
+
+    // ------------------------------------------------------------ Constructors
+
+    /**
+     * <p>Construct a default instance.</p>
+     */
+    public MockValueBinding()
+    {
+
+        this(null, null);
+
+    }
+
+    /**
+     * <p>Construct a new value binding for the specified expression.</p>
+     *
+     * @param application Application instance to be wrapped
+     * @param ref Expression to be wrapped
+     */
+    public MockValueBinding(Application application, String ref)
+    {
+
+        this.application = application;
+        if (ref != null)
+        {
+            if (ref.startsWith("#{") && ref.endsWith("}"))
+            {
+                ref = ref.substring(2, ref.length() - 1);
+            }
+        }
+        this.ref = ref;
+
+    }
+
+    // ----------------------------------------------------- Mock Object Methods
+
+    /**
+     * <p>Return the expression string for this value binding.</p>
+     */
+    public String ref()
+    {
+
+        return this.ref;
+
+    }
+
+    // ------------------------------------------------------ Instance Variables
+
+    /**
+     * <p>The <code>Application</code> instance for this application.</p>
+     */
+    private transient Application application; // Restored as necessary
+
+    /**
+     * <p>The expression this value binding will evaluate.</p>
+     */
+    private String ref;
+
+    // ---------------------------------------------------- ValueBinding Methods
+
+    /** {@inheritDoc} */
+    public Object getValue(FacesContext context) throws EvaluationException,
+            PropertyNotFoundException
+    {
+
+        if (context == null)
+        {
+            throw new NullPointerException();
+        }
+        List names = parse(ref);
+
+        // Resolve the variable name
+        VariableResolver vr = application().getVariableResolver();
+        String name = (String) names.get(0);
+        Object base = vr.resolveVariable(context, name);
+        if (names.size() < 2)
+        {
+            return (base);
+        }
+
+        // Resolve the property names
+        PropertyResolver pr = application().getPropertyResolver();
+        for (int i = 1; i < names.size(); i++)
+        {
+            base = pr.getValue(base, (String) names.get(i));
+        }
+
+        // Return the resolved value
+        return (base);
+
+    }
+
+    /** {@inheritDoc} */
+    public void setValue(FacesContext context, Object value)
+            throws EvaluationException, PropertyNotFoundException
+    {
+
+        if (context == null)
+        {
+            throw new NullPointerException();
+        }
+        List names = parse(ref);
+
+        // Resolve the variable name
+        VariableResolver vr = application().getVariableResolver();
+        String name = (String) names.get(0);
+        Object base = vr.resolveVariable(context, name);
+        if (names.size() < 2)
+        {
+            if ("applicationScope".equals(name) || "requestScope".equals(name)
+                    || "sessionScope".equals(name))
+            {
+                throw new ReferenceSyntaxException("Cannot set '" + name + "'");
+            }
+            Map map = econtext().getRequestMap();
+            if (map.containsKey(name))
+            {
+                map.put(name, value);
+                return;
+            }
+            map = econtext().getSessionMap();
+            if ((map != null) && (map.containsKey(name)))
+            {
+                map.put(name, value);
+                return;
+            }
+            map = econtext().getApplicationMap();
+            if (map.containsKey(name))
+            {
+                map.put(name, value);
+                return;
+            }
+            econtext().getRequestMap().put(name, value);
+            return;
+        }
+
+        // Resolve the property names
+        PropertyResolver pr = application().getPropertyResolver();
+        for (int i = 1; i < (names.size() - 1); i++)
+        {
+            // System.out.println("  property=" + names.get(i));
+            base = pr.getValue(base, (String) names.get(i));
+        }
+
+        // Update the last property
+        pr.setValue(base, (String) names.get(names.size() - 1), value);
+
+    }
+
+    /** {@inheritDoc} */
+    public boolean isReadOnly(FacesContext context)
+            throws PropertyNotFoundException
+    {
+
+        if (context == null)
+        {
+            throw new NullPointerException();
+        }
+        List names = parse(ref);
+
+        // Resolve the variable name
+        VariableResolver vr = application().getVariableResolver();
+        String name = (String) names.get(0);
+        Object base = vr.resolveVariable(context, name);
+        if (names.size() < 2)
+        {
+            return true;
+        }
+
+        // Resolve the property names
+        PropertyResolver pr = application().getPropertyResolver();
+        for (int i = 1; i < names.size() - 1; i++)
+        {
+            base = pr.getValue(base, (String) names.get(i));
+        }
+
+        // Return the read only state of the final property
+        return pr.isReadOnly(base, (String) names.get(names.size() - 1));
+
+    }
+
+    /** {@inheritDoc} */
+    public Class getType(FacesContext context) throws PropertyNotFoundException
+    {
+
+        if (context == null)
+        {
+            throw new NullPointerException();
+        }
+        List names = parse(ref);
+
+        // Resolve the variable name
+        VariableResolver vr = application().getVariableResolver();
+        String name = (String) names.get(0);
+        Object base = vr.resolveVariable(context, name);
+        if (names.size() < 2)
+        {
+            return base.getClass();
+        }
+
+        // Resolve the property names
+        PropertyResolver pr = application().getPropertyResolver();
+        for (int i = 1; i < names.size() - 1; i++)
+        {
+            base = pr.getValue(base, (String) names.get(i));
+        }
+
+        // Return the type of the final property
+        return pr.getType(base, (String) names.get(names.size() - 1));
+
+    }
+
+    /** {@inheritDoc} */
+    public String getExpressionString()
+    {
+
+        return "#{" + ref + "}";
+
+    }
+
+    // ----------------------------------------------------- StateHolder Methods
+
+    /** {@inheritDoc} */
+    public Object saveState(FacesContext context)
+    {
+
+        Object[] values = new Object[1];
+        values[0] = ref;
+        return values;
+
+    }
+
+    /** {@inheritDoc} */
+    public void restoreState(FacesContext context, Object state)
+    {
+
+        Object[] values = (Object[]) state;
+        ref = (String) values[0];
+
+    }
+
+    /**
+     * <p>Flag indicating that this value is transient.</p>
+     */
+    private boolean transientFlag = false;
+
+    /** {@inheritDoc} */
+    public boolean isTransient()
+    {
+
+        return this.transientFlag;
+
+    }
+
+    /** {@inheritDoc} */
+    public void setTransient(boolean transientFlag)
+    {
+
+        this.transientFlag = transientFlag;
+
+    }
+
+    // --------------------------------------------------------- Private Methods
+
+    /**
+     * <p>Return the relevant <code>Application</code> instance.</p>
+     */
+    private Application application()
+    {
+
+        if (application == null)
+        {
+            application = FacesContext.getCurrentInstance().getApplication();
+        }
+        return (application);
+
+    }
+
+    /**
+     * <p>Return the relevant <code>ExternalContext</code> instance.</p>
+     */
+    private ExternalContext econtext()
+    {
+
+        return (FacesContext.getCurrentInstance().getExternalContext());
+
+    }
+
+    /**
+     * <p>Return a list of the expression elements in this expression.</p>
+     *
+     * @param ref Expression to be parsed
+     */
+    private List parse(String ref)
+    {
+
+        List names = new ArrayList();
+        StringBuffer expr = new StringBuffer(ref);
+        boolean isBlockOn = false;
+        for (int i = expr.length() - 1; i > -1; i--)
+        {
+            if (expr.charAt(i) == ' ')
+            {
+                expr.deleteCharAt(i);
+            }
+            else if (expr.charAt(i) == ']')
+            {
+                expr.deleteCharAt(i);
+            }
+            else if (expr.charAt(i) == '[')
+            {
+                expr.deleteCharAt(i);
+            }
+            else if (expr.charAt(i) == '\'')
+            {
+                if (!isBlockOn)
+                {
+                    expr.deleteCharAt(i);
+                }
+                else
+                {
+                    names.add(0, expr.substring(i + 1));
+                    expr.delete(i, expr.length());
+                }
+                isBlockOn = !isBlockOn;
+            }
+            else if (expr.charAt(i) == '.' && !isBlockOn)
+            {
+                names.add(0, expr.substring(i + 1));
+                expr.delete(i, expr.length());
+            }
+        }
+
+        if (expr.length() > 0)
+        {
+            names.add(0, expr.toString());
+        }
+
+        if (names.size() < 1)
+        {
+            throw new ReferenceSyntaxException("No expression in '" + ref + "'");
+        }
+        for (int i = 0; i < names.size(); i++)
+        {
+            String name = (String) names.get(i);
+            if (name.length() < 1)
+            {
+                throw new ReferenceSyntaxException("Invalid expression '" + ref
+                        + "'");
+            }
+        }
+        return (names);
+
+    }
+
+}