Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COLLECTIONS-664] Add a class that extend a load method which accept … #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.group"), "human");
Assert.assertEquals(properties.get("test.value"), "28");
} 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