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