001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.navigate.spring;
016:
017: import static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.expect;
019: import static org.easymock.EasyMock.isA;
020: import static org.testng.Assert.assertEquals;
021:
022: import java.util.HashMap;
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028:
029: import org.apache.struts.Globals;
030: import org.springframework.web.context.WebApplicationContext;
031: import org.springframework.web.servlet.DispatcherServlet;
032: import org.springframework.web.servlet.ModelAndView;
033: import org.springframework.web.servlet.View;
034: import org.springframework.web.servlet.ViewResolver;
035: import org.strecks.context.ActionContext;
036: import org.strecks.exceptions.ApplicationConfigurationException;
037: import org.strecks.exceptions.ApplicationRuntimeException;
038: import org.strecks.testng.AbstractMockTest;
039: import org.strecks.view.ViewAdapter;
040: import org.testng.annotations.BeforeMethod;
041: import org.testng.annotations.Test;
042:
043: public class TestSpringNavigationHandler extends AbstractMockTest {
044:
045: private View view;
046:
047: private Map model;
048:
049: private ActionContext context;
050:
051: private HttpServletRequest request;
052:
053: private ServletContext servletContext;
054:
055: private WebApplicationContext wac;
056:
057: private ViewResolver viewResolver;
058:
059: private Locale locale;
060:
061: @BeforeMethod
062: public void setUp() {
063:
064: model = new HashMap();
065: view = newMock(View.class);
066: context = newMock(ActionContext.class);
067: request = newMock(HttpServletRequest.class);
068: servletContext = newMock(ServletContext.class);
069: wac = newMock(WebApplicationContext.class);
070: viewResolver = newMock(ViewResolver.class);
071: locale = Locale.getDefault();
072:
073: resetMocks();
074:
075: }
076:
077: @Test
078: public void testGetViewAdapterWithResolver() throws Exception {
079:
080: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
081: "resolver_name");
082:
083: expect(context.getRequest()).andReturn(request);
084: request.setAttribute(
085: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
086: isA(StrecksLocaleResolver.class));
087: expect(context.getContext()).andReturn(servletContext);
088: expect(
089: servletContext
090: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
091: .andReturn(wac);
092: expect(wac.getBean("resolver_name")).andReturn(viewResolver);
093: expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(
094: locale);
095: expect(viewResolver.resolveViewName("view_name", locale))
096: .andReturn(view);
097:
098: replayMocks();
099: ViewAdapter adapter = handler.getActionForward(context,
100: new ModelAndView("view_name", model));
101:
102: assert adapter instanceof SpringRenderingViewAdapter;
103: SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
104: assert va.getModel().equals(model);
105: assert va.getView() == view;
106:
107: verifyMocks();
108:
109: }
110:
111: @Test
112: public void testNotViewResolver() throws Exception {
113:
114: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
115: "resolver_name");
116:
117: expect(context.getRequest()).andReturn(request);
118: request.setAttribute(
119: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
120: isA(StrecksLocaleResolver.class));
121: expect(context.getContext()).andReturn(servletContext);
122: expect(
123: servletContext
124: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
125: .andReturn(wac);
126: expect(wac.getBean("resolver_name")).andReturn(
127: "this should be a view resolver");
128:
129: replayMocks();
130:
131: try {
132: handler.getActionForward(context, new ModelAndView(
133: "view_name", model));
134: } catch (ApplicationConfigurationException e) {
135: assertEquals(
136: e.getMessage(),
137: "Bean referenced using the 'resolver' attribute of @SpringView "
138: + "annotation is not instance of "
139: + "org.springframework.web.servlet.ViewResolver");
140: }
141:
142: verifyMocks();
143:
144: }
145:
146: @Test
147: public void testResolverException() throws Exception {
148:
149: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
150: "resolver_name");
151:
152: expect(context.getRequest()).andReturn(request);
153: request.setAttribute(
154: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
155: isA(StrecksLocaleResolver.class));
156: expect(context.getContext()).andReturn(servletContext);
157: expect(
158: servletContext
159: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
160: .andReturn(wac);
161: expect(wac.getBean("resolver_name")).andReturn(viewResolver);
162: expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(
163: locale);
164: expect(viewResolver.resolveViewName("view_name", locale))
165: .andThrow(new IllegalStateException());
166:
167: replayMocks();
168:
169: try {
170: handler.getActionForward(context, new ModelAndView(
171: "view_name", model));
172: } catch (ApplicationRuntimeException e) {
173: assert e
174: .getMessage()
175: .startsWith(
176: "Exception thrown during resolution of view name view_name using ViewResolver");
177: }
178:
179: verifyMocks();
180:
181: }
182:
183: @Test
184: public void testGetViewAdapterWithNamedView() throws Exception {
185:
186: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
187: "");
188:
189: expect(context.getRequest()).andReturn(request);
190: request.setAttribute(
191: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
192: isA(StrecksLocaleResolver.class));
193: expect(context.getContext()).andReturn(servletContext);
194: expect(
195: servletContext
196: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
197: .andReturn(wac);
198: expect(wac.getBean("view_name")).andReturn(view);
199:
200: replayMocks();
201: ViewAdapter adapter = handler.getActionForward(context,
202: new ModelAndView("view_name", model));
203:
204: assert adapter instanceof SpringRenderingViewAdapter;
205: SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
206: assert va.getModel().equals(model);
207: assert va.getView() == view;
208:
209: verifyMocks();
210:
211: }
212:
213: @Test
214: public void testGetViewAdapterWithView() throws Exception {
215:
216: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
217: "");
218:
219: expect(context.getRequest()).andReturn(request);
220: request.setAttribute(
221: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
222: isA(StrecksLocaleResolver.class));
223:
224: replayMocks();
225: ViewAdapter adapter = handler.getActionForward(context,
226: new ModelAndView(view, model));
227:
228: assert adapter instanceof SpringRenderingViewAdapter;
229: SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
230: assert va.getModel().equals(model);
231: assert va.getView() == view;
232:
233: verifyMocks();
234:
235: }
236:
237: @Test
238: public void testGetViewBeanNotView() throws Exception {
239:
240: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
241: "");
242:
243: expect(context.getRequest()).andReturn(request);
244: request.setAttribute(
245: eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE),
246: isA(StrecksLocaleResolver.class));
247: expect(context.getContext()).andReturn(servletContext);
248: expect(
249: servletContext
250: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
251: .andReturn(wac);
252: expect(wac.getBean("view_name")).andReturn(
253: "This should be a view");
254:
255: replayMocks();
256:
257: try {
258: handler.getActionForward(context, new ModelAndView(
259: "view_name", model));
260: } catch (ApplicationConfigurationException e) {
261: assertEquals(
262: e.getMessage(),
263: "Bean referenced using the 'viewName' property "
264: + "of the returned ModelAndView of the method containing @SpringView "
265: + "is not an instance of org.springframework.web.servlet.View");
266: }
267:
268: verifyMocks();
269:
270: }
271:
272: @Test
273: public void testHandler() {
274:
275: SpringViewNavigationHandler handler = new SpringViewNavigationHandler(
276: "view_name");
277: assert handler.isUseResolver();
278: assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
279: assertEquals(handler.getName(), "view_name");
280:
281: handler = new SpringViewNavigationHandler("");
282: assert !handler.isUseResolver();
283: assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
284: assert null == handler.getName();
285:
286: handler = new SpringViewNavigationHandler(null);
287: assert !handler.isUseResolver();
288: assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
289: assert null == handler.getName();
290:
291: }
292:
293: }
|