Skip to content

Commit ec72299

Browse files
JavaLogFactory Failed Configuration Load Test
Updating the JavaLogFactory to have a delegate static method for loading the configuration. Allowing the LogManager to be provided as an argument to avoid the need for verbose mocking and setup in the test scope. Adding a test that asserts that Standard Error stream is provided the expected error content if the configuration cannot be resolved.
1 parent a7e0915 commit ec72299

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/main/java/org/owasp/esapi/logging/java/JavaLogFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,20 @@ public class JavaLogFactory implements LogFactory {
7171

7272
LOG_BRIDGE = new JavaLogBridgeImpl(JAVA_LOG_APPENDER, JAVA_LOG_SCRUBBER, levelLookup);
7373

74-
/*
74+
readLoggerConfiguration(LogManager.getLogManager());
75+
}
76+
77+
/**
78+
* Attempts to load the expected property file path into the provided LogManager reference.
79+
* @param logManager LogManager which is being configured.
80+
*/
81+
/*package*/ static void readLoggerConfiguration(LogManager logManager) {
82+
/*
7583
* This will load the logging properties file to control the format of the output for Java logs.
7684
*/
7785
try (InputStream stream = JavaLogFactory.class.getClassLoader().
7886
getResourceAsStream("esapi-java-logging.properties")) {
79-
LogManager.getLogManager().readConfiguration(stream);
87+
logManager.readConfiguration(stream);
8088
} catch (IOException ioe) {
8189
System.err.print(new IOException("Failed to load esapi-java-logging.properties.", ioe));
8290
}

src/test/java/org/owasp/esapi/logging/java/JavaLogFactoryTest.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
import static org.junit.Assert.assertTrue;
1919

2020
import java.io.IOException;
21+
import java.io.InputStream;
2122
import java.io.OutputStream;
2223
import java.io.PrintStream;
2324
import java.util.List;
24-
import java.util.concurrent.atomic.AtomicReference;
25+
import java.util.logging.LogManager;
2526

2627
import org.junit.Assert;
27-
import org.junit.Ignore;
2828
import org.junit.Rule;
2929
import org.junit.Test;
3030
import org.junit.rules.TestName;
3131
import org.junit.runner.RunWith;
3232
import org.mockito.ArgumentCaptor;
33+
import org.mockito.Mockito;
3334
import org.owasp.esapi.Logger;
3435
import org.owasp.esapi.logging.appender.LogAppender;
3536
import org.owasp.esapi.logging.appender.LogPrefixAppender;
@@ -46,30 +47,42 @@
4647
public class JavaLogFactoryTest {
4748
@Rule
4849
public TestName testName = new TestName();
49-
50+
5051
@Test
51-
@Ignore("Work In Progress")
5252
public void testIOExceptionOnMissingConfiguration() throws Exception {
53-
org.junit.Assert.fail("Unimplemented!");
54-
55-
IOException originException = new IOException(testName.getMethodName());
56-
final AtomicReference<IOException> stdErrOut = new AtomicReference<IOException>();
53+
final IOException originException = new IOException(testName.getMethodName());
5754

58-
//FIXME Mock static so that java.util.logging.LogManager.readConfiguration(any(java.io.InputStream.class) throws IOException
59-
55+
LogManager testLogManager = new LogManager() {
56+
@Override
57+
public void readConfiguration(InputStream ins) throws IOException, SecurityException {
58+
throw originException;
59+
}
60+
};
61+
62+
OutputStream nullOutputStream = new OutputStream() {
63+
@Override
64+
public void write(int b) throws IOException {
65+
//No Op
66+
}
67+
};
6068

61-
System.setErr(new PrintStream(new OutputStream() {
62-
public void write(int b) {
63-
//FIXME: How do I capture the object here?
64-
}
65-
}));
69+
ArgumentCaptor<Object> stdErrOut = ArgumentCaptor.forClass(Object.class);
6670

67-
IOException actual = stdErrOut.get();
68-
assertTrue(actual != null);
69-
assertTrue(originException.equals(actual.getCause()));
70-
assertEquals("Failed to load esapi-java-logging.properties.", actual.getMessage());
71+
try (PrintStream errPrinter = new PrintStream(nullOutputStream)) {
72+
PrintStream spyPrinter = PowerMockito.spy(errPrinter);
73+
Mockito.doCallRealMethod().when(spyPrinter).print(stdErrOut.capture());
74+
System.setErr(spyPrinter);
75+
76+
JavaLogFactory.readLoggerConfiguration(testLogManager);
77+
78+
Object writeData = stdErrOut.getValue();
79+
assertTrue(writeData instanceof IOException);
80+
IOException actual = (IOException) writeData;
81+
assertEquals(originException, actual.getCause());
82+
assertEquals("Failed to load esapi-java-logging.properties.", actual.getMessage());
83+
}
7184
}
72-
85+
7386
@Test
7487
public void testCreateLoggerByString() {
7588
Logger logger = new JavaLogFactory().getLogger("test");

0 commit comments

Comments
 (0)