Author: musachy
Date: Thu Nov 5 03:29:14 2009
New Revision: 832985
URL:
http://svn.apache.org/viewvc?rev=832985&view=revLog:
add support for add operator and ognl #
Added:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/BuiltinFunctionMapper.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelServletContextListener.java
- copied, changed from r832824, struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/UelServletContextListener.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java
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/JuelMethodInvocationTest.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/
struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/TestAction.properties
Removed:
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/UelServletContextListener.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/PublicMethodResolver.java
Modified:
struts/sandbox/trunk/struts2-uel-plugin/pom.xml
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/CompoundRootELContext.java
struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/ExpressionFactoryHolder.java
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/UelReflectionProvider.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/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/TestObject.java
struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java
Modified: struts/sandbox/trunk/struts2-uel-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/pom.xml?rev=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/pom.xml (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/pom.xml Thu Nov 5 03:29:14 2009
@@ -1,5 +1,11 @@
-<project xmlns="
http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="
http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.struts</groupId>
+ <artifactId>struts2-plugins</artifactId>
+ <version>2.2.0-SNAPSHOT</version>
+ </parent>
<groupId>org.apache.struts2.uelplugin</groupId>
<artifactId>struts2-uel-plugin</artifactId>
<packaging>jar</packaging>
@@ -50,6 +56,18 @@
</exclusions>
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${struts2.springPlatformVersion}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>${struts2.springPlatformVersion}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
@@ -58,13 +76,13 @@
<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-impl</artifactId>
- <version>2.1.2</version>
- <exclusions>
- <exclusion>
- <groupId>de.odysseus.juel</groupId>
- <artifactId>juel-api</artifactId>
- </exclusion>
- </exclusions>
+ <version>2.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>de.odysseus.juel</groupId>
+ <artifactId>juel-api</artifactId>
+ <version>2.2.0</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
Added: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/BuiltinFunctionMapper.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/BuiltinFunctionMapper.java?rev=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/BuiltinFunctionMapper.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/BuiltinFunctionMapper.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,46 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.TextProviderFactory;
+import com.opensymphony.xwork2.TextProvider;
+import com.opensymphony.xwork2.LocaleProvider;
+import com.opensymphony.xwork2.util.CompoundRoot;
+import com.opensymphony.xwork2.util.ValueStack;
+
+import javax.el.FunctionMapper;
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.HashMap;
+
+import org.apache.commons.lang.xwork.StringUtils;
+
+public class BuiltinFunctionMapper extends FunctionMapper {
+ private static final Map<String, Method> BUILTIN_FUNCTIONS = new HashMap<String, Method>() {
+ {
+ try {
+ Method getText = BuiltinFunctionMapper.class.getMethod("getText", new Class[]{String.class});
+ put("getText", getText);
+ } catch (NoSuchMethodException e) {
+ //this should never happen
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+
+ public Method resolveFunction(String prefix, String localName) {
+ return StringUtils.isBlank(prefix) ? BUILTIN_FUNCTIONS.get(localName) : null;
+ }
+
+ public static String getText(String key) {
+ ValueStack stack = ActionContext.getContext().getValueStack();
+ Object action = stack.findValue("#action");
+
+ if (action != null && action instanceof LocaleProvider) {
+ TextProvider textProvider = new TextProviderFactory().createInstance(action.getClass(), (LocaleProvider) action);
+ return textProvider.getText(key);
+ }
+
+ return null;
+ }
+}
Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/CompoundRootELContext.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/CompoundRootELContext.java?rev=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/CompoundRootELContext.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/CompoundRootELContext.java Thu Nov 5 03:29:14 2009
@@ -1,11 +1,11 @@
package org.apache.struts2.uelplugin;
-import javax.el.*;
-
+import de.odysseus.el.util.SimpleContext;
import org.apache.struts2.uelplugin.elresolvers.CompoundRootELResolver;
import org.apache.struts2.uelplugin.elresolvers.XWorkBeanELResolver;
-import org.apache.struts2.uelplugin.elresolvers.PublicMethodResolver;
-import de.odysseus.el.util.SimpleContext;
+import org.apache.struts2.uelplugin.elresolvers.ValueStackContextResolver;
+
+import javax.el.*;
/**
@@ -13,19 +13,18 @@
* CompoundRoot.
*/
public class CompoundRootELContext extends SimpleContext {
+ private final static BuiltinFunctionMapper BUILTIN_FUNCTION_MAPPER = new BuiltinFunctionMapper();
public CompoundRootELContext() {
super(new CompositeELResolver() {
{
- add(new BeanELResolver());
- add(new PublicMethodResolver());
add(new CompoundRootELResolver());
+ add(new ValueStackContextResolver());
add(new ArrayELResolver(false));
add(new ListELResolver(false));
add(new MapELResolver(false));
add(new ResourceBundleELResolver());
add(new XWorkBeanELResolver());
- }
- });
+ }});
}
@Override
@@ -35,6 +34,6 @@
@Override
public FunctionMapper getFunctionMapper() {
- return null;
+ return BUILTIN_FUNCTION_MAPPER;
}
}
Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/ExpressionFactoryHolder.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/ExpressionFactoryHolder.java?rev=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/ExpressionFactoryHolder.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/ExpressionFactoryHolder.java Thu Nov 5 03:29:14 2009
@@ -2,22 +2,14 @@
import javax.el.ExpressionFactory;
-public class ExpressionFactoryHolder {
- private static ThreadLocal<ExpressionFactory> expressionFactoryInstance = new ThreadLocal<ExpressionFactory>();
+public abstract class ExpressionFactoryHolder {
+ private static ExpressionFactory expressionFactory;
public static void setExpressionFactory(ExpressionFactory factory) {
- expressionFactoryInstance.set(factory);
+ expressionFactory = factory;
}
public static ExpressionFactory getExpressionFactory() {
- try {
- return ExpressionFactory.newInstance();
- } catch (Throwable t) {
- ExpressionFactory factory = expressionFactoryInstance.get();
- if (factory == null) {
- throw new RuntimeException("Expression Factory Not Found!");
- }
- return factory;
- }
+ return expressionFactory;
}
}
Added: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java?rev=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,124 @@
+package org.apache.struts2.uelplugin;
+
+import de.odysseus.el.misc.TypeConverter;
+import de.odysseus.el.misc.NumberOperations;
+import de.odysseus.el.tree.impl.Builder;
+import de.odysseus.el.tree.impl.Parser;
+import de.odysseus.el.tree.impl.Scanner;
+import de.odysseus.el.tree.impl.ast.AstBinary;
+import de.odysseus.el.tree.impl.ast.AstNode;
+import de.odysseus.el.tree.impl.ast.AstUnary;
+import de.odysseus.el.tree.impl.ast.AstIdentifier;
+import org.apache.commons.lang.xwork.StringUtils;
+
+public class JUELExtensionBuilder extends Builder {
+
+ /**
+ * We need a new token for "#".
+ */
+ static Scanner.ExtensionToken SHARP_TOKEN = new Scanner.ExtensionToken("#");
+ static Scanner.ExtensionToken EXTENDED_ADD_TOKEN = new Scanner.ExtensionToken("+");
+
+ /**
+ * This is our operator which will be passed to an <code>AstBinary</code>.
+ */
+ static AstUnary.Operator SHARP_OPERATOR = new AstUnary.SimpleOperator() {
+ public Object apply(TypeConverter converter, Object obj) {
+ return obj;
+ }
+
+ public String toString() {
+ return "#";
+ }
+ };
+
+ static AstBinary.Operator EXTENDED_ADD_OPERATOR = new AstBinary.SimpleOperator() {
+
+ public Object apply(TypeConverter converter, Object o1, Object o2) {
+ if (o1 instanceof String || o2 instanceof String)
+ return StringUtils.join(new Object[]{o1, o2});
+ else
+ return NumberOperations.add(converter, o1, o2);
+ }
+
+
+ public String toString() {
+ return "+";
+ }
+ };
+
+ /**
+ * This is our handler which will create the abstract syntax node.
+ */
+ static Parser.ExtensionHandler SHARP_HANDLER = new Parser.ExtensionHandler(Parser.ExtensionPoint.UNARY) {
+ public AstNode createAstNode(AstNode... children) {
+ AstIdentifier astIdentifier = (AstIdentifier) children[0];
+ ValueStackAstIdentifier valueStackAstIdentifier = new ValueStackAstIdentifier(astIdentifier.getName(), astIdentifier.getIndex());
+ return new AstUnary(valueStackAstIdentifier, SHARP_OPERATOR);
+ }
+ };
+
+ static Parser.ExtensionHandler EXTENDED_ADD_HANDLER = new Parser.ExtensionHandler(Parser.ExtensionPoint.ADD) {
+ public AstNode createAstNode(AstNode... children) {
+ return new AstBinary(children[0], children[1], EXTENDED_ADD_OPERATOR);
+ }
+ };
+
+
+ /**
+ * Here's our extended parser implementation.
+ */
+ static class ExtendedParser extends Parser {
+ public ExtendedParser(Builder context, String input) {
+ super(context, input);
+ putExtensionHandler(SHARP_TOKEN, SHARP_HANDLER);
+ putExtensionHandler(EXTENDED_ADD_TOKEN, EXTENDED_ADD_HANDLER);
+ }
+
+ /**
+ * Use a modified scanner which recognizes <code>'~'</code> and keyword <code>'matches'</code>.
+ */
+
+ protected Scanner createScanner(String expression) {
+ return new Scanner(expression) {
+
+ protected Token nextEval() throws ScanException {
+ String input = getInput();
+ int currentIndex = getPosition();
+ char current = input.charAt(currentIndex);
+
+ if (current == '#' && (!StringUtils.substring(input, currentIndex + 1, currentIndex + 2).equals('{'))) {
+ return SHARP_TOKEN;
+ } else if (current == '+') {
+ return EXTENDED_ADD_TOKEN;
+ }
+
+
+ return super.nextEval();
+ }
+ };
+ }
+ }
+
+ public JUELExtensionBuilder() {
+ super();
+ }
+
+ public JUELExtensionBuilder(Feature... features) {
+ super(features);
+ }
+
+ /**
+ * Make sure to use our modified parser.
+ */
+
+ protected Parser createParser(String expression) {
+ return new ExtendedParser(this, expression);
+ }
+}
+
+class ValueStackAstIdentifier extends AstIdentifier {
+ public ValueStackAstIdentifier(String name, int index) {
+ super("#"+name, index);
+ }
+}
Modified: 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=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionContextFactory.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionContextFactory.java Thu Nov 5 03:29:14 2009
@@ -1,10 +1,10 @@
package org.apache.struts2.uelplugin;
+import com.opensymphony.xwork2.util.reflection.ReflectionContextFactory;
+
import java.util.HashMap;
import java.util.Map;
-import com.opensymphony.xwork2.util.reflection.ReflectionContextFactory;
-
/**
* ReflectionContextFactory for Unified EL.
*/
Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionProvider.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionProvider.java?rev=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionProvider.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelReflectionProvider.java Thu Nov 5 03:29:14 2009
@@ -1,20 +1,16 @@
package org.apache.struts2.uelplugin;
-import java.util.Map;
-import java.lang.reflect.InvocationTargetException;
-
-import javax.el.ELContext;
-import javax.el.ExpressionFactory;
-import javax.el.ValueExpression;
-
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlReflectionProvider;
-import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.reflection.ReflectionException;
-import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.lang.xwork.StringUtils;
+
+import javax.el.ExpressionFactory;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
/**
* A OgnlReflectionProvider based on Unified EL.
@@ -29,7 +25,6 @@
}
-
@Override
public Object getValue(String expr, Map context, Object root) throws ReflectionException {
try {
@@ -62,7 +57,6 @@
sb.append(expr);
sb.append("}");
return sb.toString();
- } else
- return expr;
+ } else return expr;
}
}
Copied: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelServletContextListener.java (from r832824, struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/UelServletContextListener.java)
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelServletContextListener.java?p2=struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelServletContextListener.java&p1=struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/UelServletContextListener.java&r1=832824&r2=832985&rev=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/contextlistener/UelServletContextListener.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelServletContextListener.java Thu Nov 5 03:29:14 2009
@@ -1,30 +1,30 @@
-package org.apache.struts2.uelplugin.contextlistener;
+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 javax.servlet.jsp.JspApplicationContext;
-import javax.servlet.jsp.JspFactory;
+import java.util.Properties;
import org.apache.struts2.uelplugin.ExpressionFactoryHolder;
-import org.apache.struts2.uelplugin.elresolvers.CompoundRootELResolver;
-import org.apache.struts2.uelplugin.elresolvers.XWorkBeanELResolver;
/**
* Responsible for registering the ELResolvers.
*/
public class UelServletContextListener implements ServletContextListener {
-
public void contextInitialized(ServletContextEvent contextEvent) {
- ServletContext servletContext = contextEvent.getServletContext();
- JspApplicationContext jspApplicationContext = JspFactory
- .getDefaultFactory().getJspApplicationContext(servletContext);
- jspApplicationContext.addELResolver(new CompoundRootELResolver());
- jspApplicationContext.addELResolver(new XWorkBeanELResolver());
- contextEvent.getServletContext().log(
- "CompoundRootELResolver and XWorkBeanELResolver registered");
- ExpressionFactoryHolder.setExpressionFactory(jspApplicationContext
- .getExpressionFactory());
+ 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) {
Modified: 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=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelValueStack.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelValueStack.java Thu Nov 5 03:29:14 2009
@@ -1,43 +1,39 @@
package org.apache.struts2.uelplugin;
-import java.util.Map;
-import java.util.TreeMap;
-
-import javax.el.ELContext;
-import javax.el.ELException;
-import javax.el.ExpressionFactory;
-import javax.el.PropertyNotFoundException;
-import javax.el.ValueExpression;
-
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import com.opensymphony.xwork2.util.logging.Logger;
+
+import javax.el.*;
+import java.util.Map;
+import java.util.TreeMap;
/**
* A ValueStack that uses Unified EL as the underlying Expression Language.
*/
public class UelValueStack implements ValueStack {
+ 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 XWorkConverter xworkConverter;
- private ExpressionFactory factory;
-
private ELContext elContext;
- public UelValueStack(ExpressionFactory factory, XWorkConverter xworkConverter) {
- this(factory, xworkConverter, new CompoundRoot());
+ public UelValueStack(XWorkConverter xworkConverter) {
+ this(xworkConverter, new CompoundRoot());
}
- public UelValueStack(ExpressionFactory factory, XWorkConverter xworkConverter, ValueStack vs) {
- this(factory, xworkConverter, new CompoundRoot(vs.getRoot()));
+ public UelValueStack(XWorkConverter xworkConverter, ValueStack vs) {
+ this(xworkConverter, new CompoundRoot(vs.getRoot()));
}
- public UelValueStack(ExpressionFactory factory, XWorkConverter xworkConverter, CompoundRoot root) {
+ public UelValueStack(XWorkConverter xworkConverter, CompoundRoot root) {
this.xworkConverter = xworkConverter;
- this.factory = factory;
setRoot(new CompoundRoot(root));
}
@@ -64,18 +60,6 @@
public Object findValue(String expr, Class asType, boolean throwException) {
String originalExpression = expr;
try {
- if (expr != null && expr.startsWith("#") && !expr.startsWith("#{")) {
- int firstDot = expr.indexOf('.');
- if (firstDot < 0) {
- String key = expr.substring(1);
- return (Object) context.get(key);
- } else {
- String key = expr.substring(1, firstDot);
- String value = expr.substring(firstDot + 1);
- Map map = (Map) context.get(key);
- return map.get(value);
- }
- }
if ((overrides != null) && overrides.containsKey(expr)) {
expr = (String) overrides.get(expr);
}
@@ -88,7 +72,9 @@
}
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)) {
@@ -107,6 +93,23 @@
}
}
+ 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;
}
@@ -158,7 +161,9 @@
}
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 (ELException e) {
Modified: 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=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelValueStackFactory.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/UelValueStackFactory.java Thu Nov 5 03:29:14 2009
@@ -1,7 +1,5 @@
package org.apache.struts2.uelplugin;
-import javax.el.ExpressionFactory;
-
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
@@ -9,6 +7,8 @@
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
+import javax.el.ExpressionFactory;
+
/**
* Creates UelValueStacks.
*/
@@ -29,22 +29,15 @@
this.container = container;
}
- public void initExpressionFactory() {
- if (factory == null) {
- factory = ExpressionFactoryHolder.getExpressionFactory();
- }
- }
public ValueStack createValueStack() {
- initExpressionFactory();
- ValueStack results = new UelValueStack(factory, xworkConverter);
+ ValueStack results = new UelValueStack(xworkConverter);
results.getContext().put(ActionContext.CONTAINER, container);
return results;
}
public ValueStack createValueStack(ValueStack stack) {
- initExpressionFactory();
- ValueStack results = new UelValueStack(factory, xworkConverter, stack);
+ ValueStack results = new UelValueStack(xworkConverter, stack);
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=832985&r1=832984&r2=832985&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 Thu Nov 5 03:29:14 2009
@@ -1,5 +1,13 @@
package org.apache.struts2.uelplugin.elresolvers;
+import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import com.opensymphony.xwork2.util.CompoundRoot;
+import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.beanutils.PropertyUtils;
+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;
@@ -8,15 +16,6 @@
import java.util.ArrayList;
import java.util.Iterator;
-import javax.el.ELContext;
-import javax.el.ELResolver;
-
-import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.beanutils.PropertyUtils;
-
-import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
-import com.opensymphony.xwork2.util.CompoundRoot;
-
/**
* An ELResolver that is capable of resolving properties against the
* CompoundRoot if available in the ELContext.
@@ -33,15 +32,13 @@
}
@Override
- public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
- Object base) {
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
// only resolve at the root of the context
if (base != null) {
return null;
}
- CompoundRoot root = (CompoundRoot) context
- .getContext(CompoundRoot.class);
+ CompoundRoot root = (CompoundRoot) context.getContext(CompoundRoot.class);
if (root == null) {
return null;
}
@@ -78,8 +75,7 @@
return null;
}
- CompoundRoot root = (CompoundRoot) context
- .getContext(CompoundRoot.class);
+ CompoundRoot root = (CompoundRoot) context.getContext(CompoundRoot.class);
if (root == null) {
return null;
}
@@ -104,22 +100,28 @@
@Override
public Object getValue(ELContext context, Object base, Object property) {
if (context == null) {
- throw new NullPointerException();
+ throw new IllegalArgumentException("ElContext cannot be null");
}
+
+ String propertyName = (String) property;
+
+ if (StringUtils.startsWith(propertyName, "#"))
+ return null;
+
// only resolve at the root of the context
if (base != null) {
return null;
}
- CompoundRoot root = (CompoundRoot) context
- .getContext(CompoundRoot.class);
+ CompoundRoot root = (CompoundRoot) context.getContext(CompoundRoot.class);
if (root == null) {
return null;
}
- String propertyName = (String) property;
+
if ("top".equals(propertyName) && root.size() > 0) {
return root.get(0);
}
+
try {
Object bean = findObjectForProperty(root, propertyName);
if (bean != null) {
@@ -147,8 +149,7 @@
}
@Override
- public void setValue(ELContext context, Object base, Object property,
- Object value) {
+ public void setValue(ELContext context, Object base, Object property, Object value) {
if (context == null) {
throw new NullPointerException();
}
@@ -157,15 +158,13 @@
return;
}
- CompoundRoot root = (CompoundRoot) context
- .getContext(CompoundRoot.class);
+ CompoundRoot root = (CompoundRoot) context.getContext(CompoundRoot.class);
String propertyName = (String) property;
try {
if (base == null && property != null && root != null) {
Object bean = findObjectForProperty(root, propertyName);
if (bean != null) {
- XWorkConverter converter = (XWorkConverter) context
- .getContext(XWorkConverter.class);
+ XWorkConverter converter = (XWorkConverter) context.getContext(XWorkConverter.class);
if (converter != null && root != null) {
Class propType = determineType(bean, propertyName);
value = converter.convertValue(null, value, propType);
@@ -183,8 +182,7 @@
}
}
- protected Class<?> determineType(Object bean, String property)
- throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+ protected Class<?> determineType(Object bean, String property) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
return PropertyUtils.getPropertyType(bean, property);
}
@@ -193,8 +191,7 @@
return root.get(0);
}
for (int i = 0; i < root.size(); i++) {
- if (PropertyUtils.isReadable(root.get(i), propertyName)
- || PropertyUtils.isWriteable(root.get(i), propertyName)) {
+ if (PropertyUtils.isReadable(root.get(i), propertyName) || PropertyUtils.isWriteable(root.get(i), propertyName)) {
return root.get(i);
}
}
Added: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java?rev=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,50 @@
+package org.apache.struts2.uelplugin.elresolvers;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.util.ValueStack;
+
+import javax.el.ELResolver;
+import javax.el.ELContext;
+import java.util.Iterator;
+import java.beans.FeatureDescriptor;
+
+
+public class ValueStackContextResolver extends ELResolver {
+ public Object getValue(ELContext context, Object base, Object property) {
+ String objectName = property.toString();
+
+ ActionContext actionContext = ActionContext.getContext();
+ if (context != null) {
+ ValueStack valueStack = actionContext.getValueStack();
+ if (valueStack != null) {
+ Object obj = valueStack.getContext().get(objectName);
+ if (obj != null) {
+ context.setPropertyResolved(true);
+ return obj;
+ }
+ }
+ }
+
+ return null;
+ }
+
+
+ public Class<?> getCommonPropertyType(ELContext elContext, Object o) {
+ return null;
+ }
+
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext elContext, Object o) {
+ return null;
+ }
+
+ public Class<?> getType(ELContext elContext, Object o, Object o1) {
+ return null;
+ }
+
+ public boolean isReadOnly(ELContext elContext, Object o, Object o1) {
+ return true;
+ }
+
+ public void setValue(ELContext elContext, Object o, Object o1, Object o2) {
+ }
+}
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=832985&r1=832984&r2=832985&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 Thu Nov 5 03:29:14 2009
@@ -1,13 +1,11 @@
package org.apache.struts2.uelplugin.elresolvers;
-import java.lang.reflect.InvocationTargetException;
+import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import org.apache.commons.beanutils.PropertyUtils;
import javax.el.BeanELResolver;
import javax.el.ELContext;
-
-import org.apache.commons.beanutils.PropertyUtils;
-
-import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
+import java.lang.reflect.InvocationTargetException;
public class XWorkBeanELResolver extends BeanELResolver {
@@ -35,14 +33,11 @@
}
@Override
- public void setValue(ELContext context, Object base, Object property,
- Object value) {
- XWorkConverter converter = (XWorkConverter) context
- .getContext(XWorkConverter.class);
+ public void setValue(ELContext context, Object base, Object property, Object value) {
+ XWorkConverter converter = (XWorkConverter) context.getContext(XWorkConverter.class);
try {
if (converter != null && base != null) {
- Class propType = PropertyUtils.getPropertyType(base, property
- .toString());
+ Class propType = PropertyUtils.getPropertyType(base, property.toString());
value = converter.convertValue(null, value, propType);
}
super.setValue(context, base, property, value);
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=832985&r1=832984&r2=832985&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 Thu Nov 5 03:29:14 2009
@@ -5,9 +5,12 @@
"
http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
- <bean type="com.opensymphony.xwork2.util.ValueStackFactory" name="uel" class="org.apache.struts2.uelplugin.UelValueStackFactory"/>
- <bean type="com.opensymphony.xwork2.util.reflection.ReflectionProvider" name="uel" class="org.apache.struts2.uelplugin.UelReflectionProvider"/>
- <bean type="com.opensymphony.xwork2.util.reflection.ReflectionContextFactory" name="uel" class="org.apache.struts2.uelplugin.UelReflectionContextFactory"/>
+ <bean type="com.opensymphony.xwork2.util.ValueStackFactory" name="uel"
+ class="org.apache.struts2.uelplugin.UelValueStackFactory"/>
+ <bean type="com.opensymphony.xwork2.util.reflection.ReflectionProvider" name="uel"
+ class="org.apache.struts2.uelplugin.UelReflectionProvider"/>
+ <bean type="com.opensymphony.xwork2.util.reflection.ReflectionContextFactory" name="uel"
+ class="org.apache.struts2.uelplugin.UelReflectionContextFactory"/>
<constant name="struts.valueStackFactory" value="uel"/>
<constant name="struts.reflectionProvider" value="uel"/>
Added: 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=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/BuiltinFunctionsTest.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,51 @@
+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 XWorkTestCase {
+ private XWorkConverter converter;
+ private CompoundRoot root;
+ private UelValueStack stack;
+
+ public void testGetText() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+ TestAction action = new TestAction();
+ stack.push(action);
+ stack.getContext().put(ContextUtil.ACTION, action);
+
+ assertEquals("This is the key!", stack.findValue("${getText('key')}"));
+ }
+
+
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ converter = container.getInstance(XWorkConverter.class);
+ this.root = new CompoundRoot();
+ this.stack = new UelValueStack(converter);
+ stack.setRoot(root);
+ stack.getContext().put(ActionContext.CONTAINER, container);
+
+ MockServletContext servletContext = new MockServletContext();
+ ActionContext context = new ActionContext(stack.getContext());
+ ActionContext.setContext(context);
+ ServletActionContext.setServletContext(servletContext);
+
+ //simulate start up
+ UelServletContextListener listener = new UelServletContextListener();
+ listener.contextInitialized(new ServletContextEvent(servletContext));
+ }
+}
Added: 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/JuelMethodInvocationTest.java?rev=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,54 @@
+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 XWorkTestCase {
+ private XWorkConverter converter;
+ private CompoundRoot root;
+ private UelValueStack stack;
+
+ public void testBasicMethods() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+
+ ActionContext.getContext().setValueStack(stack);
+
+ assertEquals("text", stack.findValue("${' text '.trim()}"));
+ assertEquals(3, stack.findValue("${'123'.length()}"));
+ }
+
+ public void testMethodsWithParams() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+
+ ActionContext.getContext().setValueStack(stack);
+
+ assertEquals('2', stack.findValue("${'123'.charAt(1)}"));
+ assertEquals("123456", stack.findValue("${'123'.concat('456')}"));
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ MockServletContext servletContext = new MockServletContext();
+ ActionContext context = new ActionContext(new HashMap());
+ ActionContext.setContext(context);
+ ServletActionContext.setServletContext(servletContext);
+
+ //simulate start up
+ UelServletContextListener listener = new UelServletContextListener();
+ listener.contextInitialized(new ServletContextEvent(servletContext));
+
+ converter = container.getInstance(XWorkConverter.class);
+ this.root = new CompoundRoot();
+ this.stack = new UelValueStack(converter);
+ stack.setRoot(root);
+ }
+}
Added: 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=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestAction.java Thu Nov 5 03:29:14 2009
@@ -0,0 +1,6 @@
+package org.apache.struts2.uelplugin;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+public class TestAction extends ActionSupport {
+}
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=832985&r1=832984&r2=832985&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 Thu Nov 5 03:29:14 2009
@@ -5,49 +5,49 @@
public class TestObject {
- private String value;
- private int age;
- private Date date;
- private TestObject inner;
- private Map parameters;
-
- public String getValue() {
- return value;
- }
-
- public void setValue(String value) {
- this.value = value;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public Date getDate() {
- return date;
- }
-
- public void setDate(Date date) {
- this.date = date;
- }
-
- public TestObject getInner() {
- return inner;
- }
-
- public void setInner(TestObject inner) {
- this.inner = inner;
- }
-
- public Map getParameters() {
- return parameters;
- }
-
- public void setParameters(Map parameters) {
- this.parameters = parameters;
- }
+ private String value;
+ private int age;
+ private Date date;
+ private TestObject inner;
+ private Map parameters;
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public Date getDate() {
+ return date;
+ }
+
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+ public TestObject getInner() {
+ return inner;
+ }
+
+ public void setInner(TestObject inner) {
+ this.inner = inner;
+ }
+
+ public Map getParameters() {
+ return parameters;
+ }
+
+ public void setParameters(Map parameters) {
+ this.parameters = parameters;
}
+}
Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java?rev=832985&r1=832984&r2=832985&view=diff==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java Thu Nov 5 03:29:14 2009
@@ -1,5 +1,16 @@
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.apache.struts2.util.StrutsTypeConverter;
+import org.springframework.mock.web.MockServletContext;
+
+import javax.el.ExpressionFactory;
+import javax.servlet.ServletContextEvent;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
@@ -7,25 +18,17 @@
import java.util.HashMap;
import java.util.Map;
-import javax.el.ExpressionFactory;
-
-import org.apache.struts2.uelplugin.UelValueStack;
-import org.apache.struts2.util.StrutsTypeConverter;
-
-import com.opensymphony.xwork2.XWorkTestCase;
-import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
-import com.opensymphony.xwork2.util.CompoundRoot;
-
public class UelTest extends XWorkTestCase {
private ExpressionFactory factory = ExpressionFactory.newInstance();
private XWorkConverter converter;
private DateFormat format = DateFormat.getDateInstance();
+ private CompoundRoot root;
+ private UelValueStack stack;
private class DateConverter extends StrutsTypeConverter {
@Override
- public Object convertFromString(Map context, String[] values,
- Class toClass) {
+ public Object convertFromString(Map context, String[] values, Class toClass) {
try {
return format.parseObject(values[0]);
} catch (ParseException e) {
@@ -45,47 +48,92 @@
converter = container.getInstance(XWorkConverter.class);
converter.registerConverter("java.util.Date", new DateConverter());
+ this.root = new CompoundRoot();
+ this.stack = new UelValueStack(converter);
+ stack.setRoot(root);
+ stack.getContext().put(ActionContext.CONTAINER, container);
+
+ MockServletContext servletContext = new MockServletContext();
+ ActionContext context = new ActionContext(stack.getContext());
+ ActionContext.setContext(context);
+ ServletActionContext.setServletContext(servletContext);
+
+ //simulate start up
+ UelServletContextListener listener = new UelServletContextListener();
+ listener.contextInitialized(new ServletContextEvent(servletContext));
}
- public void testBasicFind() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+
+ 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 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"));
+ }
+
+ public void testBasicFind() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
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);
+ assertEquals(56, (int) age);
}
- public void testNestedFind() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void testNestedFind() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
TestObject obj2 = new TestObject();
obj2.setAge(100);
obj.setInner(obj2);
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
-
assertSame(obj2, stack.findValue("${inner}"));
assertEquals(100, stack.findValue("${inner.age}"));
}
- public void testSetStringArray() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void testSetStringArray() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
+
stack.setValue("${value}", new String[]{"Hello World"});
String value = stack.findString("${value}");
assertEquals("Hello World", value);
@@ -94,14 +142,10 @@
assertEquals(new Integer(67), stack.findValue("${age}"));
}
- public void testDeferredFind() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void testDeferredFind() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
stack.setValue("#{value}", "Hello World");
String value = stack.findString("#{value}");
assertEquals("Hello World", value);
@@ -114,52 +158,38 @@
assertEquals(stack.findString("#{date}"), format.format(obj.getDate()));
}
- public void testMap() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void testMap() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
HashMap map = new HashMap();
map.put("nameValue", "Lex");
TestObject obj = new TestObject();
obj.setParameters(map);
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
- String value = (String) stack.findValue("parameters.nameValue",
- String.class);
+ String value = (String) stack.findValue("parameters.nameValue", String.class);
assertEquals("Lex", value);
}
- public void test2LevelSet() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void test2LevelSet() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
TestObject nestedObj = new TestObject();
obj.setInner(nestedObj);
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
+
stack.setValue("${inner.age}", "66");
assertEquals(66, obj.getInner().getAge());
}
- public void testTypeConversion() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
-
- CompoundRoot root = new CompoundRoot();
+ public void testTypeConversion() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
TestObject inner = new TestObject();
obj.setInner(inner);
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
stack.setValue("${age}", "22");
assertEquals(stack.findValue("${age}"), obj.getAge());
stack.setValue("${inner.value}", "George");
- assertEquals(stack.findValue("${inner.value}"), obj.getInner()
- .getValue());
+ assertEquals(stack.findValue("${inner.value}"), obj.getInner().getValue());
stack.setValue("${inner.age}", "44");
assertEquals(stack.findValue("${inner.age}"), obj.getInner().getAge());
@@ -168,13 +198,10 @@
assertEquals(stack.findString("${date}"), format.format(obj.getDate()));
}
- public void testNotFound() throws IllegalAccessException,
- InvocationTargetException, NoSuchMethodException {
- CompoundRoot root = new CompoundRoot();
+ public void testNotFound() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
TestObject obj = new TestObject();
root.add(obj);
- UelValueStack stack = new UelValueStack(factory, converter);
- stack.setRoot(root);
+
stack.setValue("${value}", "Hello World");
String value = stack.findString("${VALUENOTHERE}");
assertNull(value);
Added: struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/TestAction.properties
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/TestAction.properties?rev=832985&view=auto==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/TestAction.properties (added)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/resources/org/apache/struts2/uelplugin/TestAction.properties Thu Nov 5 03:29:14 2009
@@ -0,0 +1 @@
+key=This is the key!
\ No newline at end of file