Convert CRLF (dos) to LF (unix)

Prior to this change, roughly 5% (~300 out of 6000+) of files under the
source tree had CRLF line endings as opposed to the majority which have
LF endings.

This change normalizes these files to LF for consistency going forward.

Command used:

$ git ls-files | xargs file | grep CRLF | cut -d":" -f1 | xargs dos2unix

Issue: SPR-5608
This commit is contained in:
Chris Beams
2011-12-21 14:42:42 +01:00
parent e1b645368a
commit 88913f2b23
293 changed files with 31091 additions and 31091 deletions

View File

@@ -1,58 +1,58 @@
/*
* 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.io.PrintStream;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpression;
/**
* Utilities for working with Spring Expressions.
*
* @author Andy Clement
*/
public class SpelUtilities {
/**
* Output an indented representation of the expression syntax tree to the specified output stream.
* @param printStream the output stream to print into
* @param expression the expression to be displayed
*/
public static void printAbstractSyntaxTree(PrintStream printStream, Expression expression) {
printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST start");
printAST(printStream, ((SpelExpression) expression).getAST(), "");
printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST end");
}
/*
* Helper method for printing the AST with indentation
*/
private static void printAST(PrintStream out, SpelNode t, String indent) {
if (t != null) {
StringBuilder sb = new StringBuilder();
sb.append(indent).append(t.getClass().getSimpleName());
sb.append(" value:").append(t.toStringAST());
sb.append(t.getChildCount() < 2 ? "" : " #children:" + t.getChildCount());
out.println(sb.toString());
for (int i = 0; i < t.getChildCount(); i++) {
printAST(out, t.getChild(i), indent + " ");
}
}
}
}
/*
* 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.io.PrintStream;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpression;
/**
* Utilities for working with Spring Expressions.
*
* @author Andy Clement
*/
public class SpelUtilities {
/**
* Output an indented representation of the expression syntax tree to the specified output stream.
* @param printStream the output stream to print into
* @param expression the expression to be displayed
*/
public static void printAbstractSyntaxTree(PrintStream printStream, Expression expression) {
printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST start");
printAST(printStream, ((SpelExpression) expression).getAST(), "");
printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST end");
}
/*
* Helper method for printing the AST with indentation
*/
private static void printAST(PrintStream out, SpelNode t, String indent) {
if (t != null) {
StringBuilder sb = new StringBuilder();
sb.append(indent).append(t.getClass().getSimpleName());
sb.append(" value:").append(t.toStringAST());
sb.append(t.getChildCount() < 2 ? "" : " #children:" + t.getChildCount());
out.println(sb.toString());
for (int i = 0; i < t.getChildCount(); i++) {
printAST(out, t.getChild(i), indent + " ");
}
}
}
}

View File

@@ -1,44 +1,44 @@
/*
* Copyright 2002-2011 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.ast;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import static org.junit.Assert.*;
/**
* @author Andy Wilkinson
*/
public class FormatHelperTests {
@Test
public void formatMethodWithSingleArgumentForMessage() {
String message = FormatHelper.formatMethodForMessage("foo", Arrays.asList(TypeDescriptor.forObject("a string")));
assertEquals("foo(java.lang.String)", message);
}
@Test
public void formatMethodWithMultipleArgumentsForMessage() {
String message = FormatHelper.formatMethodForMessage("foo", Arrays.asList(TypeDescriptor.forObject("a string"), TypeDescriptor.forObject(Integer.valueOf(5))));
assertEquals("foo(java.lang.String,java.lang.Integer)", message);
}
}
/*
* Copyright 2002-2011 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.ast;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import static org.junit.Assert.*;
/**
* @author Andy Wilkinson
*/
public class FormatHelperTests {
@Test
public void formatMethodWithSingleArgumentForMessage() {
String message = FormatHelper.formatMethodForMessage("foo", Arrays.asList(TypeDescriptor.forObject("a string")));
assertEquals("foo(java.lang.String)", message);
}
@Test
public void formatMethodWithMultipleArgumentsForMessage() {
String message = FormatHelper.formatMethodForMessage("foo", Arrays.asList(TypeDescriptor.forObject("a string"), TypeDescriptor.forObject(Integer.valueOf(5))));
assertEquals("foo(java.lang.String,java.lang.Integer)", message);
}
}

View File

@@ -1,106 +1,106 @@
/*
* Copyright 2002-2010 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.standard;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* @author Mark Fisher
*/
public class PropertiesConversionSpelTests {
private static final SpelExpressionParser parser = new SpelExpressionParser();
@Test
public void props() {
Properties props = new Properties();
props.setProperty("x", "1");
props.setProperty("y", "2");
props.setProperty("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", props);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("123", result);
}
@Test
public void mapWithAllStringValues() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "1");
map.put("y", "2");
map.put("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("123", result);
}
@Test
public void mapWithNonStringValue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
map.put("a", new UUID(1, 1));
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("1null3", result);
}
@Test
public void customMapWithNonStringValue() {
CustomMap map = new CustomMap();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("1null3", result);
}
private static class TestBean {
@SuppressWarnings("unused")
public String foo(Properties props) {
return props.getProperty("x") + props.getProperty("y") + props.getProperty("z");
}
}
@SuppressWarnings("serial")
private static class CustomMap extends HashMap<String, Object> {
}
}
/*
* Copyright 2002-2010 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.standard;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* @author Mark Fisher
*/
public class PropertiesConversionSpelTests {
private static final SpelExpressionParser parser = new SpelExpressionParser();
@Test
public void props() {
Properties props = new Properties();
props.setProperty("x", "1");
props.setProperty("y", "2");
props.setProperty("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", props);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("123", result);
}
@Test
public void mapWithAllStringValues() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "1");
map.put("y", "2");
map.put("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("123", result);
}
@Test
public void mapWithNonStringValue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
map.put("a", new UUID(1, 1));
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("1null3", result);
}
@Test
public void customMapWithNonStringValue() {
CustomMap map = new CustomMap();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("1null3", result);
}
private static class TestBean {
@SuppressWarnings("unused")
public String foo(Properties props) {
return props.getProperty("x") + props.getProperty("y") + props.getProperty("z");
}
}
@SuppressWarnings("serial")
private static class CustomMap extends HashMap<String, Object> {
}
}

View File

@@ -1,370 +1,370 @@
/*
* 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.standard;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionException;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.ast.OpAnd;
import org.springframework.expression.spel.ast.OpOr;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* @author Andy Clement
*/
public class SpelParserTests {
@Test
public void theMostBasic() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
Assert.assertEquals(2,expr.getValue());
Assert.assertEquals(Integer.class,expr.getValueType());
Assert.assertEquals(2,expr.getAST().getValue(null));
}
@Test
public void valueType() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext();
Class c = parser.parseRaw("2").getValueType();
Assert.assertEquals(Integer.class,c);
c = parser.parseRaw("12").getValueType(ctx);
Assert.assertEquals(Integer.class,c);
c = parser.parseRaw("null").getValueType();
Assert.assertNull(c);
c = parser.parseRaw("null").getValueType(ctx);
Assert.assertNull(c);
Object o = parser.parseRaw("null").getValue(ctx,Integer.class);
Assert.assertNull(o);
}
@Test
public void whitespace() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2\n+ 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2\r\n+\t3");
Assert.assertEquals(5,expr.getValue());
}
@Test
public void arithmeticPlus1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+2");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
Assert.assertEquals(4,expr.getValue());
}
@Test
public void arithmeticPlus2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("37+41");
Assert.assertEquals(78,expr.getValue());
}
@Test
public void arithmeticMultiply1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
// printAst(expr.getAST(),0);
Assert.assertEquals(6,expr.getValue());
}
@Test
public void arithmeticPrecedence1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3+5");
Assert.assertEquals(11,expr.getValue());
}
@Test
public void generalExpressions() throws Exception {
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.MISSING_CONSTRUCTOR_ARGS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(3,");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(3");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("\"abc");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.NON_TERMINATING_DOUBLE_QUOTED_STRING,spe.getMessageCode());
Assert.assertEquals(0,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("'abc");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.NON_TERMINATING_QUOTED_STRING,spe.getMessageCode());
Assert.assertEquals(0,spe.getPosition());
}
}
@Test
public void arithmeticPrecedence2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+3*5");
Assert.assertEquals(17,expr.getValue());
}
@Test
public void arithmeticPrecedence3() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("3+10/2");
Assert.assertEquals(8,expr.getValue());
}
@Test
public void arithmeticPrecedence4() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("10/2+3");
Assert.assertEquals(8,expr.getValue());
}
@Test
public void arithmeticPrecedence5() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("(4+10)/2");
Assert.assertEquals(7,expr.getValue());
}
@Test
public void arithmeticPrecedence6() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("(3+2)*2");
Assert.assertEquals(10,expr.getValue());
}
@Test
public void booleanOperators() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("true");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false and false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("true and (true or false)");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("true and true or false");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("!true");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("!(false or true)");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
}
@Test
public void testStringLiterals() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'");
Assert.assertEquals("howdy",expr.getValue());
expr = new SpelExpressionParser().parseRaw("'hello '' world'");
Assert.assertEquals("hello ' world",expr.getValue());
}
@Test
public void testStringLiterals2() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'.substring(0,2)");
Assert.assertEquals("ho",expr.getValue());
}
@Test
public void testPositionalInformation() throws EvaluationException, ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false");
SpelNode rootAst = expr.getAST();
OpOr operatorOr = (OpOr)rootAst;
OpAnd operatorAnd = (OpAnd)operatorOr.getLeftOperand();
SpelNode rightOrOperand = operatorOr.getRightOperand();
// check position for final 'false'
Assert.assertEquals(17, rightOrOperand.getStartPosition());
Assert.assertEquals(22, rightOrOperand.getEndPosition());
// check position for first 'true'
Assert.assertEquals(0, operatorAnd.getLeftOperand().getStartPosition());
Assert.assertEquals(4, operatorAnd.getLeftOperand().getEndPosition());
// check position for second 'true'
Assert.assertEquals(9, operatorAnd.getRightOperand().getStartPosition());
Assert.assertEquals(13, operatorAnd.getRightOperand().getEndPosition());
// check position for OperatorAnd
Assert.assertEquals(5, operatorAnd.getStartPosition());
Assert.assertEquals(8, operatorAnd.getEndPosition());
// check position for OperatorOr
Assert.assertEquals(14, operatorOr.getStartPosition());
Assert.assertEquals(16, operatorOr.getEndPosition());
}
@Test
public void testTokenKind() {
TokenKind tk = TokenKind.NOT;
Assert.assertFalse(tk.hasPayload());
Assert.assertEquals("NOT(!)",tk.toString());
tk = TokenKind.MINUS;
Assert.assertFalse(tk.hasPayload());
Assert.assertEquals("MINUS(-)",tk.toString());
tk = TokenKind.LITERAL_STRING;
Assert.assertEquals("LITERAL_STRING",tk.toString());
Assert.assertTrue(tk.hasPayload());
}
@Test
public void testToken() {
Token token = new Token(TokenKind.NOT,0,3);
Assert.assertEquals(TokenKind.NOT,token.kind);
Assert.assertEquals(0,token.startpos);
Assert.assertEquals(3,token.endpos);
Assert.assertEquals("[NOT(!)](0,3)",token.toString());
token = new Token(TokenKind.LITERAL_STRING,"abc".toCharArray(),0,3);
Assert.assertEquals(TokenKind.LITERAL_STRING,token.kind);
Assert.assertEquals(0,token.startpos);
Assert.assertEquals(3,token.endpos);
Assert.assertEquals("[LITERAL_STRING:abc](0,3)",token.toString());
}
@Test
public void testExceptions() {
ExpressionException exprEx = new ExpressionException("test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("test", exprEx.toDetailedString());
exprEx = new ExpressionException("wibble","test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("Expression 'wibble': test", exprEx.toDetailedString());
exprEx = new ExpressionException("wibble",3, "test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("Expression 'wibble' @ 3: test", exprEx.toDetailedString());
}
@Test
public void testNumerics() {
checkNumber("2",2,Integer.class);
checkNumber("22",22,Integer.class);
checkNumber("+22",22,Integer.class);
checkNumber("-22",-22,Integer.class);
checkNumber("2L",2L,Long.class);
checkNumber("22l",22L,Long.class);
checkNumber("0x1",1,Integer.class);
checkNumber("0x1L",1L,Long.class);
checkNumber("0xa",10,Integer.class);
checkNumber("0xAL",10L,Long.class);
checkNumberError("0x",SpelMessage.NOT_AN_INTEGER);
checkNumberError("0xL",SpelMessage.NOT_A_LONG);
checkNumberError(".324",SpelMessage.UNEXPECTED_DATA_AFTER_DOT);
checkNumberError("3.4L",SpelMessage.REAL_CANNOT_BE_LONG);
// Number is parsed as a float, but immediately promoted to a double
checkNumber("3.5f",3.5d,Double.class);
checkNumber("1.2e3", 1.2e3d, Double.class);
checkNumber("1.2e+3", 1.2e3d, Double.class);
checkNumber("1.2e-3", 1.2e-3d, Double.class);
checkNumber("1.2e3", 1.2e3d, Double.class);
checkNumber("1.e+3", 1.e3d, Double.class);
checkNumber("1e+3", 1e3d, Double.class);
}
private void checkNumber(String expression, Object value, Class<?> type) {
try {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw(expression);
Object o = expr.getValue();
Assert.assertEquals(value,o);
Assert.assertEquals(type,o.getClass());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
private void checkNumberError(String expression, SpelMessage expectedMessage) {
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw(expression);
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(expectedMessage,spe.getMessageCode());
}
}
}
/*
* 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.standard;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionException;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.ast.OpAnd;
import org.springframework.expression.spel.ast.OpOr;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* @author Andy Clement
*/
public class SpelParserTests {
@Test
public void theMostBasic() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
Assert.assertEquals(2,expr.getValue());
Assert.assertEquals(Integer.class,expr.getValueType());
Assert.assertEquals(2,expr.getAST().getValue(null));
}
@Test
public void valueType() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext();
Class c = parser.parseRaw("2").getValueType();
Assert.assertEquals(Integer.class,c);
c = parser.parseRaw("12").getValueType(ctx);
Assert.assertEquals(Integer.class,c);
c = parser.parseRaw("null").getValueType();
Assert.assertNull(c);
c = parser.parseRaw("null").getValueType(ctx);
Assert.assertNull(c);
Object o = parser.parseRaw("null").getValue(ctx,Integer.class);
Assert.assertNull(o);
}
@Test
public void whitespace() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2 + 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2\n+ 3");
Assert.assertEquals(5,expr.getValue());
expr = parser.parseRaw("2\r\n+\t3");
Assert.assertEquals(5,expr.getValue());
}
@Test
public void arithmeticPlus1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+2");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
Assert.assertEquals(4,expr.getValue());
}
@Test
public void arithmeticPlus2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("37+41");
Assert.assertEquals(78,expr.getValue());
}
@Test
public void arithmeticMultiply1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3");
Assert.assertNotNull(expr);
Assert.assertNotNull(expr.getAST());
// printAst(expr.getAST(),0);
Assert.assertEquals(6,expr.getValue());
}
@Test
public void arithmeticPrecedence1() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2*3+5");
Assert.assertEquals(11,expr.getValue());
}
@Test
public void generalExpressions() throws Exception {
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.MISSING_CONSTRUCTOR_ARGS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(3,");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(3");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("new String(");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.RUN_OUT_OF_ARGUMENTS,spe.getMessageCode());
Assert.assertEquals(10,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("\"abc");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.NON_TERMINATING_DOUBLE_QUOTED_STRING,spe.getMessageCode());
Assert.assertEquals(0,spe.getPosition());
}
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw("'abc");
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(SpelMessage.NON_TERMINATING_QUOTED_STRING,spe.getMessageCode());
Assert.assertEquals(0,spe.getPosition());
}
}
@Test
public void arithmeticPrecedence2() throws EvaluationException,ParseException {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw("2+3*5");
Assert.assertEquals(17,expr.getValue());
}
@Test
public void arithmeticPrecedence3() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("3+10/2");
Assert.assertEquals(8,expr.getValue());
}
@Test
public void arithmeticPrecedence4() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("10/2+3");
Assert.assertEquals(8,expr.getValue());
}
@Test
public void arithmeticPrecedence5() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("(4+10)/2");
Assert.assertEquals(7,expr.getValue());
}
@Test
public void arithmeticPrecedence6() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("(3+2)*2");
Assert.assertEquals(10,expr.getValue());
}
@Test
public void booleanOperators() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("true");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("false and false");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("true and (true or false)");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("true and true or false");
Assert.assertEquals(Boolean.TRUE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("!true");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
expr = new SpelExpressionParser().parseRaw("!(false or true)");
Assert.assertEquals(Boolean.FALSE,expr.getValue(Boolean.class));
}
@Test
public void testStringLiterals() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'");
Assert.assertEquals("howdy",expr.getValue());
expr = new SpelExpressionParser().parseRaw("'hello '' world'");
Assert.assertEquals("hello ' world",expr.getValue());
}
@Test
public void testStringLiterals2() throws EvaluationException,ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("'howdy'.substring(0,2)");
Assert.assertEquals("ho",expr.getValue());
}
@Test
public void testPositionalInformation() throws EvaluationException, ParseException {
SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false");
SpelNode rootAst = expr.getAST();
OpOr operatorOr = (OpOr)rootAst;
OpAnd operatorAnd = (OpAnd)operatorOr.getLeftOperand();
SpelNode rightOrOperand = operatorOr.getRightOperand();
// check position for final 'false'
Assert.assertEquals(17, rightOrOperand.getStartPosition());
Assert.assertEquals(22, rightOrOperand.getEndPosition());
// check position for first 'true'
Assert.assertEquals(0, operatorAnd.getLeftOperand().getStartPosition());
Assert.assertEquals(4, operatorAnd.getLeftOperand().getEndPosition());
// check position for second 'true'
Assert.assertEquals(9, operatorAnd.getRightOperand().getStartPosition());
Assert.assertEquals(13, operatorAnd.getRightOperand().getEndPosition());
// check position for OperatorAnd
Assert.assertEquals(5, operatorAnd.getStartPosition());
Assert.assertEquals(8, operatorAnd.getEndPosition());
// check position for OperatorOr
Assert.assertEquals(14, operatorOr.getStartPosition());
Assert.assertEquals(16, operatorOr.getEndPosition());
}
@Test
public void testTokenKind() {
TokenKind tk = TokenKind.NOT;
Assert.assertFalse(tk.hasPayload());
Assert.assertEquals("NOT(!)",tk.toString());
tk = TokenKind.MINUS;
Assert.assertFalse(tk.hasPayload());
Assert.assertEquals("MINUS(-)",tk.toString());
tk = TokenKind.LITERAL_STRING;
Assert.assertEquals("LITERAL_STRING",tk.toString());
Assert.assertTrue(tk.hasPayload());
}
@Test
public void testToken() {
Token token = new Token(TokenKind.NOT,0,3);
Assert.assertEquals(TokenKind.NOT,token.kind);
Assert.assertEquals(0,token.startpos);
Assert.assertEquals(3,token.endpos);
Assert.assertEquals("[NOT(!)](0,3)",token.toString());
token = new Token(TokenKind.LITERAL_STRING,"abc".toCharArray(),0,3);
Assert.assertEquals(TokenKind.LITERAL_STRING,token.kind);
Assert.assertEquals(0,token.startpos);
Assert.assertEquals(3,token.endpos);
Assert.assertEquals("[LITERAL_STRING:abc](0,3)",token.toString());
}
@Test
public void testExceptions() {
ExpressionException exprEx = new ExpressionException("test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("test", exprEx.toDetailedString());
exprEx = new ExpressionException("wibble","test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("Expression 'wibble': test", exprEx.toDetailedString());
exprEx = new ExpressionException("wibble",3, "test");
Assert.assertEquals("test", exprEx.getMessage());
Assert.assertEquals("Expression 'wibble' @ 3: test", exprEx.toDetailedString());
}
@Test
public void testNumerics() {
checkNumber("2",2,Integer.class);
checkNumber("22",22,Integer.class);
checkNumber("+22",22,Integer.class);
checkNumber("-22",-22,Integer.class);
checkNumber("2L",2L,Long.class);
checkNumber("22l",22L,Long.class);
checkNumber("0x1",1,Integer.class);
checkNumber("0x1L",1L,Long.class);
checkNumber("0xa",10,Integer.class);
checkNumber("0xAL",10L,Long.class);
checkNumberError("0x",SpelMessage.NOT_AN_INTEGER);
checkNumberError("0xL",SpelMessage.NOT_A_LONG);
checkNumberError(".324",SpelMessage.UNEXPECTED_DATA_AFTER_DOT);
checkNumberError("3.4L",SpelMessage.REAL_CANNOT_BE_LONG);
// Number is parsed as a float, but immediately promoted to a double
checkNumber("3.5f",3.5d,Double.class);
checkNumber("1.2e3", 1.2e3d, Double.class);
checkNumber("1.2e+3", 1.2e3d, Double.class);
checkNumber("1.2e-3", 1.2e-3d, Double.class);
checkNumber("1.2e3", 1.2e3d, Double.class);
checkNumber("1.e+3", 1.e3d, Double.class);
checkNumber("1e+3", 1e3d, Double.class);
}
private void checkNumber(String expression, Object value, Class<?> type) {
try {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expr = parser.parseRaw(expression);
Object o = expr.getValue();
Assert.assertEquals(value,o);
Assert.assertEquals(type,o.getClass());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
private void checkNumberError(String expression, SpelMessage expectedMessage) {
try {
SpelExpressionParser parser = new SpelExpressionParser();
parser.parseRaw(expression);
Assert.fail();
} catch (ParseException e) {
Assert.assertTrue(e instanceof SpelParseException);
SpelParseException spe = (SpelParseException)e;
Assert.assertEquals(expectedMessage,spe.getMessageCode());
}
}
}

View File

@@ -1,79 +1,79 @@
/*
* 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.support;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Operation;
import org.springframework.expression.OperatorOverloader;
import org.springframework.expression.TypeComparator;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.TypeLocator;
public class StandardComponentsTests {
@Test
public void testStandardEvaluationContext() {
StandardEvaluationContext context = new StandardEvaluationContext();
Assert.assertNotNull(context.getTypeComparator());
TypeComparator tc = new StandardTypeComparator();
context.setTypeComparator(tc);
Assert.assertEquals(tc,context.getTypeComparator());
TypeLocator tl = new StandardTypeLocator();
context.setTypeLocator(tl);
Assert.assertEquals(tl,context.getTypeLocator());
}
@Test
public void testStandardOperatorOverloader() throws EvaluationException {
OperatorOverloader oo = new StandardOperatorOverloader();
Assert.assertFalse(oo.overridesOperation(Operation.ADD, null, null));
try {
oo.operate(Operation.ADD, 2, 3);
Assert.fail("should have failed");
} catch (EvaluationException e) {
// success
}
}
@Test
public void testStandardTypeLocator() {
StandardTypeLocator tl = new StandardTypeLocator();
List<String> prefixes = tl.getImportPrefixes();
Assert.assertEquals(1,prefixes.size());
tl.registerImport("java.util");
prefixes = tl.getImportPrefixes();
Assert.assertEquals(2,prefixes.size());
tl.removeImport("java.util");
prefixes = tl.getImportPrefixes();
Assert.assertEquals(1,prefixes.size());
}
@Test
public void testStandardTypeConverter() throws EvaluationException {
TypeConverter tc = new StandardTypeConverter();
tc.convertValue(3, TypeDescriptor.forObject(3), TypeDescriptor.valueOf(Double.class));
}
}
/*
* 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.support;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Operation;
import org.springframework.expression.OperatorOverloader;
import org.springframework.expression.TypeComparator;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.TypeLocator;
public class StandardComponentsTests {
@Test
public void testStandardEvaluationContext() {
StandardEvaluationContext context = new StandardEvaluationContext();
Assert.assertNotNull(context.getTypeComparator());
TypeComparator tc = new StandardTypeComparator();
context.setTypeComparator(tc);
Assert.assertEquals(tc,context.getTypeComparator());
TypeLocator tl = new StandardTypeLocator();
context.setTypeLocator(tl);
Assert.assertEquals(tl,context.getTypeLocator());
}
@Test
public void testStandardOperatorOverloader() throws EvaluationException {
OperatorOverloader oo = new StandardOperatorOverloader();
Assert.assertFalse(oo.overridesOperation(Operation.ADD, null, null));
try {
oo.operate(Operation.ADD, 2, 3);
Assert.fail("should have failed");
} catch (EvaluationException e) {
// success
}
}
@Test
public void testStandardTypeLocator() {
StandardTypeLocator tl = new StandardTypeLocator();
List<String> prefixes = tl.getImportPrefixes();
Assert.assertEquals(1,prefixes.size());
tl.registerImport("java.util");
prefixes = tl.getImportPrefixes();
Assert.assertEquals(2,prefixes.size());
tl.removeImport("java.util");
prefixes = tl.getImportPrefixes();
Assert.assertEquals(1,prefixes.size());
}
@Test
public void testStandardTypeConverter() throws EvaluationException {
TypeConverter tc = new StandardTypeConverter();
tc.convertValue(3, TypeDescriptor.forObject(3), TypeDescriptor.valueOf(Double.class));
}
}

View File

@@ -1,21 +1,21 @@
package org.springframework.expression.spel.testresources;
import java.util.List;
public class TestAddress{
private String street;
private List<String> crossStreets;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public List<String> getCrossStreets() {
return crossStreets;
}
public void setCrossStreets(List<String> crossStreets) {
this.crossStreets = crossStreets;
}
}
package org.springframework.expression.spel.testresources;
import java.util.List;
public class TestAddress{
private String street;
private List<String> crossStreets;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public List<String> getCrossStreets() {
return crossStreets;
}
public void setCrossStreets(List<String> crossStreets) {
this.crossStreets = crossStreets;
}
}

View File

@@ -1,19 +1,19 @@
package org.springframework.expression.spel.testresources;
public class TestPerson {
private String name;
private TestAddress address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TestAddress getAddress() {
return address;
}
public void setAddress(TestAddress address) {
this.address = address;
}
package org.springframework.expression.spel.testresources;
public class TestPerson {
private String name;
private TestAddress address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TestAddress getAddress() {
return address;
}
public void setAddress(TestAddress address) {
this.address = address;
}
}