Skip to content

Commit

Permalink
[COLLECTIONS-664] Add a class that extend a load method which accept …
Browse files Browse the repository at this point in the history
…a filename.
  • Loading branch information
zhangminglei committed Nov 6, 2017
1 parent 0b1460d commit 9434fd8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Use context classloader to load current activated file.
* </p>
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* The following classes are provided in the package:
* <ul>
* <li>SortedProperties- A drop-in replacement for Properties for sorting keys.</li>
* <li>FileProperties- A class that extend load functionality for getting Properties.<li/>
* </ul>
*/
package org.apache.commons.collections4.properties;
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 3 additions & 0 deletions src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.key=age
test.value=28
test.group=human

0 comments on commit 9434fd8

Please sign in to comment.