add unit tests
This commit is contained in:
parent
dd6946585f
commit
d2e52b4368
@ -1,57 +0,0 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
|
||||
public class OperationsDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
Properties appProps = new Properties();
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
// retrieve values
|
||||
String appVersion = appProps.getProperty("version");
|
||||
String appName = appProps.getProperty("name", "defaultName");
|
||||
String appGroup = appProps.getProperty("group", "baeldung");
|
||||
String appDownloadAddr = appProps.getProperty("downloadAddr");
|
||||
System.out.println(appVersion);
|
||||
System.out.println(appName);
|
||||
System.out.println(appGroup);
|
||||
System.out.println(appDownloadAddr);
|
||||
|
||||
// set values
|
||||
appProps.setProperty("name", "NewAppName");
|
||||
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
|
||||
appName = appProps.getProperty("name");
|
||||
appDownloadAddr = appProps.getProperty("downloadAddr");
|
||||
System.out.println("new app name: " + appName);
|
||||
System.out.println("new app downloadAddr: " + appDownloadAddr);
|
||||
|
||||
// remove a key-value pair
|
||||
System.out.println("before removal, version is: " + appProps.getProperty("version"));
|
||||
appProps.remove("version");
|
||||
System.out.println("after removal, version is: " + appProps.getProperty("version"));
|
||||
|
||||
// list all key-value pairs
|
||||
appProps.list(System.out);
|
||||
|
||||
// get an enumeration of all values
|
||||
Enumeration<Object> valueEnumeration = appProps.elements();
|
||||
while (valueEnumeration.hasMoreElements()) {
|
||||
System.out.println(valueEnumeration.nextElement());
|
||||
}
|
||||
|
||||
// get an enumeration of all keys
|
||||
Enumeration<Object> keyEnumeration = appProps.keys();
|
||||
while (keyEnumeration.hasMoreElements()) {
|
||||
System.out.println(keyEnumeration.nextElement());
|
||||
}
|
||||
|
||||
// get the count of key-value pairs
|
||||
int size = appProps.size();
|
||||
System.out.println(size);
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class StoreDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
|
||||
// load the original properties
|
||||
Properties appProps = new Properties();
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
// update property list
|
||||
appProps.setProperty("name", "NewAppName");
|
||||
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
|
||||
|
||||
// store new property list in properties file
|
||||
String newAppConfigPropertiesFile = rootPath + "newApp.properties";
|
||||
appProps.store(new FileWriter(newAppConfigPropertiesFile), "store to properties file");
|
||||
|
||||
// store new property list in XML file
|
||||
String newAppConfigXmlFile = rootPath + "newApp.xml";
|
||||
appProps.storeToXML(new FileOutputStream(newAppConfigXmlFile), "store to xml file");
|
||||
}
|
||||
}
|
@ -1,11 +1,18 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DefaultListDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class PropertiesDefaultListTest {
|
||||
@Test
|
||||
public void testDefaultList() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String defaultConfigPath = rootPath + "default.properties";
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
@ -16,8 +23,8 @@ public class DefaultListDemo {
|
||||
Properties appProps = new Properties(defaultProps);
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
System.out.println(appProps.getProperty("name"));
|
||||
System.out.println(appProps.getProperty("version"));
|
||||
System.out.println(appProps.getProperty("site"));
|
||||
assertThat(appProps.getProperty("name"), equalTo("TestApp"));
|
||||
assertThat(appProps.getProperty("version"), equalTo("1.0"));
|
||||
assertThat(appProps.getProperty("site"), equalTo("www.google.com"));
|
||||
}
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class LoadDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
public class PropertiesLoadTest {
|
||||
@Test
|
||||
public void testLoadFromPropertiesFiles() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
String catalogConfigPath = rootPath + "catalog";
|
||||
String iconConfigPath = rootPath + "icons.xml";
|
||||
|
||||
// load from properties file which use .properties as the suffix
|
||||
Properties appProps = new Properties();
|
||||
@ -18,8 +20,12 @@ public class LoadDemo {
|
||||
// load from properties file which not use .properties as the suffix
|
||||
Properties catalogProps = new Properties();
|
||||
catalogProps.load(new FileInputStream(catalogConfigPath));
|
||||
}
|
||||
|
||||
// load from XML file
|
||||
@Test
|
||||
public void testLoadFromXmlFile() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String iconConfigPath = rootPath + "icons.xml";
|
||||
Properties iconProps = new Properties();
|
||||
iconProps.loadFromXML(new FileInputStream(iconConfigPath));
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PropertiesOperationsTest {
|
||||
private Properties appProps;
|
||||
|
||||
@Before
|
||||
public void beforeClass() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
appProps = new Properties();
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProperties() {
|
||||
String appVersion = appProps.getProperty("version");
|
||||
String appName = appProps.getProperty("name", "defaultName");
|
||||
String appGroup = appProps.getProperty("group", "baeldung");
|
||||
String appDownloadAddr = appProps.getProperty("downloadAddr");
|
||||
|
||||
assertThat(appVersion, equalTo("1.0"));
|
||||
assertThat(appName, equalTo("TestApp"));
|
||||
assertThat(appGroup, equalTo("baeldung"));
|
||||
assertThat(appDownloadAddr, equalTo(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetProperties() {
|
||||
appProps.setProperty("name", "NewAppName");
|
||||
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
|
||||
|
||||
String appName = appProps.getProperty("name");
|
||||
String appDownloadAddr = appProps.getProperty("downloadAddr");
|
||||
|
||||
assertThat(appName, equalTo("NewAppName"));
|
||||
assertThat(appDownloadAddr, equalTo("www.baeldung.com/downloads"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveProperties() {
|
||||
String versionBeforeRemoval = appProps.getProperty("version");
|
||||
assertThat(versionBeforeRemoval, equalTo("1.0"));
|
||||
|
||||
appProps.remove("version");
|
||||
|
||||
String versionAfterRemoval = appProps.getProperty("version");
|
||||
assertThat(versionAfterRemoval, equalTo(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEnumerationOfValues() {
|
||||
Enumeration<Object> valueEnumeration = appProps.elements();
|
||||
List<String> values = Lists.newArrayList();
|
||||
while (valueEnumeration.hasMoreElements()) {
|
||||
String val = String.valueOf(valueEnumeration.nextElement());
|
||||
values.add(val);
|
||||
}
|
||||
assertArrayEquals(new String[] {"1.0", "2016-11-12", "TestApp"}, values.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEnumerationOfKeys() {
|
||||
Enumeration<Object> keyEnumeration = appProps.keys();
|
||||
List<String> keys = Lists.newArrayList();
|
||||
while (keyEnumeration.hasMoreElements()) {
|
||||
String key = String.valueOf(keyEnumeration.nextElement());
|
||||
keys.add(key);
|
||||
}
|
||||
assertArrayEquals(new String[] {"version", "date", "name"}, keys.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSize() {
|
||||
int size = appProps.size();
|
||||
assertThat(size, equalTo(3));
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class PropertiesStoreTest {
|
||||
private static Properties appProps = new Properties();
|
||||
|
||||
@Before
|
||||
public void before() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String appConfigPath = rootPath + "app.properties";
|
||||
|
||||
// load the original properties
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
// update property list
|
||||
appProps.setProperty("name", "NewAppName");
|
||||
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreToPropertiesFile() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String newAppConfigPropertiesFile = rootPath + "newApp.properties";
|
||||
appProps.store(new FileWriter(newAppConfigPropertiesFile), "store to properties file");
|
||||
|
||||
|
||||
Properties verifyProps = new Properties();
|
||||
verifyProps.load(new FileInputStream(newAppConfigPropertiesFile));
|
||||
assertThat(verifyProps.getProperty("name"), equalTo("NewAppName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreToXmlFile() throws IOException {
|
||||
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
||||
String newAppConfigXmlFile = rootPath + "newApp.xml";
|
||||
appProps.storeToXML(new FileOutputStream(newAppConfigXmlFile), "store to xml file");
|
||||
|
||||
Properties verifyProps = new Properties();
|
||||
verifyProps.loadFromXML(new FileInputStream(newAppConfigXmlFile));
|
||||
assertThat(verifyProps.getProperty("downloadAddr"), equalTo("www.baeldung.com/downloads"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user