View Javadoc

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