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.source;
016:
017: import static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createStrictMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021:
022: import javax.servlet.ServletContext;
023:
024: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
025: import org.springframework.web.context.WebApplicationContext;
026: import org.strecks.context.impl.TestContextImpl;
027: import org.strecks.controller.impl.TestAction;
028: import org.testng.Assert;
029: import org.testng.annotations.Test;
030:
031: /**
032: * @author Phil Zoio
033: */
034: public class TestSpringActionBeanSource {
035:
036: @Test
037: public void testSource() {
038: Class actionBeanClass = TestAction.class;
039:
040: ServletContext servletContext = createStrictMock(ServletContext.class);
041: WebApplicationContext wac = createStrictMock(WebApplicationContext.class);
042:
043: expect(
044: servletContext
045: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
046: .andReturn(wac);
047: expect(wac.isSingleton("beanName")).andReturn(false);
048: expect(wac.getBean("beanName", TestAction.class)).andReturn(
049: new TestAction());
050:
051: // second call - note that isSingleton is not called second time
052: expect(
053: servletContext
054: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
055: .andReturn(wac);
056: expect(wac.getBean("beanName", TestAction.class)).andReturn(
057: new TestAction());
058:
059: replay(servletContext);
060: replay(wac);
061:
062: SpringActionBeanSource source = new SpringActionBeanSource(
063: actionBeanClass, "beanName");
064: TestContextImpl context = new TestContextImpl(servletContext);
065: Object bean1 = source.createBean(context);
066: source.createBean(context);
067:
068: assert bean1 instanceof TestAction;
069:
070: verify(servletContext);
071: verify(wac);
072:
073: }
074:
075: @Test
076: public void testNoWac() {
077: Class actionBeanClass = TestAction.class;
078:
079: ServletContext servletContext = createStrictMock(ServletContext.class);
080:
081: expect(
082: servletContext
083: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
084: .andReturn(null);
085:
086: replay(servletContext);
087:
088: SpringActionBeanSource source = new SpringActionBeanSource(
089: actionBeanClass, "beanName");
090: TestContextImpl context = new TestContextImpl(servletContext);
091:
092: try {
093: source.createBean(context);
094: Assert.fail();
095: } catch (IllegalStateException e) {
096: }
097:
098: verify(servletContext);
099:
100: }
101:
102: @Test
103: public void testNoBeanRegistered() {
104:
105: Class actionBeanClass = TestAction.class;
106:
107: ServletContext servletContext = createStrictMock(ServletContext.class);
108: WebApplicationContext wac = createStrictMock(WebApplicationContext.class);
109:
110: expect(
111: servletContext
112: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
113: .andReturn(wac);
114: expect(wac.isSingleton("beanName")).andReturn(false);
115: expect(wac.getBean("beanName", TestAction.class))
116: .andThrow(
117: new NoSuchBeanDefinitionException("beanName",
118: "message"));
119:
120: replay(servletContext);
121: replay(wac);
122:
123: SpringActionBeanSource source = new SpringActionBeanSource(
124: actionBeanClass, "beanName");
125: TestContextImpl context = new TestContextImpl(servletContext);
126:
127: try {
128: source.createBean(context);
129: Assert.fail();
130: } catch (NoSuchBeanDefinitionException e) {
131: }
132:
133: verify(servletContext);
134: verify(wac);
135:
136: }
137:
138: }
|