@Test public void testAopJdkProxyWithDetectedInterfaces() throws Exception { ModelMap map = new ModelMap(); Map<?, ?> target = new HashMap<>(); ProxyFactory factory = new ProxyFactory(target); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void handleMapReturnValue() throws Exception { mavContainer.addAttribute("attr1", "value1"); Map<String, Object> returnValue = new ModelMap("attr2", "value2"); processor.handleReturnValue(returnValue , returnParamMap, mavContainer, webRequest); assertEquals("value1", mavContainer.getModel().get("attr1")); assertEquals("value2", mavContainer.getModel().get("attr2")); }
@Test public void testAopJdkProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); Map<?, ?> target = new HashMap<>(); factory.setTarget(target); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void handleAnnotatedReturnValue() throws Exception { this.processor.handleReturnValue("expected", this.returnParamNamedModelAttr, this.container, this.request); assertEquals("expected", this.container.getModel().get("modelAttrName")); }
@Test public void handleViewInstance() throws Exception { ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); }
@Test public void testMergeMapWithOverriding() throws Exception { Map<String, TestBean> beans = new HashMap<>(); beans.put("one", new TestBean("one")); beans.put("two", new TestBean("two")); beans.put("three", new TestBean("three")); ModelMap model = new ModelMap(); model.put("one", new TestBean("oneOld")); model.mergeAttributes(beans); assertEquals(3, model.size()); assertEquals("oneOld", ((TestBean) model.get("one")).getName()); }
@Test public void testInnerClassWithTwoUpperCaseLetters() throws Exception { ModelMap map = new ModelMap(); UKInnerClass inner = new UKInnerClass(); map.addAttribute(inner); assertSame(inner, map.get("UKInnerClass")); }
@Test public void modelAndViewResolver() throws Exception { MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("testBeanReturnValue"), -1); mavResolvers.add(new TestModelAndViewResolver(TestBean.class)); TestBean testBean = new TestBean("name"); handler.handleReturnValue(testBean, returnType, mavContainer, request); assertEquals("viewName", mavContainer.getViewName()); assertSame(testBean, mavContainer.getModel().get("modelAttrName")); assertFalse(mavContainer.isRequestHandled()); }
@Test public void testAddNullObjectWithExplicitKey() throws Exception { ModelMap model = new ModelMap(); model.addAttribute("foo", null); assertTrue(model.containsKey("foo")); assertNull(model.get("foo")); }
@Test public void testNamedObjectCtor() throws Exception { ModelMap model = new ModelMap("foo", "bing"); assertEquals(1, model.size()); String bing = (String) model.get("foo"); assertNotNull(bing); assertEquals("bing", bing); }
@Test public void testOneArgCtorWithScalar() throws Exception { ModelMap model = new ModelMap("bing"); assertEquals(1, model.size()); String string = (String) model.get("string"); assertNotNull(string); assertEquals("bing", string); }
@Test public void modelAttributeMethodWithNameByConvention() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrConvention"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("boolean")); }
@Test public void modelAttributeMethodWithExplicitName() throws Exception { ModelFactory modelFactory = createModelFactory("modelAttrWithName"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("name")); }
@Test public void handleAndValidateRequestPart() throws Exception { MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest(); multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8"))); HandlerMethod handlerMethod = handlerMethod("handleAndValidateRequestPart", String.class, Errors.class, Model.class); ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod); assertNotNull(mav); assertEquals(1, mav.getModelMap().get("error count")); }
@Test public void handleRedirectAttributesWithViewReference() throws Exception { RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap(); mavContainer.setRedirectModel(redirectAttributes); ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); assertEquals(RedirectView.class, mavContainer.getView().getClass()); assertEquals("attrValue", mavContainer.getModel().get("attrName")); assertSame("RedirectAttributes should be used if controller redirects", redirectAttributes, mavContainer.getModel()); }
@Test public void testAddObjectNoKeyOfSameTypeOverrides() throws Exception { ModelMap model = new ModelMap(); model.addAttribute("foo"); model.addAttribute("bar"); assertEquals(1, model.size()); String bar = (String) model.get("string"); assertEquals("bar", bar); }
@Test public void testAopCglibProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); SomeInnerClass val = new SomeInnerClass(); factory.setTarget(val); factory.setProxyTargetClass(true); map.addAttribute(factory.getProxy()); assertTrue(map.containsKey("someInnerClass")); assertEquals(val, map.get("someInnerClass")); }
@Test public void sessionAttribute() throws Exception { this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue"); ModelFactory modelFactory = createModelFactory("modelAttr", Model.class); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertEquals("sessionAttrValue", this.mavContainer.getModel().get("sessionAttr")); }
@Test public void modelAttributeMethodWithNullReturnValue() throws Exception { ModelFactory modelFactory = createModelFactory("nullModelAttr"); HandlerMethod handlerMethod = createHandlerMethod("handle"); modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod); assertTrue(this.mavContainer.containsAttribute("name")); assertNull(this.mavContainer.getModel().get("name")); }
@Test public void redirectScenarioWithRedirectModel() { this.mavContainer.addAttribute("name1", "value1"); this.mavContainer.setRedirectModel(new ModelMap("name2", "value2")); this.mavContainer.setRedirectModelScenario(true); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value2", this.mavContainer.getModel().get("name2")); }