org.unitils.util.ConfigUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unitils Show documentation
Show all versions of unitils Show documentation
Unitils provides utilities to further simplify unit-testing with JUnit, DBUnit, EasyMock
Hibernate and Spring. The goal is to make unit-testing easy and maintainable by offering
utilities such as automatic DB-schema maintainance and equality assertion through reflection.
/*
* Copyright 2006-2007, Unitils.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.unitils.util;
import static org.unitils.util.ReflectionUtils.createInstanceOfType;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.unitils.core.UnitilsException;
/**
* Class containing configuration related utilities
*
* @author Filip Neven
* @author Tim Ducheyne
*/
public class ConfigUtils {
/* The logger instance for this class */
private static Log logger = LogFactory.getLog(ConfigUtils.class);
/**
* Retrieves the concrete instance of the class with the given type as configured by the given Configuration
.
* Tries to retrieve a specific implementation first (propery key = fully qualified name of the interface
* type + '.impl.className.' + implementationDiscriminatorValue). If this key does not exist, the generally configured
* instance is retrieved (same property key without the implementationDiscriminatorValue).
*
* @param type The type of the instance
* @param configuration The configuration containing the necessary properties for configuring the instance
* @param implementationDiscriminatorValues
* The values that define which specific implementation class should be used.
* This is typically an environment specific property, like the DBMS that is used.
* @return The configured instance
*/
@SuppressWarnings({"unchecked"})
public static T getConfiguredInstance(Class type, Properties configuration, String... implementationDiscriminatorValues) {
String implClassName = getConfiguredClassName(type, configuration, implementationDiscriminatorValues);
logger.debug("Creating instance of " + type + ". Implementation class " + implClassName);
return (T) createInstanceOfType(implClassName, false);
}
/**
* Retrieves the class name of the concrete instance of the class with the given type as configured by the given Configuration
.
* Tries to retrieve a specific implementation first (propery key = fully qualified name of the interface
* type + '.impl.className.' + implementationDiscriminatorValue). If this key does not exist, the generally configured
* instance is retrieved (same property key without the implementationDiscriminatorValue).
*
* @param type The type of the instance
* @param configuration The configuration containing the necessary properties for configuring the instance
* @param implementationDiscriminatorValues
* The values that define which specific implementation class should be used.
* This is typically an environment specific property, like the DBMS that is used.
* @return The configured class name
*/
public static String getConfiguredClassName(Class> type, Properties configuration, String... implementationDiscriminatorValues) {
String propKey = type.getName() + ".implClassName";
// first try specific instance using the given discriminators
if (implementationDiscriminatorValues != null) {
String implementationSpecificPropKey = propKey;
for (String implementationDiscriminatorValue : implementationDiscriminatorValues) {
implementationSpecificPropKey += '.' + implementationDiscriminatorValue;
}
if (configuration.containsKey(implementationSpecificPropKey)) {
return PropertyUtils.getString(implementationSpecificPropKey, configuration);
}
}
// specifig not found, try general configured instance
if (configuration.containsKey(propKey)) {
return PropertyUtils.getString(propKey, configuration);
}
// no configuration found
throw new UnitilsException("Missing configuration for " + propKey);
}
}