From 9434fd883fc820c0aac2b953a9c6ad73bd969378 Mon Sep 17 00:00:00 2001 From: zhangminglei Date: Mon, 6 Nov 2017 17:36:28 +0800 Subject: [PATCH] [COLLECTIONS-664] Add a class that extend a load method which accept a filename. --- .../properties/FileProperties.java | 42 +++++++++++++++++++ .../collections4/properties/package-info.java | 1 + .../properties/FilePropertiesTest.java | 40 ++++++++++++++++++ src/test/resources/test.properties | 3 ++ 4 files changed, 86 insertions(+) create mode 100644 src/main/java/org/apache/commons/collections4/properties/FileProperties.java create mode 100644 src/test/java/org/apache/commons/collections4/properties/FilePropertiesTest.java create mode 100644 src/test/resources/test.properties diff --git a/src/main/java/org/apache/commons/collections4/properties/FileProperties.java b/src/main/java/org/apache/commons/collections4/properties/FileProperties.java new file mode 100644 index 0000000000..8801bd392e --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/properties/FileProperties.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.collections4.properties; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * A class that extend a load method which accept a filename. + *

+ * Use context classloader to load current activated file. + *

+ * + * @since 4.2 + */ +public class FileProperties extends Properties { + + private static final long serialVersionUID = 1L; + + public synchronized Properties load(String fileName) throws IOException { + Properties properties = new Properties(); + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName); + properties.load(inputStream); + return properties; + } +} diff --git a/src/main/java/org/apache/commons/collections4/properties/package-info.java b/src/main/java/org/apache/commons/collections4/properties/package-info.java index 310795af85..66dbff136b 100644 --- a/src/main/java/org/apache/commons/collections4/properties/package-info.java +++ b/src/main/java/org/apache/commons/collections4/properties/package-info.java @@ -21,6 +21,7 @@ * The following classes are provided in the package: * */ package org.apache.commons.collections4.properties; diff --git a/src/test/java/org/apache/commons/collections4/properties/FilePropertiesTest.java b/src/test/java/org/apache/commons/collections4/properties/FilePropertiesTest.java new file mode 100644 index 0000000000..d5f50243a2 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/properties/FilePropertiesTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.collections4.properties; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.Properties; + +public class FilePropertiesTest { + + @Test + public void testLoad() { + final FileProperties fileProperties = new FileProperties(); + try { + Properties properties = fileProperties.load("test.properties"); + Assert.assertEquals(properties.get("test.key"), "age"); + Assert.assertEquals(properties.get("test.value"), "28"); + Assert.assertEquals(properties.get("test.group"), "human"); + } catch (IOException iox) { + iox.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties new file mode 100644 index 0000000000..41638f147d --- /dev/null +++ b/src/test/resources/test.properties @@ -0,0 +1,3 @@ +test.key=age +test.value=28 +test.group=human \ No newline at end of file