Search in sources :

Example 1 with IntegrationEvaluationContextFactoryBean

use of org.springframework.integration.config.IntegrationEvaluationContextFactoryBean in project spring-integration by spring-projects.

the class ExpressionEvaluatingMessageProcessorTests method testProcessMessageWithParameterCoercionToNonPrimitive.

@Test
public void testProcessMessageWithParameterCoercionToNonPrimitive() throws Exception {
    class TestTarget {

        @SuppressWarnings("unused")
        public String find(Resource[] resources) {
            return Arrays.toString(resources);
        }
    }
    Expression expression = expressionParser.parseExpression("#target.find(payload)");
    ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
    AbstractApplicationContext applicationContext = new GenericApplicationContext();
    processor.setBeanFactory(applicationContext);
    IntegrationEvaluationContextFactoryBean factoryBean = new IntegrationEvaluationContextFactoryBean();
    factoryBean.setApplicationContext(applicationContext);
    applicationContext.getBeanFactory().registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, factoryBean.getObject());
    applicationContext.refresh();
    processor.afterPropertiesSet();
    EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
    evaluationContext.setVariable("target", new TestTarget());
    String result = processor.processMessage(new GenericMessage<>("classpath*:*-test.xml"));
    assertTrue("Wrong result: " + result, result.contains("log4j2-test.xml"));
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Example 2 with IntegrationEvaluationContextFactoryBean

use of org.springframework.integration.config.IntegrationEvaluationContextFactoryBean in project spring-integration by spring-projects.

the class OutboundGatewayTests method testExpressionsBeanResolver.

@Test
@SuppressWarnings("unchecked")
public void testExpressionsBeanResolver() throws Exception {
    ApplicationContext context = mock(ApplicationContext.class);
    doAnswer(invocation -> invocation.getArguments()[0] + "bar").when(context).getBean(anyString());
    when(context.containsBean(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)).thenReturn(true);
    when(context.getBean(SpelPropertyAccessorRegistrar.class)).thenThrow(NoSuchBeanDefinitionException.class);
    IntegrationEvaluationContextFactoryBean integrationEvaluationContextFactoryBean = new IntegrationEvaluationContextFactoryBean();
    integrationEvaluationContextFactoryBean.setApplicationContext(context);
    integrationEvaluationContextFactoryBean.afterPropertiesSet();
    StandardEvaluationContext evalContext = integrationEvaluationContextFactoryBean.getObject();
    when(context.getBean(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, StandardEvaluationContext.class)).thenReturn(evalContext);
    RabbitTemplate template = spy(new RabbitTemplate());
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(template);
    endpoint.setRoutingKeyExpression(PARSER.parseExpression("@foo"));
    endpoint.setExchangeNameExpression(PARSER.parseExpression("@bar"));
    endpoint.setConfirmCorrelationExpressionString("@baz");
    endpoint.setBeanFactory(context);
    endpoint.afterPropertiesSet();
    Message<?> message = new GenericMessage<String>("Hello, world!");
    assertEquals("foobar", TestUtils.getPropertyValue(endpoint, "routingKeyGenerator", MessageProcessor.class).processMessage(message));
    assertEquals("barbar", TestUtils.getPropertyValue(endpoint, "exchangeNameGenerator", MessageProcessor.class).processMessage(message));
    assertEquals("bazbar", TestUtils.getPropertyValue(endpoint, "correlationDataGenerator", MessageProcessor.class).processMessage(message));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Example 3 with IntegrationEvaluationContextFactoryBean

use of org.springframework.integration.config.IntegrationEvaluationContextFactoryBean in project spring-integration by spring-projects.

the class ExpressionUtilsTests method testEvaluationContext.

@Test
public void testEvaluationContext() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class));
    context.registerBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, new RootBeanDefinition(ConversionServiceFactoryBean.class));
    context.refresh();
    StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(context);
    assertNotNull(evalContext.getBeanResolver());
    assertNotNull(evalContext.getTypeConverter());
    IntegrationEvaluationContextFactoryBean factory = context.getBean("&" + IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, IntegrationEvaluationContextFactoryBean.class);
    assertSame(evalContext.getTypeConverter(), TestUtils.getPropertyValue(factory, "typeConverter"));
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConversionServiceFactoryBean(org.springframework.context.support.ConversionServiceFactoryBean) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Example 4 with IntegrationEvaluationContextFactoryBean

use of org.springframework.integration.config.IntegrationEvaluationContextFactoryBean in project spring-integration by spring-projects.

the class PublisherExpressionTests method setup.

@Before
public void setup() throws Exception {
    context.registerSingleton("testChannel", QueueChannel.class);
    IntegrationEvaluationContextFactoryBean factory = new IntegrationEvaluationContextFactoryBean();
    factory.setApplicationContext(context);
    factory.afterPropertiesSet();
    EvaluationContext ec = factory.getObject();
    context.getBeanFactory().registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, ec);
    context.getBeanFactory().registerSingleton("foo", "foo");
}
Also used : EvaluationContext(org.springframework.expression.EvaluationContext) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Before(org.junit.Before)

Example 5 with IntegrationEvaluationContextFactoryBean

use of org.springframework.integration.config.IntegrationEvaluationContextFactoryBean in project spring-integration by spring-projects.

the class ParentContextTests method testSpelBeanReferencesInChildAndParent.

/**
 * Verifies that beans in hierarchical contexts get an evaluation context that has the proper
 * BeanResolver. Verifies that the two Foos in the parent context get an evaluation context
 * with the same bean resolver. Verifies that the one Foo in the child context gets a different
 * bean resolver. Verifies that bean references in SpEL expressions to beans in the child
 * and parent contexts work. Verifies that PropertyAccessors are inherited in the child context
 * and the parent's ones are last in the propertyAccessors list of EvaluationContext.
 * Verifies that SpEL functions are inherited from parent context and overridden with the same 'id'.
 * Verifies that child and parent contexts can have different message builders.
 * <p>
 * Only single test method is allowed for 'ParentContext-context.xml',
 * since it relies on static 'evalContexts' variable.
 */
@Test
@SuppressWarnings("unchecked")
public void testSpelBeanReferencesInChildAndParent() throws Exception {
    AbstractApplicationContext parent = new ClassPathXmlApplicationContext("ParentContext-context.xml", this.getClass());
    Object parentEvaluationContextFactoryBean = parent.getBean(IntegrationEvaluationContextFactoryBean.class);
    Map<?, ?> parentFunctions = TestUtils.getPropertyValue(parentEvaluationContextFactoryBean, "functions", Map.class);
    assertEquals(4, parentFunctions.size());
    Object jsonPath = parentFunctions.get("jsonPath");
    assertNotNull(jsonPath);
    assertThat((Method) jsonPath, Matchers.isOneOf(JsonPathUtils.class.getMethods()));
    assertEquals(2, evalContexts.size());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(parent);
    child.setConfigLocation("org/springframework/integration/expression/ChildContext-context.xml");
    child.refresh();
    Object childEvaluationContextFactoryBean = child.getBean(IntegrationEvaluationContextFactoryBean.class);
    Map<?, ?> childFunctions = TestUtils.getPropertyValue(childEvaluationContextFactoryBean, "functions", Map.class);
    assertEquals(5, childFunctions.size());
    assertTrue(childFunctions.containsKey("barParent"));
    assertTrue(childFunctions.containsKey("fooFunc"));
    jsonPath = childFunctions.get("jsonPath");
    assertNotNull(jsonPath);
    assertThat((Method) jsonPath, Matchers.not(Matchers.isOneOf(JsonPathUtils.class.getMethods())));
    assertEquals(3, evalContexts.size());
    assertSame(evalContexts.get(0).getBeanResolver(), evalContexts.get(1).getBeanResolver());
    List<PropertyAccessor> propertyAccessors = evalContexts.get(0).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    PropertyAccessor parentPropertyAccessorOverride = parent.getBean("jsonPropertyAccessor", PropertyAccessor.class);
    PropertyAccessor parentPropertyAccessor = parent.getBean("parentJsonPropertyAccessor", PropertyAccessor.class);
    assertTrue(propertyAccessors.contains(parentPropertyAccessorOverride));
    assertTrue(propertyAccessors.contains(parentPropertyAccessor));
    assertTrue(propertyAccessors.indexOf(parentPropertyAccessorOverride) > propertyAccessors.indexOf(parentPropertyAccessor));
    Map<String, Object> variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(0), "variables");
    assertEquals(4, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("jsonPath"));
    assertNotSame(evalContexts.get(1).getBeanResolver(), evalContexts.get(2).getBeanResolver());
    propertyAccessors = evalContexts.get(1).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    assertTrue(propertyAccessors.contains(parentPropertyAccessorOverride));
    variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(1), "variables");
    assertEquals(4, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("jsonPath"));
    propertyAccessors = evalContexts.get(2).getPropertyAccessors();
    assertEquals(4, propertyAccessors.size());
    PropertyAccessor childPropertyAccessor = child.getBean("jsonPropertyAccessor", PropertyAccessor.class);
    assertTrue(propertyAccessors.contains(childPropertyAccessor));
    assertTrue(propertyAccessors.contains(parentPropertyAccessor));
    assertFalse(propertyAccessors.contains(parentPropertyAccessorOverride));
    assertTrue(propertyAccessors.indexOf(childPropertyAccessor) < propertyAccessors.indexOf(parentPropertyAccessor));
    variables = (Map<String, Object>) TestUtils.getPropertyValue(evalContexts.get(2), "variables");
    assertEquals(5, variables.size());
    assertTrue(variables.containsKey("bar"));
    assertTrue(variables.containsKey("barParent"));
    assertTrue(variables.containsKey("fooFunc"));
    assertTrue(variables.containsKey("barChild"));
    assertTrue(variables.containsKey("jsonPath"));
    // Test transformer expressions
    child.getBean("input", MessageChannel.class).send(new GenericMessage<String>("baz"));
    Message<?> out = child.getBean("output", QueueChannel.class).receive(0);
    assertNotNull(out);
    assertEquals("foobar", out.getPayload());
    child.getBean("parentIn", MessageChannel.class).send(MutableMessageBuilder.withPayload("bar").build());
    out = child.getBean("parentOut", QueueChannel.class).receive(0);
    assertNotNull(out);
    assertThat(out, instanceOf(GenericMessage.class));
    assertEquals("foo", out.getPayload());
    IntegrationEvaluationContextFactoryBean evaluationContextFactoryBean = child.getBean("&" + IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, IntegrationEvaluationContextFactoryBean.class);
    try {
        evaluationContextFactoryBean.setPropertyAccessors(Collections.<String, PropertyAccessor>emptyMap());
        fail("IllegalArgumentException expected.");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(IllegalArgumentException.class));
    }
    parent.getBean("fromParentToChild", MessageChannel.class).send(new GenericMessage<String>("foo"));
    out = child.getBean("output", QueueChannel.class).receive(0);
    assertNotNull(out);
    assertEquals("org.springframework.integration.support.MutableMessage", out.getClass().getName());
    assertEquals("FOO", out.getPayload());
    assertTrue(parent.containsBean(IntegrationContextUtils.TO_STRING_FRIENDLY_JSON_NODE_TO_STRING_CONVERTER_BEAN_NAME));
    assertTrue(child.containsBean(IntegrationContextUtils.TO_STRING_FRIENDLY_JSON_NODE_TO_STRING_CONVERTER_BEAN_NAME));
    Object converterRegistrar = parent.getBean(IntegrationContextUtils.CONVERTER_REGISTRAR_BEAN_NAME);
    assertNotNull(converterRegistrar);
    Set<?> converters = TestUtils.getPropertyValue(converterRegistrar, "converters", Set.class);
    boolean toStringFriendlyJsonNodeToStringConverterPresent = false;
    for (Object converter : converters) {
        if ("ToStringFriendlyJsonNodeToStringConverter".equals(converter.getClass().getSimpleName())) {
            toStringFriendlyJsonNodeToStringConverterPresent = true;
            break;
        }
    }
    assertTrue(toStringFriendlyJsonNodeToStringConverterPresent);
    MessageChannel input = parent.getBean("testJsonNodeToStringConverterInputChannel", MessageChannel.class);
    PollableChannel output = parent.getBean("testJsonNodeToStringConverterOutputChannel", PollableChannel.class);
    TestPerson person = new TestPerson();
    person.setFirstName("John");
    input.send(new GenericMessage<Object>(person));
    Message<?> result = output.receive(1000);
    assertNotNull(result);
    assertEquals("JOHN", result.getPayload());
    child.close();
    parent.close();
}
Also used : PropertyAccessor(org.springframework.expression.PropertyAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) JsonPathUtils(org.springframework.integration.json.JsonPathUtils) BeansException(org.springframework.beans.BeansException) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) TestPerson(org.springframework.integration.json.TestPerson) Map(java.util.Map) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Aggregations

IntegrationEvaluationContextFactoryBean (org.springframework.integration.config.IntegrationEvaluationContextFactoryBean)5 Test (org.junit.Test)4 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 EvaluationContext (org.springframework.expression.EvaluationContext)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Map (java.util.Map)1 Before (org.junit.Before)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 BeansException (org.springframework.beans.BeansException)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConversionServiceFactoryBean (org.springframework.context.support.ConversionServiceFactoryBean)1 Expression (org.springframework.expression.Expression)1 PropertyAccessor (org.springframework.expression.PropertyAccessor)1 AmqpOutboundEndpoint (org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 JsonPathUtils (org.springframework.integration.json.JsonPathUtils)1