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