JAVA-19115 Create new core-java-properties sub module under core-java-modules (#13669)

* JAVA-19115 Create new core-java-properties sub module under core-java-modules
This commit is contained in:
anuragkumawat
2023-04-12 17:02:52 +05:30
committed by GitHub
parent 1ffa919db6
commit 584556bd46
13 changed files with 30 additions and 3 deletions

View File

@@ -0,0 +1,19 @@
package com.baeldung.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoader {
public static Properties loadProperties(String resourceFileName) throws IOException {
Properties configuration = new Properties();
InputStream inputStream = PropertiesLoader.class
.getClassLoader()
.getResourceAsStream(resourceFileName);
configuration.load(inputStream);
inputStream.close();
return configuration;
}
}

View File

@@ -0,0 +1,171 @@
package com.baeldung.java.properties;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import org.junit.Test;
public class PropertiesUnitTest {
@Test
public void givenPropertyValue_whenPropertiesFileLoaded_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
String catalogConfigPath = rootPath + "catalog";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
Properties catalogProps = new Properties();
catalogProps.load(new FileInputStream(catalogConfigPath));
String appVersion = appProps.getProperty("version");
assertEquals("1.0", appVersion);
assertEquals("files", catalogProps.getProperty("c1"));
}
@Test
public void givenPropertyValue_whenXMLPropertiesFileLoaded_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String iconConfigPath = rootPath + "icons.xml";
Properties iconProps = new Properties();
iconProps.loadFromXML(new FileInputStream(iconConfigPath));
assertEquals("icon1.jpg", iconProps.getProperty("fileIcon"));
}
@Test
public void givenAbsentProperty_whenPropertiesFileLoaded_thenReturnsDefault() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
String appVersion = appProps.getProperty("version");
String appName = appProps.getProperty("name", "defaultName");
String appGroup = appProps.getProperty("group", "baeldung");
String appDownloadAddr = appProps.getProperty("downloadAddr");
assertEquals("1.0", appVersion);
assertEquals("TestApp", appName);
assertEquals("baeldung", appGroup);
assertNull(appDownloadAddr);
}
@Test(expected = Exception.class)
public void givenImproperObjectCasting_whenPropertiesFileLoaded_thenThrowsException() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
float appVerFloat = (float) appProps.get("version");
}
@Test
public void givenPropertyValue_whenPropertiesSet_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
appProps.setProperty("name", "NewAppName");
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
String newAppName = appProps.getProperty("name");
assertEquals("NewAppName", newAppName);
String newAppDownloadAddr = appProps.getProperty("downloadAddr");
assertEquals("www.baeldung.com/downloads", newAppDownloadAddr);
}
@Test
public void givenPropertyValueNull_whenPropertiesRemoved_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
String versionBeforeRemoval = appProps.getProperty("version");
assertEquals("1.0", versionBeforeRemoval);
appProps.remove("version");
String versionAfterRemoval = appProps.getProperty("version");
assertNull(versionAfterRemoval);
}
@Test
public void whenPropertiesStoredInFile_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
String newAppConfigPropertiesFile = rootPath + "newApp.properties";
appProps.store(new FileWriter(newAppConfigPropertiesFile), "store to properties file");
String newAppConfigXmlFile = rootPath + "newApp.xml";
appProps.storeToXML(new FileOutputStream(newAppConfigXmlFile), "store to xml file");
}
@Test
public void givenPropertyValueAbsent_LoadsValuesFromDefaultProperties() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String defaultConfigPath = rootPath + "default.properties";
Properties defaultProps = new Properties();
defaultProps.load(new FileInputStream(defaultConfigPath));
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties(defaultProps);
appProps.load(new FileInputStream(appConfigPath));
String appName = appProps.getProperty("name");
String appVersion = appProps.getProperty("version");
String defaultSite = appProps.getProperty("site");
assertEquals("1.0", appVersion);
assertEquals("TestApp", appName);
assertEquals("www.google.com", defaultSite);
}
@Test
public void givenPropertiesSize_whenPropertyFileLoaded_thenCorrect() throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appPropsPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appPropsPath));
appProps.list(System.out); // list all key-value pairs
Enumeration<Object> valueEnumeration = appProps.elements();
while (valueEnumeration.hasMoreElements()) {
System.out.println(valueEnumeration.nextElement());
}
Enumeration<Object> keyEnumeration = appProps.keys();
while (keyEnumeration.hasMoreElements()) {
System.out.println(keyEnumeration.nextElement());
}
int size = appProps.size();
assertEquals(3, size);
}
}

