[COLLECTIONS-664] Add a class that extend a load method which accept a filename.
This commit is contained in:
parent
0b1460dadb
commit
e1e762ec14
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@
|
||||||
* The following classes are provided in the package:
|
* The following classes are provided in the package:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>SortedProperties- A drop-in replacement for Properties for sorting keys.</li>
|
* <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>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections4.properties;
|
package org.apache.commons.collections4.properties;
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
test.key=age
|
||||||
|
test.value=28
|
||||||
|
test.group=human
|
Loading…
Reference in New Issue