Search in sources :

Example 21 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method copyHeaders.

@Test
public void copyHeaders() {
    Map<String, Object> map1 = new HashMap<>();
    map1.put("foo", "bar");
    GenericMessage<String> message = new GenericMessage<>("payload", map1);
    MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
    Map<String, Object> map2 = new HashMap<>();
    map2.put("foo", "BAR");
    map2.put("bar", "baz");
    accessor.copyHeaders(map2);
    MessageHeaders actual = accessor.getMessageHeaders();
    assertEquals(3, actual.size());
    assertEquals("BAR", actual.get("foo"));
    assertEquals("baz", actual.get("bar"));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 22 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method leaveMutable.

@Test
public void leaveMutable() {
    MessageHeaderAccessor accessor = new MessageHeaderAccessor();
    accessor.setHeader("foo", "bar");
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<?> message = MessageBuilder.createMessage("payload", headers);
    accessor.setHeader("foo", "baz");
    assertEquals("baz", headers.get("foo"));
    assertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class));
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 23 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method removeHeaders.

@Test
public void removeHeaders() {
    Map<String, Object> map = new HashMap<>();
    map.put("foo", "bar");
    map.put("bar", "baz");
    GenericMessage<String> message = new GenericMessage<>("payload", map);
    MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
    accessor.removeHeaders("fo*");
    MessageHeaders actual = accessor.getMessageHeaders();
    assertEquals(2, actual.size());
    assertNull(actual.get("foo"));
    assertEquals("baz", actual.get("bar"));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 24 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class StompBrokerRelayMessageHandlerTests method systemSubscription.

@Test
public void systemSubscription() throws Exception {
    MessageHandler handler = mock(MessageHandler.class);
    this.brokerRelay.setSystemSubscriptions(Collections.singletonMap("/topic/foo", handler));
    this.brokerRelay.start();
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    this.tcpClient.handleMessage(MessageBuilder.createMessage(new byte[0], headers));
    assertEquals(2, this.tcpClient.getSentMessages().size());
    assertEquals(StompCommand.CONNECT, this.tcpClient.getSentHeaders(0).getCommand());
    assertEquals(StompCommand.SUBSCRIBE, this.tcpClient.getSentHeaders(1).getCommand());
    assertEquals("/topic/foo", this.tcpClient.getSentHeaders(1).getDestination());
    Message<byte[]> message = message(StompCommand.MESSAGE, null, null, "/topic/foo");
    this.tcpClient.handleMessage(message);
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(handler).handleMessage(captor.capture());
    assertSame(message, captor.getValue());
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 25 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class UserRegistryMessageHandlerTests method broadcastRegistry.

@Test
public void broadcastRegistry() throws Exception {
    TestSimpUser simpUser1 = new TestSimpUser("joe");
    TestSimpUser simpUser2 = new TestSimpUser("jane");
    simpUser1.addSessions(new TestSimpSession("123"));
    simpUser1.addSessions(new TestSimpSession("456"));
    HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2));
    when(this.localRegistry.getUsers()).thenReturn(simpUsers);
    getUserRegistryTask().run();
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.brokerChannel).send(captor.capture());
    Message<?> message = captor.getValue();
    assertNotNull(message);
    MessageHeaders headers = message.getHeaders();
    assertEquals("/topic/simp-user-registry", SimpMessageHeaderAccessor.getDestination(headers));
    MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(mock(SimpUserRegistry.class));
    remoteRegistry.addRemoteRegistryDto(message, this.converter, 20000);
    assertEquals(2, remoteRegistry.getUserCount());
    assertNotNull(remoteRegistry.getUser("joe"));
    assertNotNull(remoteRegistry.getUser("jane"));
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)54 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)9 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1