View File

@@ -0,0 +1,84 @@
package com.baeldung.properties;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.Test;
public class MergePropertiesUnitTest {
@Test
public void givenTwoProperties_whenMergedUsingIteration_thenAllPropertiesInResult() {
Properties globalProperties = mergePropertiesByIteratingKeySet(propertiesA(), propertiesB());
testMergedProperties(globalProperties);
}
@Test
public void givenTwoProperties_whenMergedUsingPutAll_thenAllPropertiesInResult() {
Properties globalProperties = mergePropertiesByUsingPutAll(propertiesA(), propertiesB());
testMergedProperties(globalProperties);
}
@Test
public void givenTwoProperties_whenMergedUsingStreamAPI_thenAllPropertiesInResult() {
Properties globalProperties = mergePropertiesByUsingStreamApi(propertiesB(), propertiesA());
testMergedProperties(globalProperties);
}
private Properties mergePropertiesByIteratingKeySet(Properties... properties) {
Properties mergedProperties = new Properties();
for (Properties property : properties) {
Set<String> propertyNames = property.stringPropertyNames();
for (String name : propertyNames) {
String propertyValue = property.getProperty(name);
mergedProperties.setProperty(name, propertyValue);
}
}
return mergedProperties;
}
private Properties mergePropertiesByUsingPutAll(Properties... properties) {
Properties mergedProperties = new Properties();
for (Properties property : properties) {
mergedProperties.putAll(property);
}
return mergedProperties;
}
private Properties mergePropertiesByUsingStreamApi(Properties... properties) {
return Stream.of(properties)
.collect(Properties::new, Map::putAll, Map::putAll);
}
private Properties propertiesA() {
Properties properties = new Properties();
properties.setProperty("application.name", "my-app");
properties.setProperty("application.version", "1.0");
return properties;
}
private Properties propertiesB() {
Properties properties = new Properties();
properties.setProperty("property-1", "sample property");
properties.setProperty("property-2", "another sample property");
return properties;
}
private void testMergedProperties(Properties globalProperties) {
assertThat("There should be 4 properties", globalProperties.size(), equalTo(4));
assertEquals("Property should be", globalProperties.getProperty("application.name"), "my-app");
assertEquals("Property should be", globalProperties.getProperty("application.version"), "1.0");
assertEquals("Property should be", globalProperties.getProperty("property-1"), "sample property");
assertEquals("Property should be", globalProperties.getProperty("property-2"), "another sample property");
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.util;
import org.junit.Test;
import java.io.IOException;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
public class PropertiesLoaderUnitTest {
@Test
public void loadProperties_whenPropertyReaded_thenSuccess() throws IOException {
//given
final String RESOURCE_FILE_NAME = "configuration.properties";
final String SAMPLE_CONF_ENTRY = "sampleConfEntry";
final String COLON_SEPARATED_CONF_ENTRY = "colonSeparatedEntry";
final String GIVEN_CONF_ENTRY_VALUE = "sample String value";
final String COLON_SEPARATED_CONF_ENTRY_VALUE = "colon separated entry value";
//when
Properties config = PropertiesLoader.loadProperties(RESOURCE_FILE_NAME);
String sampleConfEntryValue = config.getProperty(SAMPLE_CONF_ENTRY);
String colonSeparatedConfEntryValue = config.getProperty(COLON_SEPARATED_CONF_ENTRY);
//then
assertEquals(GIVEN_CONF_ENTRY_VALUE, sampleConfEntryValue);
assertEquals(COLON_SEPARATED_CONF_ENTRY_VALUE, colonSeparatedConfEntryValue);
}
}

View File

@@ -0,0 +1,3 @@
version=1.0
name=TestApp
date=2016-11-12

View File

@@ -0,0 +1,3 @@
c1=files
c2=images
c3=videos

View File

@@ -0,0 +1,4 @@
# this is sample property file for PropertiesLoaderTest configuration needs
! this is also a comment
sampleConfEntry = sample String value
colonSeparatedEntry : colon separated entry value

View File

@@ -0,0 +1,4 @@
site=www.google.com
name=DefaultAppName
topic=Properties
category=core-java

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>xml example</comment>
<entry key="fileIcon">icon1.jpg</entry>
<entry key="imageIcon">icon2.jpg</entry>
<entry key="videoIcon">icon3.jpg</entry>
</properties>