|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2009 the original author or authors. |
| 2 | + * Copyright 2002-2012 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.expression.spel;
|
18 | 18 |
|
19 | 19 | import org.junit.Test;
|
| 20 | +import org.springframework.core.convert.TypeDescriptor; |
| 21 | +import org.springframework.core.convert.support.GenericConversionService; |
| 22 | +import org.springframework.expression.spel.support.StandardTypeConverter; |
20 | 23 |
|
21 | 24 | /**
|
22 | 25 | * Tests the evaluation of real boolean expressions, these use AND, OR, NOT, TRUE, FALSE
|
23 |
| - * |
| 26 | + * |
24 | 27 | * @author Andy Clement
|
| 28 | + * @author Oliver Becker |
25 | 29 | */
|
26 | 30 | public class BooleanExpressionTests extends ExpressionTestCase {
|
27 | 31 |
|
@@ -83,4 +87,27 @@ public void testBooleanErrors01() {
|
83 | 87 | evaluateAndCheckError("!35.2", SpelMessage.TYPE_CONVERSION_ERROR, 1);
|
84 | 88 | evaluateAndCheckError("! 'foob'", SpelMessage.TYPE_CONVERSION_ERROR, 2);
|
85 | 89 | }
|
| 90 | + |
| 91 | + @Test |
| 92 | + public void testConvertAndHandleNull() { // SPR-9445 |
| 93 | + // without null conversion |
| 94 | + evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); |
| 95 | + evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); |
| 96 | + evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean"); |
| 97 | + evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); |
| 98 | + |
| 99 | + // with null conversion (null -> false) |
| 100 | + GenericConversionService conversionService = new GenericConversionService() { |
| 101 | + @Override |
| 102 | + protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 103 | + return targetType.getType() == Boolean.class ? false : null; |
| 104 | + } |
| 105 | + }; |
| 106 | + eContext.setTypeConverter(new StandardTypeConverter(conversionService)); |
| 107 | + |
| 108 | + evaluate("null or true", Boolean.TRUE, Boolean.class, false); |
| 109 | + evaluate("null and true", Boolean.FALSE, Boolean.class, false); |
| 110 | + evaluate("!null", Boolean.TRUE, Boolean.class, false); |
| 111 | + evaluate("null ? 'foo' : 'bar'", "bar", String.class, false); |
| 112 | + } |
86 | 113 | }
|
0 commit comments