|
1 | | -/* |
2 | | - * #%L |
3 | | - * SciJava Common shared library for SciJava software. |
4 | | - * %% |
5 | | - * Copyright (C) 2009 - 2017 Board of Regents of the University of |
6 | | - * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
7 | | - * Institute of Molecular Cell Biology and Genetics, University of |
8 | | - * Konstanz, and KNIME GmbH. |
9 | | - * %% |
10 | | - * Redistribution and use in source and binary forms, with or without |
11 | | - * modification, are permitted provided that the following conditions are met: |
12 | | - * |
13 | | - * 1. Redistributions of source code must retain the above copyright notice, |
14 | | - * this list of conditions and the following disclaimer. |
15 | | - * 2. Redistributions in binary form must reproduce the above copyright notice, |
16 | | - * this list of conditions and the following disclaimer in the documentation |
17 | | - * and/or other materials provided with the distribution. |
18 | | - * |
19 | | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | | - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | | - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | | - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
23 | | - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | | - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | | - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | | - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | | - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | | - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | | - * POSSIBILITY OF SUCH DAMAGE. |
30 | | - * #L% |
31 | | - */ |
32 | | - |
33 | 1 | package org.scijava.log; |
34 | 2 |
|
35 | | -import static org.junit.Assert.assertTrue; |
36 | | -import static org.scijava.log.LogService.WARN; |
37 | | - |
38 | 3 | import org.junit.Test; |
39 | 4 |
|
| 5 | +import java.util.function.BiConsumer; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import static org.junit.Assert.*; |
| 9 | + |
40 | 10 | /** |
41 | 11 | * Tests {@link LogService}. |
42 | | - * |
43 | | - * @author Johannes Schindelin |
| 12 | + * |
| 13 | + * @author Matthias Arzt |
44 | 14 | */ |
45 | 15 | public class LogServiceTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testGetPrefix() { |
| 19 | + TestableLogService logService = new TestableLogService(); |
| 20 | + assertEquals("[ERROR]", logService.getPrefix(LogService.ERROR)); |
| 21 | + assertEquals("[TRACE]", logService.getPrefix(LogService.TRACE)); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testCompleteLogMethod() { |
| 26 | + testCompleteLogMethod("ERROR", (logService, msg, t) -> logService.error(msg, t)); |
| 27 | + testCompleteLogMethod("WARN", (logService, msg, t) -> logService.warn(msg, t)); |
| 28 | + testCompleteLogMethod("INFO", (logService, msg, t) -> logService.info(msg, t)); |
| 29 | + testCompleteLogMethod("DEBUG", (logService, msg, t) -> logService.debug(msg, t)); |
| 30 | + testCompleteLogMethod("TRACE", (logService, msg, t) -> logService.trace(msg, t)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testMessageLogMethod() { |
| 35 | + testMessageLogMethod("ERROR", (logService, msg) -> logService.error(msg)); |
| 36 | + testMessageLogMethod("WARN", (logService, msg) -> logService.warn(msg)); |
| 37 | + testMessageLogMethod("INFO", (logService, msg) -> logService.info(msg)); |
| 38 | + testMessageLogMethod("DEBUG", (logService, msg) -> logService.debug(msg)); |
| 39 | + testMessageLogMethod("TRACE", (logService, msg) -> logService.trace(msg)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testExceptionLogMethod() { |
| 44 | + testExceptionLogMethod("ERROR", (logService, t) -> logService.error(t)); |
| 45 | + testExceptionLogMethod("WARN", (logService, t) -> logService.warn(t)); |
| 46 | + testExceptionLogMethod("INFO", (logService, t) -> logService.info(t)); |
| 47 | + testExceptionLogMethod("DEBUG", (logService, t) -> logService.debug(t)); |
| 48 | + testExceptionLogMethod("TRACE", (logService, t) -> logService.trace(t)); |
| 49 | + } |
| 50 | + |
| 51 | + private void testCompleteLogMethod(String prefix, LogMethodCall logMethodCall) { |
| 52 | + testLogMethod(prefix, logMethodCall, true, true); |
| 53 | + } |
| 54 | + |
| 55 | + private void testMessageLogMethod(String prefix, BiConsumer<LogService, Object> call) { |
| 56 | + testLogMethod(prefix, (log, text, exception) -> call.accept(log, text), true, false); |
| 57 | + } |
| 58 | + |
| 59 | + private void testExceptionLogMethod(String prefix, BiConsumer<LogService, Throwable> call) { |
| 60 | + testLogMethod(prefix, (log, text, exception) -> call.accept(log, exception), false, true); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + private void testLogMethod(String prefix, LogMethodCall logMethodCall, boolean testMessage, boolean testException) { |
| 65 | + // setup |
| 66 | + TestableLogService logService = new TestableLogService(); |
| 67 | + logService.setLevel(LogService.TRACE); |
| 68 | + String text = "Message"; |
| 69 | + NullPointerException exception = new NullPointerException(); |
| 70 | + // process |
| 71 | + logMethodCall.run(logService, text, exception); |
| 72 | + // test |
| 73 | + if(testMessage) { |
| 74 | + assertTrue(logService.message().contains(prefix)); |
| 75 | + assertTrue(logService.message().contains(text)); |
| 76 | + } |
| 77 | + if(testException) |
| 78 | + assertEquals(exception, logService.exception()); |
| 79 | + } |
| 80 | + |
46 | 81 | @Test |
47 | | - public void testDefaultLevel() { |
48 | | - final LogService log = new StderrLogService(); |
49 | | - int level = log.getLevel(); |
50 | | - assertTrue("default level (" + level + ") is at least INFO(" + WARN + ")", level >= WARN); |
| 82 | + public void testSetLevel() { |
| 83 | + TestableLogService logService = new TestableLogService(); |
| 84 | + logService.setLevel(LogService.TRACE); |
| 85 | + assertEquals(LogService.TRACE, logService.getLevel()); |
| 86 | + logService.setLevel(LogService.ERROR); |
| 87 | + assertEquals(LogService.ERROR, logService.getLevel()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSetClassSpecificLevel() { |
| 92 | + TestableLogService logService = new TestableLogService(); |
| 93 | + MyTestClass testClass = new MyTestClass(logService); |
| 94 | + logService.setLevel(LogService.ERROR); |
| 95 | + logService.setLevel(MyTestClass.class.getName(), LogService.TRACE); |
| 96 | + assertEquals(LogService.ERROR, logService.getLevel()); |
| 97 | + assertEquals(LogService.TRACE, testClass.getLevel()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testIsWarn() { |
| 102 | + testIsLevel(LogService.ERROR, LogService::isError); |
| 103 | + testIsLevel(LogService.WARN, LogService::isWarn); |
| 104 | + testIsLevel(LogService.INFO, LogService::isInfo); |
| 105 | + testIsLevel(LogService.DEBUG, LogService::isDebug); |
| 106 | + testIsLevel(LogService.TRACE, LogService::isTrace); |
| 107 | + } |
| 108 | + |
| 109 | + private void testIsLevel(int level, Function<LogService, Boolean> isLevel) { |
| 110 | + TestableLogService logService = new TestableLogService(); |
| 111 | + logService.setLevel(LogService.NONE); |
| 112 | + assertFalse(isLevel.apply(logService)); |
| 113 | + logService.setLevel(level); |
| 114 | + assertTrue(isLevel.apply(logService)); |
| 115 | + logService.setLevel(LogService.TRACE); |
| 116 | + assertTrue(isLevel.apply(logService)); |
| 117 | + } |
| 118 | + |
| 119 | + // -- Helper classes -- |
| 120 | + |
| 121 | + private static class MyTestClass { |
| 122 | + |
| 123 | + private final LogService log; |
| 124 | + |
| 125 | + MyTestClass(LogService log) { |
| 126 | + this.log = log; |
| 127 | + } |
| 128 | + |
| 129 | + int getLevel() { |
| 130 | + return log.getLevel(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private interface LogMethodCall { |
| 135 | + void run(LogService logService, Object text, Throwable exception); |
| 136 | + } |
| 137 | + |
| 138 | + private static class TestableLogService extends AbstractLogService { |
| 139 | + |
| 140 | + String message = null; |
| 141 | + Throwable exception = null; |
| 142 | + |
| 143 | + public String message() { |
| 144 | + return message; |
| 145 | + } |
| 146 | + |
| 147 | + public Throwable exception() { |
| 148 | + return exception; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + protected void log(String msg) { |
| 153 | + this.message = msg; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected void log(Throwable t) { |
| 158 | + this.exception = t; |
| 159 | + } |
51 | 160 | } |
52 | 161 | } |
0 commit comments