001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.injection.handler;
016:
017: import static org.easymock.EasyMock.anyObject;
018: import static org.easymock.EasyMock.createNiceMock;
019: import static org.easymock.EasyMock.eq;
020: import static org.easymock.EasyMock.expect;
021: import static org.easymock.classextension.EasyMock.createMock;
022: import static org.easymock.classextension.EasyMock.replay;
023: import static org.easymock.classextension.EasyMock.verify;
024:
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029:
030: import org.easymock.classextension.EasyMock;
031: import org.strecks.context.ActionContext;
032: import org.strecks.context.impl.TestContextImpl;
033: import org.strecks.exceptions.StaleSessionException;
034: import org.strecks.injection.handler.impl.QueryResults;
035: import org.strecks.injection.handler.impl.RequestSessionAction;
036: import org.strecks.injection.internal.InjectionAnnotationReader;
037: import org.strecks.injection.internal.InjectionWrapper;
038: import org.testng.annotations.Test;
039:
040: /**
041: * @author Phil Zoio
042: */
043: public class TestAutoCreateAction {
044:
045: @Test
046: public void testAutoCreateSession() {
047:
048: InjectionAnnotationReader c = new InjectionAnnotationReader();
049: RequestSessionAction action = new RequestSessionAction();
050:
051: c.readAnnotations(action.getClass());
052: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
053:
054: HttpServletRequest request = createMock(HttpServletRequest.class);
055: HttpSession session = createNiceMock(HttpSession.class);
056:
057: expect(request.getSession(false)).andReturn(session);
058: expect(session.getAttribute("autoCreateSession")).andReturn(
059: null);
060:
061: replay(request);
062: replay(session);
063:
064: InjectionWrapper inputWrapper = inputs.get("autoCreateSession");
065:
066: ActionContext injectionContext = new TestContextImpl(request);
067: inputWrapper.inject(action, injectionContext);
068:
069: verify(request);
070: verify(session);
071:
072: assert action.getAutoCreateSession() != null;
073: assert action.getAutoCreateSession() instanceof QueryResults;
074:
075: }
076:
077: @Test
078: public void testNonAutoCreateSession() {
079:
080: InjectionAnnotationReader c = new InjectionAnnotationReader();
081: RequestSessionAction action = new RequestSessionAction();
082:
083: c.readAnnotations(action.getClass());
084: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
085:
086: HttpServletRequest request = EasyMock
087: .createMock(HttpServletRequest.class);
088: HttpSession session = EasyMock.createMock(HttpSession.class);
089:
090: expect(request.getSession(false)).andReturn(session);
091: expect(session.getAttribute("nonAutoCreateSession")).andReturn(
092: null);
093:
094: replay(request);
095: replay(session);
096:
097: InjectionWrapper inputWrapper = inputs
098: .get("nonAutoCreateSession");
099:
100: ActionContext injectionContext = new TestContextImpl(request);
101: inputWrapper.inject(action, injectionContext);
102:
103: verify(request);
104: verify(session);
105:
106: assert action.getAutoCreateSession() == null;
107:
108: }
109:
110: @Test
111: public void testAutoCreateWithNoSession() {
112:
113: InjectionAnnotationReader c = new InjectionAnnotationReader();
114: RequestSessionAction action = new RequestSessionAction();
115:
116: c.readAnnotations(action.getClass());
117: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
118:
119: HttpServletRequest request = EasyMock
120: .createMock(HttpServletRequest.class);
121: HttpSession session = EasyMock.createMock(HttpSession.class);
122:
123: expect(request.getSession(false)).andReturn(null);
124: expect(request.getSession()).andReturn(session);
125: session.setAttribute(eq("autoCreateSession"), anyObject());
126:
127: replay(request);
128: replay(session);
129:
130: InjectionWrapper inputWrapper = inputs.get("autoCreateSession");
131:
132: ActionContext injectionContext = new TestContextImpl(request);
133: inputWrapper.inject(action, injectionContext);
134:
135: verify(request);
136: verify(session);
137:
138: assert action.getAutoCreateSession() != null;
139:
140: }
141:
142: @Test(expectedExceptions=StaleSessionException.class)
143: public void testRequiredSession() {
144:
145: InjectionAnnotationReader c = new InjectionAnnotationReader();
146: RequestSessionAction action = new RequestSessionAction();
147:
148: c.readAnnotations(action.getClass());
149: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
150:
151: HttpServletRequest request = EasyMock
152: .createMock(HttpServletRequest.class);
153: HttpSession session = EasyMock.createMock(HttpSession.class);
154:
155: expect(request.getSession(false)).andReturn(session);
156: expect(session.getAttribute("requiredSessionAttribute"))
157: .andReturn(null);
158:
159: replay(request);
160: replay(session);
161:
162: InjectionWrapper inputWrapper = inputs
163: .get("requiredSessionAttribute");
164:
165: ActionContext injectionContext = new TestContextImpl(request);
166: inputWrapper.inject(action, injectionContext);
167:
168: verify(request);
169: verify(session);
170:
171: }
172:
173: /*
174: * Verify same behaviour as autocreate
175: */
176: @Test(expectedExceptions=StaleSessionException.class)
177: public void testRequiredAutoCreateSession() {
178:
179: InjectionAnnotationReader c = new InjectionAnnotationReader();
180: RequestSessionAction action = new RequestSessionAction();
181:
182: c.readAnnotations(action.getClass());
183: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
184:
185: HttpServletRequest request = EasyMock
186: .createMock(HttpServletRequest.class);
187: HttpSession session = EasyMock.createMock(HttpSession.class);
188:
189: expect(request.getSession(false)).andReturn(session);
190: expect(
191: session
192: .getAttribute("requiredAutoCreateSessionAttribute"))
193: .andReturn(null);
194:
195: replay(request);
196: replay(session);
197:
198: InjectionWrapper inputWrapper = inputs
199: .get("requiredAutoCreateSessionAttribute");
200:
201: ActionContext injectionContext = new TestContextImpl(request);
202: inputWrapper.inject(action, injectionContext);
203:
204: verify(request);
205: verify(session);
206:
207: }
208:
209: @Test
210: public void testAutoCreateRequest() {
211:
212: InjectionAnnotationReader c = new InjectionAnnotationReader();
213: RequestSessionAction action = new RequestSessionAction();
214:
215: c.readAnnotations(action.getClass());
216: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
217:
218: HttpServletRequest request = EasyMock.createNiceControl()
219: .createMock(HttpServletRequest.class);
220:
221: expect(request.getAttribute("autoCreateRequest")).andReturn(
222: null);
223:
224: replay(request);
225:
226: InjectionWrapper inputWrapper = inputs.get("autoCreateRequest");
227:
228: ActionContext injectionContext = new TestContextImpl(request);
229: inputWrapper.inject(action, injectionContext);
230:
231: verify(request);
232:
233: assert action.getAutoCreateRequest() != null;
234: assert action.getAutoCreateRequest() instanceof QueryResults;
235:
236: }
237:
238: @Test
239: public void testNonAutoCreateRequest() {
240:
241: InjectionAnnotationReader c = new InjectionAnnotationReader();
242: RequestSessionAction action = new RequestSessionAction();
243:
244: c.readAnnotations(action.getClass());
245: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
246:
247: HttpServletRequest request = EasyMock
248: .createMock(HttpServletRequest.class);
249:
250: expect(request.getAttribute("nonAutoCreateRequest")).andReturn(
251: null);
252:
253: replay(request);
254:
255: InjectionWrapper inputWrapper = inputs
256: .get("nonAutoCreateRequest");
257:
258: ActionContext injectionContext = new TestContextImpl(request);
259: inputWrapper.inject(action, injectionContext);
260:
261: verify(request);
262:
263: assert action.getAutoCreateRequest() == null;
264:
265: }
266:
267: }
|