-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
299 additions
and
0 deletions.
There are no files selected for viewing
299 changes: 299 additions & 0 deletions
299
src/test/java/com/uber/cadence/internal/logging/ReplayAwareLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,299 @@ | ||
/** | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
* except in compliance with the License. A copy of the License is located at | ||
* | ||
* <p>http://aws.amazon.com/apache2.0 | ||
* | ||
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.uber.cadence.internal.logging; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.uber.cadence.internal.replay.ReplayAware; | ||
import java.util.function.Supplier; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.slf4j.Logger; | ||
import org.slf4j.Marker; | ||
|
||
public class ReplayAwareLoggerTest { | ||
|
||
@Mock private Logger mockLogger; | ||
|
||
@Mock private ReplayAware mockReplayAware; | ||
|
||
@Mock private Supplier<Boolean> enableLoggingInReplay; | ||
|
||
private ReplayAwareLogger replayAwareLogger; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
replayAwareLogger = new ReplayAwareLogger(mockLogger, mockReplayAware, enableLoggingInReplay); | ||
} | ||
|
||
// =========================== | ||
// Utility Methods for Testing | ||
// =========================== | ||
private void setReplayMode(boolean isReplaying, boolean loggingEnabledInReplay) { | ||
when(mockReplayAware.isReplaying()).thenReturn(isReplaying); | ||
when(enableLoggingInReplay.get()).thenReturn(loggingEnabledInReplay); | ||
} | ||
|
||
// =========================== | ||
// Tests for Each Log Level | ||
// =========================== | ||
|
||
// TRACE LEVEL | ||
@Test | ||
public void testTraceLoggingWhenEnabled() { | ||
setReplayMode(false, false); | ||
replayAwareLogger.trace("Trace message"); | ||
verify(mockLogger).trace("Trace message"); | ||
} | ||
|
||
@Test | ||
public void testTraceLoggingWhenReplayAndDisabled() { | ||
setReplayMode(true, false); | ||
replayAwareLogger.trace("Trace message"); | ||
verify(mockLogger, never()).trace("Trace message"); | ||
} | ||
|
||
@Test | ||
public void testTraceLoggingWhenReplayAndEnabled() { | ||
setReplayMode(true, true); | ||
replayAwareLogger.trace("Trace message"); | ||
verify(mockLogger).trace("Trace message"); | ||
} | ||
|
||
@Test | ||
public void testTraceLoggingWithMarker() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(false, false); | ||
replayAwareLogger.trace(marker, "Marker trace message"); | ||
verify(mockLogger).trace(marker, "Marker trace message"); | ||
} | ||
|
||
@Test | ||
public void testTraceLoggingWithMarkerReplayAndDisabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, false); | ||
replayAwareLogger.trace(marker, "Marker trace message"); | ||
verify(mockLogger, never()).trace(marker, "Marker trace message"); | ||
} | ||
|
||
@Test | ||
public void testTraceLoggingWithMarkerReplayAndEnabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, true); | ||
replayAwareLogger.trace(marker, "Marker trace message"); | ||
verify(mockLogger).trace(marker, "Marker trace message"); | ||
} | ||
|
||
// DEBUG LEVEL | ||
@Test | ||
public void testDebugLoggingWhenEnabled() { | ||
setReplayMode(false, false); | ||
replayAwareLogger.debug("Debug message"); | ||
verify(mockLogger).debug("Debug message"); | ||
} | ||
|
||
@Test | ||
public void testDebugLoggingWhenReplayAndDisabled() { | ||
setReplayMode(true, false); | ||
replayAwareLogger.debug("Debug message"); | ||
verify(mockLogger, never()).debug("Debug message"); | ||
} | ||
|
||
@Test | ||
public void testDebugLoggingWhenReplayAndEnabled() { | ||
setReplayMode(true, true); | ||
replayAwareLogger.debug("Debug message"); | ||
verify(mockLogger).debug("Debug message"); | ||
} | ||
|
||
@Test | ||
public void testDebugLoggingWithMarker() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(false, false); | ||
replayAwareLogger.debug(marker, "Marker debug message"); | ||
verify(mockLogger).debug(marker, "Marker debug message"); | ||
} | ||
|
||
@Test | ||
public void testDebugLoggingWithMarkerReplayAndDisabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, false); | ||
replayAwareLogger.debug(marker, "Marker debug message"); | ||
verify(mockLogger, never()).debug(marker, "Marker debug message"); | ||
} | ||
|
||
@Test | ||
public void testDebugLoggingWithMarkerReplayAndEnabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, true); | ||
replayAwareLogger.debug(marker, "Marker debug message"); | ||
verify(mockLogger).debug(marker, "Marker debug message"); | ||
} | ||
|
||
// INFO LEVEL | ||
@Test | ||
public void testInfoLoggingWhenEnabled() { | ||
setReplayMode(false, false); | ||
replayAwareLogger.info("Info message"); | ||
verify(mockLogger).info("Info message"); | ||
} | ||
|
||
@Test | ||
public void testInfoLoggingWhenReplayAndDisabled() { | ||
setReplayMode(true, false); | ||
replayAwareLogger.info("Info message"); | ||
verify(mockLogger, never()).info("Info message"); | ||
} | ||
|
||
@Test | ||
public void testInfoLoggingWhenReplayAndEnabled() { | ||
setReplayMode(true, true); | ||
replayAwareLogger.info("Info message"); | ||
verify(mockLogger).info("Info message"); | ||
} | ||
|
||
@Test | ||
public void testInfoLoggingWithMarker() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(false, false); | ||
replayAwareLogger.info(marker, "Marker info message"); | ||
verify(mockLogger).info(marker, "Marker info message"); | ||
} | ||
|
||
@Test | ||
public void testInfoLoggingWithMarkerReplayAndDisabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, false); | ||
replayAwareLogger.info(marker, "Marker info message"); | ||
verify(mockLogger, never()).info(marker, "Marker info message"); | ||
} | ||
|
||
@Test | ||
public void testInfoLoggingWithMarkerReplayAndEnabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, true); | ||
replayAwareLogger.info(marker, "Marker info message"); | ||
verify(mockLogger).info(marker, "Marker info message"); | ||
} | ||
|
||
// WARN LEVEL | ||
@Test | ||
public void testWarnLoggingWhenEnabled() { | ||
setReplayMode(false, false); | ||
replayAwareLogger.warn("Warn message"); | ||
verify(mockLogger).warn("Warn message"); | ||
} | ||
|
||
@Test | ||
public void testWarnLoggingWhenReplayAndDisabled() { | ||
setReplayMode(true, false); | ||
replayAwareLogger.warn("Warn message"); | ||
verify(mockLogger, never()).warn("Warn message"); | ||
} | ||
|
||
@Test | ||
public void testWarnLoggingWhenReplayAndEnabled() { | ||
setReplayMode(true, true); | ||
replayAwareLogger.warn("Warn message"); | ||
verify(mockLogger).warn("Warn message"); | ||
} | ||
|
||
@Test | ||
public void testWarnLoggingWithMarker() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(false, false); | ||
replayAwareLogger.warn(marker, "Marker warn message"); | ||
verify(mockLogger).warn(marker, "Marker warn message"); | ||
} | ||
|
||
@Test | ||
public void testWarnLoggingWithMarkerReplayAndDisabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, false); | ||
replayAwareLogger.warn(marker, "Marker warn message"); | ||
verify(mockLogger, never()).warn(marker, "Marker warn message"); | ||
} | ||
|
||
@Test | ||
public void testWarnLoggingWithMarkerReplayAndEnabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, true); | ||
replayAwareLogger.warn(marker, "Marker warn message"); | ||
verify(mockLogger).warn(marker, "Marker warn message"); | ||
} | ||
|
||
// ERROR LEVEL | ||
@Test | ||
public void testErrorLoggingWhenEnabled() { | ||
setReplayMode(false, false); | ||
replayAwareLogger.error("Error message"); | ||
verify(mockLogger).error("Error message"); | ||
} | ||
|
||
@Test | ||
public void testErrorLoggingWhenReplayAndDisabled() { | ||
setReplayMode(true, false); | ||
replayAwareLogger.error("Error message"); | ||
verify(mockLogger, never()).error("Error message"); | ||
} | ||
|
||
@Test | ||
public void testErrorLoggingWhenReplayAndEnabled() { | ||
setReplayMode(true, true); | ||
replayAwareLogger.error("Error message"); | ||
verify(mockLogger).error("Error message"); | ||
} | ||
|
||
@Test | ||
public void testErrorLoggingWithMarker() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(false, false); | ||
replayAwareLogger.error(marker, "Marker error message"); | ||
verify(mockLogger).error(marker, "Marker error message"); | ||
} | ||
|
||
@Test | ||
public void testErrorLoggingWithMarkerReplayAndDisabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, false); | ||
replayAwareLogger.error(marker, "Marker error message"); | ||
verify(mockLogger, never()).error(marker, "Marker error message"); | ||
} | ||
|
||
@Test | ||
public void testErrorLoggingWithMarkerReplayAndEnabled() { | ||
Marker marker = mock(Marker.class); | ||
setReplayMode(true, true); | ||
replayAwareLogger.error(marker, "Marker error message"); | ||
verify(mockLogger).error(marker, "Marker error message"); | ||
} | ||
|
||
// =========================== | ||
// Test getName Delegation | ||
// =========================== | ||
@Test | ||
public void testGetNameDelegation() { | ||
when(mockLogger.getName()).thenReturn("TestLogger"); | ||
assertEquals("TestLogger", replayAwareLogger.getName()); | ||
verify(mockLogger).getName(); | ||
} | ||
} |