View Javadoc

1   /*
2    * $Id: StrutsSpringObjectFactoryTest.java 817418 2009-09-21 21:36:17Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.spring;
23  
24  import javax.servlet.ServletContext;
25  
26  import junit.framework.TestCase;
27  import org.apache.struts2.StrutsConstants;
28  import org.easymock.EasyMock;
29  import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
30  import org.springframework.mock.web.MockServletContext;
31  import org.springframework.web.context.ConfigurableWebApplicationContext;
32  import org.springframework.web.context.WebApplicationContext;
33  import org.springframework.web.context.support.XmlWebApplicationContext;
34  
35  import com.opensymphony.xwork2.inject.Container;
36  
37  /***
38   * Unit test for {@link StrutsSpringObjectFactory}.
39   *
40   */
41  public class StrutsSpringObjectFactoryTest extends TestCase {
42  
43      public void testNoSpringContext() throws Exception {
44          // to cover situations where there will be logged an error
45          Container container = EasyMock.createNiceMock(Container.class);
46          EasyMock.replay(container);
47          
48          StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory(null, null, null, new MockServletContext(), null, container);
49  
50          assertEquals(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, fac.getAutowireStrategy());
51      }
52  
53      public void testWithSpringContext() throws Exception {
54          Container container = EasyMock.createNiceMock(Container.class);
55          EasyMock.replay(container);
56  
57          ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
58          ServletContext msc = (ServletContext) new MockServletContext();
59          msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
60          ac.setServletContext(msc);
61          ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
62          ac.refresh();
63          StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, null, msc, null, container);
64  
65          assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
66      }
67  
68  
69  }