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.http.HttpServletRequest;
024:
025: import org.easymock.classextension.EasyMock;
026: import org.strecks.context.ActionContext;
027: import org.strecks.context.impl.TestContextImpl;
028: import org.strecks.injection.handler.impl.TestAction;
029: import org.strecks.injection.internal.InjectionAnnotationReader;
030: import org.strecks.injection.internal.InjectionWrapper;
031: import org.testng.annotations.BeforeMethod;
032: import org.testng.annotations.Test;
033:
034: /**
035: * @author Phil Zoio
036: */
037: public class TestRequestParameterHandler {
038:
039: private Map<String, InjectionWrapper> inputs;
040:
041: private HttpServletRequest request;
042:
043: private TestAction action;
044:
045: @BeforeMethod
046: public void beforeTest() {
047:
048: InjectionAnnotationReader c = new InjectionAnnotationReader();
049: action = new TestAction();
050: c.readAnnotations(action.getClass());
051: inputs = c.getInjectionMap();
052: request = EasyMock.createMock(HttpServletRequest.class);
053:
054: }
055:
056: /**
057: * Tests where null is okay
058: */
059: @Test
060: public void testRequestParameterNullHandler() {
061:
062: expect(request.getParameter("integerInput")).andReturn(null);
063:
064: replay(request);
065:
066: InjectionWrapper inputWrapper = inputs.get("integerInput");
067:
068: ActionContext injectionContext = new TestContextImpl(request);
069: inputWrapper.inject(action, injectionContext);
070:
071: verify(request);
072:
073: assert action.getIntegerInput() == null;
074:
075: }
076:
077: /**
078: * Tests where null is okay
079: */
080: @Test
081: public void testRequestParameterOK() {
082:
083: expect(request.getParameter("integerInput")).andReturn("1");
084:
085: replay(request);
086:
087: InjectionWrapper inputWrapper = inputs.get("integerInput");
088:
089: ActionContext injectionContext = new TestContextImpl(request);
090: inputWrapper.inject(action, injectionContext);
091:
092: verify(request);
093:
094: assert action.getIntegerInput() != null;
095:
096: }
097:
098: /**
099: * Tests where null is okay
100: */
101: @Test(expectedExceptions=IllegalStateException.class)
102: public void testRequestParameterRequired() {
103:
104: expect(request.getParameter("longInput")).andReturn(null);
105:
106: replay(request);
107:
108: InjectionWrapper inputWrapper = inputs.get("longInput");
109:
110: ActionContext injectionContext = new TestContextImpl(request);
111: inputWrapper.inject(action, injectionContext);
112:
113: verify(request);
114:
115: }
116:
117: }
|