Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@963 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
18 changed files with 862 additions and 406 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,241 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2009 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. |
||||||
|
* 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.springframework.expression.spel; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.springframework.core.convert.TypeDescriptor; |
||||||
|
import org.springframework.expression.EvaluationContext; |
||||||
|
import org.springframework.expression.EvaluationException; |
||||||
|
import org.springframework.expression.Operation; |
||||||
|
import org.springframework.expression.TypedValue; |
||||||
|
import org.springframework.expression.spel.support.StandardEvaluationContext; |
||||||
|
import org.springframework.expression.spel.testresources.Inventor; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for the expression state object - some features are not yet exploited in the language (eg nested scopes) |
||||||
|
* |
||||||
|
* @author Andy Clement |
||||||
|
*/ |
||||||
|
public class ExpressionStateTests extends ExpressionTestCase { |
||||||
|
|
||||||
|
public void testConstruction() { |
||||||
|
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); |
||||||
|
ExpressionState state = new ExpressionState(context); |
||||||
|
assertEquals(context,state.getEvaluationContext()); |
||||||
|
} |
||||||
|
|
||||||
|
// Local variables are in variable scopes which come and go during evaluation. Normal variables are
|
||||||
|
// accessible through the evaluation context
|
||||||
|
|
||||||
|
|
||||||
|
public void testLocalVariables() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
|
||||||
|
Object value = state.lookupLocalVariable("foo"); |
||||||
|
assertNull(value); |
||||||
|
|
||||||
|
state.setLocalVariable("foo",34); |
||||||
|
value = state.lookupLocalVariable("foo"); |
||||||
|
assertEquals(34,value); |
||||||
|
|
||||||
|
state.setLocalVariable("foo",null); |
||||||
|
value = state.lookupLocalVariable("foo"); |
||||||
|
assertEquals(null,value); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void testVariables() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
TypedValue typedValue = state.lookupVariable("foo"); |
||||||
|
assertEquals(TypedValue.NULL_TYPED_VALUE,typedValue); |
||||||
|
|
||||||
|
state.setVariable("foo",34); |
||||||
|
typedValue = state.lookupVariable("foo"); |
||||||
|
assertEquals(34,typedValue.getValue()); |
||||||
|
assertEquals(Integer.class,typedValue.getTypeDescriptor().getType()); |
||||||
|
|
||||||
|
state.setVariable("foo","abc"); |
||||||
|
typedValue = state.lookupVariable("foo"); |
||||||
|
assertEquals("abc",typedValue.getValue()); |
||||||
|
assertEquals(String.class,typedValue.getTypeDescriptor().getType()); |
||||||
|
} |
||||||
|
|
||||||
|
public void testNoVariableInteference() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
TypedValue typedValue = state.lookupVariable("foo"); |
||||||
|
assertEquals(TypedValue.NULL_TYPED_VALUE,typedValue); |
||||||
|
|
||||||
|
state.setLocalVariable("foo",34); |
||||||
|
typedValue = state.lookupVariable("foo"); |
||||||
|
assertEquals(TypedValue.NULL_TYPED_VALUE,typedValue); |
||||||
|
|
||||||
|
state.setVariable("goo","hello"); |
||||||
|
assertNull(state.lookupLocalVariable("goo")); |
||||||
|
} |
||||||
|
|
||||||
|
public void testLocalVariableNestedScopes() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertEquals(null,state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.setLocalVariable("foo",12); |
||||||
|
assertEquals(12,state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.enterScope(null); |
||||||
|
assertEquals(12,state.lookupLocalVariable("foo")); // found in upper scope
|
||||||
|
|
||||||
|
state.setLocalVariable("foo","abc"); |
||||||
|
assertEquals("abc",state.lookupLocalVariable("foo")); // found in nested scope
|
||||||
|
|
||||||
|
state.exitScope(); |
||||||
|
assertEquals(12,state.lookupLocalVariable("foo")); // found in nested scope
|
||||||
|
} |
||||||
|
|
||||||
|
public void testRootContextObject() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertEquals(Inventor.class,state.getRootContextObject().getValue().getClass()); |
||||||
|
|
||||||
|
state.getEvaluationContext().setRootObject(null); |
||||||
|
assertEquals(null,state.getRootContextObject().getValue()); |
||||||
|
|
||||||
|
state = new ExpressionState(new StandardEvaluationContext()); |
||||||
|
assertEquals(TypedValue.NULL_TYPED_VALUE,state.getRootContextObject()); |
||||||
|
} |
||||||
|
|
||||||
|
public void testActiveContextObject() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue()); |
||||||
|
|
||||||
|
state.pushActiveContextObject(new TypedValue(34)); |
||||||
|
assertEquals(34,state.getActiveContextObject().getValue()); |
||||||
|
|
||||||
|
state.pushActiveContextObject(new TypedValue("hello")); |
||||||
|
assertEquals("hello",state.getActiveContextObject().getValue()); |
||||||
|
|
||||||
|
state.popActiveContextObject(); |
||||||
|
assertEquals(34,state.getActiveContextObject().getValue()); |
||||||
|
|
||||||
|
state.popActiveContextObject(); |
||||||
|
assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue()); |
||||||
|
|
||||||
|
state = new ExpressionState(new StandardEvaluationContext()); |
||||||
|
assertEquals(TypedValue.NULL_TYPED_VALUE,state.getActiveContextObject()); |
||||||
|
} |
||||||
|
|
||||||
|
public void testPopulatedNestedScopes() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertNull(state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.enterScope("foo",34); |
||||||
|
assertEquals(34,state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.enterScope(null); |
||||||
|
state.setLocalVariable("foo",12); |
||||||
|
assertEquals(12,state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.exitScope(); |
||||||
|
assertEquals(34,state.lookupLocalVariable("foo")); |
||||||
|
|
||||||
|
state.exitScope(); |
||||||
|
assertNull(state.lookupLocalVariable("goo")); |
||||||
|
} |
||||||
|
|
||||||
|
public void testPopulatedNestedScopesMap() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertNull(state.lookupLocalVariable("foo")); |
||||||
|
assertNull(state.lookupLocalVariable("goo")); |
||||||
|
|
||||||
|
Map<String,Object> m = new HashMap<String,Object>(); |
||||||
|
m.put("foo",34); |
||||||
|
m.put("goo","abc"); |
||||||
|
|
||||||
|
state.enterScope(m); |
||||||
|
assertEquals(34,state.lookupLocalVariable("foo")); |
||||||
|
assertEquals("abc",state.lookupLocalVariable("goo")); |
||||||
|
|
||||||
|
state.enterScope(null); |
||||||
|
state.setLocalVariable("foo",12); |
||||||
|
assertEquals(12,state.lookupLocalVariable("foo")); |
||||||
|
assertEquals("abc",state.lookupLocalVariable("goo")); |
||||||
|
|
||||||
|
state.exitScope(); |
||||||
|
state.exitScope(); |
||||||
|
assertNull(state.lookupLocalVariable("foo")); |
||||||
|
assertNull(state.lookupLocalVariable("goo")); |
||||||
|
} |
||||||
|
|
||||||
|
public void testOperators() throws Exception { |
||||||
|
ExpressionState state = getState(); |
||||||
|
try { |
||||||
|
state.operate(Operation.ADD,1,2); |
||||||
|
fail("should have failed"); |
||||||
|
} catch (EvaluationException ee) { |
||||||
|
SpelException sEx = (SpelException)ee; |
||||||
|
assertEquals(SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageUnformatted()); |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
state.operate(Operation.ADD,null,null); |
||||||
|
fail("should have failed"); |
||||||
|
} catch (EvaluationException ee) { |
||||||
|
SpelException sEx = (SpelException)ee; |
||||||
|
assertEquals(SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageUnformatted()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void testComparator() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertEquals(state.getEvaluationContext().getTypeComparator(),state.getTypeComparator()); |
||||||
|
} |
||||||
|
|
||||||
|
public void testTypeLocator() throws EvaluationException { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertNotNull(state.getEvaluationContext().getTypeLocator()); |
||||||
|
assertEquals(Integer.class,state.findType("java.lang.Integer")); |
||||||
|
try { |
||||||
|
state.findType("someMadeUpName"); |
||||||
|
fail("Should have failed to find it"); |
||||||
|
} catch (EvaluationException ee) { |
||||||
|
SpelException sEx = (SpelException)ee; |
||||||
|
assertEquals(SpelMessages.TYPE_NOT_FOUND,sEx.getMessageUnformatted()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void testTypeConversion() throws EvaluationException { |
||||||
|
ExpressionState state = getState(); |
||||||
|
String s = (String)state.convertValue(34,TypeDescriptor.valueOf(String.class)); |
||||||
|
assertEquals("34",s); |
||||||
|
|
||||||
|
s = (String)state.convertValue(new TypedValue(34),TypeDescriptor.valueOf(String.class)); |
||||||
|
assertEquals("34",s); |
||||||
|
} |
||||||
|
|
||||||
|
public void testPropertyAccessors() { |
||||||
|
ExpressionState state = getState(); |
||||||
|
assertEquals(state.getEvaluationContext().getPropertyAccessors(),state.getPropertyAccessors()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return a new ExpressionState |
||||||
|
*/ |
||||||
|
private ExpressionState getState() { |
||||||
|
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); |
||||||
|
ExpressionState state = new ExpressionState(context); |
||||||
|
return state; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,395 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2009 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. |
||||||
|
* 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.springframework.expression.spel; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
|
||||||
|
import org.springframework.expression.spel.support.StandardEvaluationContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* These are tests for language features that are not yet considered 'live'. Either missing implementation, or documentation. |
||||||
|
* |
||||||
|
* Where implementation is missing the tests are commented out. |
||||||
|
* |
||||||
|
* @author Andy Clement |
||||||
|
*/ |
||||||
|
public class InProgressTests extends ExpressionTestCase { |
||||||
|
|
||||||
|
// Constructor invocation
|
||||||
|
|
||||||
|
// public void testPrimitiveTypeArrayConstructors() {
|
||||||
|
// evaluate("new int[]{1,2,3,4}.count()", 4, Integer.class);
|
||||||
|
// evaluate("new boolean[]{true,false,true}.count()", 3, Integer.class);
|
||||||
|
// evaluate("new char[]{'a','b','c'}.count()", 3, Integer.class);
|
||||||
|
// evaluate("new long[]{1,2,3,4,5}.count()", 5, Integer.class);
|
||||||
|
// evaluate("new short[]{2,3,4,5,6}.count()", 5, Integer.class);
|
||||||
|
// evaluate("new double[]{1d,2d,3d,4d}.count()", 4, Integer.class);
|
||||||
|
// evaluate("new float[]{1f,2f,3f,4f}.count()", 4, Integer.class);
|
||||||
|
// evaluate("new byte[]{1,2,3,4}.count()", 4, Integer.class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void testPrimitiveTypeArrayConstructorsElements() {
|
||||||
|
// evaluate("new int[]{1,2,3,4}[0]", 1, Integer.class);
|
||||||
|
// evaluate("new boolean[]{true,false,true}[0]", true, Boolean.class);
|
||||||
|
// evaluate("new char[]{'a','b','c'}[0]", 'a', Character.class);
|
||||||
|
// evaluate("new long[]{1,2,3,4,5}[0]", 1L, Long.class);
|
||||||
|
// evaluate("new short[]{2,3,4,5,6}[0]", (short) 2, Short.class);
|
||||||
|
// evaluate("new double[]{1d,2d,3d,4d}[0]", (double) 1, Double.class);
|
||||||
|
// evaluate("new float[]{1f,2f,3f,4f}[0]", (float) 1, Float.class);
|
||||||
|
// evaluate("new byte[]{1,2,3,4}[0]", (byte) 1, Byte.class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void testErrorCases() {
|
||||||
|
// evaluateAndCheckError("new char[7]{'a','c','d','e'}", SpelMessages.INITIALIZER_LENGTH_INCORRECT);
|
||||||
|
// evaluateAndCheckError("new char[3]{'a','c','d','e'}", SpelMessages.INITIALIZER_LENGTH_INCORRECT);
|
||||||
|
// evaluateAndCheckError("new char[2]{'hello','world'}", SpelMessages.TYPE_CONVERSION_ERROR);
|
||||||
|
// evaluateAndCheckError("new String('a','c','d')", SpelMessages.CONSTRUCTOR_NOT_FOUND);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void testTypeArrayConstructors() {
|
||||||
|
// evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
|
||||||
|
// evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessages.METHOD_NOT_FOUND, 30, "size()",
|
||||||
|
// "java.lang.String[]");
|
||||||
|
// evaluateAndCheckError("new String[]{'a','b','c','d'}.juggernaut", SpelMessages.PROPERTY_OR_FIELD_NOT_FOUND, 30,
|
||||||
|
// "juggernaut", "java.lang.String[]");
|
||||||
|
// evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void testMultiDimensionalArrays() {
|
||||||
|
// evaluate(
|
||||||
|
// "new String[3,4]",
|
||||||
|
// "[Ljava.lang.String;[3]{java.lang.String[4]{null,null,null,null},java.lang.String[4]{null,null,null,null},java.lang.String[4]{null,null,null,null}}"
|
||||||
|
// ,
|
||||||
|
// new String[3][4].getClass());
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// evaluate("new String(new char[]{'h','e','l','l','o'})", "hello", String.class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// public void testRelOperatorsIn01() {
|
||||||
|
// evaluate("3 in {1,2,3,4,5}", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void testRelOperatorsIn02() {
|
||||||
|
// evaluate("name in {null, \"Nikola Tesla\"}", "true", Boolean.class);
|
||||||
|
// evaluate("name in {null, \"Anonymous\"}", "false", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
public void testRelOperatorsBetween01() { |
||||||
|
evaluate("1 between listOneFive", "true", Boolean.class); |
||||||
|
// evaluate("1 between {1, 5}", "true", Boolean.class); // no inline list building at the moment
|
||||||
|
} |
||||||
|
//
|
||||||
|
// public void testRelOperatorsBetween02() {
|
||||||
|
// evaluate("'efg' between {'abc', 'xyz'}", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
public void testRelOperatorsBetweenErrors01() { |
||||||
|
evaluateAndCheckError("1 between T(String)", SpelMessages.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 12); |
||||||
|
} |
||||||
|
//
|
||||||
|
// public void testRelOperatorsBetweenErrors02() {
|
||||||
|
// evaluateAndCheckError("'abc' between {5,7}", SpelMessages.NOT_COMPARABLE, 6);
|
||||||
|
// }
|
||||||
|
public void testRelOperatorsBetweenErrors03() { |
||||||
|
evaluateAndCheckError("1 between listOfNumbersUpToTen", SpelMessages.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 10); |
||||||
|
} |
||||||
|
|
||||||
|
// Lambda calculations
|
||||||
|
|
||||||
|
//
|
||||||
|
// public void testLambda02() {
|
||||||
|
// evaluate("(#max={|x,y| $x > $y ? $x : $y };true)", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaMax() {
|
||||||
|
// evaluate("(#max = {|x,y| $x > $y ? $x : $y }; #max(5,25))", "25", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaFactorial01() {
|
||||||
|
// evaluate("(#fact = {|n| $n <= 1 ? 1 : $n * #fact($n-1) }; #fact(5))", "120", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaFactorial02() {
|
||||||
|
// evaluate("(#fact = {|n| $n <= 1 ? 1 : #fact($n-1) * $n }; #fact(5))", "120", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaAlphabet01() {
|
||||||
|
// evaluate("(#alpha = {|l,s| $l>'z'?$s:#alpha($l+1,$s+$l)};#alphabet={||#alpha('a','')}; #alphabet())",
|
||||||
|
// "abcdefghijklmnopqrstuvwxyz", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaAlphabet02() {
|
||||||
|
// evaluate("(#alphabet = {|l,s| $l>'z'?$s:#alphabet($l+1,$s+$l)};#alphabet('a',''))",
|
||||||
|
// "abcdefghijklmnopqrstuvwxyz", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testLambdaDelegation01() {
|
||||||
|
// evaluate("(#sqrt={|n| T(Math).sqrt($n)};#delegate={|f,n| $f($n)};#delegate(#sqrt,4))", "2.0", Double.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testVariableReferences() {
|
||||||
|
// evaluate("(#answer=42;#answer)", "42", Integer.class, true);
|
||||||
|
// evaluate("($answer=42;$answer)", "42", Integer.class, true);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public void testRelOperatorsIs02() {
|
||||||
|
// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testRelOperatorsIs03() {
|
||||||
|
// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// inline list creation
|
||||||
|
// public void testInlineListCreation01() {
|
||||||
|
// evaluate("{1, 2, 3, 4, 5}", "[1, 2, 3, 4, 5]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testInlineListCreation02() {
|
||||||
|
// evaluate("{'abc', 'xyz'}", "[abc, xyz]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // inline map creation
|
||||||
|
// public void testInlineMapCreation01() {
|
||||||
|
// evaluate("#{'key1':'Value 1', 'today':'Monday'}", "{key1=Value 1, today=Monday}", HashMap.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testInlineMapCreation02() {
|
||||||
|
// evaluate("#{1:'January', 2:'February', 3:'March'}.size()", 3, Integer.class);// "{2=February, 1=January,
|
||||||
|
// // 3=March}", HashMap.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testInlineMapCreation03() {
|
||||||
|
// evaluate("#{'key1':'Value 1', 'today':'Monday'}['key1']", "Value 1", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testInlineMapCreation04() {
|
||||||
|
// evaluate("#{1:'January', 2:'February', 3:'March'}[3]", "March", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testInlineMapCreation05() {
|
||||||
|
// evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // set construction
|
||||||
|
// public void testSetConstruction01() {
|
||||||
|
// evaluate("new HashSet().addAll({'a','b','c'})", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testConstructorInvocation02() {
|
||||||
|
// evaluate("new String[3]", "java.lang.String[3]{null,null,null}", String[].class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testConstructorInvocation03() {
|
||||||
|
// evaluateAndCheckError("new String[]", SpelMessages.NO_SIZE_OR_INITIALIZER_FOR_ARRAY_CONSTRUCTION, 4);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testConstructorInvocation04() {
|
||||||
|
// evaluateAndCheckError("new String[3]{'abc',3,'def'}", SpelMessages.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
|
||||||
|
// }
|
||||||
|
// // array construction
|
||||||
|
// public void testArrayConstruction01() {
|
||||||
|
// evaluate("new int[] {1, 2, 3, 4, 5}", "int[5]{1,2,3,4,5}", int[].class);
|
||||||
|
// }
|
||||||
|
// public void testArrayConstruction02() {
|
||||||
|
// evaluate("new String[] {'abc', 'xyz'}", "java.lang.String[2]{abc,xyz}", String[].class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// collection processors
|
||||||
|
// from spring.net: count,sum,max,min,average,sort,orderBy,distinct,nonNull
|
||||||
|
// public void testProcessorsCount01() {
|
||||||
|
// evaluate("new String[] {'abc','def','xyz'}.count()", "3", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsCount02() {
|
||||||
|
// evaluate("new int[] {1,2,3}.count()", "3", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsMax01() {
|
||||||
|
// evaluate("new int[] {1,2,3}.max()", "3", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsMin01() {
|
||||||
|
// evaluate("new int[] {1,2,3}.min()", "1", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsKeys01() {
|
||||||
|
// evaluate("#{1:'January', 2:'February', 3:'March'}.keySet().sort()", "[1, 2, 3]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsValues01() {
|
||||||
|
// evaluate("#{1:'January', 2:'February', 3:'March'}.values().sort()", "[February, January, March]",
|
||||||
|
// ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsAverage01() {
|
||||||
|
// evaluate("new int[] {1,2,3}.average()", "2", Integer.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProcessorsSort01() {
|
||||||
|
// evaluate("new int[] {3,2,1}.sort()", "int[3]{1,2,3}", int[].class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testCollectionProcessorsNonNull01() {
|
||||||
|
// evaluate("{'a','b',null,'d',null}.nonnull()", "[a, b, d]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testCollectionProcessorsDistinct01() {
|
||||||
|
// evaluate("{'a','b','a','d','e'}.distinct()", "[a, b, d, e]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProjection03() {
|
||||||
|
// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#this>5}",
|
||||||
|
// "[false, false, false, false, false, true, true, true, true, true]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testProjection04() {
|
||||||
|
// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{$index>5?'y':'n'}", "[n, n, n, n, n, n, y, y, y, y]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
// Bean references
|
||||||
|
// public void testReferences01() {
|
||||||
|
// evaluate("@(apple).name", "Apple", String.class, true);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testReferences02() {
|
||||||
|
// evaluate("@(fruits:banana).name", "Banana", String.class, true);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testReferences03() {
|
||||||
|
// evaluate("@(a.b.c)", null, null);
|
||||||
|
// } // null - no context, a.b.c treated as name
|
||||||
|
//
|
||||||
|
// public void testReferences05() {
|
||||||
|
// evaluate("@(a/b/c:orange).name", "Orange", String.class, true);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testReferences06() {
|
||||||
|
// evaluate("@(apple).color.getRGB() == T(java.awt.Color).green.getRGB()", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testReferences07() {
|
||||||
|
// evaluate("@(apple).color.getRGB().equals(T(java.awt.Color).green.getRGB())", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// value is not public, it is accessed through getRGB()
|
||||||
|
// public void testStaticRef01() {
|
||||||
|
// evaluate("T(Color).green.value!=0", "true", Boolean.class);
|
||||||
|
// }
|
||||||
|
// Indexer
|
||||||
|
// public void testCutProcessor01() {
|
||||||
|
// evaluate("{1,2,3,4,5}.cut(1,3)", "[2, 3, 4]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testCutProcessor02() {
|
||||||
|
// evaluate("{1,2,3,4,5}.cut(3,1)", "[4, 3, 2]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
// Ternary operator
|
||||||
|
// public void testTernaryOperator01() {
|
||||||
|
// evaluate("{1}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is odd", String.class);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void testTernaryOperator02() {
|
||||||
|
// evaluate("{2}.#isEven(#this[0]) == 'y'?'it is even':'it is odd'", "it is even", String.class);
|
||||||
|
// }
|
||||||
|
// public void testSelectionUsingIndex() {
|
||||||
|
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }", "[7, 8, 9, 10]", ArrayList.class);
|
||||||
|
// }
|
||||||
|
//public void testSelection01() {
|
||||||
|
// inline list creation not supported:
|
||||||
|
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'}", "[2, 4, 6, 8, 10]", ArrayList.class);
|
||||||
|
//}
|
||||||
|
|
||||||
|
// projection and selection
|
||||||
|
public void testProjection01() { |
||||||
|
evaluate("listOfNumbersUpToTen.!{#this<5?'y':'n'}","[y, y, y, y, n, n, n, n, n, n]",ArrayList.class); |
||||||
|
// inline list creation not supported at the moment
|
||||||
|
// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}", "[n, y, n, y, n, y, n, y, n, y]", ArrayList.class);
|
||||||
|
} |
||||||
|
|
||||||
|
public void testProjection02() { |
||||||
|
// inline map creation not supported at the moment
|
||||||
|
// evaluate("#{'a':'y','b':'n','c':'y'}.!{value=='y'?key:null}.nonnull().sort()", "[a, c]", ArrayList.class);
|
||||||
|
evaluate("mapOfNumbersUpToTen.!{key>5?value:null}", "[null, null, null, null, null, six, seven, eight, nine, ten]", ArrayList.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testProjection05() { |
||||||
|
evaluateAndCheckError("'abc'.!{true}", SpelMessages.PROJECTION_NOT_SUPPORTED_ON_TYPE); |
||||||
|
} |
||||||
|
|
||||||
|
public void testProjection06() throws Exception { |
||||||
|
SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'.!{true}"); |
||||||
|
assertEquals("'abc'.!{true}",expr.toStringAST()); |
||||||
|
assertFalse(expr.isWritable(new StandardEvaluationContext())); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelection02() { |
||||||
|
evaluate("testMap.keySet().?{#this matches '.*o.*'}", "[monday]", ArrayList.class); |
||||||
|
evaluate("testMap.keySet().?{#this matches '.*r.*'}.contains('saturday')", "true", Boolean.class); |
||||||
|
evaluate("testMap.keySet().?{#this matches '.*r.*'}.size()", "3", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionError_NonBooleanSelectionCriteria() { |
||||||
|
evaluateAndCheckError("listOfNumbersUpToTen.?{'nonboolean'}", |
||||||
|
SpelMessages.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelection03() { |
||||||
|
evaluate("mapOfNumbersUpToTen.?{key>5}.size()", "5", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelection04() { |
||||||
|
evaluateAndCheckError("mapOfNumbersUpToTen.?{'hello'}.size()",SpelMessages.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionFirst01() { |
||||||
|
evaluate("listOfNumbersUpToTen.^{#isEven(#this) == 'y'}", "2", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionFirst02() { |
||||||
|
evaluate("mapOfNumbersUpToTen.^{key>5}.size()", "1", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionLast01() { |
||||||
|
evaluate("listOfNumbersUpToTen.${#isEven(#this) == 'y'}", "10", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionLast02() { |
||||||
|
evaluate("mapOfNumbersUpToTen.${key>5}.size()", "1", Integer.class); |
||||||
|
} |
||||||
|
|
||||||
|
public void testSelectionAST() throws Exception { |
||||||
|
SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'.^{true}"); |
||||||
|
assertEquals("'abc'.^{true}",expr.toStringAST()); |
||||||
|
assertFalse(expr.isWritable(new StandardEvaluationContext())); |
||||||
|
expr = (SpelExpression)parser.parseExpression("'abc'.?{true}"); |
||||||
|
assertEquals("'abc'.?{true}",expr.toStringAST()); |
||||||
|
assertFalse(expr.isWritable(new StandardEvaluationContext())); |
||||||
|
expr = (SpelExpression)parser.parseExpression("'abc'.${true}"); |
||||||
|
assertEquals("'abc'.${true}",expr.toStringAST()); |
||||||
|
assertFalse(expr.isWritable(new StandardEvaluationContext())); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,55 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2004-2008 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. |
||||||
|
* 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.springframework.expression.spel; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import junit.framework.TestCase; |
||||||
|
|
||||||
|
import org.springframework.expression.EvaluationException; |
||||||
|
import org.springframework.expression.spel.support.StandardTypeLocator; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for type comparison |
||||||
|
* |
||||||
|
* @author Andy Clement |
||||||
|
*/ |
||||||
|
public class StandardTypeLocatorTests extends TestCase { |
||||||
|
|
||||||
|
public void testPrimitives() throws EvaluationException { |
||||||
|
StandardTypeLocator locator = new StandardTypeLocator(); |
||||||
|
assertEquals(Integer.class,locator.findType("java.lang.Integer")); |
||||||
|
assertEquals(String.class,locator.findType("java.lang.String")); |
||||||
|
|
||||||
|
List<String> prefixes = locator.getImportPrefixes(); |
||||||
|
assertEquals(2,prefixes.size()); |
||||||
|
assertTrue(prefixes.contains("java.lang")); |
||||||
|
assertTrue(prefixes.contains("java.util")); |
||||||
|
|
||||||
|
assertEquals(Boolean.class,locator.findType("Boolean")); |
||||||
|
assertEquals(java.util.List.class,locator.findType("List")); |
||||||
|
|
||||||
|
try { |
||||||
|
locator.findType("URL"); |
||||||
|
fail("Should have failed"); |
||||||
|
} catch (EvaluationException ee) { |
||||||
|
SpelException sEx = (SpelException)ee; |
||||||
|
assertEquals(SpelMessages.TYPE_NOT_FOUND,sEx.getMessageUnformatted()); |
||||||
|
} |
||||||
|
locator.registerImport("java.net"); |
||||||
|
assertEquals(java.net.URL.class,locator.findType("URL")); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue