@Test
 @SuppressWarnings({"unchecked", "rawtypes"})
 public void resolveChannelNameFromMapAndCustomeResolver() {
   final StaticApplicationContext context = new StaticApplicationContext();
   ManagedMap channelMappings = new ManagedMap();
   channelMappings.put("testKey", "testChannel");
   RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
   routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
   routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
   routerBeanDefinition.getPropertyValues().addPropertyValue("channelMappings", channelMappings);
   routerBeanDefinition.getPropertyValues().addPropertyValue("beanFactory", context);
   routerBeanDefinition
       .getPropertyValues()
       .addPropertyValue(
           "channelResolver",
           (DestinationResolver<MessageChannel>)
               channelName -> context.getBean("anotherChannel", MessageChannel.class));
   context.registerBeanDefinition("router", routerBeanDefinition);
   context.registerBeanDefinition("testChannel", new RootBeanDefinition(QueueChannel.class));
   context.registerBeanDefinition("anotherChannel", new RootBeanDefinition(QueueChannel.class));
   context.refresh();
   MessageHandler handler = (MessageHandler) context.getBean("router");
   Message<?> message =
       MessageBuilder.withPayload("test").setHeader("testHeaderName", "testKey").build();
   handler.handleMessage(message);
   QueueChannel channel = (QueueChannel) context.getBean("anotherChannel");
   Message<?> result = channel.receive(1000);
   assertNotNull(result);
   assertSame(message, result);
   context.close();
 }
  @Test
  public void resolveChannelNameFromContext() {
    StaticApplicationContext context = new StaticApplicationContext();
    RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
    routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
    routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
    context.registerBeanDefinition("router", routerBeanDefinition);
    context.registerBeanDefinition("testChannel", new RootBeanDefinition(QueueChannel.class));
    context.registerBeanDefinition("newChannel", new RootBeanDefinition(QueueChannel.class));
    context.refresh();
    MessageHandler handler = (MessageHandler) context.getBean("router");
    Message<?> message =
        MessageBuilder.withPayload("test").setHeader("testHeaderName", "testChannel").build();
    handler.handleMessage(message);
    QueueChannel channel = (QueueChannel) context.getBean("testChannel");
    Message<?> result = channel.receive(1000);
    assertNotNull(result);
    assertSame(message, result);

    // validate dynamics
    HeaderValueRouter router = (HeaderValueRouter) context.getBean("router");
    router.setChannelMapping("testChannel", "newChannel");
    router.handleMessage(message);
    QueueChannel newChannel = (QueueChannel) context.getBean("newChannel");
    result = newChannel.receive(10);
    assertNotNull(result);

    router.removeChannelMapping("testChannel");
    router.handleMessage(message);
    result = channel.receive(1000);
    assertNotNull(result);
    assertSame(message, result);
    context.close();
  }
 @Test
 public void objectPayloadHeaderMapAndStringHeaders() throws Exception {
   MessageHandler handler =
       this.getHandler(
           "objectPayloadHeaderMapAndStringHeaders",
           String.class,
           Map.class,
           String.class,
           Object.class);
   Object payload = "test";
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   headers.put("baz", 99);
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertEquals(payload, bean.lastPayload);
   assertTrue(bean.lastHeaders.containsKey("foo"));
   assertEquals("1", bean.lastHeaders.get("foo"));
   assertTrue(bean.lastHeaders.containsKey("bar"));
   assertEquals("2", bean.lastHeaders.get("bar"));
   assertTrue(bean.lastHeaders.containsKey("baz"));
   assertTrue(bean.lastHeaders.containsKey("foo2"));
   assertEquals("1", bean.lastHeaders.get("foo2"));
   assertTrue(bean.lastHeaders.containsKey("bar2"));
   assertEquals("2", bean.lastHeaders.get("bar2"));
 }
 @Test
 public void advised() {
   MessageHandler handler =
       TestUtils.getPropertyValue(context.getBean("advised"), "handler", MessageHandler.class);
   handler.handleMessage(new GenericMessage<String>("foo"));
   assertEquals(1, adviceCalled);
 }
 @Test
 public void dynamicChannelCache() {
   StaticApplicationContext context = new StaticApplicationContext();
   RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
   routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
   routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
   routerBeanDefinition.getPropertyValues().addPropertyValue("dynamicChannelLimit", "2");
   context.registerBeanDefinition("router", routerBeanDefinition);
   context.registerBeanDefinition("channel1", new RootBeanDefinition(QueueChannel.class));
   context.registerBeanDefinition("channel2", new RootBeanDefinition(QueueChannel.class));
   context.registerBeanDefinition("channel3", new RootBeanDefinition(QueueChannel.class));
   context.refresh();
   MessageHandler handler = (MessageHandler) context.getBean("router");
   String channels = "channel1, channel2, channel1, channel3";
   Message<?> message =
       MessageBuilder.withPayload("test").setHeader("testHeaderName", channels).build();
   handler.handleMessage(message);
   QueueChannel channel1 = (QueueChannel) context.getBean("channel1");
   QueueChannel channel2 = (QueueChannel) context.getBean("channel2");
   QueueChannel channel3 = (QueueChannel) context.getBean("channel3");
   assertThat(channel1.getQueueSize(), equalTo(2));
   assertThat(channel2.getQueueSize(), equalTo(1));
   assertThat(channel3.getQueueSize(), equalTo(1));
   assertThat(
       context.getBean(HeaderValueRouter.class).getDynamicChannelNames(),
       contains("channel1", "channel3"));
   context.close();
 }
 @Test
 public void resolveMultipleChannelsWithCommaDelimitedString() {
   StaticApplicationContext context = new StaticApplicationContext();
   RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
   routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
   routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
   context.registerBeanDefinition("router", routerBeanDefinition);
   context.registerBeanDefinition("channel1", new RootBeanDefinition(QueueChannel.class));
   context.registerBeanDefinition("channel2", new RootBeanDefinition(QueueChannel.class));
   context.refresh();
   MessageHandler handler = (MessageHandler) context.getBean("router");
   String channels = "channel1, channel2";
   Message<?> message =
       MessageBuilder.withPayload("test").setHeader("testHeaderName", channels).build();
   handler.handleMessage(message);
   QueueChannel channel1 = (QueueChannel) context.getBean("channel1");
   QueueChannel channel2 = (QueueChannel) context.getBean("channel2");
   Message<?> result1 = channel1.receive(1000);
   Message<?> result2 = channel2.receive(1000);
   assertNotNull(result1);
   assertNotNull(result2);
   assertSame(message, result1);
   assertSame(message, result2);
   context.close();
 }
  @Test
  public void adviceCalled() throws Exception {
    setUp("advisedStoredProcOutboundChannelAdapterTest.xml", getClass());

    MessageHandler handler =
        TestUtils.getPropertyValue(this.consumer, "handler", MessageHandler.class);
    handler.handleMessage(new GenericMessage<String>("foo"));
    assertEquals(1, adviceCalled);
  }
 @Test
 public void propertiesOnlyNoAnnotationsWithMapPayload() throws Exception {
   MessageHandler handler = this.getHandler("propertiesOnlyNoAnnotations", Properties.class);
   Map<String, Object> payload = new HashMap<String, Object>();
   payload.put("payload", 1);
   Map<String, Object> headers = new HashMap<String, Object>();
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertEquals(payload, bean.lastPayload);
   assertNull(bean.lastHeaders);
 }
 @Test
 public void singleStringHeaderOnlyWithStringPayload() throws Exception {
   MessageHandler handler = this.getHandler("singleStringHeaderOnly", String.class);
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Message<?> message = MessageBuilder.withPayload("test").copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertEquals("1", bean.lastHeaders.get("foo"));
   assertNull(bean.lastHeaders.get("bar"));
 }
 @Test
 public void singleObjectHeaderOnlyWithIntegerPayload() throws Exception {
   MessageHandler handler = this.getHandler("singleObjectHeaderOnly", Object.class);
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", new Integer(123));
   headers.put("bar", new Integer(456));
   Message<?> message = MessageBuilder.withPayload(new Integer(789)).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertEquals(new Integer(123), bean.lastHeaders.get("foo"));
   assertNull(bean.lastHeaders.get("bar"));
 }
 protected boolean tryOptimizedDispatch(Message<?> message) {
   MessageHandler handler = this.theOneHandler;
   if (handler != null) {
     try {
       handler.handleMessage(message);
       return true;
     } catch (Exception e) {
       throw wrapExceptionIfNecessary(message, e);
     }
   }
   return false;
 }
 @Test
 public void twoPayloadExpressions() throws Exception {
   MessageHandler handler = this.getHandler("twoPayloadExpressions", String.class, String.class);
   Map<String, Object> payload = new HashMap<String, Object>();
   payload.put("foo", new Integer(123));
   payload.put("bar", new Integer(456));
   Message<?> message = MessageBuilder.withPayload(payload).build();
   handler.handleMessage(message);
   assertNull(bean.lastHeaders);
   assertNotNull(bean.lastPayload);
   assertEquals("123456", bean.lastPayload);
 }
 @Test
 public void propertiesOnlyNoAnnotationsWithIntegerPayload() throws Exception {
   MessageHandler handler = this.getHandler("propertiesOnlyNoAnnotations", Properties.class);
   Integer payload = new Integer(123);
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertTrue(bean.lastHeaders.containsKey("foo"));
   assertTrue(bean.lastHeaders.containsKey("bar"));
 }
 @Test
 public void payloadMapAndHeaderString() throws Exception {
   MessageHandler handler = this.getHandler("payloadMapAndHeaderString", Map.class, String.class);
   Map<String, Object> payload = new HashMap<String, Object>();
   payload.put("abc", 1);
   payload.put("xyz", "test");
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertEquals(payload, bean.lastPayload);
   assertTrue(bean.lastHeaders.containsKey("foo"));
   assertFalse(bean.lastHeaders.containsKey("bar"));
 }
 @Test
 public void headerPropertiesOnlyWithStringPayload() throws Exception {
   MessageHandler handler = this.getHandler("headerPropertiesOnly", Properties.class);
   String payload = "test";
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   headers.put("baz", 99);
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertTrue(bean.lastHeaders.containsKey("foo"));
   assertTrue(bean.lastHeaders.containsKey("bar"));
   // assertFalse(bean.lastHeaders.containsKey("baz"));
 }
  @Test
  public void exportAdvisedHandler() throws Exception {

    DummyInterceptor interceptor = new DummyInterceptor();
    NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(interceptor);
    advisor.addMethodName("handleMessage");

    ProxyFactory factory = new ProxyFactory(handler);
    factory.addAdvisor(advisor);
    MessageHandler advised = (MessageHandler) factory.getProxy();

    MessageHandler exported =
        (MessageHandler) mBeanExporter.postProcessAfterInitialization(advised, "test");
    exported.handleMessage(MessageBuilder.withPayload("test").build());
  }
 @Test
 public void mapAndAnnotatedStringHeaderWithStringPayload() throws Exception {
   MessageHandler handler =
       this.getHandler(
           "mapAndAnnotatedStringHeaderExpectingMapAsHeaders", Map.class, String.class);
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Message<?> message = MessageBuilder.withPayload("test").copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertEquals("1", bean.lastHeaders.get("foo"));
   assertEquals("2", bean.lastHeaders.get("bar"));
   assertEquals("1", bean.lastHeaders.get("foo2"));
 }
 @Test
 public void twoMapsWithAnnotationsWithMapPayload() throws Exception {
   MessageHandler handler = this.getHandler("twoMapsWithAnnotations", Map.class, Map.class);
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Map<String, Object> payloadMap = new HashMap<String, Object>();
   payloadMap.put("baz", "99");
   Message<?> message = MessageBuilder.withPayload(payloadMap).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertEquals("1", bean.lastHeaders.get("foo"));
   assertEquals("2", bean.lastHeaders.get("bar"));
   assertEquals("1", bean.lastHeaders.get("foo2"));
   assertEquals("2", bean.lastHeaders.get("bar2"));
   assertEquals(null, bean.lastHeaders.get("baz"));
 }
 @Test
 public void headerMapOnlyWithMapPayload() throws Exception {
   MessageHandler handler = this.getHandler("headerMapOnly", Map.class);
   Map<String, Object> payload = new HashMap<String, Object>();
   payload.put("abc", 1);
   payload.put("xyz", "test");
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   headers.put("baz", 99);
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastPayload);
   assertTrue(bean.lastHeaders.containsKey("foo"));
   assertTrue(bean.lastHeaders.containsKey("bar"));
   assertTrue(bean.lastHeaders.containsKey("baz"));
 }
 @Test
 public void propertiesOnlyNoAnnotationsWithStringPayload() throws Exception {
   MessageHandler handler = this.getHandler("propertiesOnlyNoAnnotations", Properties.class);
   String payload = "payload=abc";
   Map<String, Object> headers = new HashMap<String, Object>();
   headers.put("foo", "1");
   headers.put("bar", "2");
   Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
   handler.handleMessage(message);
   assertNull(bean.lastHeaders);
   // String payload should have been converted to Properties
   assertNotNull(bean.lastPayload);
   assertTrue(bean.lastPayload instanceof Properties);
   Properties payloadProps = (Properties) bean.lastPayload;
   assertTrue(payloadProps.containsKey("payload"));
   assertEquals("abc", payloadProps.get("payload"));
 }
 @Test
 public void channelAsHeaderValue() {
   StaticApplicationContext context = new StaticApplicationContext();
   RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
   routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
   routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
   context.registerBeanDefinition("router", routerBeanDefinition);
   context.refresh();
   MessageHandler handler = (MessageHandler) context.getBean("router");
   QueueChannel testChannel = new QueueChannel();
   Message<?> message =
       MessageBuilder.withPayload("test").setHeader("testHeaderName", testChannel).build();
   handler.handleMessage(message);
   Message<?> result = testChannel.receive(1000);
   assertNotNull(result);
   assertSame(message, result);
   context.close();
 }
 protected final void setAdviceChainIfPresent(
     String beanName, T annotation, MessageHandler handler) {
   String[] adviceChainNames =
       (String[]) AnnotationUtils.getValue(annotation, ADVICE_CHAIN_ATTRIBUTE);
   if (adviceChainNames != null && adviceChainNames.length > 0) {
     if (!(handler instanceof AbstractReplyProducingMessageHandler)) {
       throw new IllegalArgumentException(
           "Cannot apply advice chain to " + handler.getClass().getName());
     }
     List<Advice> adviceChain = new ArrayList<Advice>();
     for (String adviceChainName : adviceChainNames) {
       Object adviceChainBean = this.beanFactory.getBean(adviceChainName);
       if (adviceChainBean instanceof Advice) {
         adviceChain.add((Advice) adviceChainBean);
       } else if (adviceChainBean instanceof Advice[]) {
         for (Advice advice : (Advice[]) adviceChainBean) {
           adviceChain.add(advice);
         }
       } else if (adviceChainBean instanceof Collection) {
         @SuppressWarnings("unchecked")
         Collection<Advice> adviceChainEntries = (Collection<Advice>) adviceChainBean;
         for (Advice advice : adviceChainEntries) {
           adviceChain.add(advice);
         }
       } else {
         throw new IllegalArgumentException(
             "Invalid advice chain type:"
                 + adviceChainName.getClass().getName()
                 + " for bean '"
                 + beanName
                 + "'");
       }
     }
     ((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain);
   }
 }