@Test
  public void copyHeadersNullMap() {
    MessageHeaderAccessor headers = new MessageHeaderAccessor();
    headers.copyHeaders(null);
    headers.copyHeadersIfAbsent(null);

    assertEquals(0, headers.toMap().size());
  }
 /**
  * Return a mutable {@code MessageHeaderAccessor} for the given message attempting to match the
  * type of accessor used to create the message headers, or otherwise wrapping the message with a
  * {@code MessageHeaderAccessor} instance.
  *
  * <p>This is for cases where a header needs to be updated in generic code while preserving the
  * accessor type for downstream processing.
  *
  * @return an accessor of the required type (never {@code null})
  * @since 4.1
  */
 public static MessageHeaderAccessor getMutableAccessor(Message<?> message) {
   if (message.getHeaders() instanceof MutableMessageHeaders) {
     MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) message.getHeaders();
     MessageHeaderAccessor accessor = mutableHeaders.getMessageHeaderAccessor();
     if (accessor != null) {
       return (accessor.isMutable() ? accessor : accessor.createAccessor(message));
     }
   }
   return new MessageHeaderAccessor(message);
 }
  /**
   * A variation of {@link #getAccessor(org.springframework.messaging.Message, Class)} with a {@code
   * MessageHeaders} instance instead of a {@code Message}.
   *
   * <p>This is for cases when a full message may not have been created yet.
   *
   * @return an accessor instance of the specified type, or {@code null} if none
   * @since 4.1
   */
  @SuppressWarnings("unchecked")
  public static <T extends MessageHeaderAccessor> T getAccessor(
      MessageHeaders messageHeaders, Class<T> requiredType) {

    if (messageHeaders instanceof MutableMessageHeaders) {
      MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders;
      MessageHeaderAccessor headerAccessor = mutableHeaders.getMessageHeaderAccessor();
      if (requiredType.isAssignableFrom(headerAccessor.getClass())) {
        return (T) headerAccessor;
      }
    }
    return null;
  }
  @Test
  public void wrapMessage() {
    Map<String, Object> original = new HashMap<>();
    original.put("foo", "bar");
    original.put("bar", "baz");
    GenericMessage<String> message = new GenericMessage<>("p", original);

    MessageHeaderAccessor headers = new MessageHeaderAccessor(message);
    Map<String, Object> actual = headers.toMap();

    assertEquals(4, actual.size());
    assertNotNull(actual.get(MessageHeaders.ID));
    assertNotNull(actual.get(MessageHeaders.TIMESTAMP));
    assertEquals("bar", actual.get("foo"));
    assertEquals("baz", actual.get("bar"));
  }
    public WebSocketMessage<?> encode(
        Message<byte[]> message, Class<? extends WebSocketSession> sessionType) {
      StompHeaderAccessor accessor =
          MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
      Assert.notNull(accessor, "No StompHeaderAccessor available");
      byte[] payload = message.getPayload();
      byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload);

      boolean useBinary =
          (payload.length > 0
              && !(SockJsSession.class.isAssignableFrom(sessionType))
              && MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(
                  accessor.getContentType()));

      return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes));
    }
 @Test
 public void empty() {
   MessageHeaderAccessor headers = new MessageHeaderAccessor();
   assertEquals(Collections.emptyMap(), headers.toMap());
 }