Skip to content
Snippets Groups Projects
Commit d485a842 authored by ribeaudc's avatar ribeaudc
Browse files

remove: - Up to Franz-Josef can be removed.

SVN: 4364
parent 6b3913c6
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.config;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
/**
* Provider of configuration data.
*
* @author Franz-Josef Elmer
*/
public class Configuration
{
private final IConfigurationDataProvider configuration;
public Configuration(IConfigurationDataProvider configuration)
{
this.configuration = configuration;
}
/**
* Returns the specified string property.
*
* @param key The key of the requested property. Has to be a non-empty string.
* @throws EnvironmentFailureException if property not found.
*/
public String getStringProperty(String key)
{
String property = configuration.getProperty(key);
if (property == null)
{
throw new EnvironmentFailureException("Undefined property '" + key + "'.");
}
return property;
}
/**
* Returns the specified string property or the specified default value if not found.
*
* @param key The key of the requested property. Has to be a non-empty string.
*/
public String getStringProperty(String key, String defaultValue)
{
String property = configuration.getProperty(key);
return property == null ? defaultValue : property;
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.config;
/**
* Interface for objects able to provide configuration data.
*
* @author Franz-Josef Elmer
*/
public interface IConfigurationDataProvider
{
/**
* Returns the property for the specified key.
*
* @param key The key of the requested property. Has to be a non-empty string.
* @return <code>null</code> if no property could be found.
*/
public String getProperty(String key);
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.config;
/**
* Configuration data provider based on system properties.
*
* @author Franz-Josef Elmer
*/
public class SystemPropertiesProvider implements IConfigurationDataProvider
{
/**
* Delegate call to <code>System.getProperty()</code>.
*/
public String getProperty(String key)
{
assert key != null && key.length() > 0;
return System.getProperty(key);
}
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.config;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
/**
* Tests for {@link Configuration}.
*
* @author Franz-Josef Elmer
*/
public class ConfigurationTest
{
private static final class MockConfiguration implements IConfigurationDataProvider
{
private final Map<String, String> map;
MockConfiguration(Map<String, String> map)
{
this.map = map;
}
public String getProperty(String key)
{
return map.get(key);
}
}
@Test
public void testGetStringProperty()
{
Map<String, String> map = new HashMap<String, String>();
map.put("greetings", "hello world");
Configuration configuration = new Configuration(new MockConfiguration(map));
assertEquals("hello world", configuration.getStringProperty("greetings"));
}
@Test
public void testGetUndefinedStringProperty()
{
Map<String, String> map = new HashMap<String, String>();
Configuration configuration = new Configuration(new MockConfiguration(map));
try
{
configuration.getStringProperty("greetings");
fail("EnvironmentFailureException expected");
} catch (EnvironmentFailureException e)
{
String message = e.getMessage();
assertTrue(message, message.indexOf("greetings") >= 0);
}
}
@Test
public void testGetStringPropertyWithDefault()
{
Map<String, String> map = new HashMap<String, String>();
map.put("greetings", "hello world");
Configuration configuration = new Configuration(new MockConfiguration(map));
assertEquals("default", configuration.getStringProperty("blabla", "default"));
assertEquals("hello world", configuration.getStringProperty("greetings", "default"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment