Polishing

This commit is contained in:
Juergen Hoeller 2014-01-16 16:54:49 +01:00
parent fd13c994c9
commit 16bf501b30
7 changed files with 463 additions and 520 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,30 +17,32 @@
package org.springframework.expression;
/**
* Instances of a type comparator should be able to compare pairs of objects for equality, the specification of the
* return value is the same as for {@link Comparable}.
* Instances of a type comparator should be able to compare pairs of objects for equality.
* The specification of the return value is the same as for {@link java.lang.Comparable}.
*
* @author Andy Clement
* @since 3.0
* @see java.lang.Comparable
*/
public interface TypeComparator {
/**
* Compare two objects.
* Return {@code true} if the comparator can compare these two objects.
* @param firstObject the first object
* @param secondObject the second object
* @return 0 if they are equal, <0 if the first is smaller than the second, or >0 if the first is larger than the
* second
* @throws EvaluationException if a problem occurs during comparison (or they are not comparable)
*/
int compare(Object firstObject, Object secondObject) throws EvaluationException;
/**
* Return true if the comparator can compare these two objects
* @param firstObject the first object
* @param secondObject the second object
* @return true if the comparator can compare these objects
* @return {@code true} if the comparator can compare these objects
*/
boolean canCompare(Object firstObject, Object secondObject);
/**
* Compare two given objects.
* @param firstObject the first object
* @param secondObject the second object
* @return 0 if they are equal, <0 if the first is smaller than the second,
* or >0 if the first is larger than the second
* @throws EvaluationException if a problem occurs during comparison
* (or if they are not comparable in the first place)
*/
int compare(Object firstObject, Object secondObject) throws EvaluationException;
}

View File

