1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.testhelpers;
18
19 import java.io.File;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.commons.configuration.CompositeConfiguration;
24 import org.apache.commons.configuration.Configuration;
25 import org.apache.commons.configuration.ConfigurationException;
26 import org.apache.commons.configuration.PropertiesConfiguration;
27 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
28 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
29
30 public abstract class AbstractTestHelper implements TestHelper
31 {
32 public static final String APP_CONTEXT = "AppContext";
33
34 private final Map context;
35
36 private static final CompositeConfiguration USER_PROPERTIES;
37 static
38 {
39 try
40 {
41 File userBuildFile = new File(System.getProperty("user.home"), "build.properties");
42 Configuration userBuildProps = loadConfiguration(userBuildFile);
43
44 File mavenBuildFile = new File("../../build.properties");
45 Configuration mavenBuildProps = loadConfiguration(mavenBuildFile);
46
47 File mavenProjectFile = new File("../../project.properties");
48 Configuration mavenProjectProps = loadConfiguration(mavenProjectFile);
49
50 USER_PROPERTIES = new CompositeConfiguration();
51 USER_PROPERTIES.addConfiguration(userBuildProps);
52 USER_PROPERTIES.addConfiguration(mavenBuildProps);
53 USER_PROPERTIES.addConfiguration(mavenProjectProps);
54 }
55 catch (ConfigurationException e)
56 {
57
58 throw new IllegalStateException("Unable to load ${USER_HOME}/build.properties");
59 }
60 }
61
62 private static Configuration loadConfiguration(File propsFile) throws ConfigurationException
63 {
64 if (propsFile.exists())
65 {
66 return new PropertiesConfiguration(propsFile);
67 }
68 else
69 {
70 return new PropertiesConfiguration();
71 }
72 }
73
74 public AbstractTestHelper(Map context)
75 {
76 this.context = context;
77 }
78
79 public AbstractTestHelper()
80 {
81 context = new HashMap();
82 }
83
84 public Map getContext()
85 {
86 return context;
87 }
88
89 public final String getUserProperty(String key)
90 {
91
92
93
94 String prop = System.getProperty(key);
95 if (prop == null)
96 {
97 return USER_PROPERTIES.getString(key);
98 }
99 else
100 {
101 return prop;
102 }
103 }
104
105 protected final void addBeanFactory(ConfigurableBeanFactory bf)
106 {
107 ConfigurableBeanFactory currentBf = (ConfigurableBeanFactory) context.get(APP_CONTEXT);
108 if (currentBf != null)
109 {
110 bf.setParentBeanFactory(currentBf);
111 context.put(APP_CONTEXT, new DefaultListableBeanFactory(bf));
112 }
113 else
114 {
115 context.put(APP_CONTEXT, bf);
116 }
117 }
118
119 }