Removed unnecessary code. Increased test coverage from 70>75% - still some way to go
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.expression.spel;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
/**
|
||||
* Tests the evaluation of real expressions in a real context.
|
||||
@@ -208,6 +209,22 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
public void testConstructorInvocation05() {
|
||||
evaluate("new java.lang.String('foobar')", "foobar", String.class);
|
||||
}
|
||||
|
||||
public void testConstructorInvocation06() throws Exception {
|
||||
// repeated evaluation to drive use of cached executor
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("new String('wibble')");
|
||||
String newString = expr.getValue(String.class);
|
||||
assertEquals("wibble",newString);
|
||||
newString = expr.getValue(String.class);
|
||||
assertEquals("wibble",newString);
|
||||
|
||||
// not writable
|
||||
assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
|
||||
// ast
|
||||
assertEquals("new String('wibble')",expr.toStringAST());
|
||||
}
|
||||
|
||||
|
||||
// array construction
|
||||
// public void testArrayConstruction01() {
|
||||
@@ -275,14 +292,18 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
// }
|
||||
|
||||
// projection and selection
|
||||
// public void testProjection01() {
|
||||
// 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() {
|
||||
// evaluate("#{'a':'y','b':'n','c':'y'}.!{value=='y'?key:null}.nonnull().sort()", "[a, c]", ArrayList.class);
|
||||
// }
|
||||
//
|
||||
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 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);
|
||||
@@ -291,11 +312,21 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
// 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);
|
||||
// }
|
||||
|
||||
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);
|
||||
|
||||
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 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);
|
||||
//}
|
||||
|
||||
public void testSelection02() {
|
||||
evaluate("testMap.keySet().?{#this matches '.*o.*'}", "[monday]", ArrayList.class);
|
||||
@@ -303,23 +334,40 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
evaluate("testMap.keySet().?{#this matches '.*r.*'}.size()", "3", Integer.class);
|
||||
}
|
||||
|
||||
// public void testSelectionError_NonBooleanSelectionCriteria() {
|
||||
// evaluateAndCheckError("{1,2,3,4,5,6,7,8,9,10}.?{'nonboolean'}",
|
||||
// SpelMessages.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
// }
|
||||
public void testSelectionError_NonBooleanSelectionCriteria() {
|
||||
evaluateAndCheckError("listOfNumbersUpToTen.?{'nonboolean'}",
|
||||
SpelMessages.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
}
|
||||
|
||||
// public void testSelectionUsingIndex() {
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }", "[7, 8, 9, 10]", ArrayList.class);
|
||||
// }
|
||||
|
||||
public void testSelection03() {
|
||||
evaluate("mapOfNumbersUpToTen.?{key>5}.size()", "5", Integer.class);
|
||||
}
|
||||
|
||||
// public void testSelectionFirst01() {
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.^{#isEven(#this) == 'y'}", "2", Integer.class);
|
||||
// }
|
||||
//
|
||||
// public void testSelectionLast01() {
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.${#isEven(#this) == 'y'}", "10", Integer.class);
|
||||
// }
|
||||
public void testSelectionFirst01() {
|
||||
evaluate("listOfNumbersUpToTen.^{#isEven(#this) == 'y'}", "2", Integer.class);
|
||||
}
|
||||
|
||||
public void testSelectionLast01() {
|
||||
evaluate("listOfNumbersUpToTen.${#isEven(#this) == 'y'}", "10", 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()));
|
||||
}
|
||||
|
||||
|
||||
// assignment
|
||||
public void testAssignmentToVariables01() {
|
||||
evaluate("#var1='value1'", "value1", String.class);
|
||||
@@ -451,6 +499,16 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
public void testTypeReferences01() {
|
||||
evaluate("T(java.lang.String)", "class java.lang.String", Class.class);
|
||||
}
|
||||
|
||||
public void testTypeReferencesAndQualifiedIdentifierCaching() throws Exception {
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("T(java.lang.String)");
|
||||
assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
assertEquals("T(java.lang.String)",expr.toStringAST());
|
||||
assertEquals(String.class,expr.getValue(Class.class));
|
||||
// use cached QualifiedIdentifier:
|
||||
assertEquals("T(java.lang.String)",expr.toStringAST());
|
||||
assertEquals(String.class,expr.getValue(Class.class));
|
||||
}
|
||||
|
||||
public void testTypeReferencesPrimitive() {
|
||||
evaluate("T(int)", "int", Class.class);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 org.springframework.expression.spel.ast.FormatHelper;
|
||||
|
||||
/**
|
||||
* Tests for any helper code.
|
||||
*
|
||||
* @author Andy Clement
|
||||
*/
|
||||
public class HelperTests extends ExpressionTestCase {
|
||||
|
||||
public void testFormatHelperForClassName() {
|
||||
assertEquals("java.lang.String",FormatHelper.formatClassNameForMessage(String.class));
|
||||
assertEquals("java.lang.String[]",FormatHelper.formatClassNameForMessage(new String[1].getClass()));
|
||||
assertEquals("int[]",FormatHelper.formatClassNameForMessage(new int[1].getClass()));
|
||||
assertEquals("int[][]",FormatHelper.formatClassNameForMessage(new int[1][2].getClass()));
|
||||
assertEquals("null",FormatHelper.formatClassNameForMessage(null));
|
||||
}
|
||||
|
||||
public void testFormatHelperForMethod() {
|
||||
assertEquals("foo(java.lang.String)",FormatHelper.formatMethodForMessage("foo", String.class));
|
||||
assertEquals("goo(java.lang.String,int[])",FormatHelper.formatMethodForMessage("goo", String.class,new int[1].getClass()));
|
||||
assertEquals("boo()",FormatHelper.formatMethodForMessage("boo"));
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
/**
|
||||
* Tests the evaluation of basic literals: boolean, integer, hex integer, long, real, null, date
|
||||
*
|
||||
@@ -134,4 +136,13 @@ public class LiteralTests extends ExpressionTestCase {
|
||||
evaluate("new Integer(37).byteValue()", (byte) 37, Byte.class); // calling byteValue() on Integer.class
|
||||
evaluateAndAskForReturnType("new Integer(37)", (byte) 37, Byte.class); // relying on registered type converters
|
||||
}
|
||||
|
||||
public void testNotWritable() throws Exception {
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("37");
|
||||
assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
expr = (SpelExpression)parser.parseExpression("37L");
|
||||
assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
expr = (SpelExpression)parser.parseExpression("true");
|
||||
assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,8 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
evaluate("3 == 5", false, Boolean.class);
|
||||
evaluate("5 == 3", false, Boolean.class);
|
||||
evaluate("6 == 6", true, Boolean.class);
|
||||
evaluate("3.0f == 5.0f", false, Boolean.class);
|
||||
evaluate("3.0f == 3.0f", true, Boolean.class);
|
||||
evaluate("'abc' == null", false, Boolean.class);
|
||||
}
|
||||
|
||||
@@ -70,6 +72,8 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
evaluate("3 != 5", true, Boolean.class);
|
||||
evaluate("5 != 3", true, Boolean.class);
|
||||
evaluate("6 != 6", false, Boolean.class);
|
||||
evaluate("3.0f != 5.0f", true, Boolean.class);
|
||||
evaluate("3.0f != 3.0f", false, Boolean.class);
|
||||
}
|
||||
|
||||
public void testGreaterThanOrEqual() {
|
||||
@@ -122,23 +126,36 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
}
|
||||
|
||||
public void testPlus() throws Exception {
|
||||
evaluate("'a' + 2", "c", String.class);
|
||||
evaluate("7 + 2", "9", Integer.class);
|
||||
evaluate("3.0f + 5.0f", 8.0d, Double.class);
|
||||
evaluate("3.0d + 5.0d", 8.0d, Double.class);
|
||||
|
||||
evaluateAndCheckError("'ab' + 2", SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
evaluate("2+'a' ", "c", String.class);
|
||||
evaluateAndCheckError("2+'a' ", SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
evaluateAndCheckError("2+'ab'",SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
|
||||
// AST:
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("+3");
|
||||
assertEquals("+3",expr.toStringAST());
|
||||
expr = (SpelExpression)parser.parseExpression("2+3");
|
||||
assertEquals("(2 + 3)",expr.toStringAST());
|
||||
|
||||
// use as a unary operator
|
||||
evaluate("+5d",5d,Double.class);
|
||||
evaluate("+5L",5L,Long.class);
|
||||
evaluate("+5",5,Integer.class);
|
||||
evaluateAndCheckError("+'abc'",SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
|
||||
// string concatenation
|
||||
evaluate("'abc'+'def'","abcdef",String.class);
|
||||
|
||||
//
|
||||
evaluate("5 + new Integer('37')",42,Integer.class);
|
||||
}
|
||||
|
||||
public void testMinus() throws Exception {
|
||||
evaluate("'c' - 2", "a", String.class);
|
||||
evaluate("3.0f - 5.0f", -2.0d, Double.class);
|
||||
evaluateAndCheckError("'ab' - 2", SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
evaluateAndCheckError("2-'ab'",SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("-3");
|
||||
@@ -156,11 +173,14 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
evaluate("3%2",1,Integer.class);
|
||||
evaluate("3L%2L",1L,Long.class);
|
||||
evaluate("3.0d%2.0d",1d,Double.class);
|
||||
evaluate("5.0f % 3.1f", 1.9d, Double.class);
|
||||
evaluateAndCheckError("'abc'%'def'",SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
}
|
||||
|
||||
public void testDivide() {
|
||||
evaluate("3.0f / 5.0f", 0.6d, Double.class);
|
||||
evaluate("4L/2L",2L,Long.class);
|
||||
evaluateAndCheckError("'abc'/'def'",SpelMessages.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
|
||||
}
|
||||
|
||||
public void testMathOperatorDivide_ConvertToDouble() {
|
||||
@@ -182,20 +202,8 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
evaluate("3.0d / 5.0d", 0.6d, Double.class);
|
||||
evaluate("6.0d % 3.5d", 2.5d, Double.class);
|
||||
}
|
||||
|
||||
public void testFloats() {
|
||||
evaluate("3.0f == 5.0f", false, Boolean.class);
|
||||
evaluate("3.0f == 3.0f", true, Boolean.class);
|
||||
evaluate("3.0f != 5.0f", true, Boolean.class);
|
||||
evaluate("3.0f != 3.0f", false, Boolean.class);
|
||||
evaluate("3.0f + 5.0f", 8.0d, Double.class);
|
||||
evaluate("3.0f - 5.0f", -2.0d, Double.class);
|
||||
evaluate("3.0f * 5.0f", 15.0d, Double.class);
|
||||
evaluate("3.0f / 5.0f", 0.6d, Double.class);
|
||||
evaluate("5.0f % 3.1f", 1.9d, Double.class);
|
||||
}
|
||||
|
||||
public void testOperatorStrings() throws Exception {
|
||||
public void testOperatorNames() throws Exception {
|
||||
Operator node = getOperatorNode((SpelExpression)parser.parseExpression("1==3"));
|
||||
assertEquals("==",node.getOperatorName());
|
||||
|
||||
@@ -266,6 +274,8 @@ public class OperatorTests extends ExpressionTestCase {
|
||||
evaluate("3L - 50L", -47L, Long.class);
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
private Operator getOperatorNode(SpelExpression e) {
|
||||
SpelNode node = e.getAST();
|
||||
return (Operator)findNode(node,Operator.class);
|
||||
|
||||
@@ -29,6 +29,7 @@ public class ParserErrorMessagesTests extends ExpressionTestCase {
|
||||
// will not fit into an int, needs L suffix
|
||||
parseAndCheckError("0xCAFEBABE", SpelMessages.NOT_AN_INTEGER);
|
||||
evaluate("0xCAFEBABEL", 0xCAFEBABEL, Long.class);
|
||||
parseAndCheckError("0xCAFEBABECAFEBABEL", SpelMessages.NOT_A_LONG);
|
||||
}
|
||||
|
||||
public void testBrokenExpression02() {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.expression.spel;
|
||||
import org.springframework.expression.spel.antlr.SpelAntlrExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
|
||||
/**
|
||||
* Tests the evaluation of expressions that access variables and functions (lambda/java).
|
||||
*
|
||||
@@ -26,10 +27,15 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
*/
|
||||
public class VariableAndFunctionTests extends ExpressionTestCase {
|
||||
|
||||
public void testVariableAccess() {
|
||||
public void testVariableAccess01() {
|
||||
evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE);
|
||||
evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE);
|
||||
}
|
||||
|
||||
public void testVariableAccess_WellKnownVariables() {
|
||||
evaluate("#this.getName()","Nikola Tesla",String.class);
|
||||
evaluate("#root.getName()","Nikola Tesla",String.class);
|
||||
}
|
||||
|
||||
public void testFunctionAccess01() {
|
||||
evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class);
|
||||
@@ -71,7 +77,7 @@ public class VariableAndFunctionTests extends ExpressionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this method is used by the test above
|
||||
public void nonStatic() {
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
package org.springframework.expression.spel.testresources;
|
||||
|
||||
///CLOVER:OFF
|
||||
public class Company {
|
||||
String address;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ package org.springframework.expression.spel.testresources;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
///CLOVER:OFF
|
||||
public class Fruit {
|
||||
public String name; // accessible as property field
|
||||
public Color color; // accessible as property through getter/setter
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
///CLOVER:OFF
|
||||
@SuppressWarnings("unused")
|
||||
public class Inventor {
|
||||
private String name;
|
||||
@@ -26,6 +27,8 @@ public class Inventor {
|
||||
public List<Integer> listOfInteger = new ArrayList<Integer>();
|
||||
public List<Boolean> booleanList = new ArrayList<Boolean>();
|
||||
public Map<String,Boolean> mapOfStringToBoolean = new HashMap<String,Boolean>();
|
||||
public Map<Integer,String> mapOfNumbersUpToTen = new HashMap<Integer,String>();
|
||||
public List<Integer> listOfNumbersUpToTen = new ArrayList<Integer>();
|
||||
|
||||
public Inventor(String name, Date birthdate, String nationality) {
|
||||
this.name = name;
|
||||
@@ -42,6 +45,26 @@ public class Inventor {
|
||||
testMap.put("sunday", "sonntag");
|
||||
booleanList.add(false);
|
||||
booleanList.add(false);
|
||||
listOfNumbersUpToTen.add(1);
|
||||
listOfNumbersUpToTen.add(2);
|
||||
listOfNumbersUpToTen.add(3);
|
||||
listOfNumbersUpToTen.add(4);
|
||||
listOfNumbersUpToTen.add(5);
|
||||
listOfNumbersUpToTen.add(6);
|
||||
listOfNumbersUpToTen.add(7);
|
||||
listOfNumbersUpToTen.add(8);
|
||||
listOfNumbersUpToTen.add(9);
|
||||
listOfNumbersUpToTen.add(10);
|
||||
mapOfNumbersUpToTen.put(1,"one");
|
||||
mapOfNumbersUpToTen.put(2,"two");
|
||||
mapOfNumbersUpToTen.put(3,"three");
|
||||
mapOfNumbersUpToTen.put(4,"four");
|
||||
mapOfNumbersUpToTen.put(5,"five");
|
||||
mapOfNumbersUpToTen.put(6,"six");
|
||||
mapOfNumbersUpToTen.put(7,"seven");
|
||||
mapOfNumbersUpToTen.put(8,"eight");
|
||||
mapOfNumbersUpToTen.put(9,"nine");
|
||||
mapOfNumbersUpToTen.put(10,"ten");
|
||||
}
|
||||
|
||||
public void setPlaceOfBirth(PlaceOfBirth placeOfBirth2) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.springframework.expression.spel.testresources;
|
||||
|
||||
|
||||
///CLOVER:OFF
|
||||
public class Person {
|
||||
private String privateName;
|
||||
Company company;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.expression.spel.testresources;
|
||||
|
||||
///CLOVER:OFF
|
||||
public class PlaceOfBirth {
|
||||
private String city;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user