-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2764 from cloudfoundry/klaus-sap-patch-1
more methods for sanitized logging
- Loading branch information
Showing
2 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
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
150 changes: 114 additions & 36 deletions
150
server/src/test/java/org/cloudfoundry/identity/uaa/logging/SanitizedLogFactoryTest.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 |
---|---|---|
@@ -1,77 +1,155 @@ | ||
package org.cloudfoundry.identity.uaa.logging; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
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 org.apache.commons.lang3.RandomStringUtils; | ||
|
||
public class SanitizedLogFactoryTest { | ||
|
||
private final String dirtyMessage = "one\ntwo\tthree\rfour"; | ||
private final String sanitizedMsg = "one|two|three|four[SANITIZED]"; | ||
private final String cleanMessage = "one two three four"; | ||
|
||
Logger mockLog; | ||
SanitizedLogFactory.SanitizedLog log; | ||
Exception ex; | ||
|
||
@Before | ||
public void setUp() { | ||
mockLog = mock(Logger.class); | ||
log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
ex = new Exception(RandomStringUtils.randomAlphanumeric(8)); | ||
} | ||
|
||
@Test | ||
public void testSanitizeDebug() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isDebugEnabled(); | ||
log.debug("one\ntwo\tthree\rfour"); | ||
verify(mockLog).debug("one|two|three|four[SANITIZED]"); | ||
public void testInit() { | ||
Assert.assertNotNull(SanitizedLogFactory.getLog(SanitizedLogFactoryTest.class)); | ||
} | ||
|
||
@Test | ||
public void testSanitizeDebugCleanMessage() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isDebugEnabled(); | ||
log.debug("one two three four"); | ||
verify(mockLog).debug("one two three four"); | ||
public void testSanitizeInfo() { | ||
when(mockLog.isInfoEnabled()).thenReturn(true); | ||
log.info(dirtyMessage); | ||
verify(mockLog).info(sanitizedMsg); | ||
log.info(dirtyMessage, ex); | ||
verify(mockLog).info(sanitizedMsg, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeDebugCleanMessageException() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isDebugEnabled(); | ||
Exception exception = new Exception(""); | ||
log.debug("one two three four", exception); | ||
verify(mockLog).debug("one two three four", exception); | ||
public void testSanitizeInfoCleanMessage() { | ||
when(mockLog.isInfoEnabled()).thenReturn(true); | ||
log.info(cleanMessage); | ||
verify(mockLog).info(cleanMessage); | ||
log.info(cleanMessage, ex); | ||
verify(mockLog).info(cleanMessage, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeInfo() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isInfoEnabled(); | ||
log.info("one\ntwo\tthree\rfour"); | ||
verify(mockLog).info("one|two|three|four[SANITIZED]"); | ||
public void testSanitizeDebug() { | ||
when(mockLog.isDebugEnabled()).thenReturn(true); | ||
log.debug(dirtyMessage); | ||
verify(mockLog).debug(sanitizedMsg); | ||
log.debug(dirtyMessage, ex); | ||
verify(mockLog).debug(sanitizedMsg, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeInfoCleanMessage() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isInfoEnabled(); | ||
log.info("one two three four"); | ||
verify(mockLog).info("one two three four"); | ||
public void testSanitizeDebugCleanMessage() { | ||
when(mockLog.isDebugEnabled()).thenReturn(true); | ||
log.debug(cleanMessage); | ||
verify(mockLog).debug(cleanMessage); | ||
log.debug(cleanMessage, ex); | ||
verify(mockLog).debug(cleanMessage, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeDebugCleanMessageNotEnabled() { | ||
when(mockLog.isDebugEnabled()).thenReturn(false); | ||
log.debug(cleanMessage); | ||
verify(mockLog, never()).debug(cleanMessage); | ||
log.debug(cleanMessage, ex); | ||
verify(mockLog, never()).debug(cleanMessage, ex); | ||
Assert.assertFalse(log.isDebugEnabled()); | ||
} | ||
|
||
|
||
@Test | ||
public void testSanitizeWarn() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isWarnEnabled(); | ||
log.warn("one\ntwo\tthree\rfour"); | ||
verify(mockLog).warn("one|two|three|four[SANITIZED]"); | ||
when(mockLog.isWarnEnabled()).thenReturn(true); | ||
log.warn(dirtyMessage); | ||
verify(mockLog).warn(sanitizedMsg); | ||
log.warn(dirtyMessage, ex); | ||
verify(mockLog).warn(sanitizedMsg, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeWarnCleanMessage() { | ||
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog); | ||
doReturn(true).when(mockLog).isWarnEnabled(); | ||
log.warn("one two three four"); | ||
verify(mockLog).warn("one two three four"); | ||
when(mockLog.isWarnEnabled()).thenReturn(true); | ||
log.warn(cleanMessage); | ||
verify(mockLog).warn(cleanMessage); | ||
log.warn(cleanMessage, ex); | ||
verify(mockLog).warn(cleanMessage, ex); | ||
} | ||
|
||
} | ||
@Test | ||
public void testSanitizeWarnCleanMessageNotEnabled() { | ||
when(mockLog.isWarnEnabled()).thenReturn(false); | ||
log.warn(cleanMessage); | ||
verify(mockLog, never()).warn(cleanMessage); | ||
log.warn(cleanMessage, ex); | ||
verify(mockLog, never()).warn(cleanMessage, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeError() { | ||
when(mockLog.isErrorEnabled()).thenReturn(true); | ||
log.error(dirtyMessage); | ||
verify(mockLog).error(sanitizedMsg); | ||
log.error(dirtyMessage, ex); | ||
verify(mockLog).error(sanitizedMsg, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeErrorCleanMessage() { | ||
when(mockLog.isErrorEnabled()).thenReturn(true); | ||
log.error(cleanMessage); | ||
verify(mockLog).error(cleanMessage); | ||
log.error(cleanMessage, ex); | ||
verify(mockLog).error(cleanMessage, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeTrace() { | ||
when(mockLog.isTraceEnabled()).thenReturn(true); | ||
log.trace(dirtyMessage); | ||
verify(mockLog).trace(sanitizedMsg); | ||
log.trace(dirtyMessage, ex); | ||
verify(mockLog).trace(sanitizedMsg, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeTraceCleanMessage() { | ||
when(mockLog.isTraceEnabled()).thenReturn(true); | ||
log.trace(cleanMessage); | ||
verify(mockLog).trace(cleanMessage); | ||
log.trace(cleanMessage, ex); | ||
verify(mockLog).trace(cleanMessage, ex); | ||
} | ||
|
||
@Test | ||
public void testSanitizeTraceCleanMessageWhenNotEnabled() { | ||
when(mockLog.isTraceEnabled()).thenReturn(false); | ||
log.trace(cleanMessage); | ||
verify(mockLog, never()).trace(cleanMessage); | ||
log.trace(cleanMessage, ex); | ||
verify(mockLog, never()).trace(cleanMessage, ex); | ||
} | ||
} |