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:
025: import org.easymock.EasyMock;
026: import org.springframework.web.context.WebApplicationContext;
027: import org.strecks.context.ActionContext;
028: import org.strecks.context.impl.TestContextImpl;
029: import org.strecks.injection.handler.impl.SpringBeanAction;
030: import org.strecks.injection.internal.InjectionAnnotationReader;
031: import org.strecks.injection.internal.InjectionWrapper;
032: import org.testng.annotations.BeforeMethod;
033: import org.testng.annotations.Test;
034:
035: /**
036: * @author Phil Zoio
037: */
038: public class TestSpringBeanHandler {
039:
040: private SpringBeanAction action;
041:
042: private Map<String, InjectionWrapper> inputs;
043:
044: private ServletContext context;
045:
046: private WebApplicationContext wac;
047:
048: @BeforeMethod
049: public void beforeTest() {
050:
051: InjectionAnnotationReader c = new InjectionAnnotationReader();
052: action = new SpringBeanAction();
053: c.readAnnotations(action.getClass());
054: inputs = c.getInjectionMap();
055: context = EasyMock.createMock(ServletContext.class);
056: wac = EasyMock.createMock(WebApplicationContext.class);
057:
058: }
059:
060: @Test
061: public void testSpringBean() {
062:
063: expect(
064: context.getAttribute(WebApplicationContext.class
065: .getName()
066: + ".ROOT")).andReturn(wac);
067: expect(wac.getBean("springBean")).andReturn(new Integer(1));
068:
069: replay(context);
070: replay(wac);
071:
072: InjectionWrapper inputWrapper = inputs.get("springBean");
073:
074: ActionContext injectionContext = new TestContextImpl(context);
075: inputWrapper.inject(action, injectionContext);
076:
077: verify(context);
078: verify(wac);
079:
080: assert action.getSpringBean() != null;
081:
082: }
083:
084: @Test
085: public void testNamedSpringBean() {
086:
087: expect(
088: context.getAttribute(WebApplicationContext.class
089: .getName()
090: + ".ROOT")).andReturn(wac);
091: expect(wac.getBean("springBean")).andReturn(new Integer(1));
092:
093: replay(context);
094: replay(wac);
095:
096: InjectionWrapper inputWrapper = inputs.get("namedSpringBean");
097:
098: ActionContext injectionContext = new TestContextImpl(context);
099: inputWrapper.inject(action, injectionContext);
100:
101: verify(context);
102: verify(wac);
103:
104: assert action.getNamedSpringBean() != null;
105:
106: }
107:
108: }
|