map access tests added
This commit is contained in:
@@ -48,6 +48,7 @@ public class AllTests {
|
||||
suite.addTestSuite(TemplateExpressionParsingTests.class);
|
||||
suite.addTestSuite(ExpressionLanguageScenarioTests.class);
|
||||
suite.addTestSuite(ScenariosForSpringSecurity.class);
|
||||
suite.addTestSuite(MapAccessTests.class);
|
||||
suite.addTestSuite(SpelUtilitiesTests.class);
|
||||
suite.addTestSuite(LiteralExpressionTests.class);
|
||||
suite.addTestSuite(CompositeStringExpressionTests.class);
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
|
||||
/**
|
||||
* Tests the evaluation of real expressions in a real context.
|
||||
*
|
||||
@@ -457,4 +456,5 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
evaluateAndAskForReturnType("3*4+5", (short) 17, Short.class);
|
||||
evaluateAndAskForReturnType("3*4+5", "17", String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.Map;
|
||||
|
||||
import org.springframework.expression.AccessException;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.PropertyAccessor;
|
||||
import org.springframework.expression.spel.standard.StandardEvaluationContext;
|
||||
|
||||
/**
|
||||
* Testing variations on map access.
|
||||
*
|
||||
* @author Andy Clement
|
||||
*/
|
||||
public class MapAccessTests extends ExpressionTestCase {
|
||||
|
||||
static class MapAccessor implements PropertyAccessor {
|
||||
|
||||
public boolean canRead(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return (((Map) target).containsKey(name));
|
||||
}
|
||||
|
||||
public Object read(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return ((Map) target).get(name);
|
||||
}
|
||||
|
||||
public boolean canWrite(EvaluationContext context, Object target, Object name) throws AccessException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void write(EvaluationContext context, Object target, Object name, Object newValue)
|
||||
throws AccessException {
|
||||
((Map) target).put(name, newValue);
|
||||
}
|
||||
|
||||
public Class<?>[] getSpecificTargetClasses() {
|
||||
return new Class[] { Map.class };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testSimpleMapAccess01() {
|
||||
evaluate("testMap.get('monday')", "montag", String.class);
|
||||
}
|
||||
|
||||
public void testMapAccessThroughIndexer() {
|
||||
evaluate("testMap['monday']", "montag", String.class);
|
||||
}
|
||||
|
||||
public void testCustomMapAccessor() throws Exception {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
|
||||
ctx.addPropertyAccessor(new MapAccessor());
|
||||
|
||||
Expression expr = parser.parseExpression("testMap.monday");
|
||||
Object value = expr.getValue(ctx, String.class);
|
||||
assertEquals("montag", value);
|
||||
}
|
||||
|
||||
public void testVariableMapAccess() throws Exception {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
|
||||
ctx.setVariable("day", "saturday");
|
||||
|
||||
Expression expr = parser.parseExpression("testMap[#day]");
|
||||
Object value = expr.getValue(ctx, String.class);
|
||||
assertEquals("samstag", value);
|
||||
}
|
||||
|
||||
// public void testMapAccess04() {
|
||||
// evaluate("testMap[monday]", "montag", String.class);
|
||||
// }
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.springframework.expression.spel.testresources;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Inventor {
|
||||
@@ -11,15 +13,24 @@ public class Inventor {
|
||||
private String nationality;
|
||||
private String[] inventions;
|
||||
public String randomField;
|
||||
public Map testMap;
|
||||
|
||||
public Inventor(String name, Date birthdate, String nationality) {
|
||||
this.name = name;
|
||||
this.birthdate = birthdate;
|
||||
this.nationality = nationality;
|
||||
testMap = new HashMap();
|
||||
testMap.put("monday", "montag");
|
||||
testMap.put("tuesday", "dienstag");
|
||||
testMap.put("wednesday", "mittwoch");
|
||||
testMap.put("thursday", "donnerstag");
|
||||
testMap.put("friday", "freitag");
|
||||
testMap.put("saturday", "samstag");
|
||||
testMap.put("sunday", "sonntag");
|
||||
}
|
||||
|
||||
public void setPlaceOfBirth(PlaceOfBirth placeOfBirth2) {
|
||||
this.placeOfBirth = placeOfBirth2;
|
||||
placeOfBirth = placeOfBirth2;
|
||||
}
|
||||
|
||||
public void setInventions(String[] inventions) {
|
||||
@@ -45,17 +56,20 @@ public class Inventor {
|
||||
public String joinThreeStrings(String a, String b, String c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
public int aVarargsMethod(String...strings ) {
|
||||
if (strings==null) return 0;
|
||||
|
||||
public int aVarargsMethod(String... strings) {
|
||||
if (strings == null)
|
||||
return 0;
|
||||
return strings.length;
|
||||
}
|
||||
public int aVarargsMethod2(int i, String...strings ) {
|
||||
if (strings==null) return i;
|
||||
return strings.length+i;
|
||||
|
||||
public int aVarargsMethod2(int i, String... strings) {
|
||||
if (strings == null)
|
||||
return i;
|
||||
return strings.length + i;
|
||||
}
|
||||
|
||||
public Inventor(String...strings ) {
|
||||
|
||||
public Inventor(String... strings) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user