View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.components.test;
18  
19  import java.util.Properties;
20  
21  import org.apache.jetspeed.engine.JetspeedEngineConstants;
22  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
23  import org.springframework.context.ApplicationContext;
24  import org.springframework.context.support.ClassPathXmlApplicationContext;
25  
26  import junit.framework.TestCase;
27  
28  /***
29   * <p>
30   * AbstractSpringTestCase
31   * </p>
32   * <p>
33   * 
34   * </p>
35   * 
36   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
37   * @version $Id: AbstractSpringTestCase.java 598474 2007-11-27 00:37:16Z ate $
38   *  
39   */
40  public abstract class AbstractSpringTestCase extends TestCase
41  {
42      /***
43       * Provides access to the Spring ApplicationContext.
44       */
45      protected ClassPathXmlApplicationContext ctx;
46  
47      /***
48       * setup Spring context as part of test setup
49       */
50      protected void setUp() throws Exception
51      {        
52          super.setUp();
53          if (ctx == null)
54          {
55              String [] bootConfigurations = getBootConfigurations();
56              if (bootConfigurations != null)
57              {
58                  ApplicationContext bootContext = new ClassPathXmlApplicationContext(bootConfigurations, true);
59                  ctx = new ClassPathXmlApplicationContext(getConfigurations(), false, bootContext);
60              }
61              else
62              {
63                  ctx = new ClassPathXmlApplicationContext(getConfigurations(), false);
64              }
65              PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
66              Properties p = getPostProcessProperties();
67              p.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, "../../src/webapp");
68              ppc.setProperties(p);
69              ctx.addBeanFactoryPostProcessor(ppc);
70              ctx.refresh();
71          }
72      }
73  
74      /***
75       * close Spring context as part of test teardown
76       */
77      protected void tearDown() throws Exception
78      {        
79          super.tearDown();
80          if (ctx != null)
81          {
82              ctx.close();
83          }
84      }
85  
86      /***
87       * required specification of spring configurations
88       */
89      protected abstract String[] getConfigurations();
90      
91      /***
92       * optional specification of boot spring configurations
93       */
94      protected String[] getBootConfigurations()
95      {
96          return null;
97      }
98      
99      protected Properties getPostProcessProperties()
100     {
101         return new Properties();
102     }
103 }