delete last topic code
This commit is contained in:
parent
42fc383518
commit
d13f7b9ed8
|
@ -1,30 +0,0 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
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";
|
||||
|
||||
Properties defaultProps = new Properties();
|
||||
defaultProps.load(new FileInputStream(defaultConfigPath));
|
||||
|
||||
Properties appProps = new Properties(defaultProps);
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
assertThat(appProps.getProperty("name"), equalTo("TestApp"));
|
||||
assertThat(appProps.getProperty("version"), equalTo("1.0"));
|
||||
assertThat(appProps.getProperty("site"), equalTo("www.google.com"));
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
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";
|
||||
|
||||
// load from properties file which use .properties as the suffix
|
||||
Properties appProps = new Properties();
|
||||
appProps.load(new FileInputStream(appConfigPath));
|
||||
|
||||
// load from properties file which not use .properties as the suffix
|
||||
Properties catalogProps = new Properties();
|
||||
catalogProps.load(new FileInputStream(catalogConfigPath));
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
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));
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
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"));
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
version=1.0
|
||||
name=TestApp
|
||||
date=2016-11-12
|
|
@ -1,3 +0,0 @@
|
|||
c1=files
|
||||
c2=images
|
||||
c3=videos
|
|
@ -1,4 +0,0 @@
|
|||
site=www.google.com
|
||||
name=DefaultAppName
|
||||
topic=Properties
|
||||
category=core-java
|
|
@ -1,8 +0,0 @@
|
|||
<?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>
|
Loading…
Reference in New Issue