1 /*
2  * Copyright 2002-2011 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.servlet.mvc.support;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.springframework.beans.TestBean;
28 import org.springframework.core.convert.converter.Converter;
29 import org.springframework.format.support.DefaultFormattingConversionService;
30 import org.springframework.format.support.FormattingConversionService;
31 import org.springframework.validation.DataBinder;
32 
33 /**
34  *
35  * Test fixture for {@link RedirectAttributesModelMap} tests.
36  *
37  * @author Rossen Stoyanchev
38  * @since 3.1
39  */
40 public class RedirectAttributesModelMapTests {
41 
42 	private RedirectAttributesModelMap redirectAttributes;
43 
44 	private FormattingConversionService conversionService;
45 
46 	@Before
setup()47 	public void setup() {
48 		this.conversionService = new DefaultFormattingConversionService();
49 		DataBinder dataBinder = new DataBinder(null);
50 		dataBinder.setConversionService(conversionService);
51 
52 		this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
53 	}
54 
55 	@Test
addAttributePrimitiveType()56 	public void addAttributePrimitiveType() {
57 		this.redirectAttributes.addAttribute("speed", 65);
58 		assertEquals("65", this.redirectAttributes.get("speed"));
59 	}
60 
61 	@Test
addAttributeCustomType()62 	public void addAttributeCustomType() {
63 		String attrName = "person";
64 		this.redirectAttributes.addAttribute(attrName, new TestBean("Fred"));
65 
66 		assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectAttributes.get(attrName));
67 
68 		this.conversionService.addConverter(new TestBeanConverter());
69 		this.redirectAttributes.addAttribute(attrName, new TestBean("Fred"));
70 
71 		assertEquals("Type converter should have been used", "[Fred]", this.redirectAttributes.get(attrName));
72 	}
73 
74 	@Test
addAttributeToString()75 	public void addAttributeToString() {
76 		String attrName = "person";
77 		RedirectAttributesModelMap model = new RedirectAttributesModelMap();
78 		model.addAttribute(attrName, new TestBean("Fred"));
79 
80 		assertEquals("toString() should have been used", "Fred", model.get(attrName));
81 	}
82 
83 	@Test
addAttributeValue()84 	public void addAttributeValue() {
85 		this.redirectAttributes.addAttribute(new TestBean("Fred"));
86 
87 		assertEquals("Fred", this.redirectAttributes.get("testBean"));
88 	}
89 
90 	@Test
addAllAttributesList()91 	public void addAllAttributesList() {
92 		this.redirectAttributes.addAllAttributes(Arrays.asList(new TestBean("Fred"), new Integer(5)));
93 
94 		assertEquals("Fred", this.redirectAttributes.get("testBean"));
95 		assertEquals("5", this.redirectAttributes.get("integer"));
96 	}
97 
98 	@Test
addAttributesMap()99 	public void addAttributesMap() {
100 		Map<String, Object> map = new HashMap<String, Object>();
101 		map.put("person", new TestBean("Fred"));
102 		map.put("age", 33);
103 		this.redirectAttributes.addAllAttributes(map);
104 
105 		assertEquals("Fred", this.redirectAttributes.get("person"));
106 		assertEquals("33", this.redirectAttributes.get("age"));
107 	}
108 
109 	@Test
mergeAttributes()110 	public void mergeAttributes() {
111 		Map<String, Object> map = new HashMap<String, Object>();
112 		map.put("person", new TestBean("Fred"));
113 		map.put("age", 33);
114 
115 		this.redirectAttributes.addAttribute("person", new TestBean("Ralph"));
116 		this.redirectAttributes.mergeAttributes(map);
117 
118 		assertEquals("Ralph", this.redirectAttributes.get("person"));
119 		assertEquals("33", this.redirectAttributes.get("age"));
120 	}
121 
122 	@Test
put()123 	public void put() {
124 		this.redirectAttributes.put("testBean", new TestBean("Fred"));
125 
126 		assertEquals("Fred", this.redirectAttributes.get("testBean"));
127 	}
128 
129 	@Test
putAll()130 	public void putAll() {
131 		Map<String, Object> map = new HashMap<String, Object>();
132 		map.put("person", new TestBean("Fred"));
133 		map.put("age", 33);
134 		this.redirectAttributes.putAll(map);
135 
136 		assertEquals("Fred", this.redirectAttributes.get("person"));
137 		assertEquals("33", this.redirectAttributes.get("age"));
138 	}
139 
140 	public static class TestBeanConverter implements Converter<TestBean, String> {
141 
convert(TestBean source)142 		public String convert(TestBean source) {
143 			return "[" + source.getName() + "]";
144 		}
145 	}
146 
147 }
148