diff --git a/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java b/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java deleted file mode 100644 index c33caf877c..0000000000 --- a/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java +++ /dev/null @@ -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 valueEnumeration = appProps.elements(); - while (valueEnumeration.hasMoreElements()) { - System.out.println(valueEnumeration.nextElement()); - } - - // get an enumeration of all keys - Enumeration 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); - } -} diff --git a/core-java/src/main/java/com/baeldung/properties/StoreDemo.java b/core-java/src/main/java/com/baeldung/properties/StoreDemo.java deleted file mode 100644 index e8a7892c40..0000000000 --- a/core-java/src/main/java/com/baeldung/properties/StoreDemo.java +++ /dev/null @@ -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"); - } -} diff --git a/core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java b/core-java/src/test/java/com/baeldung/properties/PropertiesDefaultListTest.java similarity index 55% rename from core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java rename to core-java/src/test/java/com/baeldung/properties/PropertiesDefaultListTest.java index 758bd2a71b..704bb19759 100644 --- a/core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java +++ b/core-java/src/test/java/com/baeldung/properties/PropertiesDefaultListTest.java @@ -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")); } } diff --git a/core-java/src/main/java/com/baeldung/properties/LoadDemo.java b/core-java/src/test/java/com/baeldung/properties/PropertiesLoadTest.java similarity index 74% rename from core-java/src/main/java/com/baeldung/properties/LoadDemo.java rename to core-java/src/test/java/com/baeldung/properties/PropertiesLoadTest.java index 3a75113b79..bcf386bd0f 100644 --- a/core-java/src/main/java/com/baeldung/properties/LoadDemo.java +++ b/core-java/src/test/java/com/baeldung/properties/PropertiesLoadTest.java @@ -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)); } diff --git a/core-java/src/test/java/com/baeldung/properties/PropertiesOperationsTest.java b/core-java/src/test/java/com/baeldung/properties/PropertiesOperationsTest.java new file mode 100644 index 0000000000..20aa64c385 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/properties/PropertiesOperationsTest.java @@ -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 valueEnumeration = appProps.elements(); + List 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 keyEnumeration = appProps.keys(); + List 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)); + } +} diff --git a/core-java/src/test/java/com/baeldung/properties/PropertiesStoreTest.java b/core-java/src/test/java/com/baeldung/properties/PropertiesStoreTest.java new file mode 100644 index 0000000000..582fdb3808 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/properties/PropertiesStoreTest.java @@ -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")); + } +} diff --git a/core-java/src/main/resources/app.properties b/core-java/src/test/resources/app.properties similarity index 100% rename from core-java/src/main/resources/app.properties rename to core-java/src/test/resources/app.properties diff --git a/core-java/src/main/resources/catalog b/core-java/src/test/resources/catalog similarity index 100% rename from core-java/src/main/resources/catalog rename to core-java/src/test/resources/catalog diff --git a/core-java/src/main/resources/default.properties b/core-java/src/test/resources/default.properties similarity index 100% rename from core-java/src/main/resources/default.properties rename to core-java/src/test/resources/default.properties diff --git a/core-java/src/main/resources/icons.xml b/core-java/src/test/resources/icons.xml similarity index 100% rename from core-java/src/main/resources/icons.xml rename to core-java/src/test/resources/icons.xml