Merge branch 'feature/properties_files' of Locusworks/crypto into develop
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package net.locusworks.crypto.tests;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.crypto.configuration.PropertiesManager;
|
||||
|
||||
/**
|
||||
* Test cases for the properties manager class
|
||||
* @author Isaac Parenteau
|
||||
* @since 1.0.0-RELEASE
|
||||
*
|
||||
*/
|
||||
public class PropertiesManagerTest {
|
||||
|
||||
private static final String PROPERTIES_FILE = "test.properties";
|
||||
private static final String TMP_PROPS = "temp.properties";
|
||||
private static final int ENTRY_SIZE = 4;
|
||||
|
||||
public static enum Configuration {
|
||||
DB_HOST("dbHost"),
|
||||
DB_PORT("dbPort"),
|
||||
USER_EXPIRATION_DAYS("userExpirationDays"),
|
||||
LOG_LEVEL("logLevel");
|
||||
|
||||
private String value;
|
||||
|
||||
private Configuration(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current value of the enumeration
|
||||
* @return value
|
||||
*/
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void removeSavedProps() {
|
||||
File tmp = new File(TMP_PROPS);
|
||||
if (tmp.exists()) {
|
||||
tmp.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesLoad() {
|
||||
try {
|
||||
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
|
||||
assertTrue(props != null);
|
||||
assertTrue(props.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
|
||||
assertTrue(props.containsKey(Configuration.DB_HOST.toString()));
|
||||
assertTrue(props.containsKey(Configuration.DB_PORT.toString()));
|
||||
assertTrue(props.containsKey(Configuration.LOG_LEVEL.toString()));
|
||||
} catch (IOException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddConfiguration() {
|
||||
try {
|
||||
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
|
||||
Properties tmp = new Properties();
|
||||
assertTrue(tmp.keySet().size() == 0);
|
||||
PropertiesManager.addConfiguration(tmp, props);
|
||||
assertTrue(tmp.keySet().size() == ENTRY_SIZE);
|
||||
assertTrue(tmp.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.DB_HOST.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.DB_PORT.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.LOG_LEVEL.toString()));
|
||||
} catch (IOException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveConfiguration() {
|
||||
try {
|
||||
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
|
||||
Properties tmp = new Properties();
|
||||
assertTrue(props.keySet().size() == ENTRY_SIZE);
|
||||
assertTrue(tmp.keySet().size() == 0);
|
||||
PropertiesManager.removeConfiguration(props, tmp);
|
||||
assertTrue(props.keySet().size() == 0);
|
||||
assertTrue(tmp.keySet().size() == 0);
|
||||
} catch (IOException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveConfiguration() {
|
||||
try {
|
||||
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
|
||||
Path tmpFile = Paths.get(TMP_PROPS);
|
||||
PropertiesManager.saveConfiguration(props, tmpFile, "test propertis");
|
||||
Properties tmp = PropertiesManager.loadConfiguration(tmpFile);
|
||||
assertTrue(tmp.keySet().size() == ENTRY_SIZE);
|
||||
assertTrue(tmp.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.DB_HOST.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.DB_PORT.toString()));
|
||||
assertTrue(tmp.containsKey(Configuration.LOG_LEVEL.toString()));
|
||||
} catch (IOException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/test/resources/log4j2-test.xml
Normal file
13
src/test/resources/log4j2-test.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="ConsoleAppender" level="INFO"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
4
src/test/resources/test.properties
Normal file
4
src/test/resources/test.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
userExpirationDays=3650
|
||||
dbHost=localhost
|
||||
dbPort=3306
|
||||
logLevel=INFO
|
||||
Reference in New Issue
Block a user