Author: musachy
Date: Sat Nov 7 23:14:32 2009
New Revision: 833776
URL: http://svn.apache.org/viewvc?rev=833776&view=rev
Log:
rename classes, add more tests
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELReflectionContextFactory.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELServletContextListener.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStack.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStackFactory.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUELTest.java
- copied, changed from r833650,
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUelBaseTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ChildTestAction.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELMethodInvocationTest.java
- copied, changed from r833650,
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackReadValueTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackSetValueTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELValueStackOtherTests.java
Removed:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUelBaseTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ParametersTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/XWorkBeanELResolver.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/resources/struts-plugin.xml
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELReflectionContextFactory.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELReflectionContextFactory.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELReflectionContextFactory.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELReflectionContextFactory.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,15 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.util.reflection.ReflectionContextFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * ReflectionContextFactory for Unified EL.
+ */
+public class UELReflectionContextFactory implements ReflectionContextFactory {
+ public Map createDefaultContext(Object root) {
+ return new HashMap();
+ }
+}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELServletContextListener.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELServletContextListener.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELServletContextListener.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELServletContextListener.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,33 @@
+package org.apache.struts2.uelplugin;
+
+import de.odysseus.el.ExpressionFactoryImpl;
+import de.odysseus.el.tree.TreeBuilder;
+
+import javax.el.ExpressionFactory;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import java.util.Properties;
+
+import org.apache.struts2.uelplugin.ExpressionFactoryHolder;
+
+/**
+ * Responsible for creating the ExpressionFactory that will be used by the
+ * UelValueStack
+ */
+public class UELServletContextListener implements ServletContextListener {
+ public void contextInitialized(ServletContextEvent contextEvent) {
+ Properties juelProperties = new Properties();
+ juelProperties.setProperty("javax.el.methodInvocations", "true");
+
+ //custom parser
+ juelProperties.setProperty(TreeBuilder.class.getName(),
JUELExtensionBuilder.class.getName());
+
+ ExpressionFactory factory = new ExpressionFactoryImpl(juelProperties);
+
+ ExpressionFactoryHolder.setExpressionFactory(factory);
+ }
+
+ public void contextDestroyed(ServletContextEvent contextEvent) {
+ }
+}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStack.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStack.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStack.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStack.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,229 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.XWorkException;
+import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.util.ClearableValueStack;
+import com.opensymphony.xwork2.util.CompoundRoot;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.uelplugin.elresolvers.AccessorsContextKey;
+
+import javax.el.ELContext;
+import javax.el.ExpressionFactory;
+import javax.el.PropertyNotFoundException;
+import javax.el.ValueExpression;
+import java.util.Map;
+import java.util.TreeMap;
+import java.io.Serializable;
+
+/**
+ * A ValueStack that uses Unified EL as the underlying Expression Language.
+ */
+public class UELValueStack implements ValueStack, ClearableValueStack,
Serializable {
+ private static final Logger LOG =
LoggerFactory.getLogger(UELValueStack.class);
+
+ private CompoundRoot root = new CompoundRoot();
+ private transient Map context;
+ private Class defaultType;
+ private Map overrides;
+
+ private ELContext elContext;
+ private Container container;
+ private XWorkConverter xworkConverter;
+
+ private boolean logMissingProperties;
+ private boolean devMode;
+
+ public UELValueStack(Container container) {
+ this(container, new CompoundRoot());
+ }
+
+ public UELValueStack(Container container, ValueStack vs) {
+ this(container, new CompoundRoot(vs.getRoot()));
+ }
+
+ public UELValueStack(Container container, CompoundRoot root) {
+ this.container = container;
+ this.xworkConverter = container.getInstance(XWorkConverter.class);
+ setRoot(new CompoundRoot(root));
+ }
+
+ @Inject("devMode")
+ public void setDevMode(String mode) {
+ devMode = "true".equalsIgnoreCase(mode);
+ }
+
+ @Inject(value = "logMissingProperties", required = false)
+ public void setLogMissingProperties(String logMissingProperties) {
+ this.logMissingProperties =
"true".equalsIgnoreCase(logMissingProperties);
+ }
+
+ public String findString(String expr, boolean throwException) {
+ return (String) findValue(expr, String.class);
+ }
+
+ public String findString(String expr) {
+ return findString(expr, false);
+ }
+
+ public Object findValue(String expr) {
+ return findValue(expr, Object.class, false);
+ }
+
+ public Object findValue(String expr, boolean throwException) {
+ return findValue(expr, Object.class, throwException);
+ }
+
+ public Object findValue(String expr, Class asType) {
+ return findValue(expr, asType, false);
+ }
+
+ public Object findValue(String expr, Class asType, boolean throwException)
{
+ String originalExpression = expr;
+ try {
+ if ((overrides != null) && overrides.containsKey(expr)) {
+ expr = (String) overrides.get(expr);
+ }
+ if (expr != null && expr.startsWith("%{")) {
+ // replace %{ with ${
+ expr = "#" + expr.substring(1);
+ }
+ if (expr != null && !expr.startsWith("${") &&
!expr.startsWith("#{")) {
+ expr = "#{" + expr + "}";
+ }
+
+ elContext.putContext(AccessorsContextKey.class, context);
+ elContext.putContext(XWorkConverter.class, xworkConverter);
+ elContext.putContext(CompoundRoot.class, root);
+
+ // parse our expression
+ ExpressionFactory factory = getExpressionFactory();
+ ValueExpression valueExpr =
factory.createValueExpression(elContext, expr, Object.class);
+ Object retVal = valueExpr.getValue(elContext);
+ if (!Object.class.equals(asType)) {
+ retVal = xworkConverter.convertValue(context, root, null,
null, retVal, asType);
+ }
+ return retVal;
+ } catch (Exception e) {
+ return handleException(e, originalExpression, throwException);
+ }
+ }
+
+ private Object handleException(Exception exception, String expression,
boolean throwException) {
+ Object ret = context.get(expression);
+
+ if (ret != null)
+ return ret;
+ else {
+ if (exception instanceof PropertyNotFoundException && devMode &&
logMissingProperties)
+ LOG.warn(exception.getMessage());
+
+ if (throwException)
+ throw new XWorkException(exception);
+ else
+ return null;
+ }
+
+ }
+
+ protected ExpressionFactory getExpressionFactory() {
+ ExpressionFactory factory =
ExpressionFactoryHolder.getExpressionFactory();
+ if (factory == null) {
+ String message = "********** FATAL ERROR STARTING UP STRUTS-UEL
INTEGRATION **********\n" +
+ "Looks like the UEL listener was not configured for your
web app! \n" +
+ "Nothing will work until UELServletContextListener is
added as a listener in web.xml.\n" +
+ "You might need to add the following to web.xml: \n" +
+ " <listener>\n" +
+ "
<listener-class>org.apache.struts2.uelplugin.UELServletContextListener</listener-class>\n"
+
+ " </listener>";
+ LOG.fatal(message);
+ throw new IllegalStateException("Unable to find ExpressionFactory
instance. Make sure that 'UELServletContextListener' " +
+ "is configured in web.xml as a listener");
+ } else
+ return factory;
+ }
+
+ public Map getContext() {
+ return context;
+ }
+
+ public Map getExprOverrides() {
+ return overrides;
+ }
+
+ public CompoundRoot getRoot() {
+ return root;
+ }
+
+ public Object peek() {
+ return root.peek();
+ }
+
+ public Object pop() {
+ return root.pop();
+ }
+
+ public void push(Object o) {
+ root.push(o);
+ }
+
+ public void setDefaultType(Class defaultType) {
+ this.defaultType = defaultType;
+ }
+
+ public void setExprOverrides(Map overrides) {
+ if (this.overrides == null) {
+ this.overrides = overrides;
+ } else {
+ this.overrides.putAll(overrides);
+ }
+ }
+
+ public void set(String key, Object o) {
+ overrides.put(key, o);
+ }
+
+ public void setValue(String expr, Object value) {
+ setValue(expr, value, false);
+ }
+
+ public void setValue(String expr, Object value, boolean
throwExceptionOnFailure) {
+ try {
+ if (expr != null && !expr.startsWith("${") &&
!expr.startsWith("#{")) {
+ expr = "#{" + expr + "}";
+ }
+ elContext.putContext(AccessorsContextKey.class, context);
+ elContext.putContext(XWorkConverter.class, xworkConverter);
+ elContext.putContext(CompoundRoot.class, root);
+
+ // parse our expression
+ ExpressionFactory factory = getExpressionFactory();
+ ValueExpression valueExpr =
factory.createValueExpression(elContext, expr, Object.class);
+ valueExpr.setValue(elContext, value);
+ } catch (Exception e) {
+ if (e instanceof PropertyNotFoundException && devMode &&
logMissingProperties)
+ LOG.warn("Could not find property [" +
((PropertyNotFoundException) e).getMessage() + "]");
+
+ if (throwExceptionOnFailure)
+ throw new XWorkException(e);
+ }
+ }
+
+ public int size() {
+ return root.size();
+ }
+
+ protected void setRoot(CompoundRoot root) {
+ this.context = new TreeMap();
+ context.put(VALUE_STACK, this);
+ this.root = root;
+ elContext = new CompoundRootELContext(container);
+ }
+
+ public void clearContextValues() {
+ getContext().clear();
+ }
+}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStackFactory.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStackFactory.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStackFactory.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UELValueStackFactory.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,45 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.ValueStackFactory;
+
+import javax.el.ExpressionFactory;
+
+/**
+ * Creates UelValueStacks.
+ */
+public class UELValueStackFactory implements ValueStackFactory {
+ private ExpressionFactory factory;
+
+ private XWorkConverter xworkConverter;
+
+ private Container container;
+
+ @Inject
+ public void setXWorkConverter(XWorkConverter conv) {
+ this.xworkConverter = conv;
+ }
+
+ @Inject
+ public void setContainer(Container container) throws
ClassNotFoundException {
+ this.container = container;
+ }
+
+
+ public ValueStack createValueStack() {
+ ValueStack results = new UELValueStack(container);
+ results.getContext().put(ActionContext.CONTAINER, container);
+ return results;
+ }
+
+ public ValueStack createValueStack(ValueStack stack) {
+ ValueStack results = new UELValueStack(container, stack);
+ container.inject(results);
+ results.getContext().put(ActionContext.CONTAINER, container);
+ return results;
+ }
+}
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
Sat Nov 7 23:14:32 2009
@@ -1,7 +1,6 @@
package org.apache.struts2.uelplugin.elresolvers;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
-import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
import com.opensymphony.xwork2.inject.Container;
@@ -10,14 +9,7 @@
import org.apache.commons.lang.xwork.StringUtils;
import javax.el.ELContext;
-import javax.el.ELResolver;
-import java.beans.BeanInfo;
-import java.beans.FeatureDescriptor;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.Iterator;
import java.util.Map;
/**
@@ -31,8 +23,8 @@
}
@Override
- public Object getValue(ELContext context, Object base, Object property) {
- if (context == null) {
+ public Object getValue(ELContext elContext, Object base, Object property) {
+ if (elContext == null) {
throw new IllegalArgumentException("ElContext cannot be null");
}
@@ -46,16 +38,17 @@
return null;
}
- CompoundRoot root = (CompoundRoot)
context.getContext(CompoundRoot.class);
+ CompoundRoot root = (CompoundRoot)
elContext.getContext(CompoundRoot.class);
if (root == null) {
return null;
}
if ("top".equals(propertyName) && root.size() > 0) {
+ elContext.setPropertyResolved(true);
return root.get(0);
}
- Map<String, Object> reflectionContext = (Map)
context.getContext(AccessorsContextKey.class);
+ Map<String, Object> reflectionContext = (Map)
elContext.getContext(AccessorsContextKey.class);
Object bean = findObjectForProperty(root, propertyName);
if (bean != null) {
@@ -70,10 +63,10 @@
reflectionProvider.setValue(propertyName, reflectionContext,
bean, retVal);
}
- context.setPropertyResolved(true);
+ elContext.setPropertyResolved(true);
return retVal;
}
-
+
return null;
}
@@ -93,15 +86,20 @@
}
CompoundRoot root = (CompoundRoot)
context.getContext(CompoundRoot.class);
+ Map<String, Object> reflectionContext = (Map)
context.getContext(AccessorsContextKey.class);
String propertyName = (String) property;
try {
if (base == null && property != null && root != null) {
Object bean = findObjectForProperty(root, propertyName);
if (bean != null) {
+
reflectionContext.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, bean.getClass());
+
reflectionContext.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED, propertyName);
+
+
XWorkConverter converter = (XWorkConverter)
context.getContext(XWorkConverter.class);
if (converter != null && root != null) {
Class propType = determineType(bean, propertyName);
- value = converter.convertValue(null, value, propType);
+ value = converter.convertValue(reflectionContext,
bean, null, propertyName, value, propType);
}
BeanUtils.setProperty(bean, propertyName, value);
context.setPropertyResolved(true);
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/XWorkBeanELResolver.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/XWorkBeanELResolver.java?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/XWorkBeanELResolver.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/XWorkBeanELResolver.java
Sat Nov 7 23:14:32 2009
@@ -18,14 +18,15 @@
if (target != null && property != null) {
Map<String, Object> reflectionContext = (Map<String, Object>)
elContext.getContext(AccessorsContextKey.class);
String propertyName = property.toString();
+ Class targetType = target.getClass();
//only handle this if there is such a property
if (PropertyUtils.isReadable(target, propertyName)) {
try {
Object obj = reflectionProvider.getValue(propertyName,
reflectionContext, target);
-
reflectionContext.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED,
target.getClass());
-
reflectionContext.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED,
property.toString());
+
reflectionContext.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, targetType);
+
reflectionContext.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED, propertyName);
//if object is null, and create objects is enabled, lets
do it
if (obj == null &&
ReflectionContextState.isCreatingNullObjects(reflectionContext)) {
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/main/resources/struts-plugin.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/resources/struts-plugin.xml?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/main/resources/struts-plugin.xml
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/main/resources/struts-plugin.xml
Sat Nov 7 23:14:32 2009
@@ -6,11 +6,11 @@
<struts>
<bean type="com.opensymphony.xwork2.util.ValueStackFactory" name="uel"
- class="org.apache.struts2.uelplugin.UelValueStackFactory"/>
+ class="org.apache.struts2.uelplugin.UELValueStackFactory"/>
<bean type="com.opensymphony.xwork2.util.reflection.ReflectionProvider"
name="uel"
class="org.apache.struts2.uelplugin.reflection.GenericReflectionProvider"/>
<bean
type="com.opensymphony.xwork2.util.reflection.ReflectionContextFactory"
name="uel"
- class="org.apache.struts2.uelplugin.UelReflectionContextFactory"/>
+ class="org.apache.struts2.uelplugin.UELReflectionContextFactory"/>
<constant name="struts.valueStackFactory" value="uel"/>
<constant name="struts.reflectionProvider" value="uel"/>
Copied:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUELTest.java
(from r833650,
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUelBaseTest.java)
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUELTest.java?p2=struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUELTest.java&p1=struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUelBaseTest.java&r1=833650&r2=833776&rev=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUelBaseTest.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/AbstractUELTest.java
Sat Nov 7 23:14:32 2009
@@ -2,19 +2,12 @@
import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.inject.ContainerBuilder;
-import com.opensymphony.xwork2.inject.Factory;
-import com.opensymphony.xwork2.inject.Context;
-import com.opensymphony.xwork2.inject.Scope;
import com.opensymphony.xwork2.test.StubConfigurationProvider;
-import com.opensymphony.xwork2.config.ConfigurationManager;
-import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
-import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.location.LocatableProperties;
import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
@@ -22,7 +15,6 @@
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.uelplugin.reflection.GenericReflectionProvider;
import org.apache.struts2.util.StrutsTypeConverter;
-import org.apache.commons.beanutils.converters.DateConverter;
import javax.servlet.ServletContextEvent;
import javax.el.ExpressionFactory;
@@ -31,11 +23,11 @@
import java.text.DateFormat;
-public abstract class AbstractUelBaseTest extends XWorkTestCase {
+public abstract class AbstractUELTest extends XWorkTestCase {
private ExpressionFactory factory = ExpressionFactory.newInstance();
protected XWorkConverter converter;
protected CompoundRoot root;
- protected UelValueStack stack;
+ protected UELValueStack stack;
protected DateFormat format = DateFormat.getDateInstance();
protected ReflectionProvider reflectionProvider;
@@ -63,8 +55,8 @@
loadConfigurationProviders(new StubConfigurationProvider() {
public void register(ContainerBuilder builder, LocatableProperties
props) throws ConfigurationException {
- builder.factory(ValueStack.class, UelValueStack.class);
- builder.factory(ValueStackFactory.class,
UelValueStackFactory.class);
+ builder.factory(ValueStack.class, UELValueStack.class);
+ builder.factory(ValueStackFactory.class,
UELValueStackFactory.class);
builder.factory(ReflectionProvider.class,
GenericReflectionProvider.class);
//builder.factory(StrutsTypeConverter)
}
@@ -74,7 +66,7 @@
reflectionProvider = container.getInstance(ReflectionProvider.class);
converter.registerConverter("java.util.Date", new DateConverter());
this.root = new CompoundRoot();
- this.stack = new UelValueStack(container);
+ this.stack = new UELValueStack(container);
stack.setRoot(root);
stack.getContext().put(ActionContext.CONTAINER, container);
@@ -84,7 +76,7 @@
ServletActionContext.setServletContext(servletContext);
//simulate start up
- UelServletContextListener listener = new UelServletContextListener();
+ UELServletContextListener listener = new UELServletContextListener();
listener.contextInitialized(new ServletContextEvent(servletContext));
}
}
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java
Sat Nov 7 23:14:32 2009
@@ -1,21 +1,11 @@
package org.apache.struts2.uelplugin;
-import com.opensymphony.xwork2.XWorkTestCase;
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.util.CompoundRoot;
-import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
-
import java.lang.reflect.InvocationTargetException;
-import org.springframework.mock.web.MockServletContext;
-import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.util.ContextUtil;
-import org.apache.struts2.uelplugin.UelServletContextListener;
-
-import javax.servlet.ServletContextEvent;
-public class BuiltinFunctionsTest extends AbstractUelBaseTest {
+public class BuiltinFunctionsTest extends AbstractUELTest {
public void testGetText() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
TestAction action = new TestAction();
stack.push(action);
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ChildTestAction.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ChildTestAction.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ChildTestAction.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/ChildTestAction.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,4 @@
+package org.apache.struts2.uelplugin;
+
+public class ChildTestAction extends TestAction {
+}
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java
Sat Nov 7 23:14:32 2009
@@ -8,10 +8,18 @@
@Conversion
public class TestAction extends ActionSupport {
private TestObject object;
+ private int bar;
+
+ public int getBar() {
+ return bar;
+ }
+
+ public void setBar(int bar) {
+ this.bar = bar;
+ }
private String converted;
- @TypeConversion(type = ConversionType.APPLICATION, converter =
"org.apache.struts2.uelplugin.DummyTypeConverter")
public String getConverted() {
return converted;
}
Modified:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java?rev=833776&r1=833775&r2=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
Sat Nov 7 23:14:32 2009
@@ -18,9 +18,32 @@
private Map map;
private Map<Integer, TestObject> typedMap;
private Object[] objectArray;
- private Integer[] typedArray;
+ private int[] typedArray;
+ private TestObject[] typedArray2;
private Set set;
+ private ChildTestAction childTestAction;
+
+ public ChildTestAction getChildTestAction() {
+ return childTestAction;
+ }
+
+ public void setChildTestAction(ChildTestAction childTestAction) {
+ this.childTestAction = childTestAction;
+ }
+
+ public Object getFail() {
+ throw new RuntimeException("kaboom");
+ }
+
+ public TestObject[] getTypedArray2() {
+ return typedArray2;
+ }
+
+ public void setTypedArray2(TestObject[] typedArray2) {
+ this.typedArray2 = typedArray2;
+ }
+
public Set getSet() {
return set;
}
@@ -37,11 +60,11 @@
this.objectArray = objectArray;
}
- public Integer[] getTypedArray() {
+ public int[] getTypedArray() {
return typedArray;
}
- public void setTypedArray(Integer[] typedArray) {
+ public void setTypedArray(int[] typedArray) {
this.typedArray = typedArray;
}
Copied:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELMethodInvocationTest.java
(from r833650,
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java)
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELMethodInvocationTest.java?p2=struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELMethodInvocationTest.java&p1=struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java&r1=833650&r2=833776&rev=833776&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java
(original)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELMethodInvocationTest.java
Sat Nov 7 23:14:32 2009
@@ -1,19 +1,9 @@
package org.apache.struts2.uelplugin;
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.XWorkTestCase;
-import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
-import com.opensymphony.xwork2.util.CompoundRoot;
-import org.apache.struts2.ServletActionContext;
-import org.apache.struts2.uelplugin.UelServletContextListener;
-import org.springframework.mock.web.MockServletContext;
-
-import javax.servlet.ServletContextEvent;
import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-public class JuelMethodInvocationTest extends AbstractUelBaseTest {
+public class UELMethodInvocationTest extends AbstractUELTest {
public void testBasicMethods() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
assertEquals("text", stack.findValue("${' text '.trim()}"));
assertEquals(3, stack.findValue("${'123'.length()}"));
@@ -29,4 +19,14 @@
stack.getContext().put("s1", "Luthor");
assertEquals("Lex Luthor", stack.findValue("${#s0.concat('
').concat(#s1)}"));
}
+
+ public void testCallMethodsOnCompundRoot() {
+ //this shuld not fail as the property is defined on a parent class
+ TestObject obj = new TestObject();
+ root.push(obj);
+ ChildTestAction childTestAction = new ChildTestAction();
+ obj.setChildTestAction(childTestAction);
+
+ assertSame(childTestAction,
stack.findValue("top.getChildTestAction()", true));
+ }
}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackReadValueTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackReadValueTest.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackReadValueTest.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackReadValueTest.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,198 @@
+package org.apache.struts2.uelplugin;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.*;
+
+public class UELStackReadValueTest extends AbstractUELTest {
+
+ public void testTop() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ obj.setValue("0");
+ root.push(obj);
+
+ obj = new TestObject();
+ obj.setValue("1");
+ root.push(obj);
+
+ assertEquals("1", stack.findValue("top.value"));
+ }
+
+ public void testReadList() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ root.push(obj);
+
+ //list
+ List someList = new ArrayList(3);
+ obj.setObject(someList);
+ someList.add(10);
+ assertEquals(10, stack.findValue("object[0]"));
+ }
+
+ public void testReadArray() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ int[] ints = {10, 20};
+ obj.setTypedArray(ints);
+ root.push(obj);
+
+ //list
+ assertEquals(20, stack.findValue("typedArray[1]"));
+ }
+
+
+ public void testReadMap() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ HashMap map = new HashMap();
+ map.put("nameValue", "Lex");
+ TestObject obj = new TestObject();
+ obj.setParameters(map);
+ root.add(obj);
+
+ assertEquals("Lex", stack.findValue("parameters.nameValue"));
+ assertEquals("Lex", stack.findValue("parameters['nameValue']"));
+ }
+
+
+ public void testContextReferencesWithSameObjectInStack() throws
IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+ //if there as object in the stack with the property "X" and there is
an object in the
+ //stack context under the key "X" then:
+ //${X} : should return the value of X from object in stack
+ //${#X} : should return object from the stack context
+
+ TestObject obj = new TestObject();
+ obj.setValue("ref");
+ stack.push(obj);
+
+ TestObject obj2 = new TestObject();
+ stack.getContext().put("value", obj2);
+
+ //simple
+ assertEquals("ref", stack.findValue("value"));
+ assertSame(obj2, stack.findValue("#value"));
+
+ }
+
+ public void testExpressionSyntax() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ obj.setValue("val");
+ obj.setAge(1);
+ stack.getContext().put("obj", obj);
+
+ assertEquals("val", stack.findValue("${#obj.value}"));
+ assertEquals("val", stack.findValue("%{#obj.value}"));
+ assertEquals("val", stack.findValue("#{#obj.value}"));
+ }
+
+ public void testSuperNested() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj0 = new TestObject("0");
+ root.push(obj0);
+
+ TestObject obj1 = new TestObject("1");
+ obj0.setInner(obj1);
+
+ TestObject obj2 = new TestObject("2");
+ Map map = new HashMap();
+ map.put("key0", obj2);
+ obj1.setParameters(map);
+
+ TestObject obj3 = new TestObject("3");
+ List list = new ArrayList();
+ list.add(obj3);
+ obj2.setObject(obj3);
+
+ TestObject obj4 = new TestObject("4");
+ TestObject[] array = new TestObject[]{obj4};
+ obj3.setObject(array);
+
+ stack.getContext().put("obj", obj0);
+
+ assertEquals("4",
stack.findValue("${inner.parameters['key0'].object.object[0].value}"));
+ }
+
+ public void testContextReferences() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ obj.setValue("val");
+ obj.setAge(1);
+ stack.getContext().put("obj", obj);
+
+ //simple
+ assertSame(obj, stack.findValue("#obj"));
+ assertEquals("val", stack.findValue("#obj.value"));
+
+ //more expressions
+ TestObject obj2 = new TestObject();
+ obj2.setValue("val2");
+ obj2.setAge(2);
+ stack.getContext().put("obj2", obj2);
+
+ //addition
+ assertSame(3L, stack.findValue("#obj.age + #obj2.age"));
+
+ //string addition
+ assertEquals("valval2", stack.findValue("#obj.value + #obj2.value"));
+ assertEquals("1val2", stack.findValue("#obj.age + #obj2.value"));
+
+ //map
+ Map someMap = new HashMap();
+ obj.setInner(obj2);
+ someMap.put("val", obj);
+ stack.getContext().put("map", someMap);
+ assertEquals("val", stack.findValue("#map[#obj.value].value"));
+
+ //list
+ List someList = new ArrayList(3);
+ obj.setAge(0);
+ someList.add(obj);
+ stack.getContext().put("list", someList);
+ assertEquals("val", stack.findValue("#list[#obj.age].value"));
+ }
+
+ public void testBasicFind() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ root.add(obj);
+ stack.setValue("${value}", "Hello World");
+ String value = stack.findString("${value}");
+ assertEquals("Hello World", value);
+
+ stack.setValue("${age}", "56");
+ Integer age = (Integer) stack.findValue("${age}");
+ assertEquals(56, (int) age);
+ }
+
+ public void testNestedFind() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ TestObject obj2 = new TestObject();
+ obj2.setAge(100);
+ obj.setInner(obj2);
+ root.add(obj);
+
+ assertSame(obj2, stack.findValue("${inner}"));
+ assertEquals(100, stack.findValue("${inner.age}"));
+ }
+
+ public void testDeferredFind() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ root.add(obj);
+
+ stack.setValue("#{value}", "Hello World");
+ String value = stack.findString("#{value}");
+ assertEquals("Hello World", value);
+
+ stack.setValue("#{age}", "56");
+ String age = stack.findString("#{age}");
+ assertEquals("56", age);
+
+ stack.setValue("#{date}", new Date());
+ assertEquals(stack.findString("#{date}"),
format.format(obj.getDate()));
+ }
+
+ public void testNotFound() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ root.add(obj);
+
+ stack.setValue("${value}", "Hello World");
+ String value = stack.findString("${VALUENOTHERE}");
+ assertNull(value);
+
+ value = stack.findString("VALUENOTHERE");
+ assertNull(value);
+ }
+}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackSetValueTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackSetValueTest.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackSetValueTest.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELStackSetValueTest.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,159 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
+
+import java.util.*;
+import java.lang.reflect.InvocationTargetException;
+
+
+public class UELStackSetValueTest extends AbstractUELTest {
+ public void testSuperNested() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+
+
stack.setValue("inner.typedMap[10].inner.typedList[2].typedMap[1].value",
"whoa");
+ assertEquals("whoa",
obj.getInner().getTypedMap().get(10).getInner().getTypedList().get(2).getTypedMap().get(1).getValue());
+ }
+
+
+ public void testWriteList() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ //not null
+ List list = new ArrayList();
+ TestObject obj = new TestObject();
+ obj.setList(list);
+ assertNotNull(obj.getList());
+ root.push(obj);
+
+ stack.setValue("list[0]", "val");
+ assertEquals(1, list.size());
+ assertEquals("val", list.get(0));
+
+ //null list
+ obj.setList(null);
+ assertNull(obj.getList());
+ stack.setValue("list[0]", "val");
+ assertNotNull(obj.getList());
+ assertEquals("val", stack.findValue("list[0]"));
+
+ //test out of index
+ obj.setList(null);
+ stack.setValue("list[3]", "val");
+ assertEquals(4, obj.getList().size());
+ assertEquals("val", obj.getList().get(3));
+
+ //test type determiner
+ obj.setTypedList(null);
+ stack.setValue("typedList[1].value", "val");
+ assertEquals(2, obj.getTypedList().size());
+ assertEquals("val", obj.getTypedList().get(1).getValue());
+ }
+
+ public void testWriteArray() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ //not null
+ TestObject[] array = new TestObject[2];
+ TestObject obj = new TestObject();
+ obj.setTypedArray2(array);
+ assertNotNull(obj.getTypedArray2());
+ root.push(obj);
+
+ stack.setValue("typedArray2[0].value", "val");
+ assertNotNull(array[0]);
+ assertEquals("val", ((TestObject) array[0]).getValue());
+ }
+
+ public void testWriteMap() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ //not null
+ Map map = new HashMap();
+ TestObject obj = new TestObject();
+ obj.setMap(map);
+ assertNotNull(obj.getMap());
+ root.push(obj);
+
+ stack.setValue("map['str']", "val");
+ assertEquals(1, map.size());
+ assertEquals("val", map.get("str"));
+
+ //null list
+ obj.setMap(null);
+ assertNull(obj.getMap());
+ stack.setValue("map['str']", "val");
+ assertNotNull(obj.getMap());
+ assertEquals("val", stack.findValue("map['str']"));
+
+ //test type determiner
+ obj.setTypedMap(null);
+ stack.setValue("typedMap[1].value", "val");
+ assertEquals(1, obj.getTypedMap().size());
+ assertEquals("val", obj.getTypedMap().get(1).getValue());
+ }
+
+ public void testSetPropertiesOnNestedNullObject() {
+ TestObject obj = new TestObject();
+ assertNull(obj.getInner());
+ root.push(obj);
+
+ //inner is null, it will be catched bye the CompoundRoolELResolver
+ stack.setValue("inner.value", "val");
+ assertNotNull(obj.getInner());
+ assertEquals("val", obj.getInner().getValue());
+
+
+ //second nested property null
+ stack.setValue("inner.inner.value", "val");
+ assertNotNull(obj.getInner().getInner());
+ assertEquals("val", obj.getInner().getInner().getValue());
+ }
+
+ public void testSetStringArray() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ root.add(obj);
+
+ stack.setValue("${value}", new String[]{"Hello World"});
+ String value = stack.findString("${value}");
+ assertEquals("Hello World", value);
+
+ stack.setValue("${age}", new String[]{"67"});
+ assertEquals(new Integer(67), stack.findValue("${age}"));
+ }
+
+ public void test2LevelSet() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ TestObject nestedObj = new TestObject();
+ obj.setInner(nestedObj);
+ root.add(obj);
+
+ stack.setValue("${inner.age}", "66");
+ assertEquals(66, obj.getInner().getAge());
+ }
+
+ public void testTypeConversion() throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
+ TestObject obj = new TestObject();
+ TestObject inner = new TestObject();
+ obj.setInner(inner);
+ root.add(obj);
+
+ stack.setValue("${age}", "22");
+ assertEquals(stack.findValue("${age}"), obj.getAge());
+
+ stack.setValue("${inner.value}", "George");
+ assertEquals(stack.findValue("${inner.value}"),
obj.getInner().getValue());
+
+ stack.setValue("${inner.age}", "44");
+ assertEquals(stack.findValue("${inner.age}"), obj.getInner().getAge());
+
+ stack.setValue("${date}", new Date());
+ assertEquals(stack.findString("${date}"),
format.format(obj.getDate()));
+ }
+
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ Map context = stack.getContext();
+
+ ReflectionContextState.setCreatingNullObjects(context, true);
+ ReflectionContextState.setDenyMethodExecution(context, true);
+ ReflectionContextState.setReportingConversionErrors(context, true);
+ }
+}
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELValueStackOtherTests.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELValueStackOtherTests.java?rev=833776&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELValueStackOtherTests.java
(added)
+++
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UELValueStackOtherTests.java
Sat Nov 7 23:14:32 2009
@@ -0,0 +1,159 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import com.opensymphony.xwork2.ActionContext;
+
+import java.util.Map;
+import java.util.LinkedHashMap;
+
+
+public class UELValueStackOtherTests extends AbstractUELTest {
+
+ public void testExpOverridesCanStackExpUp() throws Exception {
+ Map expr1 = new LinkedHashMap();
+ expr1.put("expr1", "'expr1value'");
+
+ stack.setExprOverrides(expr1);
+
+ assertEquals(stack.findValue("expr1"), "expr1value");
+
+ Map expr2 = new LinkedHashMap();
+ expr2.put("expr2", "'expr2value'");
+ expr2.put("expr3", "'expr3value'");
+ stack.setExprOverrides(expr2);
+
+ assertEquals(stack.findValue("expr2"), "expr2value");
+ assertEquals(stack.findValue("expr3"), "expr3value");
+ }
+
+ public void testArrayAsString() {
+ TestObject obj = new TestObject();
+ obj.setTypedArray(new int[]{1, 2});
+ root.push(obj);
+
+ assertEquals("1, 2", stack.findValue("typedArray", String.class));
+ }
+
+ public void testFailsOnExceptionWithThrowException() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ try {
+ stack.findValue("fail", true);
+ fail("Failed to throw exception on EL error");
+ } catch (Exception ex) {
+ //ok
+ }
+ }
+
+ public void testFailsOnMissingPropertyWithThrowException() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ try {
+ stack.findValue("someprop12", true);
+ fail("Failed to throw exception on EL error");
+ } catch (Exception ex) {
+ //ok
+ }
+ }
+
+ public void testFailsOnMissingNestedPropertyWithThrowException() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ try {
+ stack.findValue("top.someprop12", true);
+ fail("Failed to throw exception on EL error");
+ } catch (Exception ex) {
+ //ok
+ }
+ }
+
+ public void testFailsOnMissingMethodWithThrowException() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ try {
+ stack.findValue("top.somethingweird()", true);
+ fail("Failed to throw exception on EL error");
+ } catch (Exception ex) {
+ //ok
+ }
+ }
+
+ public void testDoesNotFailOnExceptionWithoutThrowException() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ stack.findValue("fail", false);
+ stack.findValue("fail");
+ }
+
+ public void testDoesNotFailOnInheritedPropertiesWithThrowException() {
+ //this shuld not fail as the property is defined on a parent class
+ TestObject obj = new TestObject();
+ root.push(obj);
+ ChildTestAction childTestAction = new ChildTestAction();
+ obj.setChildTestAction(childTestAction);
+
+ assertNull(childTestAction.getConverted());
+ stack.findValue("childTestAction.converted", true);
+ }
+
+ public void testDoesNotFailOnInheritedMethodsWithThrowException() {
+ //this shuld not fail as the property is defined on a parent class
+ TestObject obj = new TestObject();
+ root.push(obj);
+ ChildTestAction childTestAction = new ChildTestAction();
+ obj.setChildTestAction(childTestAction);
+
+ assertNull(childTestAction.getConverted());
+ stack.findValue("top.getChildTestAction().converted", true);
+ }
+
+ public void testFailsOnInheritedMethodsWithThrowException() {
+ //this shuld not fail as the property is defined on a parent class
+ TestObject obj = new TestObject();
+ root.push(obj);
+ ChildTestAction childTestAction = new ChildTestAction();
+ obj.setChildTestAction(childTestAction);
+
+ assertNull(childTestAction.getConverted());
+
+ try {
+ stack.findValue("top.getChildTestAction().converted2", true);
+ fail("should have failed because of missing property");
+ } catch (Exception e) {
+ }
+ }
+
+ public void
testPrimitiveSettingWithInvalidValueAddsFieldErrorInNonDevMode() {
+ TestAction action = new TestAction();
+ stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.TRUE);
+ stack.setDevMode("false");
+ stack.push(action);
+ stack.setValue("bar", "3x");
+
+ Map conversionErrors = (Map)
stack.getContext().get(ActionContext.CONVERSION_ERRORS);
+ assertTrue(conversionErrors.containsKey("bar"));
+ }
+
+ public void testPrimitiveSettingWithInvalidValueAddsFieldErrorInDevMode()
{
+ TestAction action = new TestAction();
+ stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS,
Boolean.TRUE);
+ stack.setDevMode("true");
+ stack.push(action);
+ stack.setValue("bar", "3x");
+
+ Map conversionErrors = (Map)
stack.getContext().get(ActionContext.CONVERSION_ERRORS);
+ assertTrue(conversionErrors.containsKey("bar"));
+ }
+
+ public void
testObjectSettingWithInvalidValueDoesNotCauseSetCalledWithNull() {
+ TestObject obj = new TestObject();
+ root.push(obj);
+ ChildTestAction obj2 = new ChildTestAction();
+ obj.setChildTestAction(obj2);
+
+ stack.setValue("childTestAction", "whoa");
+ assertNotNull(obj.getChildTestAction());
+ assertSame(obj2, obj.getChildTestAction());
+ }
+
+}