Author: markt Date: Sat Jul 6 19:31:39 2013 New Revision: 1500317 URL: http://svn.apache.org/r1500317 Log: Add unit tests for ELResolver.convertToType Fix a couple of bugs in ELContext.convertToType highlighted by / spotted while writing the unit tests Update the EL implementation to use convertToType
Added: tomcat/trunk/test/javax/el/TestELResolver.java tomcat/trunk/test/javax/el/TesterELResolverBase.java tomcat/trunk/test/javax/el/TesterELResolverOne.java tomcat/trunk/test/javax/el/TesterELResolverTwo.java Modified: tomcat/trunk/java/javax/el/ELContext.java tomcat/trunk/java/org/apache/el/MethodExpressionLiteral.java tomcat/trunk/java/org/apache/el/ValueExpressionImpl.java tomcat/trunk/java/org/apache/el/ValueExpressionLiteral.java tomcat/trunk/test/javax/el/TesterELContext.java Modified: tomcat/trunk/java/javax/el/ELContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/ELContext.java?rev=1500317&r1=1500316&r2=1500317&view=diff ============================================================================== --- tomcat/trunk/java/javax/el/ELContext.java (original) +++ tomcat/trunk/java/javax/el/ELContext.java Sat Jul 6 19:31:39 2013 @@ -216,16 +216,19 @@ public abstract class ELContext { public Object convertToType(Object obj, Class<?> type) { boolean originalResolved = isPropertyResolved(); + setPropertyResolved(false); try { ELResolver resolver = getELResolver(); - Object result = resolver.convertToType(this, obj, type); - if (isPropertyResolved()) { - return result; + if (resolver != null) { + Object result = resolver.convertToType(this, obj, type); + if (isPropertyResolved()) { + return result; + } } } finally { setPropertyResolved(originalResolved); } - return Util.getExpressionFactory().coerceToType(type, type); + return Util.getExpressionFactory().coerceToType(obj, type); } } Modified: tomcat/trunk/java/org/apache/el/MethodExpressionLiteral.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/MethodExpressionLiteral.java?rev=1500317&r1=1500316&r2=1500317&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/el/MethodExpressionLiteral.java (original) +++ tomcat/trunk/java/org/apache/el/MethodExpressionLiteral.java Sat Jul 6 19:31:39 2013 @@ -27,7 +27,6 @@ import javax.el.ELException; import javax.el.MethodExpression; import javax.el.MethodInfo; -import org.apache.el.lang.ELSupport; import org.apache.el.util.ReflectionUtil; @@ -58,7 +57,7 @@ public class MethodExpressionLiteral ext @Override public Object invoke(ELContext context, Object[] params) throws ELException { if (this.expectedType != null) { - return ELSupport.coerceToType(this.expr, this.expectedType); + return context.convertToType(this.expr, this.expectedType); } else { return this.expr; } Modified: tomcat/trunk/java/org/apache/el/ValueExpressionImpl.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/ValueExpressionImpl.java?rev=1500317&r1=1500316&r2=1500317&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/el/ValueExpressionImpl.java (original) +++ tomcat/trunk/java/org/apache/el/ValueExpressionImpl.java Sat Jul 6 19:31:39 2013 @@ -31,7 +31,6 @@ import javax.el.ValueExpression; import javax.el.ValueReference; import javax.el.VariableMapper; -import org.apache.el.lang.ELSupport; import org.apache.el.lang.EvaluationContext; import org.apache.el.lang.ExpressionBuilder; import org.apache.el.parser.AstLiteralExpression; @@ -184,7 +183,7 @@ public final class ValueExpressionImpl e this.varMapper); Object value = this.getNode().getValue(ctx); if (this.expectedType != null) { - return ELSupport.coerceToType(value, this.expectedType); + return context.convertToType(value, this.expectedType); } return value; } Modified: tomcat/trunk/java/org/apache/el/ValueExpressionLiteral.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/ValueExpressionLiteral.java?rev=1500317&r1=1500316&r2=1500317&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/el/ValueExpressionLiteral.java (original) +++ tomcat/trunk/java/org/apache/el/ValueExpressionLiteral.java Sat Jul 6 19:31:39 2013 @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.el; import java.io.Externalizable; @@ -26,7 +25,6 @@ import javax.el.ELContext; import javax.el.PropertyNotWritableException; import javax.el.ValueExpression; -import org.apache.el.lang.ELSupport; import org.apache.el.util.MessageFactory; import org.apache.el.util.ReflectionUtil; @@ -52,7 +50,7 @@ public final class ValueExpressionLitera @Override public Object getValue(ELContext context) { if (this.expectedType != null) { - return ELSupport.coerceToType(this.value, this.expectedType); + return context.convertToType(this.value, this.expectedType); } return this.value; } Added: tomcat/trunk/test/javax/el/TestELResolver.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TestELResolver.java?rev=1500317&view=auto ============================================================================== --- tomcat/trunk/test/javax/el/TestELResolver.java (added) +++ tomcat/trunk/test/javax/el/TestELResolver.java Sat Jul 6 19:31:39 2013 @@ -0,0 +1,130 @@ +/* + * 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 javax.el; + +import org.junit.Assert; +import org.junit.Test; + +public class TestELResolver { + + @Test + public void testConvertToType01() { + ELContext context = new TesterELContext(); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "1", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("1", result); + } + + + @Test + public void testConvertToType02() { + ELContext context = new TesterELContext(new TesterELResolverOne()); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "1", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("ONE", result); + } + + + @Test + public void testConvertToType03() { + ELContext context = new TesterELContext(new TesterELResolverOne()); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "2", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("2", result); + } + + + @Test + public void testConvertToType04() { + CompositeELResolver resolver = new CompositeELResolver(); + ELContext context = new TesterELContext(resolver); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "2", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("2", result); + } + + + @Test + public void testConvertToType05() { + CompositeELResolver resolver = new CompositeELResolver(); + resolver.add(new TesterELResolverOne()); + resolver.add(new TesterELResolverTwo()); + ELContext context = new TesterELContext(resolver); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "1", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("ONE", result); + } + + + @Test + public void testConvertToType06() { + CompositeELResolver resolver = new CompositeELResolver(); + resolver.add(new TesterELResolverOne()); + resolver.add(new TesterELResolverTwo()); + ELContext context = new TesterELContext(resolver); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "2", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("TWO", result); + } + + + @Test + public void testConvertToType07() { + CompositeELResolver resolver = new CompositeELResolver(); + resolver.add(new TesterELResolverOne()); + resolver.add(new TesterELResolverTwo()); + ELContext context = new TesterELContext(resolver); + + ValueExpression ve = + ELManager.getExpressionFactory().createValueExpression( + context, "3", String.class); + + String result = (String) ve.getValue(context); + + Assert.assertEquals("3", result); + } +} Modified: tomcat/trunk/test/javax/el/TesterELContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TesterELContext.java?rev=1500317&r1=1500316&r2=1500317&view=diff ============================================================================== --- tomcat/trunk/test/javax/el/TesterELContext.java (original) +++ tomcat/trunk/test/javax/el/TesterELContext.java Sat Jul 6 19:31:39 2013 @@ -18,9 +18,19 @@ package javax.el; public class TesterELContext extends ELContext { + private final ELResolver resolver; + + public TesterELContext() { + this(null); + } + + public TesterELContext(ELResolver resolver) { + this.resolver = resolver; + } + @Override public ELResolver getELResolver() { - return null; + return resolver; } @Override @@ -32,5 +42,4 @@ public class TesterELContext extends ELC public VariableMapper getVariableMapper() { return null; } - } Added: tomcat/trunk/test/javax/el/TesterELResolverBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TesterELResolverBase.java?rev=1500317&view=auto ============================================================================== --- tomcat/trunk/test/javax/el/TesterELResolverBase.java (added) +++ tomcat/trunk/test/javax/el/TesterELResolverBase.java Sat Jul 6 19:31:39 2013 @@ -0,0 +1,60 @@ +/* + * 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 javax.el; + +import java.beans.FeatureDescriptor; +import java.util.Iterator; + +public abstract class TesterELResolverBase extends ELResolver { + + + @Override + public abstract Object convertToType(ELContext context, Object obj, + Class<?> type); + + @Override + public Object getValue(ELContext context, Object base, Object property) { + return null; + } + + @Override + public Class<?> getType(ELContext context, Object base, Object property) { + return null; + } + + @Override + public void setValue(ELContext context, Object base, Object property, + Object value) { + // NO-OP + } + + @Override + public boolean isReadOnly(ELContext context, Object base, Object property) { + return false; + } + + @Override + public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, + Object base) { + return null; + } + + @Override + public Class<?> getCommonPropertyType(ELContext context, Object base) { + return null; + } +} Added: tomcat/trunk/test/javax/el/TesterELResolverOne.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TesterELResolverOne.java?rev=1500317&view=auto ============================================================================== --- tomcat/trunk/test/javax/el/TesterELResolverOne.java (added) +++ tomcat/trunk/test/javax/el/TesterELResolverOne.java Sat Jul 6 19:31:39 2013 @@ -0,0 +1,29 @@ +/* + * 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 javax.el; + +public class TesterELResolverOne extends TesterELResolverBase { + + @Override + public Object convertToType(ELContext context, Object obj, Class<?> type) { + if ("1".equals(obj) && type == String.class) { + context.setPropertyResolved(true); + return "ONE"; + } + return null; + } +} Added: tomcat/trunk/test/javax/el/TesterELResolverTwo.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TesterELResolverTwo.java?rev=1500317&view=auto ============================================================================== --- tomcat/trunk/test/javax/el/TesterELResolverTwo.java (added) +++ tomcat/trunk/test/javax/el/TesterELResolverTwo.java Sat Jul 6 19:31:39 2013 @@ -0,0 +1,29 @@ +/* + * 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 javax.el; + +public class TesterELResolverTwo extends TesterELResolverBase { + + @Override + public Object convertToType(ELContext context, Object obj, Class<?> type) { + if ("2".equals(obj) && type == String.class) { + context.setPropertyResolved(true); + return "TWO"; + } + return null; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org