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 org.springframework.context.ApplicationContext;
20  import org.springframework.context.support.ClassPathXmlApplicationContext;
21  
22  import junit.framework.TestCase;
23  
24  /***
25   * <p>
26   * AbstractSpringTestCase
27   * </p>
28   * <p>
29   * 
30   * </p>
31   * 
32   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
33   * @version $Id: AbstractSpringTestCase.java 516448 2007-03-09 16:25:47Z ate $
34   *  
35   */
36  public abstract class AbstractSpringTestCase extends TestCase
37  {
38      /***
39       * Provides access to the Spring ApplicationContext.
40       */
41      protected ClassPathXmlApplicationContext ctx;
42  
43      /***
44       * setup Spring context as part of test setup
45       */
46      protected void setUp() throws Exception
47      {        
48          super.setUp();
49          if (ctx == null)
50          {
51              String [] bootConfigurations = getBootConfigurations();
52              if (bootConfigurations != null)
53              {
54                  ApplicationContext bootContext = new ClassPathXmlApplicationContext(bootConfigurations, true);
55                  ctx = new ClassPathXmlApplicationContext(getConfigurations(), true, bootContext);
56              }
57              else
58              {
59                  ctx = new ClassPathXmlApplicationContext(getConfigurations(), true);
60              }
61          }
62      }
63  
64      /***
65       * close Spring context as part of test teardown
66       */
67      protected void tearDown() throws Exception
68      {        
69          super.tearDown();
70          if (ctx != null)
71          {
72              ctx.close();
73          }
74      }
75  
76      /***
77       * required specification of spring configurations
78       */
79      protected abstract String[] getConfigurations();
80      
81      /***
82       * optional specification of boot spring configurations
83       */
84      protected String[] getBootConfigurations()
85      {
86          return null;
87      }
88  }