org.apache.shale.test.el.MockVariableValueExpression Maven / Gradle / Ivy
/*
* 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.shale.test.el;
import javax.el.ELContext;
import javax.el.PropertyNotWritableException;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
/**
* Mock implementation of ValueExpression
that wraps a variable.
*/
public class MockVariableValueExpression extends ValueExpression {
// ------------------------------------------------------------ Constructors
/**
* Serial version UID.
*/
private static final long serialVersionUID = 4475919948345298291L;
/**
* Construct a new expression for the specified instance.
*
* @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
/**
* The expected result type for getValue()
calls.
*/
private Class expectedType = null;
/**
* The variable instance being wrapped by this expression.
*/
private Object instance = null;
// ------------------------------------------------------ Expression Methods
/**
* Return true
if this expression is equal to the
* specified expression.
*
* @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;
}
}
/**
* Return the original String used to create this expression,
* unmodified.
*/
public String getExpressionString() {
return this.instance.toString();
}
/**
* Return the hash code for this expression.
*/
public int hashCode() {
return this.instance.toString().hashCode();
}
/**
* Return true
if the expression string for this expression
* contains only literal text.
*/
public boolean isLiteralText() {
return true;
}
// ------------------------------------------------- ValueExpression Methods
/**
* Return the type that the result of this expression will
* be coerced to.
*/
public Class getExpectedType() {
return this.expectedType;
}
/**
* Evaluate this expression relative to the specified context,
* and return the most general type that is acceptable for the
* value passed in a setValue()
call.
*
* @param context ELContext for this evaluation
*/
public Class getType(ELContext context) {
if (context == null) {
throw new NullPointerException();
}
return this.instance.getClass();
}
/**
* Evaluate this expression relative to the specified context,
* and return the result.
*
* @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);
}
/**
* Evaluate this expression relative to the specified context,
* and return true
if a call to setValue()
* will always fail.
*
* @param context ELContext for this evaluation
*/
public boolean isReadOnly(ELContext context) {
if (context == null) {
throw new NullPointerException();
}
return true;
}
/**
* Evaluate this expression relative to the specified context,
* and set the result to the specified value.
*
* @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();
}
}