@ -35,22 +35,23 @@ public interface TypeConverter {
* to the desired target type.
* @param sourceType a type descriptor that describes the source type
* @param targetType a type descriptor that describes the requested result type
* @return true if that conversion can be performed
* @return {@code true} if that conversion can be performed
*/
boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
/**
* Convert (may coerce) a value from one type to another, for example from a boolean
* to a string. The typeDescriptor parameter enables support for typed collections -
* if the caller really wishes they can have a List&lt;Integer&gt; for example, rather
* than simply a List.
* Convert (or coerce) a value from one type to another, for example from a
* {@code boolean} to a {@code String}.
* <p>The {@link TypeDescriptor} parameters enable support for typed collections:
* A caller may prefer a {@code List&lt;Integer&gt;}, for example, rather than
* simply any {@code List}.
* @param value the value to be converted
* @param sourceType a type descriptor that supplies extra information about the
* source object
* @param targetType a type descriptor that supplies extra information about the
* requested result type
* @return the converted value
* @throws EvaluationException if conversion is not possible
* @throws EvaluationException if conversion failed or is not possible to begin with
*/
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,9 +17,12 @@
package org.springframework.expression;
/**
* Implementors of this interface are expected to be able to locate types. They may use custom classloaders
* or the and deal with common package prefixes (java.lang, etc) however they wish. See
* {@link org.springframework.expression.spel.support.StandardTypeLocator} for an example implementation.
* Implementers of this interface are expected to be able to locate types.
* They may use a custom {@link ClassLoader} and/or deal with common
* package prefixes (e.g. {@code java.lang}) however they wish.
*
* <p>See {@link org.springframework.expression.spel.support.StandardTypeLocator}
* for an example implementation.
*
* @author Andy Clement
* @since 3.0
@ -27,11 +30,12 @@ package org.springframework.expression;
public interface TypeLocator {
/**
* Find a type by name. The name may or may not be fully qualified (eg. String or java.lang.String)
* @param typename the type to be located
* @return the class object representing that type
* @throws EvaluationException if there is a problem finding it
* Find a type by name. The name may or may not be fully qualified
* (e.g. {@code String} or {@code java.lang.String}).
* @param typeName the type to be located
* @return the {@code Class} object representing that type
* @throws EvaluationException if there is a problem finding the type
*/
Class<?> findType(String typename) throws EvaluationException;
Class<?> findType(String typeName) throws EvaluationException;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,7 +21,8 @@ import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
/**
* A simple basic TypeComparator implementation. It supports comparison of numbers and types implementing Comparable.
* A simple basic {@link TypeComparator} implementation.
* It supports comparison of Numbers and types implementing Comparable.
*
* @author Andy Clement
* @author Juergen Hoeller
@ -29,49 +30,6 @@ import org.springframework.expression.spel.SpelMessage;
*/
public class StandardTypeComparator implements TypeComparator {
@SuppressWarnings("unchecked")
public int compare(Object left, Object right) throws SpelEvaluationException {
// If one is null, check if the other is
if (left == null) {
return right == null ? 0 : -1;
} else if (right == null) {
return 1; // left cannot be null
}
// Basic number comparisons
if (left instanceof Number && right instanceof Number) {
Number leftNumber = (Number) left;
Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
double d1 = leftNumber.doubleValue();
double d2 = rightNumber.doubleValue();
return Double.compare(d1,d2);
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
float f1 = leftNumber.floatValue();
float f2 = rightNumber.floatValue();
return Float.compare(f1,f2);
} else if (leftNumber instanceof Long || rightNumber instanceof Long) {
Long l1 = leftNumber.longValue();
Long l2 = rightNumber.longValue();
return l1.compareTo(l2);
} else {
Integer i1 = leftNumber.intValue();
Integer i2 = rightNumber.intValue();
return i1.compareTo(i2);
}
}
try {
if (left instanceof Comparable) {
return ((Comparable) left).compareTo(right);
}
} catch (ClassCastException cce) {
throw new SpelEvaluationException(cce, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
public boolean canCompare(Object left, Object right) {
if (left == null || right == null) {
return true;
@ -85,4 +43,56 @@ public class StandardTypeComparator implements TypeComparator {
return false;
}
@SuppressWarnings("unchecked")
public int compare(Object left, Object right) throws SpelEvaluationException {
// If one is null, check if the other is
if (left == null) {
return (right == null ? 0 : -1);
}
else if (right == null) {
return 1; // left cannot be null at this point
}
// Basic number comparisons
if (left instanceof Number && right instanceof Number) {
Number leftNumber = (Number) left;
Number rightNumber = (Number) right;
if (leftNumber instanceof Double || rightNumber instanceof Double) {
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
}
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
}
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
// Don't call Long.compare here - only available on JDK 1.7+
return compare(leftNumber.longValue(), rightNumber.longValue());
}
else {
// Don't call Integer.compare here - only available on JDK 1.7+
return compare(leftNumber.intValue(), rightNumber.intValue());
}
}
try {
if (left instanceof Comparable) {
return ((Comparable) left).compareTo(right);
}
}
catch (ClassCastException ex) {
throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
private static int compare(int x, int y) {
return (x < y ? -1 : (x > y ? 1 : 0));
}
private static int compare(long x, long y) {
return (x < y ? -1 : (x > y ? 1 : 0));
}
}

View File

@ -18,7 +18,6 @@ package org.springframework.expression.spel.support;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.expression.TypeConverter;
@ -27,8 +26,8 @@ import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.Assert;
/**
* Default implementation of the {@link TypeConverter} interface, delegating to a core
* Spring {@link ConversionService}.
* Default implementation of the {@link TypeConverter} interface,
* delegating to a core Spring {@link ConversionService}.
*
* @author Juergen Hoeller
* @author Andy Clement
@ -42,6 +41,9 @@ public class StandardTypeConverter implements TypeConverter {
private final ConversionService conversionService;
/**
* Create a StandardTypeConverter for the default ConversionService.
*/
public StandardTypeConverter() {
synchronized (this) {
if (defaultConversionService == null) {
@ -51,6 +53,10 @@ public class StandardTypeConverter implements TypeConverter {
this.conversionService = defaultConversionService;
}
/**
* Create a StandardTypeConverter for the given ConversionService.
* @param conversionService the ConversionService to delegate to
*/
public StandardTypeConverter(ConversionService conversionService) {
Assert.notNull(conversionService, "ConversionService must not be null");
this.conversionService = conversionService;
@ -65,10 +71,6 @@ public class StandardTypeConverter implements TypeConverter {
try {
return this.conversionService.convert(value, sourceType, targetType);
}
catch (ConverterNotFoundException ex) {
throw new SpelEvaluationException(
ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
}
catch (ConversionException ex) {
throw new SpelEvaluationException(
ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,8 +16,8 @@
package org.springframework.expression.spel.support;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.springframework.expression.EvaluationException;
@ -27,8 +27,9 @@ import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.ClassUtils;
/**
* A default implementation of a TypeLocator that uses the context classloader (or any classloader set upon it). It
* supports 'well known' packages so if a type cannot be found it will try the registered imports to locate it.
* A simple implementation of {@link TypeLocator} that uses the context ClassLoader
* (or any ClassLoader set upon it). It supports 'well-known' packages: So if a
* type cannot be found, it will try the registered imports to locate it.
*
* @author Andy Clement
* @author Juergen Hoeller
@ -36,49 +37,30 @@ import org.springframework.util.ClassUtils;
*/
public class StandardTypeLocator implements TypeLocator {
private ClassLoader loader;
private final ClassLoader classLoader;
private final List<String> knownPackagePrefixes = new ArrayList<String>();
private final List<String> knownPackagePrefixes = new LinkedList<String>();
/**
* Create a StandardTypeLocator for the default ClassLoader
* (typically, the thread context ClassLoader).
*/
public StandardTypeLocator() {
this(ClassUtils.getDefaultClassLoader());
}
public StandardTypeLocator(ClassLoader loader) {
this.loader = loader;
// Similar to when writing Java, it only knows about java.lang by default
/**
* Create a StandardTypeLocator for the given ClassLoader.
* @param classLoader the ClassLoader to delegate to
*/
public StandardTypeLocator(ClassLoader classLoader) {
this.classLoader = classLoader;
// Similar to when writing regular Java code, it only knows about java.lang by default
registerImport("java.lang");
}
/**
* Find a (possibly unqualified) type reference - first using the typename as is, then trying any registered
* prefixes if the typename cannot be found.
* @param typename the type to locate
* @return the class object for the type
* @throws EvaluationException if the type cannot be found
*/
public Class<?> findType(String typename) throws EvaluationException {
String nameToLookup = typename;
try {
return this.loader.loadClass(nameToLookup);
}
catch (ClassNotFoundException ey) {
// try any registered prefixes before giving up
}
for (String prefix : this.knownPackagePrefixes) {
try {
nameToLookup = new StringBuilder().append(prefix).append(".").append(typename).toString();
return this.loader.loadClass(nameToLookup);
}
catch (ClassNotFoundException ex) {
// might be a different prefix
}
}
throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typename);
}
/**
* Register a new import prefix that will be used when searching for unqualified types.
* Expected format is something like "java.lang".
@ -88,16 +70,48 @@ public class StandardTypeLocator implements TypeLocator {
this.knownPackagePrefixes.add(prefix);
}
/**
* Remove that specified prefix from this locator's list of imports.
* @param prefix the prefix to remove
*/
public void removeImport(String prefix) {
this.knownPackagePrefixes.remove(prefix);
}
/**
* Return a list of all the import prefixes registered with this StandardTypeLocator.
* @return list of registered import prefixes
* @return a list of registered import prefixes
*/
public List<String> getImportPrefixes() {
return Collections.unmodifiableList(this.knownPackagePrefixes);
}
public void removeImport(String prefix) {
this.knownPackagePrefixes.remove(prefix);
/**
* Find a (possibly unqualified) type reference - first using the type name as-is,
* then trying any registered prefixes if the type name cannot be found.
* @param typeName the type to locate
* @return the class object for the type
* @throws EvaluationException if the type cannot be found
*/
public Class<?> findType(String typeName) throws EvaluationException {
String nameToLookup = typeName;
try {
return this.classLoader.loadClass(nameToLookup);
}
catch (ClassNotFoundException ey) {
// try any registered prefixes before giving up
}
for (String prefix : this.knownPackagePrefixes) {
try {
nameToLookup = prefix + "." + typeName;
return this.classLoader.loadClass(nameToLookup);
}
catch (ClassNotFoundException ex) {
// might be a different prefix
}
}
throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
}
}

View File

@ -27,7 +27,6 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@ -75,9 +74,8 @@ public class SpelReproTests extends ExpressionTestCase {
}
@Test
@Ignore
public void testSWF1086() {
evaluate("printDouble(T(java.math.BigDecimal).valueOf(14.35))", "anullc", String.class);
evaluate("printDouble(T(java.math.BigDecimal).valueOf(14.35))", "14.35", String.class);
}
@Test
@ -101,7 +99,8 @@ public class SpelReproTests extends ExpressionTestCase {
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
expr.getValue();
fail("Should have failed to find a method to which it could pass null");
} catch (EvaluationException see) {
}
catch (EvaluationException see) {
// success
}
eContext.setTypeLocator(new MyTypeLocator());
@ -133,31 +132,44 @@ public class SpelReproTests extends ExpressionTestCase {
static class MyTypeLocator extends StandardTypeLocator {
@Override
public Class<?> findType(String typename) throws EvaluationException {
if (typename.equals("Spr5899Class")) {
public Class<?> findType(String typeName) throws EvaluationException {
if (typeName.equals("Spr5899Class")) {
return Spr5899Class.class;
}
if (typename.equals("Outer")) {
if (typeName.equals("Outer")) {
return Outer.class;
}
return super.findType(typename);
return super.findType(typeName);
}
}
static class Spr5899Class {
public Spr5899Class() {}
public Spr5899Class(Integer i) { }
public Spr5899Class(Integer i, String... s) { }
public Integer tryToInvokeWithNull(Integer value) { return value; }
public Integer tryToInvokeWithNull2(int i) { return new Integer(i); }
public Spr5899Class() {
}
public Spr5899Class(Integer i) {
}
public Spr5899Class(Integer i, String... s) {
}
public Integer tryToInvokeWithNull(Integer value) {
return value;
}
public Integer tryToInvokeWithNull2(int i) {
return new Integer(i);
}
public String tryToInvokeWithNull3(Integer value, String... strings) {
StringBuilder sb = new StringBuilder();
for (int i=0;i<strings.length;i++) {
if (strings[i]==null) {
for (String string : strings) {
if (string == null) {
sb.append("null");
} else {
sb.append(strings[i]);
}
else {
sb.append(string);
}
}
return sb.toString();
@ -182,11 +194,15 @@ public class SpelReproTests extends ExpressionTestCase {
}
static class Outer {
static class Inner {
public Inner() {}
public static int run() {
return 12;
}
public int run2() {
return 13;
}
@ -276,7 +292,6 @@ public class SpelReproTests extends ExpressionTestCase {
public Class<?>[] getSpecificTargetClasses() {
return new Class[] {Map.class};
}
}
@Test
@ -323,13 +338,15 @@ public class SpelReproTests extends ExpressionTestCase {
try {
propertyAccessor.read(context, null, "abc");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
}
catch (AccessException ae) {
// success
}
try {
propertyAccessor.write(context, null, "abc","foo");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
}
catch (AccessException ae) {
// success
}
}
@ -347,20 +364,25 @@ public class SpelReproTests extends ExpressionTestCase {
}
static class Foo {
public ResourceSummary resource = new ResourceSummary();
}
static class ResourceSummary {
private final Resource resource;
ResourceSummary() {
this.resource = new Resource();
}
private final Resource resource;
public Resource getResource() {
return resource;
}
}
static class Resource {
public String getServer() {
return "abc";
}
@ -498,6 +520,7 @@ public class SpelReproTests extends ExpressionTestCase {
}
static class XX {
public Map<String,String> m;
public String floo ="bar";
@ -512,7 +535,9 @@ public class SpelReproTests extends ExpressionTestCase {
static class Goo {
public static Goo instance = new Goo();
public String bar = "key";
public String value = null;
public String wibble = "wobble";
@ -524,7 +549,6 @@ public class SpelReproTests extends ExpressionTestCase {
public void setKey(String s) {
value = s;
}
}
static class Holder {
@ -553,23 +577,27 @@ public class SpelReproTests extends ExpressionTestCase {
try {
parser.parseExpression(expression,context);
fail("Should have failed");
} catch (Exception e) {
if (!e.getMessage().equals(expectedMessage)) {
e.printStackTrace();
}
assertEquals(expectedMessage,e.getMessage());
catch (Exception ex) {
if (!ex.getMessage().equals(expectedMessage)) {
ex.printStackTrace();
}
assertEquals(expectedMessage, ex.getMessage());
}
}
private static final ParserContext DOLLARSQUARE_TEMPLATE_PARSER_CONTEXT = new ParserContext() {
@Override
public String getExpressionPrefix() {
return "$[";
}
@Override
public String getExpressionSuffix() {
return "]";
}
@Override
public boolean isTemplate() {
return true;
@ -577,12 +605,14 @@ public class SpelReproTests extends ExpressionTestCase {
};
static class Foo2 {
public void execute(String str){
System.out.println("Value: " + str);
}
}
static class Message{
private String payload;
public String getPayload() {
@ -605,7 +635,8 @@ public class SpelReproTests extends ExpressionTestCase {
try {
expr = new SpelExpressionParser().parseRaw("@foo");
assertEquals("custard",expr.getValue(eContext,String.class));
} catch (SpelEvaluationException see) {
}
catch (SpelEvaluationException see) {
assertEquals(SpelMessage.NO_BEAN_RESOLVER_REGISTERED,see.getMessageCode());
assertEquals("foo",see.getInserts()[0]);
}
@ -624,7 +655,8 @@ public class SpelReproTests extends ExpressionTestCase {
expr = new SpelExpressionParser().parseRaw("@goo");
try {
assertEquals(null,expr.getValue(eContext,String.class));
} catch (SpelEvaluationException see) {
}
catch (SpelEvaluationException see) {
assertEquals(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,see.getMessageCode());
assertEquals("goo",see.getInserts()[0]);
assertTrue(see.getCause() instanceof AccessException);
@ -639,19 +671,23 @@ public class SpelReproTests extends ExpressionTestCase {
try {
expr = new SpelExpressionParser().parseRaw("@378");
assertEquals("trouble",expr.getValue(eContext,String.class));
} catch (SpelParseException spe) {
}
catch (SpelParseException spe) {
assertEquals(SpelMessage.INVALID_BEAN_REFERENCE,spe.getMessageCode());
}
}
static class MyBeanResolver implements BeanResolver {
@Override
public Object resolve(EvaluationContext context, String beanname) throws AccessException {
if (beanname.equals("foo")) {
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
if (beanName.equals("foo")) {
return "custard";
} else if (beanname.equals("foo.bar")) {
}
else if (beanName.equals("foo.bar")) {
return "trouble";
} else if (beanname.equals("goo")) {
}
else if (beanName.equals("goo")) {
throw new AccessException("DONT ASK ME ABOUT GOO");
}
return null;
@ -678,7 +714,8 @@ public class SpelReproTests extends ExpressionTestCase {
expr = new SpelExpressionParser().parseRaw("(?'abc':'default')");
expr.getValue(eContext);
fail();
} catch (SpelEvaluationException see ) {
}
catch (SpelEvaluationException see ) {
assertEquals(SpelMessage.TYPE_CONVERSION_ERROR,see.getMessageCode());
}
expr = new SpelExpressionParser().parseRaw("(false?'abc':null)");
@ -689,7 +726,8 @@ public class SpelReproTests extends ExpressionTestCase {
expr = new SpelExpressionParser().parseRaw("(='default')");
expr.getValue(eContext);
fail();
} catch (SpelEvaluationException see ) {
}
catch (SpelEvaluationException see ) {
assertEquals(SpelMessage.SETVALUE_NOT_SUPPORTED,see.getMessageCode());
}
}
@ -913,7 +951,6 @@ public class SpelReproTests extends ExpressionTestCase {
}
@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
@ -960,12 +997,11 @@ public class SpelReproTests extends ExpressionTestCase {
args.add(TypeDescriptor.forObject(23f));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext,ru,"bar",args);
me.execute(emptyEvalContext, ru, 12,23f);
}
public class ReflectionUtil<T extends Number> {
public Object methodToCall(T param) {
System.out.println(param+" "+param.getClass());
return "Object methodToCall(T param)";
@ -1040,11 +1076,11 @@ public class SpelReproTests extends ExpressionTestCase {
m.put("NE","xyz");
}
}
StandardEvaluationContext ctx = new StandardEvaluationContext(new Reserver());
SpelExpressionParser parser = new SpelExpressionParser();
String ex = "getReserver().NE";
SpelExpression exp = null;
exp = parser.parseRaw(ex);
SpelExpression exp = parser.parseRaw(ex);
String value = (String) exp.getValue(ctx);
assertEquals("abc", value);
@ -1074,7 +1110,8 @@ public class SpelReproTests extends ExpressionTestCase {
public void testReservedWordProperties_9862() throws Exception {
StandardEvaluationContext ctx = new StandardEvaluationContext();
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
SpelExpression expression = parser.parseRaw(
"T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
Object value = expression.getValue(ctx);
assertEquals(value, Reserver.CONST);
}
@ -1107,47 +1144,53 @@ public class SpelReproTests extends ExpressionTestCase {
}
class TestPropertyAccessor implements PropertyAccessor {
private String mapName;
public TestPropertyAccessor(String mapName) {
this.mapName = mapName;
}
@SuppressWarnings("unchecked")
public Map<String, String> getMap(Object target) {
try {
Field f = target.getClass().getDeclaredField(mapName);
return (Map<String,String>) f.get(target);
} catch (Exception e) {
}
catch (Exception ex) {
}
return null;
}
@Override
public boolean canRead(EvaluationContext context, Object target, String name)
throws AccessException {
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
return getMap(target).containsKey(name);
}
@Override
public boolean canWrite(EvaluationContext context, Object target, String name)
throws AccessException {
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
return getMap(target).containsKey(name);
}
@Override
public Class<?>[] getSpecificTargetClasses() {
return new Class[] {ContextObject.class};
}
@Override
public TypedValue read(EvaluationContext context, Object target, String name)
throws AccessException {
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
return new TypedValue(getMap(target).get(name));
}
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue)
throws AccessException {
getMap(target).put(name, (String) newValue);
}
}
class ContextObject {
public Map<String, String> firstContext = new HashMap<String, String>();
public Map<String, String> secondContext = new HashMap<String, String>();
public Map<String, String> thirdContext = new HashMap<String, String>();
@ -1168,6 +1211,7 @@ public class SpelReproTests extends ExpressionTestCase {
fourthContext.put("shouldBeFourth", "fourth");
}
public Map<String, String> getFirstContext() {return firstContext;}
public Map<String, String> getSecondContext() {return secondContext;}
public Map<String, String> getThirdContext() {return thirdContext;}
@ -1180,7 +1224,6 @@ public class SpelReproTests extends ExpressionTestCase {
*/
@Test
public void testCustomStaticFunctions_SPR9038() {
try {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>();
@ -1188,10 +1231,10 @@ public class SpelReproTests extends ExpressionTestCase {
@Override
protected Method[] getMethods(Class<?> type) {
try {
return new Method[] {
Integer.class.getDeclaredMethod("parseInt", new Class[] {
String.class, Integer.TYPE }) };
} catch (NoSuchMethodException e1) {
return new Method[] {Integer.class.getDeclaredMethod("parseInt",
new Class[] {String.class, Integer.TYPE })};
}
catch (NoSuchMethodException ex) {
return new Method[0];
}
}
@ -1203,10 +1246,6 @@ public class SpelReproTests extends ExpressionTestCase {
Integer result = expression.getValue(context, "", Integer.class);
assertEquals("Equal assertion failed: ", -255, result.intValue());
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: "+e.toString());
}
}
@Test
@ -1239,8 +1278,7 @@ public class SpelReproTests extends ExpressionTestCase {
}
@Test
public void SPR_9486_floatFunctionResolverTest() {
try {
public void SPR_9486_floatFunctionResolverTest() throws Exception {
Number expectedResult = Math.abs(-10.2f);
ExpressionParser parser = new SpelExpressionParser();
SPR_9486_FunctionsClass testObject = new SPR_9486_FunctionsClass();
@ -1249,13 +1287,10 @@ public class SpelReproTests extends ExpressionTestCase {
org.springframework.expression.Expression expression = parser.parseExpression("abs(-10.2f)");
Number result = expression.getValue(context, testObject, Number.class);
assertEquals("Equal assertion failed for SPR_9486_floatFunctionResolverTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatFunctionResolverTest");
}
}
class SPR_9486_FunctionsClass {
public int abs(int value) {
return Math.abs(value);
}
@ -1267,394 +1302,264 @@ public class SpelReproTests extends ExpressionTestCase {
@Test
public void SPR_9486_addFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f + 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f + 10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_addFloatWithDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_addFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_addFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f + 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f + 10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_addFloatWithFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_addFloatWithFloatTest");
}
}
@Test
public void SPR_9486_subtractFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f - 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f - 10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_subtractFloatWithDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_subtractFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_subtractFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f - 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f - 10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_subtractFloatWithFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_subtractFloatWithFloatTest");
}
}
@Test
public void SPR_9486_multiplyFloatWithDoubleTest() {
try {
Number expectedNumber = 10.21f * 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f * 10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for float multiplied by double Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_multiplyFloatWithDoubleTest");
}
}
@Test
public void SPR_9486_multiplyFloatWithFloatTest() {
try {
Number expectedNumber = 10.21f * 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f * 10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for float multiply by another float Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_multiplyFloatWithFloatTest");
}
}
@Test
public void SPR_9486_floatDivideByFloatTest() {
try {
Number expectedNumber = -10.21f/-10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f / -10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for float divide Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatDivideByFloatTest");
}
}
@Test
public void SPR_9486_floatDivideByDoubleTest() {
try {
Number expectedNumber = -10.21f/-10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f / -10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for float divide Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatDivideByDoubleTest");
}
}
@Test
public void SPR_9486_floatEqFloatUnaryMinusTest() {
try {
Boolean expectedResult = -10.21f == -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f == -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatEqFloatUnaryMinusTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatEqFloatUnaryMinusTest");
}
}
@Test
public void SPR_9486_floatEqDoubleUnaryMinusTest() {
try {
Boolean expectedResult = -10.21f == -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f == -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatEqDoubleUnaryMinusTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatEqDoubleUnaryMinusTest");
}
}
@Test
public void SPR_9486_floatEqFloatTest() {
try {
Boolean expectedResult = 10.215f == 10.2109f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f == 10.2109f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatEqFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatEqFloatTest");
}
}
@Test
public void SPR_9486_floatEqDoubleTest() {
try {
Boolean expectedResult = 10.215f == 10.2109;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f == 10.2109");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatEqDoubleTest() Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatEqDoubleTest()");
}
}
@Test
public void SPR_9486_floatNotEqFloatTest() {
try {
Boolean expectedResult = 10.215f != 10.2109f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f != 10.2109f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatEqFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatEqFloatTest");
}
}
@Test
public void SPR_9486_floatNotEqDoubleTest() {
try {
Boolean expectedResult = 10.215f != 10.2109;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.215f != 10.2109");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatNotEqDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatNotEqDoubleTest");
}
}
@Test
public void SPR_9486_floatLessThanFloatTest() {
try {
Boolean expectedNumber = -10.21f < -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f < -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatLessThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatLessThanFloatTest()");
}
}
@Test
public void SPR_9486_floatLessThanDoubleTest() {
try {
Boolean expectedNumber = -10.21f < -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f < -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatLessThanDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatLessThanDoubleTest()");
}
}
@Test
public void SPR_9486_floatLessThanOrEqualFloatTest() {
try {
Boolean expectedNumber = -10.21f <= -10.22f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f <= -10.22f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatLessThanOrEqualFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatLessThanOrEqualFloatTest");
}
}
@Test
public void SPR_9486_floatLessThanOrEqualDoubleTest() {
try {
Boolean expectedNumber = -10.21f <= -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f <= -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatLessThanOrEqualDoubleTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatLessThanOrEqualDoubleTest");
}
}
@Test
public void SPR_9486_floatGreaterThanFloatTest() {
try {
Boolean expectedNumber = -10.21f > -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f > -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanDoubleTest() {
try {
Boolean expectedResult = -10.21f > -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f > -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanOrEqualFloatTest() {
try {
Boolean expectedNumber = -10.21f >= -10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f >= -10.2f");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanFloatTest Test: ", expectedNumber, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatGreaterThanEqualDoubleTest() {
try {
Boolean expectedResult = -10.21f >= -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("-10.21f >= -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals("Equal assertion failed for SPR_9486_floatGreaterThanDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatGreaterThanTest");
}
}
@Test
public void SPR_9486_floatModulusFloatTest() {
try {
Number expectedResult = 10.21f % 10.2f;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f % 10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_floatModulusFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatModulusFloatTest");
}
}
@Test
public void SPR_9486_floatModulusDoubleTest() {
try {
Number expectedResult = 10.21f % 10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f % 10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_floatModulusDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatModulusDoubleTest");
}
}
@Test
public void SPR_9486_floatPowerFloatTest() {
try {
Number expectedResult = Math.pow(10.21f, -10.2f);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f ^ -10.2f");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_floatPowerFloatTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatPowerFloatTest");
}
}
@Test
public void SPR_9486_floatPowerDoubleTest() {
try {
Number expectedResult = Math.pow(10.21f, 10.2);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("10.21f ^ 10.2");
Number result = expression.getValue(context, null, Number.class);
assertEquals("Equal assertion failed for SPR_9486_floatPowerDoubleTest Test: ", expectedResult, result);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed - SPR_9486_floatPowerDoubleTest");
}
}
@Test
@ -1877,6 +1782,7 @@ public class SpelReproTests extends ExpressionTestCase {
public T getProperty();
}
private static class GenericImplementation implements GenericInterface<Integer> {
@Override
@ -1885,6 +1791,7 @@ public class SpelReproTests extends ExpressionTestCase {
}
}
static class PackagePrivateClassWithGetter {
public Integer getProperty() {
@ -1892,26 +1799,29 @@ public class SpelReproTests extends ExpressionTestCase {
}
}
public static class OnlyBridgeMethod extends PackagePrivateClassWithGetter {
public static class OnlyBridgeMethod extends PackagePrivateClassWithGetter {
}
public static interface StaticFinal {
public static final String VALUE = "interfaceValue";
}
public abstract static class AbstractStaticFinal implements StaticFinal {
}
public static class StaticFinalImpl1 extends AbstractStaticFinal implements StaticFinal {
}
public static class StaticFinalImpl2 extends AbstractStaticFinal {
}
/**
* The Class TestObject.
*/
public static class SPR10486 {
private String name = "name";
@ -1923,14 +1833,14 @@ public class SpelReproTests extends ExpressionTestCase {
public void setName(String name) {
this.name = name;
}
}
static class SPR11142 {
public String isSomething() {
return "";
}
}
}
}