1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 }