1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.internal.controller.impl;
17  
18  import static org.easymock.EasyMock.anyBoolean;
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.eq;
21  import static org.easymock.EasyMock.expect;
22  import static org.easymock.EasyMock.expectLastCall;
23  import static org.easymock.EasyMock.getCurrentArguments;
24  import static org.easymock.EasyMock.isA;
25  import static org.easymock.EasyMock.replay;
26  import static org.easymock.EasyMock.verify;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  
31  import java.util.HashMap;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpSession;
37  
38  import org.easymock.IAnswer;
39  import org.junit.Test;
40  
41  /**
42   * 
43   * @author baba
44   * 
45   */
46  public class FlashMapTest {
47  
48  	@Test
49  	public void sequence1() {
50  		final State state = new State();
51  		state.hasSession = false;
52  		sequence(state);
53  	}
54  
55  	@Test
56  	public void sequence2() {
57  		final State state = new State();
58  		state.hasSession = true;
59  		sequence(state);
60  	}
61  
62  	private void sequence(final State state) {
63  		final Map<String, Object> mapInSession = new ConcurrentHashMap<String, Object>();
64  		final HttpServletRequest request = createMock(HttpServletRequest.class);
65  		final HttpSession session = createMock(HttpSession.class);
66  		expect(request.getSession(anyBoolean())).andStubAnswer(
67  				new IAnswer<HttpSession>() {
68  
69  					public HttpSession answer() throws Throwable {
70  						boolean create = (Boolean) getCurrentArguments()[0];
71  						if (state.hasSession || create) {
72  							state.hasSession = true;
73  							return session;
74  						}
75  						return null;
76  					}
77  
78  				});
79  		expect(request.getSession()).andStubAnswer(new IAnswer<HttpSession>() {
80  
81  			public HttpSession answer() throws Throwable {
82  				return request.getSession(true);
83  			}
84  
85  		});
86  		expect(session.getAttribute(FlashMap.class.getName() + ".MAP"))
87  				.andStubReturn(mapInSession);
88  		session.setAttribute(eq(FlashMap.class.getName() + ".MAP"),
89  				isA(Map.class));
90  		expectLastCall().andAnswer(new IAnswer<Object>() {
91  
92  			@SuppressWarnings("unchecked")
93  			public Object answer() throws Throwable {
94  				Map<String, Object> m = (Map<String, Object>) getCurrentArguments()[1];
95  				assertEquals("abcde", m.get("value1"));
96  				return null;
97  			}
98  
99  		});
100 		expectLastCall().andAnswer(new IAnswer<Object>() {
101 
102 			@SuppressWarnings("unchecked")
103 			public Object answer() throws Throwable {
104 				Map<String, Object> m = (Map<String, Object>) getCurrentArguments()[1];
105 				assertEquals("abcde", m.get("value1"));
106 				assertEquals("fghij", m.get("value2"));
107 				return null;
108 			}
109 
110 		});
111 		expectLastCall().andAnswer(new IAnswer<Object>() {
112 
113 			@SuppressWarnings("unchecked")
114 			public Object answer() throws Throwable {
115 				Map<String, Object> m = (Map<String, Object>) getCurrentArguments()[1];
116 				assertEquals("abcde", m.get("value1"));
117 				return null;
118 			}
119 
120 		});
121 		expectLastCall().andAnswer(new IAnswer<Object>() {
122 
123 			@SuppressWarnings("unchecked")
124 			public Object answer() throws Throwable {
125 				Map<String, Object> m = (Map<String, Object>) getCurrentArguments()[1];
126 				assertTrue(m.isEmpty());
127 				return null;
128 			}
129 
130 		});
131 		replay(request, session);
132 
133 		Map<String, Object> map = new FlashMap<String, Object>(request);
134 		assertEquals(0, map.size());
135 		assertTrue(map.isEmpty());
136 		assertTrue(map.keySet().isEmpty());
137 		assertTrue(map.values().isEmpty());
138 		assertTrue(map.entrySet().isEmpty());
139 
140 		map.put("value1", "abcde");
141 		Map<String, Object> child = new HashMap<String, Object>();
142 		child.put("value2", "fghij");
143 		map.putAll(child);
144 
145 		assertEquals(2, map.size());
146 		assertFalse(map.isEmpty());
147 		assertFalse(map.keySet().isEmpty());
148 		assertFalse(map.values().isEmpty());
149 		assertFalse(map.entrySet().isEmpty());
150 		assertTrue(map.containsKey("value1"));
151 		assertTrue(map.containsKey("value2"));
152 		assertTrue(map.containsValue("abcde"));
153 		assertTrue(map.containsValue("fghij"));
154 		assertEquals("abcde", map.get("value1"));
155 		assertEquals("fghij", map.get("value2"));
156 
157 		map.remove("value2");
158 
159 		assertEquals(1, map.size());
160 		assertFalse(map.isEmpty());
161 		assertFalse(map.keySet().isEmpty());
162 		assertFalse(map.values().isEmpty());
163 		assertFalse(map.entrySet().isEmpty());
164 		assertTrue(map.containsKey("value1"));
165 		assertFalse(map.containsKey("value2"));
166 		assertTrue(map.containsValue("abcde"));
167 		assertFalse(map.containsValue("fghij"));
168 		assertEquals("abcde", map.get("value1"));
169 
170 		map.clear();
171 
172 		assertEquals(0, map.size());
173 		assertTrue(map.isEmpty());
174 		assertTrue(map.keySet().isEmpty());
175 		assertTrue(map.values().isEmpty());
176 		assertTrue(map.entrySet().isEmpty());
177 
178 		verify(request, session);
179 	}
180 
181 	private static class State {
182 		public boolean hasSession;
183 	}
184 
185 }