Created
November 17, 2023 16:28
-
-
Save edudant/42134f258087e094fae91d2d874a1ae1 to your computer and use it in GitHub Desktop.
This file contains 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
package org.springframework.expression.spel.ast | |
import cz.datalite.tsm.commons.service.SpringContext | |
import cz.datalite.tsm.commons.service.TsmSpelService | |
import cz.datalite.tsm.commons.spel.SpelTestBean | |
import cz.datalite.tsm.commons.spel.extensions.SpelContextContributorBasicFunctions | |
import org.junit.jupiter.api.Assertions.assertEquals | |
import org.junit.jupiter.api.Test | |
import org.springframework.context.support.StaticApplicationContext | |
internal class CustomFunctionReferenceTest { | |
private val spelService = TsmSpelService().also { | |
SpringContext.CONTEXT = StaticApplicationContext().also { | |
it.registerSingleton("spelContextContributorBasicFunctions", SpelContextContributorBasicFunctions::class.java) | |
it.registerSingleton("spelTestBean", SpelTestBean::class.java) | |
} | |
} | |
@Test | |
fun `test SPEL if statements`() { | |
val context = mapOf("testBean" to SpelTestBean()) | |
assertEquals(2, spelService.eval("#if(true, 1+1)", context)) | |
assertEquals(5, spelService.eval("#if(false, 1+1).else(2+3)", context)) | |
assertEquals(2, spelService.eval("#if(true, 1+1).else(2+3)", context)) | |
assertEquals(6, spelService.eval("#if(false, 1+1).elseif('true', 3+3)", context)) | |
} | |
@Test | |
fun `test SPEL case statements`() { | |
val context = mapOf("testBean" to SpelTestBean()) | |
assertEquals(2, spelService.eval("#case().when(true, 1+1)", context)) | |
assertEquals(5, spelService.eval("#case().when(false, 1+1).else(2+3)", context)) | |
assertEquals(2, spelService.eval("#case().when(true, 1+1).else(2+3)", context)) | |
assertEquals(5, spelService.eval("#case().when(false, 1+1).else(3+2)", context)) | |
assertEquals(4, spelService.eval("#caseValue(2).when(1+1, 2+2).else(3+3)", context)) | |
assertEquals(6, spelService.eval("#caseValue(2).when(1, 2+2).else(3+3)", context)) | |
} | |
@Test | |
fun `test SPEL try catch statements`() { | |
val context = mapOf("testBean" to SpelTestBean()) | |
assertEquals(2, spelService.eval("#try(1+1)", context)) | |
assertEquals(2, spelService.eval("#try(1+1).catch(3+3)", context)) | |
assertEquals(6, spelService.eval("#try(1/0).catch(3+3)", context)) | |
assertEquals("java.lang.ArithmeticException: / by zero", spelService.eval("#try(1/0).catch(#error.toString())", context)) | |
} | |
@Test | |
fun `test SPEL combine everything`() { | |
val context = mapOf("data" to | |
listOf( | |
mapOf("x" to 1), | |
mapOf("x" to 2) | |
) | |
) | |
val expr = """ | |
data.![ | |
#case() | |
.when(x == 2, {"result": "second"}) | |
.else( | |
#try(1/0) | |
.catch({"result": "first: " + #error}) | |
) | |
] | |
""".trimIndent() | |
@Suppress("UNCHECKED_CAST") | |
val result = spelService.eval(expr, context) as List<Map<String, Any?>> | |
assertEquals("first: java.lang.ArithmeticException: / by zero", result[0]["result"]) | |
assertEquals("second", result[1]["result"]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment