Skip to content

Commit f679c20

Browse files
Dump parameters unit tests
1 parent eda1871 commit f679c20

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.bordertech.config;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.PrintStream;
5+
import org.junit.After;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
/**
11+
* DefaultConfigurationDumpTest - JUnit tests for {@link DefaultConfiguration}.
12+
*/
13+
public class DefaultConfigurationDumpTest {
14+
15+
private PrintStream original;
16+
private ByteArrayOutputStream systemErr;
17+
18+
private static final String LOG_START = "Properties loaded start";
19+
private static final String LOG_END = "Properties loaded end";
20+
private static final String LOG_PARAM = "simple.param1";
21+
22+
@Before
23+
public void redirectSystemOutStream() {
24+
original = System.err;
25+
systemErr = new ByteArrayOutputStream();
26+
System.setErr(new PrintStream(systemErr));
27+
}
28+
29+
@After
30+
public void restoreSystemOutStream() {
31+
System.setErr(original);
32+
}
33+
34+
@Test
35+
public void dumpParametersEnabled() {
36+
DefaultConfiguration config = new DefaultConfiguration(
37+
"com/github/bordertech/config/DefaultConfigurationTestDumpEnabled.properties");
38+
// Check dump enabled
39+
Assert.assertTrue(config.getBoolean(DefaultConfiguration.DUMP));
40+
String log = systemErr.toString();
41+
Assert.assertTrue("Output should contain dump start load", log.contains(LOG_START));
42+
Assert.assertTrue("Output should contain dump start end", log.contains(LOG_END));
43+
Assert.assertTrue("Output should contain property", log.contains(LOG_PARAM));
44+
}
45+
46+
@Test
47+
public void dumpParametersDisabled() {
48+
DefaultConfiguration config = new DefaultConfiguration(
49+
"com/github/bordertech/config/DefaultConfigurationTestDumpDisabled.properties");
50+
// Check dump disabled
51+
Assert.assertFalse(config.getBoolean(DefaultConfiguration.DUMP));
52+
String log = systemErr.toString();
53+
Assert.assertFalse("Output should not contain dump start load", log.contains(LOG_START));
54+
Assert.assertFalse("Output should not contain dump start end", log.contains(LOG_END));
55+
Assert.assertFalse("Output should not contain property", log.contains(LOG_PARAM));
56+
}
57+
58+
}

src/test/java/com/github/bordertech/config/DefaultConfigurationTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,17 @@ public void testGetProperties() {
347347
assertPropertyEquals("key3", "value3", props);
348348
}
349349

350+
@Test
351+
public void testRefresh() {
352+
String orig = "simplePropertyValue";
353+
String newValue = "newvalue";
354+
assertPropertyEquals(STRING_PROPERTY_KEY, orig);
355+
config.setProperty(STRING_PROPERTY_KEY, newValue);
356+
assertPropertyEquals(STRING_PROPERTY_KEY, newValue);
357+
config.refresh();
358+
assertPropertyEquals(STRING_PROPERTY_KEY, orig);
359+
}
360+
350361
/**
351362
* Asserts that the configuration contains the given key/value.
352363
*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
############################################################################
2+
# This property file is for the DefaultConfiguration_Test jUnit test
3+
# It must not be included or be included by property file that is not
4+
# directly related to the test.
5+
############################################################################
6+
7+
# -------------------------------------------------------------------------------------------------
8+
# Simple property tests
9+
# -------------------------------------------------------------------------------------------------
10+
simple.param1=A
11+
simple.param2=B
12+
simple.param3=C
13+
14+
## Do not dump parameters
15+
bordertech.config.parameters.dump.console=false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
############################################################################
2+
# This property file is for the DefaultConfiguration_Test jUnit test
3+
# It must not be included or be included by property file that is not
4+
# directly related to the test.
5+
############################################################################
6+
7+
# -------------------------------------------------------------------------------------------------
8+
# Simple property tests
9+
# -------------------------------------------------------------------------------------------------
10+
simple.param1=A
11+
simple.param2=B
12+
simple.param3=C
13+
14+
## Dump parameters
15+
bordertech.config.parameters.dump.console=true

0 commit comments

Comments
 (0)