Author: musachy Date: Thu Nov 12 18:39:59 2009 New Revision: 835494 URL: http://svn.apache.org/viewvc?rev=835494&view=rev Log: Add more test, rename base test class
Added: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/PerformanceTest.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELBaseTest.java Removed: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/AbstractUELTest.java Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/CompoundRootELResolver.java struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/ValueStackContextReferenceELResolver.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/BuiltinFunctionsTest.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELMethodInvocationTest.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackReadValueTest.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackSetValueTest.java struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELValueStackOtherTests.java Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/CompoundRootELResolver.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/CompoundRootELResolver.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/CompoundRootELResolver.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/CompoundRootELResolver.java Thu Nov 12 18:39:59 2009 @@ -23,13 +23,9 @@ public Object getValue(ELContext elContext, Object base, Object property) { //EL doesn't know of value stack, so when an expression like "A.B" is evaluated //this method will be called with a null target and an "A" property - if (base == null) { - - String propertyName = property.toString(); - - if (StringUtils.startsWith(propertyName, "#")) - return null; + String propertyName = property.toString(); + if (base == null && !StringUtils.startsWith(propertyName, "#")) { CompoundRoot root = (CompoundRoot) elContext.getContext(CompoundRoot.class); if (root == null) { return null; Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/ValueStackContextReferenceELResolver.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/ValueStackContextReferenceELResolver.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/ValueStackContextReferenceELResolver.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uel/elresolvers/ValueStackContextReferenceELResolver.java Thu Nov 12 18:39:59 2009 @@ -36,19 +36,16 @@ } public Object getValue(ELContext elContext, Object base, Object property) { - String objectName = property.toString(); - if (StringUtils.startsWith(objectName, "#")) { - objectName = StringUtils.removeStart(property.toString(), "#"); + String objectName = StringUtils.removeStart(property.toString(), "#"); - Map valueStackContext = getValueStackContext(elContext); - Object obj = valueStackContext.get(objectName); + Map valueStackContext = getValueStackContext(elContext); + Object obj = valueStackContext.get(objectName); - if (obj != null) { - valueStackContext.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, obj.getClass()); - valueStackContext.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED, objectName); - elContext.setPropertyResolved(true); - return obj; - } + if (obj != null) { + valueStackContext.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, obj.getClass()); + valueStackContext.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED, objectName); + elContext.setPropertyResolved(true); + return obj; } return null; Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/BuiltinFunctionsTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/BuiltinFunctionsTest.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/BuiltinFunctionsTest.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/BuiltinFunctionsTest.java Thu Nov 12 18:39:59 2009 @@ -25,7 +25,7 @@ import java.lang.reflect.InvocationTargetException; -public class BuiltinFunctionsTest extends AbstractUELTest { +public class BuiltinFunctionsTest extends UELBaseTest { 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/uel/PerformanceTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/PerformanceTest.java?rev=835494&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/PerformanceTest.java (added) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/PerformanceTest.java Thu Nov 12 18:39:59 2009 @@ -0,0 +1,119 @@ +package org.apache.struts2.uel; + +import com.opensymphony.xwork2.ognl.OgnlValueStackFactory; +import com.opensymphony.xwork2.util.ValueStack; + +import java.lang.reflect.InvocationTargetException; +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.util.ArrayList; + +public class PerformanceTest extends UELBaseTest { + private final long ITERATIONS = 1000000; + private ValueStack ognlValueStack; + + + protected void setUp() throws Exception { + super.setUp(); + + OgnlValueStackFactory ognlValueStackFactory = new OgnlValueStackFactory(); + container.inject(ognlValueStackFactory); + this.ognlValueStack = ognlValueStackFactory.createValueStack(); + } + + public void testSuperNested() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + TestObject obj0 = new TestObject("0"); + + ognlValueStack.push(obj0); + 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); + ognlValueStack.getContext().put("obj", obj0); + + String expr = "inner.parameters['key0'].object.object[0].value"; + + assertEquals("4", stack.findValue(expr)); + assertEquals("4", ognlValueStack.findValue(expr)); + compare(expr); + } + + public void testContextReferences() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + TestObject obj = new TestObject(); + obj.setValue("val"); + obj.setAge(1); + stack.getContext().put("obj", obj); + ognlValueStack.getContext().put("obj", obj); + + //more expressions + TestObject obj2 = new TestObject(); + obj2.setValue("val2"); + obj2.setAge(2); + stack.getContext().put("obj2", obj2); + ognlValueStack.getContext().put("obj2", obj2); + + //addition + String expr = "#obj.age + #obj2.age"; + assertEquals(3L, stack.findValue(expr)); + assertEquals(3, ognlValueStack.findValue(expr)); + + compare(expr); + } + + public void testArithmetics() { + Map uelContext = stack.getContext(); + Map ognlContext = ognlValueStack.getContext(); + + uelContext.put("x", 1); + uelContext.put("y", 2); + uelContext.put("z", 3); + + ognlContext.put("x", 1); + ognlContext.put("y", 2); + ognlContext.put("z", 3); + + String expr = "#x + #y * #z"; + + assertEquals(7L, stack.findValue(expr, true)); + assertEquals(7, ognlValueStack.findValue(expr)); + + compare(expr); + } + + protected void compare(String expr) { + System.out.println("Eval: [" + expr + "] on " + ITERATIONS + " iterations"); + + long ognl = evaluate(ognlValueStack, expr); + System.out.println("OGNL: " + ognl + " ms"); + + long juel = evaluate(stack, expr); + System.out.println("JUEL: " + juel + " ms"); + } + + protected long evaluate(ValueStack valueStack, String expr) { + long start = System.currentTimeMillis(); + + for (int i = 0; i < ITERATIONS; i++) { + valueStack.findValue(expr); + } + + return System.currentTimeMillis() - start; + } +} Added: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELBaseTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELBaseTest.java?rev=835494&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELBaseTest.java (added) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELBaseTest.java Thu Nov 12 18:39:59 2009 @@ -0,0 +1,103 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.uel; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.conversion.impl.XWorkConverter; +import com.opensymphony.xwork2.inject.ContainerBuilder; +import com.opensymphony.xwork2.test.StubConfigurationProvider; +import com.opensymphony.xwork2.util.CompoundRoot; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.util.ValueStackFactory; +import com.opensymphony.xwork2.util.location.LocatableProperties; +import com.opensymphony.xwork2.util.reflection.ReflectionProvider; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.uel.reflection.GenericReflectionProvider; +import org.apache.struts2.util.StrutsTypeConverter; +import org.springframework.mock.web.MockServletContext; + +import javax.el.ExpressionFactory; +import javax.servlet.ServletContextEvent; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Map; + + +public abstract class UELBaseTest extends XWorkTestCase { + private ExpressionFactory factory = ExpressionFactory.newInstance(); + protected XWorkConverter converter; + protected CompoundRoot root; + protected UELValueStack stack; + protected DateFormat format = DateFormat.getDateInstance(); + protected ReflectionProvider reflectionProvider; + + private class DateConverter extends StrutsTypeConverter { + + @Override + public Object convertFromString(Map context, String[] values, Class toClass) { + try { + return format.parseObject(values[0]); + } catch (ParseException e) { + return null; + } + } + + @Override + public String convertToString(Map context, Object o) { + return format.format(o); + } + + } + + + protected void setUp() throws Exception { + super.setUp(); + + 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(ReflectionProvider.class, GenericReflectionProvider.class); + //builder.factory(StrutsTypeConverter) + } + }); + + converter = container.getInstance(XWorkConverter.class); + reflectionProvider = container.getInstance(ReflectionProvider.class); + converter.registerConverter("java.util.Date", new DateConverter()); + this.root = new CompoundRoot(); + this.stack = new UELValueStack(container); + 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)); + } +} Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELMethodInvocationTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELMethodInvocationTest.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELMethodInvocationTest.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELMethodInvocationTest.java Thu Nov 12 18:39:59 2009 @@ -23,7 +23,7 @@ import java.lang.reflect.InvocationTargetException; -public class UELMethodInvocationTest extends AbstractUELTest { +public class UELMethodInvocationTest extends UELBaseTest { public void testBasicMethods() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { assertEquals("text", stack.findValue("${' text '.trim()}")); assertEquals(3, stack.findValue("${'123'.length()}")); Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackReadValueTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackReadValueTest.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackReadValueTest.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackReadValueTest.java Thu Nov 12 18:39:59 2009 @@ -23,7 +23,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.*; -public class UELStackReadValueTest extends AbstractUELTest { +public class UELStackReadValueTest extends UELBaseTest { public void testPrivateMethod() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { TestObject obj = new TestObject(); @@ -258,4 +258,14 @@ value = stack.findString("VALUENOTHERE"); assertNull(value); } + + public void testArithmetics() { + Map uelContext = stack.getContext(); + + uelContext.put("x", 1); + uelContext.put("y", 2); + uelContext.put("z", 3); + + assertEquals(7L, stack.findValue("x + y * z")); + } } Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackSetValueTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackSetValueTest.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackSetValueTest.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELStackSetValueTest.java Thu Nov 12 18:39:59 2009 @@ -26,7 +26,7 @@ import java.util.*; -public class UELStackSetValueTest extends AbstractUELTest { +public class UELStackSetValueTest extends UELBaseTest { public void testPrivateMethod() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { TestObject obj = new TestObject(); Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELValueStackOtherTests.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELValueStackOtherTests.java?rev=835494&r1=835493&r2=835494&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELValueStackOtherTests.java (original) +++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uel/UELValueStackOtherTests.java Thu Nov 12 18:39:59 2009 @@ -27,7 +27,7 @@ import java.util.Map; -public class UELValueStackOtherTests extends AbstractUELTest { +public class UELValueStackOtherTests extends UELBaseTest { public void testExpOverridesCanStackExpUp() throws Exception { Map expr1 = new LinkedHashMap();