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