View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.messaging.handler.annotation.support;
18  
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.net.BindException;
22  import java.net.SocketException;
23  
24  import org.junit.Test;
25  
26  import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
27  import org.springframework.stereotype.Controller;
28  import org.springframework.util.ClassUtils;
29  
30  import static org.junit.Assert.*;
31  
32  /**
33   * Test fixture for {@link AnnotationExceptionHandlerMethodResolver} tests.
34   *
35   * @author Rossen Stoyanchev
36   */
37  public class AnnotationExceptionHandlerMethodResolverTests {
38  
39  	@Test
40  	public void resolveMethodFromAnnotation() {
41  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(ExceptionController.class);
42  		IOException exception = new IOException();
43  		assertEquals("handleIOException", resolver.resolveMethod(exception).getName());
44  	}
45  
46  	@Test
47  	public void resolveMethodFromArgument() {
48  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(ExceptionController.class);
49  		IllegalArgumentException exception = new IllegalArgumentException();
50  		assertEquals("handleIllegalArgumentException", resolver.resolveMethod(exception).getName());
51  	}
52  
53  	@Test
54  	public void resolveMethodExceptionSubType() {
55  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(ExceptionController.class);
56  		IOException ioException = new FileNotFoundException();
57  		assertEquals("handleIOException", resolver.resolveMethod(ioException).getName());
58  		SocketException bindException = new BindException();
59  		assertEquals("handleSocketException", resolver.resolveMethod(bindException).getName());
60  	}
61  
62  	@Test
63  	public void resolveMethodBestMatch() {
64  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(ExceptionController.class);
65  		SocketException exception = new SocketException();
66  		assertEquals("handleSocketException", resolver.resolveMethod(exception).getName());
67  	}
68  
69  	@Test
70  	public void resolveMethodNoMatch() {
71  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(ExceptionController.class);
72  		Exception exception = new Exception();
73  		assertNull("1st lookup", resolver.resolveMethod(exception));
74  		assertNull("2nd lookup from cache", resolver.resolveMethod(exception));
75  	}
76  
77  	@Test
78  	public void resolveMethodInherited() {
79  		AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(InheritedController.class);
80  		IOException exception = new IOException();
81  		assertEquals("handleIOException", resolver.resolveMethod(exception).getName());
82  	}
83  
84  	@Test(expected = IllegalStateException.class)
85  	public void ambiguousExceptionMapping() {
86  		new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
87  	}
88  
89  	@Test(expected = IllegalArgumentException.class)
90  	public void noExceptionMapping() {
91  		new AnnotationExceptionHandlerMethodResolver(NoExceptionController.class);
92  	}
93  
94  	@Controller
95  	static class ExceptionController {
96  
97  		public void handle() {}
98  
99  		@MessageExceptionHandler(IOException.class)
100 		public void handleIOException() {
101 		}
102 
103 		@MessageExceptionHandler(SocketException.class)
104 		public void handleSocketException() {
105 		}
106 
107 		@MessageExceptionHandler
108 		public void handleIllegalArgumentException(IllegalArgumentException exception) {
109 		}
110 	}
111 
112 	@Controller
113 	static class InheritedController extends ExceptionController {
114 
115 		@Override
116 		public void handleIOException()	{
117 		}
118 	}
119 
120 	@Controller
121 	static class AmbiguousController {
122 
123 		public void handle() {}
124 
125 		@MessageExceptionHandler({BindException.class, IllegalArgumentException.class})
126 		public String handle1(Exception ex) throws IOException {
127 			return ClassUtils.getShortName(ex.getClass());
128 		}
129 
130 		@MessageExceptionHandler
131 		public String handle2(IllegalArgumentException ex) {
132 			return ClassUtils.getShortName(ex.getClass());
133 		}
134 	}
135 
136 	@Controller
137 	static class NoExceptionController {
138 
139 		@MessageExceptionHandler
140 		public void handle() {
141 		}
142 	}
143 
144 }