SpelResolver implements EmbeddedValueResolverAware { public String resolve(Method method, Object[] arguments, String spelExpression) { if (StringUtils.isEmpty(spelExpression)) { return spelExpression; } if (spelExpression.matches(BEAN_SPEL_REGEX) && stringValueResolver != null) { return stringValueResolver.resolveStringValue(spelExpression); } if (spelExpression.matches(METHOD_SPEL_REGEX)) { SpelRootObject rootObject = new SpelRootObject(method, arguments); MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, method, arguments, parameterNameDiscoverer); Object evaluated = expressionParser.parseExpression(spelExpression).getValue(evaluationContext); return (String) evaluated; } return spelExpression; } SpelResolver(SpelExpressionParser spelExpressionParser, ParameterNameDiscoverer parameterNameDiscoverer); String resolve(Method method, Object[] arguments, String spelExpression); @Override void setEmbeddedValueResolver(StringValueResolver resolver);  }
@Test public void givenNonSpelExpression_whenParse_returnsItself() throws Exception { String testExpression = "backendA"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, testExpression); assertThat(result).isEqualTo(testExpression); }
@Test public void testRootArgs() throws Exception { String testExpression = "#root.args[0]"; String firstArgument = "test"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{firstArgument}, testExpression); assertThat(result).isEqualTo(firstArgument); }
@Test public void testRootMethodName() throws Exception { String testExpression = "#root.methodName"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, testExpression); assertThat(result).isEqualTo("testMethod"); }
@Test public void testP0() throws Exception { String testExpression = "#p0"; String firstArgument = "test"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{firstArgument}, testExpression); assertThat(result).isEqualTo(firstArgument); }
@Test public void testA0() throws Exception { String testExpression = "#a0"; String firstArgument = "test"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{firstArgument}, testExpression); assertThat(result).isEqualTo(firstArgument); }
@Test public void stringSpelTest() throws Exception { String testExpression = "#{'recover'}"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, testExpression); assertThat(result).isEqualTo("recover"); }
@Test public void placeholderSpelTest() throws Exception { String testExpression = "${missingProperty:default}"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, testExpression); assertThat(result).isEqualTo("default"); }
@Test public void placeholderSpelTest2() throws Exception { String testExpression = "${property:default}"; SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, testExpression); assertThat(result).isEqualTo("backend"); }
@Test public void nullTest() throws Exception { SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, null); assertThat(result).isNull(); }
@Test public void emptyStringTest() throws Exception { SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, ""); assertThat(result).isEqualTo(""); }
@Test public void dollarTest() throws Exception { SpelResolverTest target = new SpelResolverTest(); Method testMethod = target.getClass().getMethod("testMethod", String.class); String result = sut.resolve(testMethod, new Object[]{}, "$"); assertThat(result).isEqualTo("$"); }