Final commit before the great 'stripdown'. Used clover to determine coverage and added tests as necessary.

This commit is contained in:
Andy Clement
2008-09-15 20:14:36 +00:00
parent 23db8b58da
commit 59a4427525
14 changed files with 270 additions and 54 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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.common;
import junit.framework.TestCase;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelExpressionParser;
import org.springframework.expression.spel.standard.StandardEvaluationContext;
/**
* Test LiteralExpression
*
* @author Andy Clement
*/
public class CompositeStringExpressionTests extends TestCase {
public void testGetValue() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
Expression ex = parser.parseExpression("hello ${'world'}", DefaultTemplateParserContext.INSTANCE);
checkString("hello world", ex.getValue());
checkString("hello world", ex.getValue(String.class));
EvaluationContext ctx = new StandardEvaluationContext();
checkString("hello world", ex.getValue(ctx));
checkString("hello world", ex.getValue(ctx, String.class));
assertEquals("hello ${'world'}", ex.getExpressionString());
assertFalse(ex.isWritable(new StandardEvaluationContext()));
}
// public void testSetValue() {
// try {
// LiteralExpression lEx = new LiteralExpression("somevalue");
// lEx.setValue(new StandardEvaluationContext(), "flibble");
// fail("Should have got an exception that the value cannot be set");
// } catch (EvaluationException ee) {
// // success, not allowed - whilst here, check the expression value in the exception
// assertEquals(ee.getExpressionString(), "somevalue");
// }
// }
//
// public void testGetValueType() throws Exception {
// LiteralExpression lEx = new LiteralExpression("somevalue");
// assertEquals(String.class, lEx.getValueType());
// assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
// }
private void checkString(String expectedString, Object value) {
if (!(value instanceof String)) {
fail("Result was not a string, it was of type " + value.getClass() + " (value=" + value + ")");
}
if (!((String) value).equals(expectedString)) {
fail("Did not get expected result. Should have been '" + expectedString + "' but was '" + value + "'");
}
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.common;
import junit.framework.TestCase;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.spel.standard.StandardEvaluationContext;
/**
* Test LiteralExpression
*
* @author Andy Clement
*/
public class LiteralExpressionTests extends TestCase {
public void testGetValue() throws Exception {
LiteralExpression lEx = new LiteralExpression("somevalue");
checkString("somevalue", lEx.getValue());
checkString("somevalue", lEx.getValue(String.class));
EvaluationContext ctx = new StandardEvaluationContext();
checkString("somevalue", lEx.getValue(ctx));
checkString("somevalue", lEx.getValue(ctx, String.class));
assertEquals("somevalue", lEx.getExpressionString());
assertFalse(lEx.isWritable(new StandardEvaluationContext()));
}
public void testSetValue() {
try {
LiteralExpression lEx = new LiteralExpression("somevalue");
lEx.setValue(new StandardEvaluationContext(), "flibble");
fail("Should have got an exception that the value cannot be set");
} catch (EvaluationException ee) {
// success, not allowed - whilst here, check the expression value in the exception
assertEquals(ee.getExpressionString(), "somevalue");
}
}
public void testGetValueType() throws Exception {
LiteralExpression lEx = new LiteralExpression("somevalue");
assertEquals(String.class, lEx.getValueType());
assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
}
private void checkString(String expectedString, Object value) {
if (!(value instanceof String)) {
fail("Result was not a string, it was of type " + value.getClass() + " (value=" + value + ")");
}
if (!((String) value).equals(expectedString)) {
fail("Did not get expected result. Should have been '" + expectedString + "' but was '" + value + "'");
}
}
}

View File

@@ -18,6 +18,9 @@ package org.springframework.expression.spel;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.springframework.expression.common.CompositeStringExpressionTests;
import org.springframework.expression.common.LiteralExpressionTests;
/**
* Pulls together all the tests for Spring EL into a single suite.
*
@@ -42,9 +45,12 @@ public class AllTests {
suite.addTestSuite(TypeReferencing.class);
suite.addTestSuite(PerformanceTests.class);
suite.addTestSuite(DefaultComparatorUnitTests.class);
suite.addTestSuite(TemplateExpressionParsing.class);
suite.addTestSuite(TemplateExpressionParsingTests.class);
suite.addTestSuite(ExpressionLanguageScenarioTests.class);
suite.addTestSuite(ScenariosForSpringSecurity.class);
suite.addTestSuite(SpelUtilitiesTests.class);
suite.addTestSuite(LiteralExpressionTests.class);
suite.addTestSuite(CompositeStringExpressionTests.class);
// $JUnit-END$
return suite;
}

View File

@@ -55,6 +55,12 @@ public class BooleanExpressionTests extends ExpressionTestCase {
evaluate("true and false or false", Boolean.FALSE, Boolean.class);
}
public void testWritability() {
evaluate("true and true", Boolean.TRUE, Boolean.class, false);
evaluate("true or true", Boolean.TRUE, Boolean.class, false);
evaluate("!false", Boolean.TRUE, Boolean.class, false);
}
public void testBooleanErrors01() {
evaluateAndCheckError("1.0 or false", SpelMessages.TYPE_CONVERSION_ERROR, 0);
evaluateAndCheckError("false or 39.4", SpelMessages.TYPE_CONVERSION_ERROR, 9);

View File

@@ -47,6 +47,10 @@ public class ParsingTests extends TestCase {
parseCheck("true");
}
public void testLiteralBoolean03() {
parseCheck("!true");
}
public void testLiteralInteger01() {
parseCheck("1");
}

View File

@@ -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.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.TestCase;
/**
* Tests the SpelUtilities
*
* @author Andy Clement
*/
public class SpelUtilitiesTests extends TestCase {
public void testPrintAbstractSyntaxTree01() throws Exception {
SpelExpression sEx = new SpelExpressionParser().parseExpression("1 + 2");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SpelUtilities.printAbstractSyntaxTree(new PrintStream(baos), sEx);
String theAst = baos.toString();
System.out.println(theAst);
String[] expectedLines = new String[] { "===> Expression '1 + 2' - AST start",
"OperatorPlus value=+ children=#2", " CompoundExpression value=EXPRESSION",
" IntLiteral value=1", " CompoundExpression value=EXPRESSION", " IntLiteral value=2",
"===> Expression '1 + 2' - AST end" };
checkExpected(theAst, expectedLines);
}
private static void checkExpected(String theData, String[] expectedLines) {
String[] theDataSplit = theData.split("\n");
if (theDataSplit.length != expectedLines.length) {
System.out.println("TheData:");
System.out.println(theData);
System.out.println("ExpectedData:\n" + expectedLines);
fail("Data incorrect, expected " + expectedLines.length + " but got " + theDataSplit.length + " lines");
}
for (int i = 0; i < expectedLines.length; i++) {
assertEquals("Failure in comparison at line " + i, expectedLines[i], theDataSplit[i]);
}
}
}

View File

@@ -23,7 +23,7 @@ import org.springframework.expression.common.DefaultTemplateParserContext;
*
* @author Andy Clement
*/
public class TemplateExpressionParsing extends ExpressionTestCase {
public class TemplateExpressionParsingTests extends ExpressionTestCase {
public void testParsingSimpleTemplateExpression01() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();

View File

@@ -61,10 +61,11 @@ public class TestScenarioCreator {
new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE }));
testContext.registerFunction("reverseString", TestScenarioCreator.class.getDeclaredMethod("reverseString",
new Class[] { String.class }));
testContext.registerFunction("varargsFunctionReverseStringsAndMerge", TestScenarioCreator.class.getDeclaredMethod("varargsFunctionReverseStringsAndMerge",
new Class[] { String[].class }));
testContext.registerFunction("varargsFunctionReverseStringsAndMerge2", TestScenarioCreator.class.getDeclaredMethod("varargsFunctionReverseStringsAndMerge2",
new Class[] { Integer.TYPE,String[].class }));
testContext.registerFunction("varargsFunctionReverseStringsAndMerge", TestScenarioCreator.class
.getDeclaredMethod("varargsFunctionReverseStringsAndMerge", new Class[] { String[].class }));
testContext.registerFunction("varargsFunctionReverseStringsAndMerge2", TestScenarioCreator.class
.getDeclaredMethod("varargsFunctionReverseStringsAndMerge2", new Class[] { Integer.TYPE,
String[].class }));
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
@@ -86,8 +87,8 @@ public class TestScenarioCreator {
*/
private static void createTestClassloader(StandardEvaluationContext testContext) {
try {
ClassLoader cl = new URLClassLoader(new URL[] { new File("target/test-classes/testcode.jar").toURI().toURL() },
Thread.currentThread().getContextClassLoader());
ClassLoader cl = new URLClassLoader(new URL[] { new File("target/test-classes/testcode.jar").toURI()
.toURL() }, Thread.currentThread().getContextClassLoader());
testContext.setClassLoader(cl);
} catch (MalformedURLException mue) {
mue.printStackTrace();
@@ -97,7 +98,7 @@ public class TestScenarioCreator {
/**
* Create the root context object, an Inventor instance. Non-qualified property and method references will be
* resolved against this context object.
*
*
* @param testContext the evaluation context in which to set the root object
*/
private static void setupRootContextObject(StandardEvaluationContext testContext) {
@@ -113,11 +114,10 @@ public class TestScenarioCreator {
/**
* Create a context configuration that tests can reference into using the
* @() language construct.
* at(context:objectName) will index a particular object within a particular context. The 'root' context will be used
* for references where no context is specified, eg.
* @() language construct. at(context:objectName) will index a particular object within a particular context. The
* 'root' context will be used for references where no context is specified, eg.
* @(orange).
*
*
* @param testContext the evaluation context in which to register the new references
*/
private static void populateContextMap(StandardEvaluationContext testContext) {
@@ -205,21 +205,21 @@ public class TestScenarioCreator {
return backwards.toString();
}
public static String varargsFunctionReverseStringsAndMerge(String...strings) {
public static String varargsFunctionReverseStringsAndMerge(String... strings) {
StringBuilder sb = new StringBuilder();
if (strings!=null) {
for (int i=strings.length-1;i>=0;i--) {
if (strings != null) {
for (int i = strings.length - 1; i >= 0; i--) {
sb.append(strings[i]);
}
}
return sb.toString();
}
public static String varargsFunctionReverseStringsAndMerge2(int j, String...strings) {
public static String varargsFunctionReverseStringsAndMerge2(int j, String... strings) {
StringBuilder sb = new StringBuilder();
sb.append(j);
if (strings!=null) {
for (int i=strings.length-1;i>=0;i--) {
if (strings != null) {
for (int i = strings.length - 1; i >= 0; i--) {
sb.append(strings[i]);
}
}