|
| 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.mock; |
| 19 | +import static org.mockito.Mockito.never; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import com.uber.cadence.internal.replay.ReplayAware; |
| 24 | +import java.util.function.Supplier; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.MockitoAnnotations; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.Marker; |
| 31 | + |
| 32 | +public class ReplayAwareLoggerTest { |
| 33 | + |
| 34 | + @Mock private Logger mockLogger; |
| 35 | + |
| 36 | + @Mock private ReplayAware mockReplayAware; |
| 37 | + |
| 38 | + @Mock private Supplier<Boolean> enableLoggingInReplay; |
| 39 | + |
| 40 | + private ReplayAwareLogger replayAwareLogger; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + MockitoAnnotations.openMocks(this); |
| 45 | + replayAwareLogger = new ReplayAwareLogger(mockLogger, mockReplayAware, enableLoggingInReplay); |
| 46 | + } |
| 47 | + |
| 48 | + // =========================== |
| 49 | + // Utility Methods for Testing |
| 50 | + // =========================== |
| 51 | + private void setReplayMode(boolean isReplaying, boolean loggingEnabledInReplay) { |
| 52 | + when(mockReplayAware.isReplaying()).thenReturn(isReplaying); |
| 53 | + when(enableLoggingInReplay.get()).thenReturn(loggingEnabledInReplay); |
| 54 | + } |
| 55 | + |
| 56 | + // =========================== |
| 57 | + // Tests for Each Log Level |
| 58 | + // =========================== |
| 59 | + |
| 60 | + // TRACE LEVEL |
| 61 | + @Test |
| 62 | + public void testTraceLoggingWhenEnabled() { |
| 63 | + setReplayMode(false, false); |
| 64 | + replayAwareLogger.trace("Trace message"); |
| 65 | + verify(mockLogger).trace("Trace message"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testTraceLoggingWhenReplayAndDisabled() { |
| 70 | + setReplayMode(true, false); |
| 71 | + replayAwareLogger.trace("Trace message"); |
| 72 | + verify(mockLogger, never()).trace("Trace message"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testTraceLoggingWhenReplayAndEnabled() { |
| 77 | + setReplayMode(true, true); |
| 78 | + replayAwareLogger.trace("Trace message"); |
| 79 | + verify(mockLogger).trace("Trace message"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testTraceLoggingWithMarker() { |
| 84 | + Marker marker = mock(Marker.class); |
| 85 | + setReplayMode(false, false); |
| 86 | + replayAwareLogger.trace(marker, "Marker trace message"); |
| 87 | + verify(mockLogger).trace(marker, "Marker trace message"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testTraceLoggingWithMarkerReplayAndDisabled() { |
| 92 | + Marker marker = mock(Marker.class); |
| 93 | + setReplayMode(true, false); |
| 94 | + replayAwareLogger.trace(marker, "Marker trace message"); |
| 95 | + verify(mockLogger, never()).trace(marker, "Marker trace message"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testTraceLoggingWithMarkerReplayAndEnabled() { |
| 100 | + Marker marker = mock(Marker.class); |
| 101 | + setReplayMode(true, true); |
| 102 | + replayAwareLogger.trace(marker, "Marker trace message"); |
| 103 | + verify(mockLogger).trace(marker, "Marker trace message"); |
| 104 | + } |
| 105 | + |
| 106 | + // DEBUG LEVEL |
| 107 | + @Test |
| 108 | + public void testDebugLoggingWhenEnabled() { |
| 109 | + setReplayMode(false, false); |
| 110 | + replayAwareLogger.debug("Debug message"); |
| 111 | + verify(mockLogger).debug("Debug message"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testDebugLoggingWhenReplayAndDisabled() { |
| 116 | + setReplayMode(true, false); |
| 117 | + replayAwareLogger.debug("Debug message"); |
| 118 | + verify(mockLogger, never()).debug("Debug message"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testDebugLoggingWhenReplayAndEnabled() { |
| 123 | + setReplayMode(true, true); |
| 124 | + replayAwareLogger.debug("Debug message"); |
| 125 | + verify(mockLogger).debug("Debug message"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testDebugLoggingWithMarker() { |
| 130 | + Marker marker = mock(Marker.class); |
| 131 | + setReplayMode(false, false); |
| 132 | + replayAwareLogger.debug(marker, "Marker debug message"); |
| 133 | + verify(mockLogger).debug(marker, "Marker debug message"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testDebugLoggingWithMarkerReplayAndDisabled() { |
| 138 | + Marker marker = mock(Marker.class); |
| 139 | + setReplayMode(true, false); |
| 140 | + replayAwareLogger.debug(marker, "Marker debug message"); |
| 141 | + verify(mockLogger, never()).debug(marker, "Marker debug message"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testDebugLoggingWithMarkerReplayAndEnabled() { |
| 146 | + Marker marker = mock(Marker.class); |
| 147 | + setReplayMode(true, true); |
| 148 | + replayAwareLogger.debug(marker, "Marker debug message"); |
| 149 | + verify(mockLogger).debug(marker, "Marker debug message"); |
| 150 | + } |
| 151 | + |
| 152 | + // INFO LEVEL |
| 153 | + @Test |
| 154 | + public void testInfoLoggingWhenEnabled() { |
| 155 | + setReplayMode(false, false); |
| 156 | + replayAwareLogger.info("Info message"); |
| 157 | + verify(mockLogger).info("Info message"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testInfoLoggingWhenReplayAndDisabled() { |
| 162 | + setReplayMode(true, false); |
| 163 | + replayAwareLogger.info("Info message"); |
| 164 | + verify(mockLogger, never()).info("Info message"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testInfoLoggingWhenReplayAndEnabled() { |
| 169 | + setReplayMode(true, true); |
| 170 | + replayAwareLogger.info("Info message"); |
| 171 | + verify(mockLogger).info("Info message"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testInfoLoggingWithMarker() { |
| 176 | + Marker marker = mock(Marker.class); |
| 177 | + setReplayMode(false, false); |
| 178 | + replayAwareLogger.info(marker, "Marker info message"); |
| 179 | + verify(mockLogger).info(marker, "Marker info message"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testInfoLoggingWithMarkerReplayAndDisabled() { |
| 184 | + Marker marker = mock(Marker.class); |
| 185 | + setReplayMode(true, false); |
| 186 | + replayAwareLogger.info(marker, "Marker info message"); |
| 187 | + verify(mockLogger, never()).info(marker, "Marker info message"); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testInfoLoggingWithMarkerReplayAndEnabled() { |
| 192 | + Marker marker = mock(Marker.class); |
| 193 | + setReplayMode(true, true); |
| 194 | + replayAwareLogger.info(marker, "Marker info message"); |
| 195 | + verify(mockLogger).info(marker, "Marker info message"); |
| 196 | + } |
| 197 | + |
| 198 | + // WARN LEVEL |
| 199 | + @Test |
| 200 | + public void testWarnLoggingWhenEnabled() { |
| 201 | + setReplayMode(false, false); |
| 202 | + replayAwareLogger.warn("Warn message"); |
| 203 | + verify(mockLogger).warn("Warn message"); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void testWarnLoggingWhenReplayAndDisabled() { |
| 208 | + setReplayMode(true, false); |
| 209 | + replayAwareLogger.warn("Warn message"); |
| 210 | + verify(mockLogger, never()).warn("Warn message"); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testWarnLoggingWhenReplayAndEnabled() { |
| 215 | + setReplayMode(true, true); |
| 216 | + replayAwareLogger.warn("Warn message"); |
| 217 | + verify(mockLogger).warn("Warn message"); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testWarnLoggingWithMarker() { |
| 222 | + Marker marker = mock(Marker.class); |
| 223 | + setReplayMode(false, false); |
| 224 | + replayAwareLogger.warn(marker, "Marker warn message"); |
| 225 | + verify(mockLogger).warn(marker, "Marker warn message"); |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + public void testWarnLoggingWithMarkerReplayAndDisabled() { |
| 230 | + Marker marker = mock(Marker.class); |
| 231 | + setReplayMode(true, false); |
| 232 | + replayAwareLogger.warn(marker, "Marker warn message"); |
| 233 | + verify(mockLogger, never()).warn(marker, "Marker warn message"); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testWarnLoggingWithMarkerReplayAndEnabled() { |
| 238 | + Marker marker = mock(Marker.class); |
| 239 | + setReplayMode(true, true); |
| 240 | + replayAwareLogger.warn(marker, "Marker warn message"); |
| 241 | + verify(mockLogger).warn(marker, "Marker warn message"); |
| 242 | + } |
| 243 | + |
| 244 | + // ERROR LEVEL |
| 245 | + @Test |
| 246 | + public void testErrorLoggingWhenEnabled() { |
| 247 | + setReplayMode(false, false); |
| 248 | + replayAwareLogger.error("Error message"); |
| 249 | + verify(mockLogger).error("Error message"); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void testErrorLoggingWhenReplayAndDisabled() { |
| 254 | + setReplayMode(true, false); |
| 255 | + replayAwareLogger.error("Error message"); |
| 256 | + verify(mockLogger, never()).error("Error message"); |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + public void testErrorLoggingWhenReplayAndEnabled() { |
| 261 | + setReplayMode(true, true); |
| 262 | + replayAwareLogger.error("Error message"); |
| 263 | + verify(mockLogger).error("Error message"); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testErrorLoggingWithMarker() { |
| 268 | + Marker marker = mock(Marker.class); |
| 269 | + setReplayMode(false, false); |
| 270 | + replayAwareLogger.error(marker, "Marker error message"); |
| 271 | + verify(mockLogger).error(marker, "Marker error message"); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void testErrorLoggingWithMarkerReplayAndDisabled() { |
| 276 | + Marker marker = mock(Marker.class); |
| 277 | + setReplayMode(true, false); |
| 278 | + replayAwareLogger.error(marker, "Marker error message"); |
| 279 | + verify(mockLogger, never()).error(marker, "Marker error message"); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + public void testErrorLoggingWithMarkerReplayAndEnabled() { |
| 284 | + Marker marker = mock(Marker.class); |
| 285 | + setReplayMode(true, true); |
| 286 | + replayAwareLogger.error(marker, "Marker error message"); |
| 287 | + verify(mockLogger).error(marker, "Marker error message"); |
| 288 | + } |
| 289 | + |
| 290 | + // =========================== |
| 291 | + // Test getName Delegation |
| 292 | + // =========================== |
| 293 | + @Test |
| 294 | + public void testGetNameDelegation() { |
| 295 | + when(mockLogger.getName()).thenReturn("TestLogger"); |
| 296 | + assertEquals("TestLogger", replayAwareLogger.getName()); |
| 297 | + verify(mockLogger).getName(); |
| 298 | + } |
| 299 | +} |
0 commit comments