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.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          // use system properties passed to test via the
92          // maven.junit.sysproperties configuration from
93          // maven build.properties and/or project.properties
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 }