Search in sources :

Example 1 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class PropertyAccessTests method testAccessingOnNullObject.

/**
	 * The standard reflection resolver cannot find properties on null objects but some
	 * supplied resolver might be able to - so null shouldn't crash the reflection resolver.
	 */
@Test
public void testAccessingOnNullObject() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
    EvaluationContext context = new StandardEvaluationContext(null);
    try {
        expr.getValue(context);
        fail("Should have failed - default property resolver cannot resolve on null");
    } catch (Exception ex) {
        checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
    }
    assertFalse(expr.isWritable(context));
    try {
        expr.setValue(context, "abc");
        fail("Should have failed - default property resolver cannot resolve on null");
    } catch (Exception ex) {
        checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationException(org.springframework.expression.EvaluationException) AccessException(org.springframework.expression.AccessException) Test(org.junit.Test)

Example 2 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelReproTests method reservedWordProperties_9862.

@Test
public void reservedWordProperties_9862() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
    Object value = expression.getValue(ctx);
    assertEquals(value, Reserver.CONST);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 3 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-integration by spring-projects.

the class WebFluxInboundGatewayParserTests method reactiveFullConfig.

@Test
@SuppressWarnings("unchecked")
public void reactiveFullConfig() {
    DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.reactiveFullConfig);
    assertSame(this.requests, endpointAccessor.getPropertyValue("requestChannel"));
    assertNotNull(endpointAccessor.getPropertyValue("errorChannel"));
    assertFalse((boolean) endpointAccessor.getPropertyValue("autoStartup"));
    assertEquals(101, endpointAccessor.getPropertyValue("phase"));
    assertTrue((boolean) endpointAccessor.getPropertyValue("expectReply"));
    assertEquals("'504'", ((SpelExpression) endpointAccessor.getPropertyValue("statusCodeExpression")).getExpressionString());
    assertEquals("payload", ((SpelExpression) endpointAccessor.getPropertyValue("payloadExpression")).getExpressionString());
    Map<String, Expression> headerExpressions = (Map<String, Expression>) endpointAccessor.getPropertyValue("headerExpressions");
    assertTrue(headerExpressions.containsKey("foo"));
    assertEquals("foo", headerExpressions.get("foo").getValue());
    CrossOrigin crossOrigin = (CrossOrigin) endpointAccessor.getPropertyValue("crossOrigin");
    assertNotNull(crossOrigin);
    assertArrayEquals(new String[] { "foo" }, crossOrigin.getOrigin());
    assertEquals(ResolvableType.forClass(byte[].class), endpointAccessor.getPropertyValue("requestPayloadType"));
    assertSame(this.headerMapper, endpointAccessor.getPropertyValue("headerMapper"));
    assertSame(this.serverCodecConfigurer, endpointAccessor.getPropertyValue("codecConfigurer"));
    assertSame(this.requestedContentTypeResolver, endpointAccessor.getPropertyValue("requestedContentTypeResolver"));
    assertSame(this.reactiveAdapterRegistry, endpointAccessor.getPropertyValue("adapterRegistry"));
}
Also used : CrossOrigin(org.springframework.integration.http.inbound.CrossOrigin) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Map(java.util.Map) Test(org.junit.Test)

Example 4 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-integration by spring-projects.

the class MongoDbOutboundChannelAdapterParserTests method fullConfigWithCollectionExpression.

@Test
public void fullConfigWithCollectionExpression() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-parser-config.xml", this.getClass());
    MongoDbStoringMessageHandler handler = TestUtils.getPropertyValue(context.getBean("fullConfigWithCollectionExpression.adapter"), "handler", MongoDbStoringMessageHandler.class);
    assertEquals("fullConfigWithCollectionExpression.adapter", TestUtils.getPropertyValue(handler, "componentName"));
    assertEquals(false, TestUtils.getPropertyValue(handler, "shouldTrack"));
    assertNotNull(TestUtils.getPropertyValue(handler, "mongoTemplate"));
    assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(handler, "mongoDbFactory"));
    assertNotNull(TestUtils.getPropertyValue(handler, "evaluationContext"));
    assertTrue(TestUtils.getPropertyValue(handler, "collectionNameExpression") instanceof SpelExpression);
    assertEquals("headers.collectionName", TestUtils.getPropertyValue(handler, "collectionNameExpression.expression"));
    context.close();
}
Also used : MongoDbStoringMessageHandler(org.springframework.integration.mongodb.outbound.MongoDbStoringMessageHandler) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 5 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-integration by spring-projects.

the class OutboundChannelAdapterParserTests method testOutboundChannelAdapterWithWithRemoteDirectoryAndFileExpression.

@Test
public void testOutboundChannelAdapterWithWithRemoteDirectoryAndFileExpression() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("OutboundChannelAdapterParserTests-context.xml", this.getClass());
    Object consumer = context.getBean("sftpOutboundAdapterWithExpression");
    assertTrue(consumer instanceof EventDrivenConsumer);
    assertEquals(context.getBean("inputChannel"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("sftpOutboundAdapterWithExpression", ((EventDrivenConsumer) consumer).getComponentName());
    FileTransferringMessageHandler<?> handler = TestUtils.getPropertyValue(consumer, "handler", FileTransferringMessageHandler.class);
    SpelExpression remoteDirectoryExpression = (SpelExpression) TestUtils.getPropertyValue(handler, "remoteFileTemplate.directoryExpressionProcessor.expression");
    assertNotNull(remoteDirectoryExpression);
    assertEquals("'foo' + '/' + 'bar'", remoteDirectoryExpression.getExpressionString());
    FileNameGenerator generator = (FileNameGenerator) TestUtils.getPropertyValue(handler, "remoteFileTemplate.fileNameGenerator");
    Expression fileNameGeneratorExpression = TestUtils.getPropertyValue(generator, "expression", Expression.class);
    assertNotNull(fileNameGeneratorExpression);
    assertEquals("payload.getName() + '-foo'", fileNameGeneratorExpression.getExpressionString());
    assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "remoteFileTemplate.charset"));
    assertNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryDirectoryExpressionProcessor"));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) LiteralExpression(org.springframework.expression.common.LiteralExpression) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) FileNameGenerator(org.springframework.integration.file.FileNameGenerator) Test(org.junit.Test)

Aggregations

SpelExpression (org.springframework.expression.spel.standard.SpelExpression)56 Test (org.junit.jupiter.api.Test)30 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)27 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)18 Test (org.junit.Test)15 Expression (org.springframework.expression.Expression)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)3 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)3 HttpRequestExecutingMessageHandler (org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2