|
45 | 45 | @RunWith(PowerMockRunner.class) |
46 | 46 | @PrepareForTest (JavaLogFactory.class) |
47 | 47 | public class JavaLogFactoryTest { |
48 | | - @Rule |
| 48 | + @Rule |
49 | 49 | public TestName testName = new TestName(); |
50 | 50 |
|
51 | | - @Test |
52 | | - public void testIOExceptionOnMissingConfiguration() throws Exception { |
53 | | - final IOException originException = new IOException(testName.getMethodName()); |
54 | | - |
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 | | - }; |
68 | | - |
69 | | - ArgumentCaptor<Object> stdErrOut = ArgumentCaptor.forClass(Object.class); |
70 | | - PrintStream orig = System.err; |
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 | | - } finally { |
84 | | - System.setErr(orig); |
85 | | - } |
86 | | - |
87 | | - } |
| 51 | + @Test |
| 52 | + public void testIOExceptionOnMissingConfiguration() throws Exception { |
| 53 | + final IOException originException = new IOException(testName.getMethodName()); |
| 54 | + |
| 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 | + }; |
| 68 | + |
| 69 | + ArgumentCaptor<Object> stdErrOut = ArgumentCaptor.forClass(Object.class); |
| 70 | + PrintStream orig = System.err; |
| 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 | + } finally { |
| 84 | + System.setErr(orig); |
| 85 | + } |
| 86 | + |
| 87 | + } |
88 | 88 |
|
89 | 89 | @Test |
90 | 90 | public void testCreateLoggerByString() { |
91 | 91 | Logger logger = new JavaLogFactory().getLogger("test"); |
92 | 92 | Assert.assertTrue(logger instanceof JavaLogger); |
93 | 93 | } |
94 | | - |
| 94 | + |
95 | 95 | @Test public void testCreateLoggerByClass() { |
96 | 96 | Logger logger = new JavaLogFactory().getLogger(JavaLogBridgeImplTest.class); |
97 | 97 | Assert.assertTrue(logger instanceof JavaLogger); |
98 | 98 | } |
99 | | - |
| 99 | + |
100 | 100 | @Test |
101 | 101 | public void checkScrubberWithEncoding() throws Exception { |
102 | 102 | ArgumentCaptor<List> delegates = ArgumentCaptor.forClass(List.class); |
103 | 103 | PowerMockito.whenNew(CompositeLogScrubber.class).withArguments(delegates.capture()).thenReturn(null); |
104 | | - |
| 104 | + |
105 | 105 | //Call to invoke the constructor capture |
106 | 106 | JavaLogFactory.createLogScrubber(true); |
107 | | - |
| 107 | + |
108 | 108 | List<LogScrubber> scrubbers = delegates.getValue(); |
109 | 109 | Assert.assertEquals(2, scrubbers.size()); |
110 | 110 | Assert.assertTrue(scrubbers.get(0) instanceof NewlineLogScrubber); |
111 | 111 | Assert.assertTrue(scrubbers.get(1) instanceof CodecLogScrubber); |
112 | 112 | } |
113 | | - |
| 113 | + |
114 | 114 | @Test |
115 | 115 | public void checkScrubberWithoutEncoding() throws Exception { |
116 | 116 | ArgumentCaptor<List> delegates = ArgumentCaptor.forClass(List.class); |
117 | 117 | PowerMockito.whenNew(CompositeLogScrubber.class).withArguments(delegates.capture()).thenReturn(null); |
118 | | - |
| 118 | + |
119 | 119 | //Call to invoke the constructor capture |
120 | 120 | JavaLogFactory.createLogScrubber(false); |
121 | | - |
| 121 | + |
122 | 122 | List<LogScrubber> scrubbers = delegates.getValue(); |
123 | 123 | Assert.assertEquals(1, scrubbers.size()); |
124 | 124 | Assert.assertTrue(scrubbers.get(0) instanceof NewlineLogScrubber); |
125 | 125 | } |
126 | | - |
| 126 | + |
127 | 127 | /** |
128 | | - * At this time there are no special considerations or handling for the appender |
129 | | - * creation in this scope. It is expected that the arguments to the internal |
130 | | - * creation method are passed directly to the constructor of the |
131 | | - * LogPrefixAppender with no mutation or additional validation. |
132 | | - */ |
| 128 | + * At this time there are no special considerations or handling for the appender |
| 129 | + * creation in this scope. It is expected that the arguments to the internal |
| 130 | + * creation method are passed directly to the constructor of the |
| 131 | + * LogPrefixAppender with no mutation or additional validation. |
| 132 | + */ |
133 | 133 | @Test |
134 | 134 | public void checkPassthroughAppenderConstruct() throws Exception { |
135 | | - LogPrefixAppender stubAppender = new LogPrefixAppender(true, true, true, ""); |
136 | | - ArgumentCaptor<Boolean> clientInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
137 | | - ArgumentCaptor<Boolean> serverInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
138 | | - ArgumentCaptor<Boolean> logAppNameCapture = ArgumentCaptor.forClass(Boolean.class); |
139 | | - ArgumentCaptor<String> appNameCapture = ArgumentCaptor.forClass(String.class); |
140 | | - |
141 | | - PowerMockito.whenNew(LogPrefixAppender.class).withArguments(clientInfoCapture.capture(), serverInfoCapture.capture(), logAppNameCapture.capture(), appNameCapture.capture()).thenReturn(stubAppender); |
142 | | - |
143 | | - LogAppender appender = JavaLogFactory.createLogAppender(true, false, true, testName.getMethodName()); |
144 | | - |
145 | | - Assert.assertEquals(stubAppender, appender); |
146 | | - Assert.assertTrue(clientInfoCapture.getValue()); |
147 | | - Assert.assertFalse(serverInfoCapture.getValue()); |
148 | | - Assert.assertTrue(logAppNameCapture.getValue()); |
149 | | - Assert.assertEquals(testName.getMethodName(), appNameCapture.getValue()); |
| 135 | + LogPrefixAppender stubAppender = new LogPrefixAppender(true, true, true, ""); |
| 136 | + ArgumentCaptor<Boolean> clientInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
| 137 | + ArgumentCaptor<Boolean> serverInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
| 138 | + ArgumentCaptor<Boolean> logAppNameCapture = ArgumentCaptor.forClass(Boolean.class); |
| 139 | + ArgumentCaptor<String> appNameCapture = ArgumentCaptor.forClass(String.class); |
| 140 | + |
| 141 | + PowerMockito.whenNew(LogPrefixAppender.class).withArguments(clientInfoCapture.capture(), serverInfoCapture.capture(), logAppNameCapture.capture(), appNameCapture.capture()).thenReturn(stubAppender); |
| 142 | + |
| 143 | + LogAppender appender = JavaLogFactory.createLogAppender(true, false, true, testName.getMethodName()); |
| 144 | + |
| 145 | + Assert.assertEquals(stubAppender, appender); |
| 146 | + Assert.assertTrue(clientInfoCapture.getValue()); |
| 147 | + Assert.assertFalse(serverInfoCapture.getValue()); |
| 148 | + Assert.assertTrue(logAppNameCapture.getValue()); |
| 149 | + Assert.assertEquals(testName.getMethodName(), appNameCapture.getValue()); |
150 | 150 | } |
151 | | - |
152 | | - |
| 151 | + |
| 152 | + |
153 | 153 | } |
0 commit comments