|
| 1 | +/** |
| 2 | + * OWASP Enterprise Security API (ESAPI) |
| 3 | + * |
| 4 | + * This file is part of the Open Web Application Security Project (OWASP) |
| 5 | + * Enterprise Security API (ESAPI) project. For details, please see |
| 6 | + * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>. |
| 7 | + * |
| 8 | + * Copyright (c) 2007 - The OWASP Foundation |
| 9 | + * |
| 10 | + * The ESAPI is published by OWASP under the BSD license. You should read and accept the |
| 11 | + * LICENSE before you use, modify, and/or redistribute this software. |
| 12 | + * |
| 13 | + * @created 2018 |
| 14 | + */ |
| 15 | + |
| 16 | +package org.owasp.esapi.logging.java; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.TestName; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.owasp.esapi.Logger; |
| 25 | + |
| 26 | +public class JavaLoggerTest { |
| 27 | + @Rule |
| 28 | + public TestName testName = new TestName(); |
| 29 | + |
| 30 | + private static final String MSG = JavaLoggerTest.class.getSimpleName(); |
| 31 | + |
| 32 | + private JavaLogBridge mockBridge = Mockito.mock(JavaLogBridge.class); |
| 33 | + private java.util.logging.Logger javaLogSpy; |
| 34 | + |
| 35 | + private Throwable testEx = new Throwable(MSG + "_Exception"); |
| 36 | + private Logger testLogger; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setup() { |
| 40 | + java.util.logging.Logger wrappedLogger = java.util.logging.Logger.getLogger(testName.getMethodName()); |
| 41 | + javaLogSpy = Mockito.spy(wrappedLogger); |
| 42 | + testLogger = new JavaLogger(javaLogSpy, mockBridge, Logger.ALL); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testLevelEnablement() { |
| 47 | + testLogger.setLevel(Logger.INFO); |
| 48 | + |
| 49 | + Assert.assertFalse(testLogger.isFatalEnabled()); |
| 50 | + Assert.assertFalse(testLogger.isErrorEnabled()); |
| 51 | + Assert.assertFalse(testLogger.isWarningEnabled()); |
| 52 | + Assert.assertTrue(testLogger.isInfoEnabled()); |
| 53 | + Assert.assertTrue(testLogger.isDebugEnabled()); |
| 54 | + Assert.assertTrue(testLogger.isTraceEnabled()); |
| 55 | + |
| 56 | + Assert.assertEquals(Logger.INFO, testLogger.getESAPILevel()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testAllLevelEnablement() { |
| 61 | + testLogger.setLevel(Logger.ALL); |
| 62 | + |
| 63 | + Assert.assertTrue(testLogger.isFatalEnabled()); |
| 64 | + Assert.assertTrue(testLogger.isErrorEnabled()); |
| 65 | + Assert.assertTrue(testLogger.isWarningEnabled()); |
| 66 | + Assert.assertTrue(testLogger.isInfoEnabled()); |
| 67 | + Assert.assertTrue(testLogger.isDebugEnabled()); |
| 68 | + Assert.assertTrue(testLogger.isTraceEnabled()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testOffLevelEnablement() { |
| 73 | + testLogger.setLevel(Logger.OFF); |
| 74 | + |
| 75 | + Assert.assertFalse(testLogger.isFatalEnabled()); |
| 76 | + Assert.assertFalse(testLogger.isErrorEnabled()); |
| 77 | + Assert.assertFalse(testLogger.isWarningEnabled()); |
| 78 | + Assert.assertFalse(testLogger.isInfoEnabled()); |
| 79 | + Assert.assertFalse(testLogger.isDebugEnabled()); |
| 80 | + Assert.assertFalse(testLogger.isTraceEnabled()); |
| 81 | + } |
| 82 | + @Test |
| 83 | + public void testFatalWithMessage() { |
| 84 | + testLogger.fatal(Logger.EVENT_UNSPECIFIED, MSG); |
| 85 | + |
| 86 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.FATAL, Logger.EVENT_UNSPECIFIED, MSG); |
| 87 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 88 | + } |
| 89 | + @Test |
| 90 | + public void testFatalWithMessageAndThrowable() { |
| 91 | + testLogger.fatal(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 92 | + |
| 93 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.FATAL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 94 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testFatalWithMessageDisabled() { |
| 99 | + testLogger.setLevel(Logger.OFF); |
| 100 | + testLogger.fatal(Logger.EVENT_UNSPECIFIED, MSG); |
| 101 | + |
| 102 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.FATAL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 103 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 104 | + } |
| 105 | + @Test |
| 106 | + public void testFatalWithMessageAndThrowableDisabled() { |
| 107 | + testLogger.setLevel(Logger.OFF); |
| 108 | + testLogger.fatal(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 109 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.FATAL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 110 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testErrorWithMessage() { |
| 115 | + testLogger.error(Logger.EVENT_UNSPECIFIED, MSG); |
| 116 | + |
| 117 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.ERROR, Logger.EVENT_UNSPECIFIED, MSG); |
| 118 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 119 | + } |
| 120 | + @Test |
| 121 | + public void testErrorWithMessageAndThrowable() { |
| 122 | + testLogger.error(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 123 | + |
| 124 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.ERROR, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 125 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testErrorWithMessageDisabled() { |
| 130 | + testLogger.setLevel(Logger.OFF); |
| 131 | + testLogger.error(Logger.EVENT_UNSPECIFIED, MSG); |
| 132 | + |
| 133 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.ERROR, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 134 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 135 | + } |
| 136 | + @Test |
| 137 | + public void testErrorWithMessageAndThrowableDisabled() { |
| 138 | + testLogger.setLevel(Logger.OFF); |
| 139 | + testLogger.error(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 140 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.ERROR, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 141 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testWarnWithMessage() { |
| 146 | + testLogger.warning(Logger.EVENT_UNSPECIFIED, MSG); |
| 147 | + |
| 148 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.WARNING, Logger.EVENT_UNSPECIFIED, MSG); |
| 149 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 150 | + } |
| 151 | + @Test |
| 152 | + public void testWarnWithMessageAndThrowable() { |
| 153 | + testLogger.warning(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 154 | + |
| 155 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.WARNING, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 156 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testWarnWithMessageDisabled() { |
| 161 | + testLogger.setLevel(Logger.OFF); |
| 162 | + testLogger.warning(Logger.EVENT_UNSPECIFIED, MSG); |
| 163 | + |
| 164 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.WARNING, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 165 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 166 | + } |
| 167 | + @Test |
| 168 | + public void testWarnWithMessageAndThrowableDisabled() { |
| 169 | + testLogger.setLevel(Logger.OFF); |
| 170 | + testLogger.warning(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 171 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.WARNING, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 172 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testInfoWithMessage() { |
| 177 | + testLogger.info(Logger.EVENT_UNSPECIFIED, MSG); |
| 178 | + |
| 179 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.INFO, Logger.EVENT_UNSPECIFIED, MSG); |
| 180 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 181 | + } |
| 182 | + @Test |
| 183 | + public void testInfoWithMessageAndThrowable() { |
| 184 | + testLogger.info(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 185 | + |
| 186 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.INFO, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 187 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testInfoWithMessageDisabled() { |
| 192 | + testLogger.setLevel(Logger.OFF); |
| 193 | + testLogger.info(Logger.EVENT_UNSPECIFIED, MSG); |
| 194 | + |
| 195 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.INFO, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 196 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 197 | + } |
| 198 | + @Test |
| 199 | + public void testInfoWithMessageAndThrowableDisabled() { |
| 200 | + testLogger.setLevel(Logger.OFF); |
| 201 | + testLogger.info(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 202 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.INFO, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 203 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 204 | + } |
| 205 | + @Test |
| 206 | + public void testDebugWithMessage() { |
| 207 | + testLogger.debug(Logger.EVENT_UNSPECIFIED, MSG); |
| 208 | + |
| 209 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.DEBUG, Logger.EVENT_UNSPECIFIED, MSG); |
| 210 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 211 | + } |
| 212 | + @Test |
| 213 | + public void testDebugWithMessageAndThrowable() { |
| 214 | + testLogger.debug(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 215 | + |
| 216 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.DEBUG, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 217 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testDebugWithMessageDisabled() { |
| 222 | + testLogger.setLevel(Logger.OFF); |
| 223 | + testLogger.debug(Logger.EVENT_UNSPECIFIED, MSG); |
| 224 | + |
| 225 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.DEBUG, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 226 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 227 | + } |
| 228 | + @Test |
| 229 | + public void testDebugWithMessageAndThrowableDisabled() { |
| 230 | + testLogger.setLevel(Logger.OFF); |
| 231 | + testLogger.debug(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 232 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.DEBUG, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 233 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testTraceWithMessage() { |
| 238 | + testLogger.trace(Logger.EVENT_UNSPECIFIED, MSG); |
| 239 | + |
| 240 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.TRACE, Logger.EVENT_UNSPECIFIED, MSG); |
| 241 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 242 | + } |
| 243 | + @Test |
| 244 | + public void testTraceWithMessageAndThrowable() { |
| 245 | + testLogger.trace(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 246 | + |
| 247 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.TRACE, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 248 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void testTraceWithMessageDisabled() { |
| 253 | + testLogger.setLevel(Logger.OFF); |
| 254 | + testLogger.trace(Logger.EVENT_UNSPECIFIED, MSG); |
| 255 | + |
| 256 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.TRACE, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 257 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 258 | + } |
| 259 | + @Test |
| 260 | + public void testTraceWithMessageAndThrowableDisabled() { |
| 261 | + testLogger.setLevel(Logger.OFF); |
| 262 | + testLogger.trace(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 263 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.TRACE, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 264 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void testAlwaysWithMessage() { |
| 269 | + testLogger.always(Logger.EVENT_UNSPECIFIED, MSG); |
| 270 | + |
| 271 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.ALL, Logger.EVENT_UNSPECIFIED, MSG); |
| 272 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 273 | + } |
| 274 | + @Test |
| 275 | + public void testAlwaysWithMessageAndThrowable() { |
| 276 | + testLogger.always(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 277 | + |
| 278 | + Mockito.verify(mockBridge, Mockito.times(1)).log(javaLogSpy, Logger.ALL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 279 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + public void testAlwaysWithMessageDisabled() { |
| 284 | + testLogger.setLevel(Logger.OFF); |
| 285 | + testLogger.always(Logger.EVENT_UNSPECIFIED, MSG); |
| 286 | + |
| 287 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.ALL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 288 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 289 | + } |
| 290 | + @Test |
| 291 | + public void testAlwaysWithMessageAndThrowableDisabled() { |
| 292 | + testLogger.setLevel(Logger.OFF); |
| 293 | + testLogger.always(Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 294 | + Mockito.verify(mockBridge, Mockito.times(0)).log(javaLogSpy, Logger.ALL, Logger.EVENT_UNSPECIFIED, MSG, testEx); |
| 295 | + Mockito.verifyNoMoreInteractions(mockBridge, javaLogSpy); |
| 296 | + } |
| 297 | +} |
0 commit comments