Skip to content

Commit 33abdb6

Browse files
Merge pull request #22 from BorderTech/fix-qa
Dump to file option
2 parents 2d9e44d + 62b6ba2 commit 33abdb6

File tree

9 files changed

+207
-22
lines changed

9 files changed

+207
-22
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>com.github.bordertech.common</groupId>
1212
<artifactId>qa-parent</artifactId>
13-
<version>1.0.13</version>
13+
<version>1.0.14</version>
1414
</parent>
1515

1616
<packaging>jar</packaging>

spotbugs-exclude-filter.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<!-- False Positives -->
55
<Match>
6-
<Bug pattern="PATH_TRAVERSAL_IN,PMB_POSSIBLE_MEMORY_BLOAT,FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY,MUI_CONTAINSKEY_BEFORE_GET" />
6+
<Bug pattern="PATH_TRAVERSAL_IN,PATH_TRAVERSAL_OUT,PMB_POSSIBLE_MEMORY_BLOAT,FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY,MUI_CONTAINSKEY_BEFORE_GET" />
77
</Match>
88

99
</FindBugsFilter>

src/main/java/com/github/bordertech/config/DefaultConfiguration.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.io.ByteArrayOutputStream;
66
import java.io.File;
77
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
89
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.io.OutputStream;
12+
import java.io.PrintStream;
1113
import java.math.BigDecimal;
1214
import java.math.BigInteger;
1315
import java.net.URL;
@@ -33,6 +35,7 @@
3335
import org.apache.commons.configuration.Configuration;
3436
import org.apache.commons.configuration.ConversionException;
3537
import org.apache.commons.configuration.MapConfiguration;
38+
import org.apache.commons.lang3.StringUtils;
3639
import org.apache.commons.lang3.tuple.ImmutablePair;
3740
import org.apache.commons.lang3.tuple.Pair;
3841
import org.apache.commons.logging.Log;
@@ -90,6 +93,11 @@ public class DefaultConfiguration implements Configuration {
9093
*/
9194
public static final String DUMP = "bordertech.config.parameters.dump.console";
9295

96+
/**
97+
* If this parameter is set, then after loading the parameters, they will be dumped to the specified file.
98+
*/
99+
public static final String DUMP_FILE = "bordertech.config.parameters.dump.file";
100+
93101
/**
94102
* If this parameter is set, it will be used as the environment suffix for each property lookup.
95103
*/
@@ -292,10 +300,11 @@ private void load() {
292300
// Do nothing while loop
293301
} while (substitute());
294302

303+
// Dump Header Info
295304
LOG.info(getDumpHeader());
296-
if (isDumpProperties()) {
297-
LOG.info(getDumpMessages());
298-
LOG.info(getDumpPropertyDetails());
305+
// Dump properties
306+
if (isDumpPropertiesConsole() || isDumpPropertiesFile()) {
307+
handleDumpPropertyDetails();
299308
}
300309

301310
// We don't want the StringBuilder hanging around after 'DUMP'.
@@ -322,10 +331,50 @@ private boolean isUseSystemProperties() {
322331
/**
323332
* @return true if dump properties to the console
324333
*/
325-
private boolean isDumpProperties() {
334+
private boolean isDumpPropertiesConsole() {
326335
return getBoolean(DUMP) || getBoolean(LEGACY_DUMP);
327336
}
328337

338+
/**
339+
* @return true if dump properties to a file
340+
*/
341+
private boolean isDumpPropertiesFile() {
342+
return !StringUtils.isEmpty(getDumpFileLocation());
343+
}
344+
345+
/**
346+
* @return the dump properties to file
347+
*/
348+
private String getDumpFileLocation() {
349+
return get(DUMP_FILE, "");
350+
}
351+
352+
/**
353+
* Dump the property details.
354+
*/
355+
private void handleDumpPropertyDetails() {
356+
357+
String loadMessages = getDumpLoadMessages();
358+
String propMessages = getDumpPropertyDetails();
359+
360+
// Dump to console
361+
if (isDumpPropertiesConsole()) {
362+
LOG.info(loadMessages);
363+
LOG.info(propMessages);
364+
}
365+
366+
// Dump to File
367+
if (isDumpPropertiesFile()) {
368+
String dest = getDumpFileLocation();
369+
try (FileOutputStream fos = new FileOutputStream(dest); PrintStream stream = new PrintStream(fos)) {
370+
stream.println(loadMessages);
371+
stream.println(propMessages);
372+
} catch (IOException e) {
373+
recordException(e);
374+
}
375+
}
376+
}
377+
329378
/**
330379
* @return debugging information for logging
331380
*/
@@ -358,12 +407,17 @@ private String getDumpHeader() {
358407
info.append(codesourceStr);
359408
info.append("\nWorking directory is: ");
360409
info.append(workingDir);
361-
info.append("\nTo dump all params set ");
410+
info.append("\nTo dump all params to the console set ");
362411
info.append(DUMP);
363412
info.append(" to true; current value is ");
364-
info.append(isDumpProperties());
365-
info.append("\nLOGGING can be controlled by configuring org.apache.commons.logging.impl.SimpleLog that writes to System.err by default.");
366-
info.append("\n----Config: Info end------\n");
413+
info.append(isDumpPropertiesConsole());
414+
info.append("\nTo dump all params to a file set ");
415+
info.append(DUMP_FILE);
416+
info.append(" to file location; current value is ");
417+
info.append(getDumpFileLocation());
418+
info.append("\nLOGGING can be controlled by configuring org.apache.commons.logging.impl.SimpleLog.");
419+
info.append("\nSimpleLog writes to System.err by default.");
420+
info.append("\n----Config: Info end------");
367421

368422
return info.toString();
369423
}
@@ -397,7 +451,7 @@ private String getDumpPropertyDetails() {
397451
/**
398452
* @return debugging load messages
399453
*/
400-
private String getDumpMessages() {
454+
private String getDumpLoadMessages() {
401455

402456
StringBuilder info = new StringBuilder();
403457

src/test/java/com/github/bordertech/config/DefaultConfigurationDumpTest.java renamed to src/test/java/com/github/bordertech/config/DefaultConfigurationDumpConsoleTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* DefaultConfigurationDumpTest - JUnit tests for {@link DefaultConfiguration}.
1212
*/
13-
public class DefaultConfigurationDumpTest {
13+
public class DefaultConfigurationDumpConsoleTest {
1414

1515
private PrintStream original;
1616
private ByteArrayOutputStream systemErr;
@@ -32,27 +32,27 @@ public void restoreSystemOutStream() {
3232
}
3333

3434
@Test
35-
public void dumpParametersEnabled() {
35+
public void dumpParametersConsoleEnabled() {
3636
DefaultConfiguration config = new DefaultConfiguration(
37-
"com/github/bordertech/config/DefaultConfigurationTestDumpEnabled.properties");
37+
"com/github/bordertech/config/DefaultConfigurationTestDumpConsoleEnabled.properties");
3838
// Check dump enabled
3939
Assert.assertTrue(config.getBoolean(DefaultConfiguration.DUMP));
4040
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));
41+
Assert.assertTrue("Console output should contain dump start load", log.contains(LOG_START));
42+
Assert.assertTrue("Console output should contain dump start end", log.contains(LOG_END));
43+
Assert.assertTrue("Console output should contain property", log.contains(LOG_PARAM));
4444
}
4545

4646
@Test
47-
public void dumpParametersDisabled() {
47+
public void dumpParametersConsoleDisabled() {
4848
DefaultConfiguration config = new DefaultConfiguration(
49-
"com/github/bordertech/config/DefaultConfigurationTestDumpDisabled.properties");
49+
"com/github/bordertech/config/DefaultConfigurationTestDumpConsoleDisabled.properties");
5050
// Check dump disabled
5151
Assert.assertFalse(config.getBoolean(DefaultConfiguration.DUMP));
5252
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));
53+
Assert.assertFalse("Console output should not contain dump start load", log.contains(LOG_START));
54+
Assert.assertFalse("Console output should not contain dump start end", log.contains(LOG_END));
55+
Assert.assertFalse("Console output should not contain property", log.contains(LOG_PARAM));
5656
}
5757

5858
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.github.bordertech.config;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.PrintStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import org.junit.After;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
/**
16+
* DefaultConfigurationDumpTest - JUnit tests for {@link DefaultConfiguration}.
17+
*/
18+
public class DefaultConfigurationDumpFileTest {
19+
20+
private static final String LOG_START = "Properties loaded start";
21+
private static final String LOG_END = "Properties loaded end";
22+
private static final String LOG_PARAM = "simple.param1";
23+
private static final String LOG_FILE = "target/testdump.log";
24+
private static final String EXISTING_LOG = "ALREADY EXISTS";
25+
26+
@Before
27+
@After
28+
public void deleteTempLog() {
29+
File temp = new File(LOG_FILE);
30+
if (temp.exists()) {
31+
temp.delete();
32+
}
33+
}
34+
35+
@Test
36+
public void dumpParametersFileEnabled() {
37+
DefaultConfiguration config = new DefaultConfiguration(
38+
"com/github/bordertech/config/DefaultConfigurationTestDumpFileEnabled.properties");
39+
// Check dump file enabled
40+
Assert.assertEquals(LOG_FILE, config.get(DefaultConfiguration.DUMP_FILE));
41+
// Check log file exists
42+
Assert.assertTrue("Log file should exist", new File(LOG_FILE).exists());
43+
// Check content
44+
checkLogContent(getLogContent());
45+
}
46+
47+
@Test
48+
public void dumpParametersFileEnabledAlreadyExists() {
49+
50+
// Create an existing file
51+
File file = new File(LOG_FILE);
52+
try (FileOutputStream fos = new FileOutputStream(file); PrintStream stream = new PrintStream(fos)) {
53+
stream.println(EXISTING_LOG);
54+
} catch (IOException e) {
55+
throw new IllegalStateException("Could not create a temp file.", e);
56+
}
57+
58+
DefaultConfiguration config = new DefaultConfiguration(
59+
"com/github/bordertech/config/DefaultConfigurationTestDumpFileEnabled.properties");
60+
// Check dump file enabled
61+
Assert.assertEquals(LOG_FILE, config.get(DefaultConfiguration.DUMP_FILE));
62+
// Check log file exists
63+
Assert.assertTrue("Log file should exist", file.exists());
64+
// Check content
65+
String log = getLogContent();
66+
checkLogContent(log);
67+
// Should have been overwritten
68+
Assert.assertFalse("Existing file should have been overwritten", log.contains(EXISTING_LOG));
69+
}
70+
71+
@Test
72+
public void dumpParametersFileDisabled() {
73+
DefaultConfiguration config = new DefaultConfiguration(
74+
"com/github/bordertech/config/DefaultConfigurationTestDumpFileDisabled.properties");
75+
// Check dump file disabled
76+
Assert.assertEquals("", config.get(DefaultConfiguration.DUMP_FILE));
77+
// Log file should not exist
78+
Assert.assertFalse("Log file should not exist", new File(LOG_FILE).exists());
79+
}
80+
81+
/**
82+
* @return the log content
83+
*/
84+
private String getLogContent() {
85+
try {
86+
return new String(Files.readAllBytes(Paths.get(LOG_FILE)), StandardCharsets.UTF_8);
87+
} catch (IOException e) {
88+
throw new IllegalStateException("Could not load temporary log content", e);
89+
}
90+
}
91+
92+
/**
93+
* @param log the log content to check
94+
*/
95+
private void checkLogContent(final String log) {
96+
Assert.assertTrue("Log should contain dump start load", log.contains(LOG_START));
97+
Assert.assertTrue("Log should contain dump start end", log.contains(LOG_END));
98+
Assert.assertTrue("Log should contain property", log.contains(LOG_PARAM));
99+
}
100+
101+
}

src/test/resources/com/github/bordertech/config/DefaultConfigurationTestDumpDisabled.properties renamed to src/test/resources/com/github/bordertech/config/DefaultConfigurationTestDumpConsoleDisabled.properties

File renamed without changes.

src/test/resources/com/github/bordertech/config/DefaultConfigurationTestDumpEnabled.properties renamed to src/test/resources/com/github/bordertech/config/DefaultConfigurationTestDumpConsoleEnabled.properties

File renamed without changes.
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.file=
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.file=target/testdump.log

0 commit comments

Comments
 (0)