Skip to content

Commit 181c141

Browse files
committed
Add unit tests for replay logger
1 parent 9d4bed7 commit 181c141

File tree

1 file changed

+296
-0
lines changed

1 file changed

+296
-0
lines changed
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/**
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
7+
* except in compliance with the License. A copy of the License is located at
8+
*
9+
* <p>http://aws.amazon.com/apache2.0
10+
*
11+
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
* specific language governing permissions and limitations under the License.
14+
*/
15+
package com.uber.cadence.internal.logging;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.mockito.Mockito.*;
19+
20+
import com.uber.cadence.internal.replay.ReplayAware;
21+
import java.util.function.Supplier;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
26+
import org.slf4j.Logger;
27+
import org.slf4j.Marker;
28+
29+
public class ReplayAwareLoggerTest {
30+
31+
@Mock private Logger mockLogger;
32+
33+
@Mock private ReplayAware mockReplayAware;
34+
35+
@Mock private Supplier<Boolean> enableLoggingInReplay;
36+
37+
private ReplayAwareLogger replayAwareLogger;
38+
39+
@Before
40+
public void setUp() {
41+
MockitoAnnotations.openMocks(this);
42+
replayAwareLogger = new ReplayAwareLogger(mockLogger, mockReplayAware, enableLoggingInReplay);
43+
}
44+
45+
// ===========================
46+
// Utility Methods for Testing
47+
// ===========================
48+
private void setReplayMode(boolean isReplaying, boolean loggingEnabledInReplay) {
49+
when(mockReplayAware.isReplaying()).thenReturn(isReplaying);
50+
when(enableLoggingInReplay.get()).thenReturn(loggingEnabledInReplay);
51+
}
52+
53+
// ===========================
54+
// Tests for Each Log Level
55+
// ===========================
56+
57+
// TRACE LEVEL
58+
@Test
59+
public void testTraceLoggingWhenEnabled() {
60+
setReplayMode(false, false);
61+
replayAwareLogger.trace("Trace message");
62+
verify(mockLogger).trace("Trace message");
63+
}
64+
65+
@Test
66+
public void testTraceLoggingWhenReplayAndDisabled() {
67+
setReplayMode(true, false);
68+
replayAwareLogger.trace("Trace message");
69+
verify(mockLogger, never()).trace("Trace message");
70+
}
71+
72+
@Test
73+
public void testTraceLoggingWhenReplayAndEnabled() {
74+
setReplayMode(true, true);
75+
replayAwareLogger.trace("Trace message");
76+
verify(mockLogger).trace("Trace message");
77+
}
78+
79+
@Test
80+
public void testTraceLoggingWithMarker() {
81+
Marker marker = mock(Marker.class);
82+
setReplayMode(false, false);
83+
replayAwareLogger.trace(marker, "Marker trace message");
84+
verify(mockLogger).trace(marker, "Marker trace message");
85+
}
86+
87+
@Test
88+
public void testTraceLoggingWithMarkerReplayAndDisabled() {
89+
Marker marker = mock(Marker.class);
90+
setReplayMode(true, false);
91+
replayAwareLogger.trace(marker, "Marker trace message");
92+
verify(mockLogger, never()).trace(marker, "Marker trace message");
93+
}
94+
95+
@Test
96+
public void testTraceLoggingWithMarkerReplayAndEnabled() {
97+
Marker marker = mock(Marker.class);
98+
setReplayMode(true, true);
99+
replayAwareLogger.trace(marker, "Marker trace message");
100+
verify(mockLogger).trace(marker, "Marker trace message");
101+
}
102+
103+
// DEBUG LEVEL
104+
@Test
105+
public void testDebugLoggingWhenEnabled() {
106+
setReplayMode(false, false);
107+
replayAwareLogger.debug("Debug message");
108+
verify(mockLogger).debug("Debug message");
109+
}
110+
111+
@Test
112+
public void testDebugLoggingWhenReplayAndDisabled() {
113+
setReplayMode(true, false);
114+
replayAwareLogger.debug("Debug message");
115+
verify(mockLogger, never()).debug("Debug message");
116+
}
117+
118+
@Test
119+
public void testDebugLoggingWhenReplayAndEnabled() {
120+
setReplayMode(true, true);
121+
replayAwareLogger.debug("Debug message");
122+
verify(mockLogger).debug("Debug message");
123+
}
124+
125+
@Test
126+
public void testDebugLoggingWithMarker() {
127+
Marker marker = mock(Marker.class);
128+
setReplayMode(false, false);
129+
replayAwareLogger.debug(marker, "Marker debug message");
130+
verify(mockLogger).debug(marker, "Marker debug message");
131+
}
132+
133+
@Test
134+
public void testDebugLoggingWithMarkerReplayAndDisabled() {
135+
Marker marker = mock(Marker.class);
136+
setReplayMode(true, false);
137+
replayAwareLogger.debug(marker, "Marker debug message");
138+
verify(mockLogger, never()).debug(marker, "Marker debug message");
139+
}
140+
141+
@Test
142+
public void testDebugLoggingWithMarkerReplayAndEnabled() {
143+
Marker marker = mock(Marker.class);
144+
setReplayMode(true, true);
145+
replayAwareLogger.debug(marker, "Marker debug message");
146+
verify(mockLogger).debug(marker, "Marker debug message");
147+
}
148+
149+
// INFO LEVEL
150+
@Test
151+
public void testInfoLoggingWhenEnabled() {
152+
setReplayMode(false, false);
153+
replayAwareLogger.info("Info message");
154+
verify(mockLogger).info("Info message");
155+
}
156+
157+
@Test
158+
public void testInfoLoggingWhenReplayAndDisabled() {
159+
setReplayMode(true, false);
160+
replayAwareLogger.info("Info message");
161+
verify(mockLogger, never()).info("Info message");
162+
}
163+
164+
@Test
165+
public void testInfoLoggingWhenReplayAndEnabled() {
166+
setReplayMode(true, true);
167+
replayAwareLogger.info("Info message");
168+
verify(mockLogger).info("Info message");
169+
}
170+
171+
@Test
172+
public void testInfoLoggingWithMarker() {
173+
Marker marker = mock(Marker.class);
174+
setReplayMode(false, false);
175+
replayAwareLogger.info(marker, "Marker info message");
176+
verify(mockLogger).info(marker, "Marker info message");
177+
}
178+
179+
@Test
180+
public void testInfoLoggingWithMarkerReplayAndDisabled() {
181+
Marker marker = mock(Marker.class);
182+
setReplayMode(true, false);
183+
replayAwareLogger.info(marker, "Marker info message");
184+
verify(mockLogger, never()).info(marker, "Marker info message");
185+
}
186+
187+
@Test
188+
public void testInfoLoggingWithMarkerReplayAndEnabled() {
189+
Marker marker = mock(Marker.class);
190+
setReplayMode(true, true);
191+
replayAwareLogger.info(marker, "Marker info message");
192+
verify(mockLogger).info(marker, "Marker info message");
193+
}
194+
195+
// WARN LEVEL
196+
@Test
197+
public void testWarnLoggingWhenEnabled() {
198+
setReplayMode(false, false);
199+
replayAwareLogger.warn("Warn message");
200+
verify(mockLogger).warn("Warn message");
201+
}
202+
203+
@Test
204+
public void testWarnLoggingWhenReplayAndDisabled() {
205+
setReplayMode(true, false);
206+
replayAwareLogger.warn("Warn message");
207+
verify(mockLogger, never()).warn("Warn message");
208+
}
209+
210+
@Test
211+
public void testWarnLoggingWhenReplayAndEnabled() {
212+
setReplayMode(true, true);
213+
replayAwareLogger.warn("Warn message");
214+
verify(mockLogger).warn("Warn message");
215+
}
216+
217+
@Test
218+
public void testWarnLoggingWithMarker() {
219+
Marker marker = mock(Marker.class);
220+
setReplayMode(false, false);
221+
replayAwareLogger.warn(marker, "Marker warn message");
222+
verify(mockLogger).warn(marker, "Marker warn message");
223+
}
224+
225+
@Test
226+
public void testWarnLoggingWithMarkerReplayAndDisabled() {
227+
Marker marker = mock(Marker.class);
228+
setReplayMode(true, false);
229+
replayAwareLogger.warn(marker, "Marker warn message");
230+
verify(mockLogger, never()).warn(marker, "Marker warn message");
231+
}
232+
233+
@Test
234+
public void testWarnLoggingWithMarkerReplayAndEnabled() {
235+
Marker marker = mock(Marker.class);
236+
setReplayMode(true, true);
237+
replayAwareLogger.warn(marker, "Marker warn message");
238+
verify(mockLogger).warn(marker, "Marker warn message");
239+
}
240+
241+
// ERROR LEVEL
242+
@Test
243+
public void testErrorLoggingWhenEnabled() {
244+
setReplayMode(false, false);
245+
replayAwareLogger.error("Error message");
246+
verify(mockLogger).error("Error message");
247+
}
248+
249+
@Test
250+
public void testErrorLoggingWhenReplayAndDisabled() {
251+
setReplayMode(true, false);
252+
replayAwareLogger.error("Error message");
253+
verify(mockLogger, never()).error("Error message");
254+
}
255+
256+
@Test
257+
public void testErrorLoggingWhenReplayAndEnabled() {
258+
setReplayMode(true, true);
259+
replayAwareLogger.error("Error message");
260+
verify(mockLogger).error("Error message");
261+
}
262+
263+
@Test
264+
public void testErrorLoggingWithMarker() {
265+
Marker marker = mock(Marker.class);
266+
setReplayMode(false, false);
267+
replayAwareLogger.error(marker, "Marker error message");
268+
verify(mockLogger).error(marker, "Marker error message");
269+
}
270+
271+
@Test
272+
public void testErrorLoggingWithMarkerReplayAndDisabled() {
273+
Marker marker = mock(Marker.class);
274+
setReplayMode(true, false);
275+
replayAwareLogger.error(marker, "Marker error message");
276+
verify(mockLogger, never()).error(marker, "Marker error message");
277+
}
278+
279+
@Test
280+
public void testErrorLoggingWithMarkerReplayAndEnabled() {
281+
Marker marker = mock(Marker.class);
282+
setReplayMode(true, true);
283+
replayAwareLogger.error(marker, "Marker error message");
284+
verify(mockLogger).error(marker, "Marker error message");
285+
}
286+
287+
// ===========================
288+
// Test getName Delegation
289+
// ===========================
290+
@Test
291+
public void testGetNameDelegation() {
292+
when(mockLogger.getName()).thenReturn("TestLogger");
293+
assertEquals("TestLogger", replayAwareLogger.getName());
294+
verify(mockLogger).getName();
295+
}
296+
}

0 commit comments

Comments
 (0)