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.expect;
018: import static org.easymock.classextension.EasyMock.replay;
019: import static org.easymock.classextension.EasyMock.verify;
020:
021: import java.util.Map;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpSession;
026:
027: import org.easymock.classextension.EasyMock;
028: import org.strecks.context.ActionContext;
029: import org.strecks.context.impl.TestContextImpl;
030: import org.strecks.injection.handler.impl.RequestSessionAction;
031: import org.strecks.injection.internal.InjectionAnnotationReader;
032: import org.strecks.injection.internal.InjectionWrapper;
033: import org.testng.annotations.Test;
034:
035: /**
036: * @author Phil Zoio
037: */
038: public class TestRequestSessionHandler {
039:
040: /**
041: * Get from request attribute using ScopeAttribute
042: */
043: @Test
044: public void testScopedAttribute1() {
045:
046: InjectionAnnotationReader c = new InjectionAnnotationReader();
047: RequestSessionAction action = new RequestSessionAction();
048:
049: c.readAnnotations(action.getClass());
050: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
051:
052: HttpServletRequest request = EasyMock
053: .createMock(HttpServletRequest.class);
054:
055: expect(request.getParameter("integerInput")).andReturn(null);
056: expect(request.getAttribute("integer.Input")).andReturn(
057: new Integer(1));
058:
059: replay(request);
060:
061: InjectionWrapper inputWrapper = inputs.get("integerInput");
062:
063: ActionContext injectionContext = new TestContextImpl(request);
064: inputWrapper.inject(action, injectionContext);
065:
066: verify(request);
067:
068: assert action.getIntegerInput() != null;
069:
070: }
071:
072: /**
073: * Get from session attribute using ScopeAttribute
074: */
075: @Test
076: public void testScopedAttribute2() {
077:
078: InjectionAnnotationReader c = new InjectionAnnotationReader();
079: RequestSessionAction action = new RequestSessionAction();
080:
081: c.readAnnotations(action.getClass());
082: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
083:
084: HttpServletRequest request = EasyMock
085: .createMock(HttpServletRequest.class);
086: HttpSession session = EasyMock.createMock(HttpSession.class);
087:
088: expect(request.getParameter("integerInput")).andReturn(null);
089: expect(request.getAttribute("integer.Input")).andReturn(null);
090: expect(request.getSession()).andReturn(session);
091: expect(session.getAttribute("integer.Input")).andReturn(
092: new Integer(1));
093:
094: replay(request);
095: replay(session);
096:
097: InjectionWrapper inputWrapper = inputs.get("integerInput");
098:
099: ActionContext injectionContext = new TestContextImpl(request);
100: inputWrapper.inject(action, injectionContext);
101:
102: verify(request);
103: verify(session);
104:
105: assert action.getIntegerInput() != null;
106:
107: }
108:
109: /**
110: * Get from context attribute using ScopeAttribute
111: */
112: @Test
113: public void testScopedAttribute3() {
114:
115: InjectionAnnotationReader c = new InjectionAnnotationReader();
116: RequestSessionAction action = new RequestSessionAction();
117:
118: c.readAnnotations(action.getClass());
119: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
120:
121: HttpServletRequest request = EasyMock
122: .createMock(HttpServletRequest.class);
123: HttpSession session = EasyMock.createMock(HttpSession.class);
124: ServletContext context = EasyMock
125: .createMock(ServletContext.class);
126:
127: expect(request.getParameter("integerInput")).andReturn(null);
128: expect(request.getAttribute("integer.Input")).andReturn(null);
129:
130: expect(request.getSession()).andReturn(session);
131: expect(session.getAttribute("integer.Input")).andReturn(null);
132: expect(context.getAttribute("integer.Input")).andReturn(
133: new Integer(1));
134:
135: replay(request);
136: replay(session);
137:
138: InjectionWrapper inputWrapper = inputs.get("integerInput");
139:
140: ActionContext injectionContext = new TestContextImpl(request,
141: null, context, null, null);
142: inputWrapper.inject(action, injectionContext);
143:
144: verify(request);
145: verify(session);
146:
147: // assert action.getIntegerInput() != null;
148:
149: }
150:
151: @Test
152: public void testRequestAttribute() {
153:
154: InjectionAnnotationReader c = new InjectionAnnotationReader();
155: RequestSessionAction action = new RequestSessionAction();
156:
157: c.readAnnotations(action.getClass());
158: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
159:
160: HttpServletRequest request = EasyMock
161: .createMock(HttpServletRequest.class);
162:
163: expect(request.getAttribute("requestAttribute")).andReturn(
164: new Integer(1));
165:
166: replay(request);
167:
168: InjectionWrapper inputWrapper = inputs.get("requestAttribute");
169:
170: ActionContext injectionContext = new TestContextImpl(request);
171: inputWrapper.inject(action, injectionContext);
172:
173: verify(request);
174: assert action.getRequestAttribute() != null;
175:
176: }
177:
178: @Test
179: public void testSessionAttribute() {
180:
181: InjectionAnnotationReader c = new InjectionAnnotationReader();
182: RequestSessionAction action = new RequestSessionAction();
183:
184: c.readAnnotations(action.getClass());
185: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
186:
187: HttpServletRequest request = EasyMock
188: .createMock(HttpServletRequest.class);
189: HttpSession session = EasyMock.createMock(HttpSession.class);
190:
191: expect(request.getSession(false)).andReturn(session);
192: expect(session.getAttribute("sessionAttribute")).andReturn(
193: new Integer(1));
194:
195: replay(request);
196: replay(session);
197:
198: InjectionWrapper inputWrapper = inputs.get("sessionAttribute");
199:
200: ActionContext injectionContext = new TestContextImpl(request);
201: inputWrapper.inject(action, injectionContext);
202:
203: verify(request);
204: verify(session);
205:
206: assert action.getSessionAttribute() != null;
207:
208: }
209:
210: @Test
211: public void testContextAttribute() {
212:
213: InjectionAnnotationReader c = new InjectionAnnotationReader();
214: RequestSessionAction action = new RequestSessionAction();
215:
216: c.readAnnotations(action.getClass());
217: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
218:
219: ServletContext context = EasyMock
220: .createMock(ServletContext.class);
221:
222: expect(context.getAttribute("contextAttribute")).andReturn(
223: new Integer(1));
224:
225: replay(context);
226:
227: InjectionWrapper inputWrapper = inputs.get("contextAttribute");
228:
229: ActionContext injectionContext = new TestContextImpl(context);
230: inputWrapper.inject(action, injectionContext);
231:
232: verify(context);
233:
234: assert action.getContextAttribute() != null;
235:
236: }
237:
238: }
|