|
18 | 18 | import static org.junit.Assert.assertTrue; |
19 | 19 |
|
20 | 20 | import java.io.IOException; |
| 21 | +import java.io.InputStream; |
21 | 22 | import java.io.OutputStream; |
22 | 23 | import java.io.PrintStream; |
23 | 24 | import java.util.List; |
24 | | -import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.logging.LogManager; |
25 | 26 |
|
26 | 27 | import org.junit.Assert; |
27 | | -import org.junit.Ignore; |
28 | 28 | import org.junit.Rule; |
29 | 29 | import org.junit.Test; |
30 | 30 | import org.junit.rules.TestName; |
31 | 31 | import org.junit.runner.RunWith; |
32 | 32 | import org.mockito.ArgumentCaptor; |
| 33 | +import org.mockito.Mockito; |
33 | 34 | import org.owasp.esapi.Logger; |
34 | 35 | import org.owasp.esapi.logging.appender.LogAppender; |
35 | 36 | import org.owasp.esapi.logging.appender.LogPrefixAppender; |
|
46 | 47 | public class JavaLogFactoryTest { |
47 | 48 | @Rule |
48 | 49 | public TestName testName = new TestName(); |
49 | | - |
| 50 | + |
50 | 51 | @Test |
51 | | - @Ignore("Work In Progress") |
52 | 52 | 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()); |
57 | 54 |
|
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 | + }; |
60 | 68 |
|
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); |
66 | 70 |
|
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 | + } |
71 | 84 | } |
72 | | - |
| 85 | + |
73 | 86 | @Test |
74 | 87 | public void testCreateLoggerByString() { |
75 | 88 | Logger logger = new JavaLogFactory().getLogger("test"); |
|
0 commit comments