View Javadoc

1   /*
2    * $Id: StrutsSpringObjectFactoryTest.java 669193 2008-06-18 14:47:55Z hermanns $
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.springframework.beans.factory.config.AutowireCapableBeanFactory;
29  import org.springframework.mock.web.MockServletContext;
30  import org.springframework.web.context.ConfigurableWebApplicationContext;
31  import org.springframework.web.context.WebApplicationContext;
32  import org.springframework.web.context.support.XmlWebApplicationContext;
33  
34  /***
35   * Unit test for {@link StrutsSpringObjectFactory}.
36   *
37   */
38  public class StrutsSpringObjectFactoryTest extends TestCase {
39  
40      public void testNoSpringContext() throws Exception {
41          // to cover situations where there will be logged an error
42          StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory(null, null, null, new MockServletContext());
43  
44          assertEquals(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, fac.getAutowireStrategy());
45      }
46  
47      public void testWithSpringContext() throws Exception {
48          
49  
50          ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
51          ServletContext msc = (ServletContext) new MockServletContext();
52          msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
53          ac.setServletContext(msc);
54          ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
55          ac.refresh();
56          StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, null, msc);
57  
58          assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
59      }
60  
61  
62  }