Author: craigmcc
Date: Sat Oct 7 22:41:38 2006
New Revision: 454081
URL: http://svn.apache.org/viewvc?view=rev&rev=454081
Log:
More infrastructure for test-time expression evaluation. Still need unit
tests to prove that this stuff actually works, but this should be all the
classes we need for basic support of JSF 1.2.
SHALE-304
Added:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
(with props)
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
(with props)
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
(with props)
Modified:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockExpressionFactory.java
Modified:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockExpressionFactory.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockExpressionFactory.java?view=diff&rev=454081&r1=454080&r2=454081
==============================================================================
---
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockExpressionFactory.java
(original)
+++
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockExpressionFactory.java
Sat Oct 7 22:41:38 2006
@@ -16,6 +16,8 @@
package org.apache.shale.test.el;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
@@ -37,6 +39,15 @@
}
+ // ------------------------------------------------------ Instance
Variables
+
+
+ /**
+ * <p>Literal numeric value for zero.</p>
+ */
+ private static final Integer ZERO = new Integer(0);
+
+
// ----------------------------------------------------- Mock Object
Methods
@@ -46,7 +57,75 @@
/** [EMAIL PROTECTED] */
public Object coerceToType(Object object, Class targetType) {
- throw new UnsupportedOperationException(); // FIXME - coerceToType()
+
+ // Check for no conversion necessary
+ if ((targetType == null) || Object.class.equals(targetType)) {
+ return object;
+ }
+
+ // Coerce to String if appropriate
+ if (String.class.equals(targetType)) {
+ if (object == null) {
+ return "";
+ } else if (object instanceof String) {
+ return (String) object;
+ } else {
+ return object.toString();
+ }
+ }
+
+ // Coerce to Number (or a subclass of Number) if appropriate
+ if (isNumeric(targetType)) {
+ if (object == null) {
+ return coerce(ZERO, targetType);
+ } else if ("".equals(object)) {
+ return coerce(ZERO, targetType);
+ } else if (object instanceof String) {
+ return coerce((String) object, targetType);
+ } else if (isNumeric(object.getClass())) {
+ return coerce((Number) object, targetType);
+ }
+ throw new IllegalArgumentException("Cannot convert " + object + "
to Number");
+ }
+
+ // Coerce to Boolean if appropriate
+ if (Boolean.class.equals(targetType) || (Boolean.TYPE == targetType)) {
+ if (object == null) {
+ return Boolean.FALSE;
+ } else if ("".equals(object)) {
+ return Boolean.FALSE;
+ } else if ((object instanceof Boolean) || (object.getClass() ==
Boolean.TYPE)) {
+ return (Boolean) object;
+ } else if (object instanceof String) {
+ return Boolean.valueOf((String) object);
+ }
+ throw new IllegalArgumentException("Cannot convert " + object + "
to Boolean");
+ }
+
+ // Coerce to Character if appropriate
+ if (Character.class.equals(targetType) || (Character.TYPE ==
targetType)) {
+ if (object == null) {
+ return new Character((char) 0);
+ } else if ("".equals(object)) {
+ return new Character((char) 0);
+ } else if (object instanceof String) {
+ return new Character(((String) object).charAt(0));
+ } else if (isNumeric(object.getClass())) {
+ return new Character((char) ((Number) object).shortValue());
+ } else if ((object instanceof Character) || (object.getClass() ==
Character.TYPE)) {
+ return (Character) object;
+ }
+ throw new IllegalArgumentException("Cannot convert " + object + "
to Character");
+ }
+
+ // Is the specified value type-compatible already?
+ if ((object != null) &&
targetType.isAssignableFrom(object.getClass())) {
+ return object;
+ }
+
+ // We do not know how to perform this conversion
+ throw new IllegalArgumentException("Cannot convert " + object + " to "
+ targetType.getName());
+
}
@@ -55,7 +134,9 @@
String expression,
Class expectedType,
Class[] signature) {
- throw new UnsupportedOperationException(); // FIXME -
createMethodExpression()
+
+ return new MockMethodExpression(expression, signature, expectedType);
+
}
@@ -63,14 +144,112 @@
public ValueExpression createValueExpression(ELContext context,
String expression,
Class expectedType) {
- throw new UnsupportedOperationException(); // FIXME -
createValueExpression()
+
+ return new MockValueExpression(expression, expectedType);
+
}
/** [EMAIL PROTECTED] */
public ValueExpression createValueExpression(Object instance,
Class expectedType) {
- throw new UnsupportedOperationException(); // FIXME -
createValueExpression()
+
+ return new MockVariableValueExpression(instance, expectedType);
+
+ }
+
+
+ // --------------------------------------------------------- Private
Methods
+
+
+ /**
+ * <p>Coerce the specified value to the specified Number subclass.</p>
+ *
+ * @param value Value to be coerced
+ * @param type Destination type
+ */
+ private Number coerce(Number value, Class type) {
+
+ if ((type == Byte.TYPE) || (type == Byte.class)) {
+ return new Byte(value.byteValue());
+ } else if ((type == Double.TYPE) || (type == Double.class)) {
+ return new Double(value.doubleValue());
+ } else if ((type == Float.TYPE) || (type == Float.class)) {
+ return new Float(value.floatValue());
+ } else if ((type == Integer.TYPE) || (type == Integer.class)) {
+ return new Integer(value.intValue());
+ } else if ((type == Long.TYPE) || (type == Long.class)) {
+ return new Long(value.longValue());
+ } else if ((type == Short.TYPE) || (type == Short.class)) {
+ return new Short(value.shortValue());
+ } else if (type == BigDecimal.class) {
+ if (value instanceof BigDecimal) {
+ return (BigDecimal) value;
+ } else if (value instanceof BigInteger) {
+ return new BigDecimal((BigInteger) value);
+ } else {
+ return new BigDecimal(((Number) value).doubleValue());
+ }
+ } else if (type == BigInteger.class) {
+ if (value instanceof BigInteger) {
+ return (BigInteger) value;
+ } else if (value instanceof BigDecimal) {
+ return ((BigDecimal) value).toBigInteger();
+ } else {
+ return BigInteger.valueOf(((Number) value).longValue());
+ }
+ }
+ throw new IllegalArgumentException("Cannot convert " + value + " to "
+ type.getName());
+
+ }
+
+
+ /**
+ * <p>Coerce the specified value to the specified Number subclass.</p>
+ *
+ * @param value Value to be coerced
+ * @param type Destination type
+ */
+ private Number coerce(String value, Class type) {
+
+ if ((type == Byte.TYPE) || (type == Byte.class)) {
+ return Byte.valueOf(value);
+ } else if ((type == Double.TYPE) || (type == Double.class)) {
+ return Double.valueOf(value);
+ } else if ((type == Float.TYPE) || (type == Float.class)) {
+ return Float.valueOf(value);
+ } else if ((type == Integer.TYPE) || (type == Integer.class)) {
+ return Integer.valueOf(value);
+ } else if ((type == Long.TYPE) || (type == Long.class)) {
+ return Long.valueOf(value);
+ } else if ((type == Short.TYPE) || (type == Short.class)) {
+ return Short.valueOf(value);
+ } else if (type == BigDecimal.class) {
+ return new BigDecimal(value);
+ } else if (type == BigInteger.class) {
+ return new BigInteger(value);
+ }
+ throw new IllegalArgumentException("Cannot convert " + value + " to "
+ type.getName());
+
+ }
+
+
+ /**
+ * <p>Return <code>true</code> if the specified type is numeric.</p>
+ *
+ * @param type Type to check
+ */
+ private boolean isNumeric(Class type) {
+
+ return
+ (type == Byte.TYPE) || (type == Byte.class)
+ || (type == Double.TYPE) || (type == Double.class)
+ || (type == Float.TYPE) || (type == Float.class)
+ || (type == Integer.TYPE) || (type == Integer.class)
+ || (type == Long.TYPE) || (type == Long.class)
+ || (type == Short.TYPE) || (type == Short.class)
+ || (type == BigDecimal.class) || (type == BigInteger.class);
+
}
Added:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java?view=auto&rev=454081
==============================================================================
---
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
(added)
+++
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
Sat Oct 7 22:41:38 2006
@@ -0,0 +1,230 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.shale.test.el;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ELResolver;
+import javax.el.MethodExpression;
+import javax.el.MethodInfo;
+import javax.faces.context.FacesContext;
+
+/**
+ * <p>Mock implementation of <code>MethodExpression</code>.</p>
+ */
+public class MockMethodExpression extends MethodExpression {
+
+
+ // ------------------------------------------------------------
Constructors
+
+
+ /**
+ * <p>Construct a new expression for the specified expression string.</p>
+ *
+ * @param expression Expression string to be evaluated
+ * @param signature Parameter signature of the method to be called
+ * @param expectedType Expected type of the result
+ */
+ public MockMethodExpression(String expression, Class[] signature, Class
expectedType) {
+
+ if (expression == null) {
+ throw new NullPointerException("Expression string cannot be null");
+ }
+ this.expression = expression;
+ this.signature = signature;
+ this.expectedType = expectedType;
+ parse();
+
+ }
+
+
+ // ------------------------------------------------------ Instance
Variables
+
+
+ /**
+ * <p>The parsed elements of this expression.</p>
+ */
+ private String[] elements = null;
+
+
+ /**
+ * <p>The expected result type for <code>getValue()</code> calls.</p>
+ */
+ private Class expectedType = null;
+
+
+ /**
+ * <p>The original expression string used to create this expression.</p>
+ */
+ private String expression = null;
+
+
+ /**
+ * <p>The method signature of the method to be called.</p>
+ */
+ private Class[] signature = null;
+
+
+ // ------------------------------------------------------ Expression
Methods
+
+
+ /**
+ * <p>Return <code>true</code> if this expression is equal to the
+ * specified expression.</p>
+ *
+ * @param obj Object to be compared
+ */
+ public boolean equals(Object obj) {
+
+ if ((obj != null) & (obj instanceof MethodExpression)) {
+ return expression.equals(((MethodExpression)
obj).getExpressionString());
+ } else {
+ return false;
+ }
+
+ }
+
+
+ /**
+ * <p>Return the original String used to create this expression,
+ * unmodified.</p>
+ */
+ public String getExpressionString() {
+
+ return this.expression;
+
+ }
+
+
+ /**
+ * <p>Return the hash code for this expression.</p>
+ */
+ public int hashCode() {
+
+ return this.expression.hashCode();
+
+ }
+
+
+ /**
+ * <p>Return <code>true</code> if the expression string for this expression
+ * contains only literal text.</p>
+ */
+ public boolean isLiteralText() {
+
+ return (expression.indexOf("${") < 0) && (expression.indexOf("#{") <
0);
+
+ }
+
+
+ // ------------------------------------------------ MethodExpression
Methods
+
+
+ /**
+ * <p>Evaluate the expression relative to the specified context,
+ * and return information about the actual implementation method.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public MethodInfo getMethodInfo(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ return new MethodInfo(elements[elements.length - 1], expectedType,
signature);
+
+ }
+
+
+ /**
+ * <p>Evaluate the expression relative to the specified ocntext,
+ * and return the result after coercion to the expected result type.</p>
+ *
+ * @param context ELContext for this evaluation
+ * @param params Parameters for this method call
+ */
+ public Object invoke(ELContext context, Object[] params) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ if (isLiteralText()) {
+ return expression;
+ }
+
+ FacesContext fcontext = (FacesContext)
context.getContext(FacesContext.class);
+ ELResolver resolver = fcontext.getApplication().getELResolver();
+ Object base = null;
+ for (int i = 0; i < elements.length - 1; i++) {
+ base = resolver.getValue(context, base, elements[i]);
+ }
+
+ try {
+ Method method = base.getClass().getMethod(elements[elements.length
- 1], signature);
+ Object result = method.invoke(base, params);
+ return
fcontext.getApplication().getExpressionFactory().coerceToType(result,
expectedType);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new ELException(e);
+ }
+
+ }
+
+
+ // --------------------------------------------------------- Private
Methods
+
+
+ /**
+ * <p>Parse the expression string into its constituent elemetns.</p>
+ */
+ private void parse() {
+
+ if (isLiteralText()) {
+ elements = new String[0];
+ return;
+ }
+
+ if (expression.startsWith("${") || expression.startsWith("#{")) {
+ if (expression.endsWith("}")) {
+ String temp = expression.substring(2, expression.length() -
1).replaceAll(" ", "");
+ List names = new ArrayList();
+ while (temp.length() > 0) {
+ int period= temp.indexOf(".");
+ if (period >= 0) {
+ names.add(temp.substring(0, period));
+ temp = temp.substring(period + 1);
+ } else {
+ names.add(temp);
+ temp = "";
+ }
+ }
+ elements = (String[]) names.toArray(new String[names.size()]);
+ } else {
+ throw new IllegalArgumentException(expression);
+ }
+ } else {
+ throw new IllegalArgumentException(expression);
+ }
+
+ }
+
+
+}
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockMethodExpression.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java?view=auto&rev=454081
==============================================================================
---
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
(added)
+++
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
Sat Oct 7 22:41:38 2006
@@ -0,0 +1,284 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.shale.test.el;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.el.ELContext;
+import javax.el.ELResolver;
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+/**
+ * <p>Mock implementation of <code>ValueExpression</code>.</p>
+ *
+ * <p>This implementation supports a limited subset of overall expression
functionality:</p>
+ * <ul>
+ * <li>A literal string that contains no expression delimiters.</li>
+ * <li>An expression that starts with "#{" or "${", and ends with "}".</li>
+ * </ul>
+ */
+public class MockValueExpression extends ValueExpression {
+
+
+ // ------------------------------------------------------------
Constructors
+
+
+ /**
+ * <p>Construct a new expression for the specified expression string.</p>
+ *
+ * @param expression Expression string to be evaluated
+ * @param expectedType Expected type of the result
+ */
+ public MockValueExpression(String expression, Class expectedType) {
+
+ if (expression == null) {
+ throw new NullPointerException("Expression string cannot be null");
+ }
+ this.expression = expression;
+ this.expectedType = expectedType;
+ parse();
+
+ }
+
+
+ // ------------------------------------------------------ Instance
Variables
+
+
+ /**
+ * <p>The parsed elements of this expression.</p>
+ */
+ private String[] elements = null;
+
+
+ /**
+ * <p>The expected result type for <code>getValue()</code> calls.</p>
+ */
+ private Class expectedType = null;
+
+
+ /**
+ * <p>The original expression string used to create this expression.</p>
+ */
+ private String expression = null;
+
+
+ // ------------------------------------------------------ Expression
Methods
+
+
+ /**
+ * <p>Return <code>true</code> if this expression is equal to the
+ * specified expression.</p>
+ *
+ * @param obj Object to be compared
+ */
+ public boolean equals(Object obj) {
+
+ if ((obj != null) & (obj instanceof ValueExpression)) {
+ return expression.equals(((ValueExpression)
obj).getExpressionString());
+ } else {
+ return false;
+ }
+
+ }
+
+
+ /**
+ * <p>Return the original String used to create this expression,
+ * unmodified.</p>
+ */
+ public String getExpressionString() {
+
+ return this.expression;
+
+ }
+
+
+ /**
+ * <p>Return the hash code for this expression.</p>
+ */
+ public int hashCode() {
+
+ return this.expression.hashCode();
+
+ }
+
+
+ /**
+ * <p>Return <code>true</code> if the expression string for this expression
+ * contains only literal text.</p>
+ */
+ public boolean isLiteralText() {
+
+ return (expression.indexOf("${") < 0) && (expression.indexOf("#{") <
0);
+
+ }
+
+
+ // ------------------------------------------------- ValueExpression
Methods
+
+
+ /**
+ * <p>Return the type that the result of this expression will
+ * be coerced to.</p>
+ */
+ public Class getExpectedType() {
+
+ return this.expectedType;
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return the most general type that is acceptable for the
+ * value passed in a <code>setValue()</code> call.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public Class getType(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ Object value = getValue(context);
+ if (value == null) {
+ return null;
+ } else {
+ return value.getClass();
+ }
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return the result.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public Object getValue(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ if (isLiteralText()) {
+ return expression;
+ }
+
+ FacesContext fcontext = (FacesContext)
context.getContext(FacesContext.class);
+ ELResolver resolver = fcontext.getApplication().getELResolver();
+ Object base = null;
+ for (int i = 0; i < elements.length; i++) {
+ base = resolver.getValue(context, base, elements[i]);
+ }
+ return
fcontext.getApplication().getExpressionFactory().coerceToType(base,
getExpectedType());
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return <code>true</code> if a call to <code>setValue()</code>
+ * will always fail.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public boolean isReadOnly(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ if (isLiteralText()) {
+ return true;
+ }
+
+ FacesContext fcontext = (FacesContext)
context.getContext(FacesContext.class);
+ ELResolver resolver = fcontext.getApplication().getELResolver();
+ Object base = null;
+ for (int i = 0; i < elements.length - 1; i++) {
+ base = resolver.getValue(context, base, elements[i]);
+ }
+ return resolver.isReadOnly(context, base, elements[elements.length -
1]);
+
+ }
+
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and set the result to the specified value.</p>
+ *
+ * @param context ELContext for this evaluation
+ * @param value Value to which the result should be set
+ */
+ public void setValue(ELContext context, Object value) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+
+ FacesContext fcontext = (FacesContext)
context.getContext(FacesContext.class);
+ ELResolver resolver = fcontext.getApplication().getELResolver();
+ Object base = null;
+ for (int i = 0; i < elements.length - 1; i++) {
+ base = resolver.getValue(context, base, elements[i]);
+ }
+ resolver.setValue(context, base, elements[elements.length - 1], value);
+
+ }
+
+
+ // --------------------------------------------------------- Private
Methods
+
+
+ /**
+ * <p>Parse the expression string into its constituent elemetns.</p>
+ */
+ private void parse() {
+
+ if (isLiteralText()) {
+ elements = new String[0];
+ return;
+ }
+
+ if (expression.startsWith("${") || expression.startsWith("#{")) {
+ if (expression.endsWith("}")) {
+ String temp = expression.substring(2, expression.length() -
1).replaceAll(" ", "");
+ List names = new ArrayList();
+ while (temp.length() > 0) {
+ int period= temp.indexOf(".");
+ if (period >= 0) {
+ names.add(temp.substring(0, period));
+ temp = temp.substring(period + 1);
+ } else {
+ names.add(temp);
+ temp = "";
+ }
+ }
+ elements = (String[]) names.toArray(new String[names.size()]);
+ } else {
+ throw new IllegalArgumentException(expression);
+ }
+ } else {
+ throw new IllegalArgumentException(expression);
+ }
+
+ }
+
+
+}
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockValueExpression.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java?view=auto&rev=454081
==============================================================================
---
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
(added)
+++
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
Sat Oct 7 22:41:38 2006
@@ -0,0 +1,201 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.shale.test.el;
+
+import javax.el.ELContext;
+import javax.el.PropertyNotWritableException;
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+/**
+ * <p>Mock implementation of <code>ValueExpression</code> that wraps a
variable.</p>
+ */
+public class MockVariableValueExpression extends ValueExpression {
+
+
+ // ------------------------------------------------------------
Constructors
+
+
+ /**
+ * <p>Construct a new expression for the specified instance.</p>
+ *
+ * @param instance Variable instance to be wrapped
+ * @param expectedType Expected type of the result
+ */
+ public MockVariableValueExpression(Object instance, Class expectedType) {
+
+ if (instance == null) {
+ throw new NullPointerException("Instance cannot be null");
+ }
+ this.instance = instance;
+ this.expectedType = expectedType;
+
+ }
+
+
+ // ------------------------------------------------------ Instance
Variables
+
+
+ /**
+ * <p>The expected result type for <code>getValue()</code> calls.</p>
+ */
+ private Class expectedType = null;
+
+
+ /**
+ * <p>The variable instance being wrapped by this expression.</p>
+ */
+ private Object instance = null;
+
+
+ // ------------------------------------------------------ Expression
Methods
+
+
+ /**
+ * <p>Return <code>true</code> if this expression is equal to the
+ * specified expression.</p>
+ *
+ * @param obj Object to be compared
+ */
+ public boolean equals(Object obj) {
+
+ if ((obj != null) & (obj instanceof ValueExpression)) {
+ return instance.toString().equals(((ValueExpression)
obj).getExpressionString());
+ } else {
+ return false;
+ }
+
+ }
+
+
+ /**
+ * <p>Return the original String used to create this expression,
+ * unmodified.</p>
+ */
+ public String getExpressionString() {
+
+ return this.instance.toString();
+
+ }
+
+
+ /**
+ * <p>Return the hash code for this expression.</p>
+ */
+ public int hashCode() {
+
+ return this.instance.toString().hashCode();
+
+ }
+
+
+ /**
+ * <p>Return <code>true</code> if the expression string for this expression
+ * contains only literal text.</p>
+ */
+ public boolean isLiteralText() {
+
+ return false;
+
+ }
+
+
+ // ------------------------------------------------- ValueExpression
Methods
+
+
+ /**
+ * <p>Return the type that the result of this expression will
+ * be coerced to.</p>
+ */
+ public Class getExpectedType() {
+
+ return this.expectedType;
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return the most general type that is acceptable for the
+ * value passed in a <code>setValue()</code> call.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public Class getType(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ return this.instance.getClass();
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return the result.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public Object getValue(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ FacesContext fcontext = (FacesContext)
context.getContext(FacesContext.class);
+ return
fcontext.getApplication().getExpressionFactory().coerceToType(instance,
expectedType);
+
+ }
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and return <code>true</code> if a call to <code>setValue()</code>
+ * will always fail.</p>
+ *
+ * @param context ELContext for this evaluation
+ */
+ public boolean isReadOnly(ELContext context) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+ return true;
+
+ }
+
+
+
+ /**
+ * <p>Evaluate this expression relative to the specified context,
+ * and set the result to the specified value.</p>
+ *
+ * @param context ELContext for this evaluation
+ * @param value Value to which the result should be set
+ */
+ public void setValue(ELContext context, Object value) {
+
+ if (context == null) {
+ throw new NullPointerException();
+ }
+
+ throw new PropertyNotWritableException();
+
+ }
+
+
+}
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL