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.eq;
018: import static org.easymock.EasyMock.expect;
019: import static org.easymock.EasyMock.isA;
020: import static org.easymock.classextension.EasyMock.createMock;
021: import static org.easymock.classextension.EasyMock.createNiceMock;
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:
029: import org.strecks.constants.InfrastructureKeys;
030: import org.strecks.context.ActionContext;
031: import org.strecks.context.impl.TestContextImpl;
032: import org.strecks.injection.handler.impl.ActionWithRedirectHelper;
033: import org.strecks.injection.internal.InjectionAnnotationReader;
034: import org.strecks.injection.internal.InjectionWrapper;
035: import org.strecks.web.RedirectHelper;
036: import org.strecks.web.RedirectHelperImpl;
037: import org.testng.annotations.Test;
038:
039: /**
040: * @author Phil Zoio
041: */
042: public class TestRedirectHelperInjectionHandler {
043:
044: @Test
045: public void testPresent() {
046: InjectionAnnotationReader c = new InjectionAnnotationReader();
047: ActionWithRedirectHelper action = new ActionWithRedirectHelper();
048:
049: c.readAnnotations(action.getClass());
050: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
051:
052: HttpServletRequest request = createMock(HttpServletRequest.class);
053: RedirectHelper redirectHelper = createMock(RedirectHelper.class);
054:
055: expect(request.getAttribute(InfrastructureKeys.REDIRECT_HELPER))
056: .andReturn(redirectHelper);
057:
058: replay(request);
059: replay(redirectHelper);
060:
061: InjectionWrapper inputWrapper = inputs.get("redirectHelper");
062:
063: ActionContext injectionContext = new TestContextImpl(request);
064: inputWrapper.inject(action, injectionContext);
065:
066: verify(request);
067: verify(redirectHelper);
068:
069: assert action.getRedirectHelper() != null;
070: assert action.getRedirectHelper() == redirectHelper;
071: }
072:
073: @Test
074: public void testNotPresent() {
075: InjectionAnnotationReader c = new InjectionAnnotationReader();
076: ActionWithRedirectHelper action = new ActionWithRedirectHelper();
077:
078: c.readAnnotations(action.getClass());
079: Map<String, InjectionWrapper> inputs = c.getInjectionMap();
080:
081: HttpServletRequest request = createMock(HttpServletRequest.class);
082: RedirectHelperImpl redirectHelper = createNiceMock(RedirectHelperImpl.class);
083:
084: expect(request.getAttribute(InfrastructureKeys.REDIRECT_HELPER))
085: .andReturn(null);
086: request.setAttribute(eq(InfrastructureKeys.REDIRECT_HELPER),
087: isA(RedirectHelper.class));
088:
089: replay(request);
090: replay(redirectHelper);
091:
092: InjectionWrapper inputWrapper = inputs.get("redirectHelper");
093:
094: ActionContext injectionContext = new TestContextImpl(request);
095: inputWrapper.inject(action, injectionContext);
096:
097: verify(request);
098: verify(redirectHelper);
099:
100: assert action.getRedirectHelper() != null;
101: }
102:
103: }
|