/** * For use when coercion is happening during a setValue(). The expectedValue should be * the coerced form of the value. */ protected void setValue(String expression, Object value, Object expectedValue) { try { Expression e = parser.parseExpression(expression); if (e == null) { fail("Parser returned null for expression"); } if (DEBUG) { SpelUtilities.printAbstractSyntaxTree(System.out, e); } StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext(); assertTrue("Expression is not writeable but should be", e.isWritable(lContext)); e.setValue(lContext, value); Object a = expectedValue; Object b = e.getValue(lContext); if (!a.equals(b)) { fail("Not the same: ["+a+"] type="+a.getClass()+" ["+b+"] type="+b.getClass()); // assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext)); } } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
/** * Scenario: using the standard infrastructure and running simple expression evaluation. */ @Test public void testScenario_UsingStandardInfrastructure() { try { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Parse an expression Expression expr = parser.parseRaw("new String('hello world')"); // Evaluate it using a 'standard' context Object value = expr.getValue(); // They are reusable value = expr.getValue(); assertEquals("hello world", value); assertEquals(String.class, value.getClass()); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
protected void setValue(String expression, Object value) { try { Expression e = parser.parseExpression(expression); if (e == null) { fail("Parser returned null for expression"); } if (DEBUG) { SpelUtilities.printAbstractSyntaxTree(System.out, e); } StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext(); assertTrue("Expression is not writeable but should be", e.isWritable(lContext)); e.setValue(lContext, value); assertEquals("Retrieved value was not equal to set value", value, e.getValue(lContext,value.getClass())); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
/** * Scenario: using your own java methods and calling them from the expression */ @Test public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException { try { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class)); Expression expr = parser.parseRaw("#repeat('hello')"); Object value = expr.getValue(ctx); assertEquals("hellohello", value); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
@Test public void exceptions() { ExpressionException exprEx = new ExpressionException("test"); assertEquals("test", exprEx.getSimpleMessage()); assertEquals("test", exprEx.toDetailedString()); assertEquals("test", exprEx.getMessage()); exprEx = new ExpressionException("wibble", "test"); assertEquals("test", exprEx.getSimpleMessage()); assertEquals("Expression [wibble]: test", exprEx.toDetailedString()); assertEquals("Expression [wibble]: test", exprEx.getMessage()); exprEx = new ExpressionException("wibble", 3, "test"); assertEquals("test", exprEx.getSimpleMessage()); assertEquals("Expression [wibble] @3: test", exprEx.toDetailedString()); assertEquals("Expression [wibble] @3: test", exprEx.getMessage()); }
@Override public String interpolate(final String template, final Map<String, Object> variables) { Assert.notNull(template, "messageTemplate must not be null"); String result; try { final Expression expression = parser().parseExpression(template, new TemplateParserContext()); result = expression.getValue(this.evalContext, variables, String.class); } catch (final ExpressionException ex) { result = "Failed to interpolate message template, error: " + ex.getMessage(); // TODO test this log.warn("Failed to interpolate message template: {}, variables: {}", template, variables, ex); } return result; }
@ExceptionHandler(value = ExpressionException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public RestResponse<Void> generatePaasIdErrorHandler(ExpressionException e) { log.warn("Problem parsing right operand during the generation of PaasId : ", e); return RestResponseBuilder.<Void> builder().error( RestErrorBuilder.builder(RestErrorCode.DEPLOYMENT_NAMING_POLICY_ERROR).message("Problem parsing right operand : " + e.getMessage()).build()) .build(); }
logger.warn(String.format( "Expression %s could not be resolved against context root of type %s. Reason: %s. Set log level to TRACE to view Stacktrace.", placeholder, expressionContext.getClass(), e.getMessage())); logger.trace("Reason for skipping expression:", e);
logger.warn(String.format( "Skipping comment expression '%s' because it can not be resolved by any comment processor. Reason: %s. Set log level to TRACE to view Stacktrace.", commentString, e.getMessage())); logger.trace("Reason for skipping comment: ", e);
logger.warn(String.format( "Skipping comment expression '%s' because it can not be resolved by any comment processor. Reason: %s. Set log level to TRACE to view Stacktrace.", commentString, e.getMessage())); logger.trace("Reason for skipping comment: ", e);
logger.warn(String.format( "Skipping processor expression '%s' because it can not be resolved by any comment processor. Reason: %s. Set log level to TRACE to view Stacktrace.", processorExpression, e.getMessage())); logger.trace("Reason for skipping processor expression: ", e);