Created
August 12, 2019 14:18
-
-
Save corneil/6b3712e7418068b9e5eba85c04675718 to your computer and use it in GitHub Desktop.
Spring Spel fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert.assertEquals | |
import org.junit.Test | |
import org.springframework.core.convert.TypeDescriptor | |
import org.springframework.expression.spel.SpelParserConfiguration | |
import org.springframework.expression.spel.standard.SpelExpressionParser | |
import org.springframework.expression.spel.support.SimpleEvaluationContext | |
class PropertyHolder(val properties: Map<String, Any>) | |
class SpelTests { | |
@Test | |
fun testSpel() { | |
val exp = "properties.name + ':Extra:' + properties.value1 + ':' + properties.value2" | |
val properties = mapOf<String, Any>("name" to "joe", "value1" to 2, "value2" to 4.5) | |
val expressionParser = SpelExpressionParser(SpelParserConfiguration()) | |
val expression = expressionParser.parseExpression(exp) | |
val root = PropertyHolder(properties) | |
val context = SimpleEvaluationContext.forReadOnlyDataBinding().withTypedRootObject( | |
root, TypeDescriptor.valueOf(PropertyHolder::class.java) | |
).build() | |
val output = expression.getValue(context, String::class) | |
assertEquals(output, "joe:Extra:2:4.5") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test output: