|
| 1 | +package org.scijava.log; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.function.BiConsumer; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import static org.junit.Assert.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests {@link LogService}. |
| 12 | + * |
| 13 | + * @author Matthias Arzt |
| 14 | + */ |
| 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 | + |
| 81 | + @Test |
| 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 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments