org.unitils.core.config
Class UnitilsConfiguration

java.lang.Object
  extended by org.unitils.core.config.UnitilsConfiguration

public class UnitilsConfiguration
extends Object

Author:
Tim Ducheyne, Filip Neven

Constructor Summary
UnitilsConfiguration(Properties properties)
          Creates a configuration for the given properties.
 
Method Summary
 boolean containsProperty(String propertyName)
           
 boolean getBoolean(String propertyName)
          Gets the boolean value for the property with the given name.
 boolean getBoolean(String propertyName, boolean defaultValue)
          Gets the boolean value for the property with the given name.
<T> T
getInstance(String propertyName)
          Gets an instance of the class name specified by the property with the given name.
<T> T
getInstance(String propertyName, T defaultValue)
          Gets an instance of the class name specified by the property with the given name.
<T> T
getInstanceOf(Class<T> type, String... discriminators)
          Gets an instance of the given type (typically an interface).
 int getInt(String propertyName)
          Gets the int value for the property with the given name.
 int getInt(String propertyName, int defaultValue)
          Gets the int value for the property with the given name.
 long getLong(String propertyName)
          Gets the long value for the property with the given name.
 long getLong(String propertyName, long defaultValue)
          Gets the long value for the property with the given name.
 Properties getProperties()
           
 String getString(String propertyName)
          Gets the string value for the property with the given name.
 String getString(String propertyName, String defaultValue)
          Gets the string value for the property with the given name.
 List<String> getStringList(String propertyName)
          Gets the list of comma separated string values for the property with the given name.
 List<String> getStringList(String propertyName, boolean required)
          Gets the list of comma separated string values for the property with the given name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnitilsConfiguration

public UnitilsConfiguration(Properties properties)
Creates a configuration for the given properties.

Parameters:
properties - All configuration properties, not null
Method Detail

getString

public String getString(String propertyName)
Gets the string value for the property with the given name. If no such property is found or the value is empty, an exception will be raised.

Parameters:
propertyName - The name, not null
Returns:
The trimmed string value, not null

getString

public String getString(String propertyName,
                        String defaultValue)
Gets the string value for the property with the given name. If no such property is found or the value is empty, the given default value is returned.

Parameters:
propertyName - The name, not null
defaultValue - The default value
Returns:
The trimmed string value, not null

getStringList

public List<String> getStringList(String propertyName)
Gets the list of comma separated string values for the property with the given name. If no such property is found or the value is empty, an empty list is returned. Empty elements (",,") will not be added.

Parameters:
propertyName - The name, not null
Returns:
The trimmed string list, empty if none found

getStringList

public List<String> getStringList(String propertyName,
                                  boolean required)
Gets the list of comma separated string values for the property with the given name. If no such property is found or the value is empty, an empty list is returned if not required, else an exception is raised. Empty elements (",,") will not be added.

Parameters:
propertyName - The name, not null
required - If true an exception will be raised when the property is not found or empty
Returns:
The trimmed string list, empty or exception if none found

getBoolean

public boolean getBoolean(String propertyName)
Gets the boolean value for the property with the given name. If no such property is found, the value is empty or not a boolean, an exception will be raised.

Parameters:
propertyName - The name, not null
Returns:
The boolean value

getBoolean

public boolean getBoolean(String propertyName,
                          boolean defaultValue)
Gets the boolean value for the property with the given name. If no such property is found or the value is empty, the given default value is returned. An exception will be raised if the value is not a boolean.

Parameters:
propertyName - The name, not null
defaultValue - The default value
Returns:
The boolean value

getLong

public long getLong(String propertyName)
Gets the long value for the property with the given name. If no such property is found, the value is empty or cannot be converted to a long, an exception will be raised.

Parameters:
propertyName - The name, not null
Returns:
The long value

getLong

public long getLong(String propertyName,
                    long defaultValue)
Gets the long value for the property with the given name. If no such property is found or the value is empty, the given default value is returned. An exception will be raised if the value cannot be converted to a long.

Parameters:
propertyName - The name, not null
defaultValue - The default value
Returns:
The long value

getInt

public int getInt(String propertyName)
Gets the int value for the property with the given name. If no such property is found, the value is empty or cannot be converted to an int, an exception will be raised.

Parameters:
propertyName - The name, not null
Returns:
The int value

getInt

public int getInt(String propertyName,
                  int defaultValue)
Gets the int value for the property with the given name. If no such property is found or the value is empty, the given default value is returned. An exception will be raised if the value cannot be converted to an int.

Parameters:
propertyName - The name, not null
defaultValue - The default value
Returns:
The int value

getInstance

public <T> T getInstance(String propertyName)
Gets an instance of the class name specified by the property with the given name. If no such property is found, the value is empty or the instance cannot be created, an exception will be raised.

If the created instance is implementation of Configurable, the init method will be called.

Parameters:
propertyName - The name, not null
Returns:
The instance value, not null

getInstance

public <T> T getInstance(String propertyName,
                         T defaultValue)
Gets an instance of the class name specified by the property with the given name. If no such property is found, the value is empty, the given default value is returned. An exception will be raised if the instance cannot be created.
If the created instance is implementation of Configurable, the init method will be called.

Parameters:
propertyName - The name, not null
defaultValue - The default value
Returns:
The instance value, not null

getInstanceOf

public <T> T getInstanceOf(Class<T> type,
                           String... discriminators)
Gets an instance of the given type (typically an interface). The class name will be resolved as follows: 'fully qualified name of type'.implClassName(.discriminator1.discriminator2) If discriminators are provided, it will first look for a property using all discriminators, then look for a property without the last discriminator etc. If still no property was found, it will look for the property without discriminators. E.g. suppose you have following properties:

com.package.MyInterface.implClassName = com.package.MyInterfaceImpl
com.package.MyInterface.implClassName.oracle = com.package.MyOracleInterfaceImpl

getInstanceOf(MyInterface.class, "oracle", "v9");

will first try
com.package.MyInterface.implClassName.oracle.v9

but since no such property exists it will try
com.package.MyInterface.implClassName.oracle

and since such a property exists, it will return an instance com.package.MyOracleInterfaceImpl

If oracle did not exist it would have looked for the property without discriminators, i.e.
com.package.MyInterface.implClassName

If the created instance is implementation of Configurable, the init method will be called.

Parameters:
type - The type of the instance
discriminators - Optional. The values that define which specific implementation class should be used.
Returns:
The instance

containsProperty

public boolean containsProperty(String propertyName)
Parameters:
propertyName - The property name, not null
Returns:
True if the property exists

getProperties

public Properties getProperties()
Returns:
All properties, not null


Copyright © 2011. All Rights Reserved.