View Javadoc
1   /*
2    * Copyright 2002-2010 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.web.portlet.mvc.annotation;
18  
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.net.BindException;
22  import java.net.SocketException;
23  import javax.portlet.PortletRequest;
24  import javax.portlet.PortletResponse;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import org.springframework.mock.web.portlet.MockRenderRequest;
30  import org.springframework.mock.web.portlet.MockRenderResponse;
31  import org.springframework.stereotype.Controller;
32  import org.springframework.util.ClassUtils;
33  import org.springframework.web.bind.annotation.ExceptionHandler;
34  import org.springframework.web.portlet.ModelAndView;
35  
36  import static org.junit.Assert.*;
37  
38  /**
39   * @author Arjen Poutsma
40   * @author Juergen Hoeller
41   */
42  public class AnnotationMethodHandlerExceptionResolverTests {
43  
44  	private AnnotationMethodHandlerExceptionResolver exceptionResolver;
45  
46  	private MockRenderRequest request;
47  
48  	private MockRenderResponse response;
49  
50  
51  	@Before
52  	public void setUp() {
53  		exceptionResolver = new AnnotationMethodHandlerExceptionResolver();
54  		request = new MockRenderRequest();
55  		response = new MockRenderResponse();
56  	}
57  
58  	@Test
59  	public void simpleWithIOException() {
60  		IOException ex = new IOException();
61  		SimpleController controller = new SimpleController();
62  		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
63  		assertNotNull("No ModelAndView returned", mav);
64  		assertEquals("Invalid view name returned", "X:IOException", mav.getViewName());
65  	}
66  
67  	@Test
68  	public void simpleWithSocketException() {
69  		SocketException ex = new SocketException();
70  		SimpleController controller = new SimpleController();
71  		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
72  		assertNotNull("No ModelAndView returned", mav);
73  		assertEquals("Invalid view name returned", "Y:SocketException", mav.getViewName());
74  	}
75  
76  	@Test
77  	public void simpleWithFileNotFoundException() {
78  		FileNotFoundException ex = new FileNotFoundException();
79  		SimpleController controller = new SimpleController();
80  		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
81  		assertNotNull("No ModelAndView returned", mav);
82  		assertEquals("Invalid view name returned", "X:FileNotFoundException", mav.getViewName());
83  	}
84  
85  	@Test
86  	public void simpleWithBindException() {
87  		BindException ex = new BindException();
88  		SimpleController controller = new SimpleController();
89  		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
90  		assertNotNull("No ModelAndView returned", mav);
91  		assertEquals("Invalid view name returned", "Y:BindException", mav.getViewName());
92  	}
93  
94  	@Test
95  	public void inherited()	{
96  		IOException ex = new IOException();
97  		InheritedController controller = new InheritedController();
98  		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
99  		assertNotNull("No ModelAndView returned", mav);
100 		assertEquals("Invalid view name returned", "GenericError", mav.getViewName());
101 	}
102 
103 	@Test(expected = IllegalStateException.class)
104 	public void ambiguous() {
105 		IllegalArgumentException ex = new IllegalArgumentException();
106 		AmbiguousController controller = new AmbiguousController();
107 		exceptionResolver.resolveException(request, response, controller, ex);
108 	}
109 
110 	// SPR-9209
111 
112 	@Test
113 	public void cachingSideEffect() {
114 		IllegalArgumentException ex = new IllegalArgumentException();
115 		SimpleController controller = new SimpleController();
116 
117 		ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
118 		assertNotNull("No ModelAndView returned", mav);
119 
120 		mav = exceptionResolver.resolveException(request, response, controller, new NullPointerException());
121 		assertNull(mav);
122 	}
123 
124 
125 	@Controller
126 	private static class SimpleController {
127 
128 		@ExceptionHandler(IOException.class)
129 		public String handleIOException(IOException ex, PortletRequest request) {
130 			return "X:" + ClassUtils.getShortName(ex.getClass());
131 		}
132 
133 		@ExceptionHandler(SocketException.class)
134 		public String handleSocketException(Exception ex, PortletResponse response) {
135 			return "Y:" + ClassUtils.getShortName(ex.getClass());
136 		}
137 
138 		@ExceptionHandler(IllegalArgumentException.class)
139 		public String handleIllegalArgumentException(Exception ex) {
140 			return ClassUtils.getShortName(ex.getClass());
141 		}
142 
143 	}
144 
145 
146 	@Controller
147 	private static class InheritedController extends SimpleController {
148 
149 		@Override
150 		public String handleIOException(IOException ex, PortletRequest request)	{
151 			return "GenericError";
152 		}
153 	}
154 
155 
156 	@Controller
157 	private static class AmbiguousController {
158 
159 		@ExceptionHandler({BindException.class, IllegalArgumentException.class})
160 		public String handle1(Exception ex, PortletRequest request, PortletResponse response) {
161 			return ClassUtils.getShortName(ex.getClass());
162 		}
163 
164 		@ExceptionHandler
165 		public String handle2(IllegalArgumentException ex) {
166 			return ClassUtils.getShortName(ex.getClass());
167 		}
168 
169 	}
170 
171 }