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 |
0 |
File userBuildFile = new File(System.getProperty("user.home"), "build.properties"); |
42 |
0 |
Configuration userBuildProps = loadConfiguration(userBuildFile); |
43 |
|
|
44 |
0 |
File mavenBuildFile = new File("../../build.properties"); |
45 |
0 |
Configuration mavenBuildProps = loadConfiguration(mavenBuildFile); |
46 |
|
|
47 |
0 |
File mavenProjectFile = new File("../../project.properties"); |
48 |
0 |
Configuration mavenProjectProps = loadConfiguration(mavenProjectFile); |
49 |
|
|
50 |
0 |
USER_PROPERTIES = new CompositeConfiguration(); |
51 |
0 |
USER_PROPERTIES.addConfiguration(userBuildProps); |
52 |
0 |
USER_PROPERTIES.addConfiguration(mavenBuildProps); |
53 |
0 |
USER_PROPERTIES.addConfiguration(mavenProjectProps); |
54 |
|
} |
55 |
0 |
catch (ConfigurationException e) |
56 |
|
{ |
57 |
|
|
58 |
0 |
throw new IllegalStateException("Unable to load ${USER_HOME}/build.properties"); |
59 |
0 |
} |
60 |
0 |
} |
61 |
|
|
62 |
|
private static Configuration loadConfiguration(File propsFile) throws ConfigurationException |
63 |
|
{ |
64 |
0 |
if (propsFile.exists()) |
65 |
|
{ |
66 |
0 |
return new PropertiesConfiguration(propsFile); |
67 |
|
} |
68 |
|
else |
69 |
|
{ |
70 |
0 |
return new PropertiesConfiguration(); |
71 |
|
} |
72 |
|
} |
73 |
|
|
74 |
|
public AbstractTestHelper(Map context) |
75 |
0 |
{ |
76 |
0 |
this.context = context; |
77 |
0 |
} |
78 |
|
|
79 |
|
public AbstractTestHelper() |
80 |
0 |
{ |
81 |
0 |
context = new HashMap(); |
82 |
0 |
} |
83 |
|
|
84 |
|
public Map getContext() |
85 |
|
{ |
86 |
0 |
return context; |
87 |
|
} |
88 |
|
|
89 |
|
public final String getUserProperty(String key) |
90 |
|
{ |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
0 |
String prop = System.getProperty(key); |
95 |
0 |
if (prop == null) |
96 |
|
{ |
97 |
0 |
return USER_PROPERTIES.getString(key); |
98 |
|
} |
99 |
|
else |
100 |
|
{ |
101 |
0 |
return prop; |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
protected final void addBeanFactory(ConfigurableBeanFactory bf) |
106 |
|
{ |
107 |
0 |
ConfigurableBeanFactory currentBf = (ConfigurableBeanFactory) context.get(APP_CONTEXT); |
108 |
0 |
if (currentBf != null) |
109 |
|
{ |
110 |
0 |
bf.setParentBeanFactory(currentBf); |
111 |
0 |
context.put(APP_CONTEXT, new DefaultListableBeanFactory(bf)); |
112 |
|
} |
113 |
|
else |
114 |
|
{ |
115 |
0 |
context.put(APP_CONTEXT, bf); |
116 |
|
} |
117 |
0 |
} |
118 |
|
|
119 |
|
} |