diff --git a/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java b/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java deleted file mode 100644 index 4fc593fe0d6cf8f0781fa76200ee47ec9a783f2d..0000000000000000000000000000000000000000 --- a/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 java.util.Properties; -import java.util.Set; - -/** - * Useful utility functions concerning configuration data. - * - * @author Franz-Josef Elmer - */ -public class ConfigUtilities -{ - /** - * Extracts from the specified properties object all properties starting with the specified key prefix. - */ - public static Properties extractPropertiesStartingWith(String prefix, Properties properties) - { - assert prefix != null : "Missing prefix"; - assert properties != null : "Missing properties"; - - Properties result = new Properties(); - int prefixLength = prefix.length(); - Set<Object> keys = properties.keySet(); - for (Object object : keys) - { - String key = object.toString(); - if (key.startsWith(prefix)) - { - result.setProperty(key.substring(prefixLength), properties.getProperty(key)); - } - } - return result; - } - - private ConfigUtilities() - { - } - -} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java b/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java index 309cfba2ca0184272f73030c588a748821ec2084..03a756fe107620ba4cbd22811d4c8ab5f0dd2564 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java @@ -77,16 +77,20 @@ public final class ExtendedProperties extends Properties * Returns a subset of given <code>Properties</code> based on given property key prefix. * * @param prefix string, each property key should start with. + * @param dropPrefix If <code>true</code> the prefix will be removed from the key. */ - public final ExtendedProperties getSubset(final String prefix) + public final ExtendedProperties getSubset(final String prefix, boolean dropPrefix) { + assert prefix != null : "Missing prefix"; + ExtendedProperties result = new ExtendedProperties(); + int prefixLength = prefix.length(); for (Enumeration<?> enumeration = propertyNames(); enumeration.hasMoreElements();) { String key = enumeration.nextElement().toString(); if (key.startsWith(prefix)) { - result.put(key, getProperty(key)); + result.put(dropPrefix ? key.substring(prefixLength) : key, getProperty(key)); } } return result; diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java deleted file mode 100644 index ce70fb05297fbcfce4584af8a57045e4110e73c3..0000000000000000000000000000000000000000 --- a/common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 java.util.Properties; - -import org.testng.annotations.Test; - -/** - * - * - * @author Franz-Josef Elmer - */ -public class ConfigUtilitiesTest -{ - @Test - public void testEmptyProperties() - { - Properties props = ConfigUtilities.extractPropertiesStartingWith("blabla", new Properties()); - assertEquals(0, props.size()); - } - - @Test - public void testNonEmptyInputPropertiesButEmptyResult() - { - Properties properties = new Properties(); - properties.setProperty("bla", "blub"); - Properties props = ConfigUtilities.extractPropertiesStartingWith("blabla", properties); - assertEquals(0, props.size()); - } - - @Test - public void testNonEmptyInputPropertiesAndNonEmptyResult() - { - Properties properties = new Properties(); - properties.setProperty("hello", "world"); - properties.setProperty("bla", "bla"); - properties.setProperty("bla.", "blub"); - properties.setProperty("bla.a", "blab"); - properties.setProperty("bla.ab", "blab ab"); - Properties props = ConfigUtilities.extractPropertiesStartingWith("bla.", properties); - assertEquals(3, props.size()); - assertEquals("blub", props.getProperty("")); - assertEquals("blab", props.getProperty("a")); - assertEquals("blab ab", props.getProperty("ab")); - } -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java index c42fc87a315f75bd3d034d09729c72f24d9fe00a..5e3b68dcb58f9d6ca316da0c32f6246f6e1cb7e1 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java @@ -68,21 +68,33 @@ public final class ExtendedPropertiesTest @Test public final void testGetSubsetString() { - ExtendedProperties props = extendedProperties.getSubset("t"); + ExtendedProperties props = extendedProperties.getSubset("t", false); assert props.size() == 2; assert props.getProperty("two").equals("zwei"); - props = extendedProperties.getSubset("un"); + assert props.getProperty("three").equals("drei"); + + props = extendedProperties.getSubset("un", false); assert props.size() == 1; assert props.getProperty("un").equals("eins"); + + props = extendedProperties.getSubset("t", true); + assert props.size() == 2; + assert props.getProperty("wo").equals("zwei"); + assert props.getProperty("hree").equals("drei"); + + props = extendedProperties.getSubset("un", true); + assert props.size() == 1; + assert props.getProperty("").equals("eins"); } @Test public final void testCyclicDependency() { ExtendedProperties props = new ExtendedProperties(); - props.setProperty("a", "${b}"); - props.setProperty("b", "${a}"); - assertEquals("${b}", props.getProperty("b")); + props.setProperty("a", "A${b}"); + props.setProperty("b", "B${c}"); + props.setProperty("c", "C${a}"); + assertEquals("ABC${a}", props.getProperty("a")); } @Test