View Javadoc

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