From ee5cf9d7b90eda795b82d1d1899f370d8007cf71 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Tue, 7 Aug 2007 12:43:08 +0000 Subject: [PATCH] add a useful utilities function (with test) SVN: 1259 --- .../cisd/common/config/ConfigUtilities.java | 55 ++++++++++++++++ .../common/config/ConfigUtilitiesTest.java | 63 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java diff --git a/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java b/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java new file mode 100644 index 00000000000..4fc593fe0d6 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/config/ConfigUtilities.java @@ -0,0 +1,55 @@ +/* + * 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/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java new file mode 100644 index 00000000000..ce70fb05297 --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/config/ConfigUtilitiesTest.java @@ -0,0 +1,63 @@ +/* + * 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")); + } +} -- GitLab