001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.testhelpers;
018:
019: import java.io.File;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.configuration.CompositeConfiguration;
024: import org.apache.commons.configuration.Configuration;
025: import org.apache.commons.configuration.ConfigurationException;
026: import org.apache.commons.configuration.PropertiesConfiguration;
027: import org.springframework.beans.factory.config.ConfigurableBeanFactory;
028: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
029:
030: public abstract class AbstractTestHelper implements TestHelper {
031: public static final String APP_CONTEXT = "AppContext";
032:
033: private final Map context;
034:
035: private static final CompositeConfiguration USER_PROPERTIES;
036: static {
037: try {
038: File userBuildFile = new File(System
039: .getProperty("user.home"), "build.properties");
040: Configuration userBuildProps = loadConfiguration(userBuildFile);
041:
042: File mavenBuildFile = new File("../../build.properties");
043: Configuration mavenBuildProps = loadConfiguration(mavenBuildFile);
044:
045: File mavenProjectFile = new File("../../project.properties");
046: Configuration mavenProjectProps = loadConfiguration(mavenProjectFile);
047:
048: USER_PROPERTIES = new CompositeConfiguration();
049: USER_PROPERTIES.addConfiguration(userBuildProps);
050: USER_PROPERTIES.addConfiguration(mavenBuildProps);
051: USER_PROPERTIES.addConfiguration(mavenProjectProps);
052: } catch (ConfigurationException e) {
053:
054: throw new IllegalStateException(
055: "Unable to load ${USER_HOME}/build.properties");
056: }
057: }
058:
059: private static Configuration loadConfiguration(File propsFile)
060: throws ConfigurationException {
061: if (propsFile.exists()) {
062: return new PropertiesConfiguration(propsFile);
063: } else {
064: return new PropertiesConfiguration();
065: }
066: }
067:
068: public AbstractTestHelper(Map context) {
069: this .context = context;
070: }
071:
072: public AbstractTestHelper() {
073: context = new HashMap();
074: }
075:
076: public Map getContext() {
077: return context;
078: }
079:
080: public final String getUserProperty(String key) {
081: // use system properties passed to test via the
082: // maven.junit.sysproperties configuration from
083: // maven build.properties and/or project.properties
084: String prop = System.getProperty(key);
085: if (prop == null) {
086: return USER_PROPERTIES.getString(key);
087: } else {
088: return prop;
089: }
090: }
091:
092: protected final void addBeanFactory(ConfigurableBeanFactory bf) {
093: ConfigurableBeanFactory currentBf = (ConfigurableBeanFactory) context
094: .get(APP_CONTEXT);
095: if (currentBf != null) {
096: bf.setParentBeanFactory(currentBf);
097: context
098: .put(APP_CONTEXT,
099: new DefaultListableBeanFactory(bf));
100: } else {
101: context.put(APP_CONTEXT, bf);
102: }
103: }
104:
105: }
|