/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.web.socket.messaging;

import static org.junit.Assert.*;

import java.util.concurrent.ConcurrentHashMap;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import org.springframework.context.support.StaticApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;


/**
 * Unit tests for {@link WebSocketAnnotationMethodMessageHandler}.
 * @author Rossen Stoyanchev
 */
public class WebSocketAnnotationMethodMessageHandlerTests {

	private TestWebSocketAnnotationMethodMessageHandler messageHandler;

	private StaticApplicationContext applicationContext;


	@Before
	public void setUp() throws Exception {
		this.applicationContext = new StaticApplicationContext();
		this.applicationContext.registerSingleton("controller", TestController.class);
		this.applicationContext.registerSingleton("controllerAdvice", TestControllerAdvice.class);
		this.applicationContext.refresh();

		SubscribableChannel channel = Mockito.mock(SubscribableChannel.class);
		SimpMessageSendingOperations brokerTemplate = new SimpMessagingTemplate(channel);

		this.messageHandler = new TestWebSocketAnnotationMethodMessageHandler(brokerTemplate, channel, channel);
		this.messageHandler.setApplicationContext(this.applicationContext);
		this.messageHandler.afterPropertiesSet();
	}

	@Test
	public void globalException() throws Exception {
		SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
		headers.setSessionId("session1");
		headers.setSessionAttributes(new ConcurrentHashMap<>());
		headers.setDestination("/exception");
		Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
		this.messageHandler.handleMessage(message);

		TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class);
		assertTrue(controllerAdvice.isExceptionHandled());
	}


	@Controller
	private static class TestController {

		@MessageMapping("/exception")
		@SuppressWarnings("unused")
		public void handleWithSimulatedException() {
			throw new IllegalStateException("simulated exception");
		}
	}

	@ControllerAdvice
	private static class TestControllerAdvice {

		private boolean exceptionHandled;


		public boolean isExceptionHandled() {
			return this.exceptionHandled;
		}

		@MessageExceptionHandler
		public void handleException(IllegalStateException ex) {
			this.exceptionHandled = true;
		}
	}


	private static class TestWebSocketAnnotationMethodMessageHandler extends WebSocketAnnotationMethodMessageHandler {


		public TestWebSocketAnnotationMethodMessageHandler(SimpMessageSendingOperations brokerTemplate,
				SubscribableChannel clientInboundChannel, MessageChannel clientOutboundChannel) {

			super(clientInboundChannel, clientOutboundChannel, brokerTemplate);
		}

		public void registerHandler(Object handler) {
			super.detectHandlerMethods(handler);
		}
	}

}

@@ -614,16 +614,17 @@
 se.%0A */%0A
+%0A
 package 
@@ -670,43 +670,8 @@
 g;%0A%0A
-import static org.junit.Assert.*;%0A%0A
 impo
@@ -762,16 +762,17 @@
 t.Test;%0A
+%0A
 import o
@@ -1554,16 +1554,50 @@
 dvice;%0A%0A
+import static org.junit.Assert.*;%0A
 %0A/**%0A * 
@@ -3239,38 +3239,8 @@
 n%22)%0A
-%09%09@SuppressWarnings(%22unused%22)%0A
 %09%09pu
@@ -3656,17 +3656,16 @@
 %09%09%7D%0A%09%7D%0A%0A
-%0A
 %09private
@@ -3773,17 +3773,16 @@
 dler %7B%0A%0A
-%0A
 %09%09public
@@ -4036,101 +4036,8 @@
 %0A%09%09%7D
-%0A%0A%09%09public void registerHandler(Object handler) %7B%0A%09%09%09super.detectHandlerMethods(handler);%0A%09%09%7D
 %0A%09%7D%0A

Clean up warnings