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