diff --git a/pom.xml b/pom.xml index b589ae2..1c0a67e 100644 --- a/pom.xml +++ b/pom.xml @@ -61,9 +61,21 @@ test - org.slf4j - slf4j-simple - 2.0.5 + org.apache.logging.log4j + log4j-slf4j2-impl + 2.20.0 + test + + + org.apache.logging.log4j + log4j-core + 2.20.0 + test + + + org.mockito + mockito-core + 4.11.0 test @@ -575,6 +587,8 @@ src/test/resources/sample.rulebase src/test/resources/sample2.rulebase src/test/resources/sample3.rulebase + src/test/resources/log4j2.properties + src/test/resources/log4j2Error.properties diff --git a/src/main/java/com/teragrep/rsm_01/DebugCallback.java b/src/main/java/com/teragrep/rsm_01/DebugCallback.java new file mode 100644 index 0000000..6518d01 --- /dev/null +++ b/src/main/java/com/teragrep/rsm_01/DebugCallback.java @@ -0,0 +1,54 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Callback; +import com.sun.jna.Pointer; + +public interface DebugCallback extends Callback { + + public abstract void invoke(Pointer cookie, String msg, int length); +} diff --git a/src/main/java/com/teragrep/rsm_01/DebugCallbackImpl.java b/src/main/java/com/teragrep/rsm_01/DebugCallbackImpl.java new file mode 100644 index 0000000..866d7e1 --- /dev/null +++ b/src/main/java/com/teragrep/rsm_01/DebugCallbackImpl.java @@ -0,0 +1,60 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Pointer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DebugCallbackImpl implements DebugCallback { + + private static final Logger LOGGER = LoggerFactory.getLogger(DebugCallbackImpl.class); + + @Override + public void invoke(Pointer cookie, String msg, int length) { + LOGGER.debug("liblognorm: {}", msg); + } +} diff --git a/src/main/java/com/teragrep/rsm_01/ErrorCallback.java b/src/main/java/com/teragrep/rsm_01/ErrorCallback.java new file mode 100644 index 0000000..76c151b --- /dev/null +++ b/src/main/java/com/teragrep/rsm_01/ErrorCallback.java @@ -0,0 +1,55 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Callback; +import com.sun.jna.Pointer; + +public interface ErrorCallback extends Callback { + + public abstract void invoke(Pointer cookie, String msg, int length); + +} diff --git a/src/main/java/com/teragrep/rsm_01/ErrorCallbackImpl.java b/src/main/java/com/teragrep/rsm_01/ErrorCallbackImpl.java new file mode 100644 index 0000000..64e0683 --- /dev/null +++ b/src/main/java/com/teragrep/rsm_01/ErrorCallbackImpl.java @@ -0,0 +1,60 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Pointer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ErrorCallbackImpl implements ErrorCallback { + + private static final Logger LOGGER = LoggerFactory.getLogger(ErrorCallbackImpl.class); + + @Override + public void invoke(Pointer cookie, String msg, int length) { + LOGGER.error("liblognorm error: {}", msg); + } +} diff --git a/src/main/java/com/teragrep/rsm_01/LibJavaLognorm.java b/src/main/java/com/teragrep/rsm_01/LibJavaLognorm.java index d833e64..e69f71d 100644 --- a/src/main/java/com/teragrep/rsm_01/LibJavaLognorm.java +++ b/src/main/java/com/teragrep/rsm_01/LibJavaLognorm.java @@ -47,43 +47,11 @@ import com.sun.jna.*; import com.sun.jna.Structure.FieldOrder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public interface LibJavaLognorm extends Library { public static final LibJavaLognorm jnaInstance = Native.load("JavaLognorm", LibJavaLognorm.class); - public static interface DebugCallback extends Callback { - - public abstract void invoke(Pointer cookie, String msg, int length); - - public static class DebugCallbackImpl implements DebugCallback { - - private static final Logger LOGGER = LoggerFactory.getLogger(DebugCallbackImpl.class); - - @Override - public void invoke(Pointer cookie, String msg, int length) { - LOGGER.debug("liblognorm: {}", msg); - } - } - } - - public static interface ErrorCallback extends Callback { - - public abstract void invoke(Pointer cookie, String msg, int length); - - public static class ErrorCallbackImpl implements ErrorCallback { - - private static final Logger LOGGER = LoggerFactory.getLogger(ErrorCallbackImpl.class); - - @Override - public void invoke(Pointer cookie, String msg, int length) { - LOGGER.error("liblognorm error: {}", msg); - } - } - } - // JNA requires the @FieldOrder annotation so it can properly serialize data into a memory buffer before using it as an argument to the target method. @FieldOrder({ "CTXOPT_ALLOW_REGEX", diff --git a/src/main/java/com/teragrep/rsm_01/LognormFactory.java b/src/main/java/com/teragrep/rsm_01/LognormFactory.java index dc17a26..77349a9 100644 --- a/src/main/java/com/teragrep/rsm_01/LognormFactory.java +++ b/src/main/java/com/teragrep/rsm_01/LognormFactory.java @@ -124,7 +124,7 @@ private void liblognormLoadSamples(Pointer ctx, String rulebase) { } private void liblognormSetDebugCB(Pointer ctx) { - LibJavaLognorm.DebugCallback.DebugCallbackImpl callbackImpl = new LibJavaLognorm.DebugCallback.DebugCallbackImpl(); + DebugCallbackImpl callbackImpl = new DebugCallbackImpl(); int i = LibJavaLognorm.jnaInstance.setDebugCB(ctx, callbackImpl); if (i != 0) { LOGGER.error("ln_setDebugCB() returned error code <{}>", i); @@ -133,7 +133,7 @@ private void liblognormSetDebugCB(Pointer ctx) { } private void liblognormSetErrMsgCB(Pointer ctx) { - LibJavaLognorm.ErrorCallback.ErrorCallbackImpl callbackImpl = new LibJavaLognorm.ErrorCallback.ErrorCallbackImpl(); + ErrorCallbackImpl callbackImpl = new ErrorCallbackImpl(); int i = LibJavaLognorm.jnaInstance.setErrMsgCB(ctx, callbackImpl); if (i != 0) { LOGGER.error("ln_setErrMsgCB() returned error code <{}>", i); diff --git a/src/test/java/com/teragrep/rsm_01/CallbackTest.java b/src/test/java/com/teragrep/rsm_01/CallbackTest.java new file mode 100644 index 0000000..e141a9a --- /dev/null +++ b/src/test/java/com/teragrep/rsm_01/CallbackTest.java @@ -0,0 +1,151 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; + +public class CallbackTest { + + @Test + public void setDebugCBTest() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + + LognormFactory lognormFactory = new LognormFactory("rule=:%all:rest%"); + Logger loggerForTarget = (Logger) LogManager.getLogger(DebugCallbackImpl.class); + String expectedLogMessages = "liblognorm: ======================================="; + + final Appender appender = mock(Appender.class); + when(appender.getName()).thenReturn("Mock appender"); + when(appender.isStarted()).thenReturn(true); + final ArgumentCaptor logCaptor = ArgumentCaptor.forClass(LogEvent.class); + final Level effectiveLevel = loggerForTarget.getLevel(); // Save the initial logger state + // Attach our test appender and make sure the messages will be logged + loggerForTarget.addAppender(appender); + loggerForTarget.setLevel(Level.DEBUG); + try { + // invoke debug callback via loadSamplesFromString() + JavaLognormImpl javaLognormImpl = lognormFactory.lognorm(); + // Assert that the expected log messages are seen + verify(appender, times(28)).append(logCaptor.capture()); + Arrays.stream(new String[] { + expectedLogMessages + } + ) + .forEach( + expectedLogMessage -> Assertions + .assertEquals( + expectedLogMessage, logCaptor.getValue().getMessage().getFormattedMessage() + ) + ); + javaLognormImpl.close(); + } + finally { + // Restore logger state in case this affects other tests + loggerForTarget.removeAppender(appender); + loggerForTarget.setLevel(effectiveLevel); + } + } + + @Test + public void setErrMsgCBTest() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2Error.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + + LognormFactory lognormFactory = new LognormFactory("invalidSample"); + Logger loggerForTarget = (Logger) LogManager.getLogger(ErrorCallbackImpl.class); + String expectedLogMessages = "liblognorm error: rulebase file --NO-FILE--[0]: invalid record type detected: 'invalidSample'"; + + final Appender appender = mock(Appender.class); + when(appender.getName()).thenReturn("Mock appender"); + when(appender.isStarted()).thenReturn(true); + final ArgumentCaptor logCaptor = ArgumentCaptor.forClass(LogEvent.class); + final Level effectiveLevel = loggerForTarget.getLevel(); // Save the initial logger state + // Attach our test appender and make sure the messages will be logged + loggerForTarget.addAppender(appender); + loggerForTarget.setLevel(Level.ERROR); + try { + // invoke error callback via loadSamplesFromString() using invalid sample, + JavaLognormImpl javaLognormImpl = lognormFactory.lognorm(); + // Assert that the expected log messages are seen + verify(appender, times(1)).append(logCaptor.capture()); + Arrays.stream(new String[] { + expectedLogMessages + } + ) + .forEach( + expectedLogMessage -> Assertions + .assertEquals( + expectedLogMessage, logCaptor.getValue().getMessage().getFormattedMessage() + ) + ); + javaLognormImpl.close(); + } + finally { + // Restore logger state in case this affects other tests + loggerForTarget.removeAppender(appender); + loggerForTarget.setLevel(effectiveLevel); + } + } + +} diff --git a/src/test/java/com/teragrep/rsm_01/DebugCallbackImplTest.java b/src/test/java/com/teragrep/rsm_01/DebugCallbackImplTest.java new file mode 100644 index 0000000..386dfd5 --- /dev/null +++ b/src/test/java/com/teragrep/rsm_01/DebugCallbackImplTest.java @@ -0,0 +1,113 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Pointer; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; + +public class DebugCallbackImplTest { + + @BeforeAll + public static void log4jconfig() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + } + + @Test + public void debugCallbackImplTest() { + DebugCallbackImpl callbackImpl = new DebugCallbackImpl(); + Logger loggerForTarget = (Logger) LogManager.getLogger(DebugCallbackImpl.class); + String s = "Something happened"; + String expectedLogMessages = "liblognorm: Something happened"; + + final Appender appender = mock(Appender.class); + when(appender.getName()).thenReturn("Mock appender"); + when(appender.isStarted()).thenReturn(true); + final ArgumentCaptor logCaptor = ArgumentCaptor.forClass(LogEvent.class); + final Level effectiveLevel = loggerForTarget.getLevel(); // Save the initial logger state + // Attach our test appender and make sure the messages will be logged + loggerForTarget.addAppender(appender); + loggerForTarget.setLevel(Level.DEBUG); + try { + // invoke callback + callbackImpl.invoke(Pointer.NULL, s, s.length()); + // Assert that the expected log messages are seen + verify(appender, times(1)).append(logCaptor.capture()); + Arrays.stream(new String[] { + expectedLogMessages + } + ) + .forEach( + expectedLogMessage -> Assertions + .assertEquals( + logCaptor.getValue().getMessage().getFormattedMessage(), expectedLogMessage + ) + ); + } + finally { + // Restore logger state in case this affects other tests + loggerForTarget.removeAppender(appender); + loggerForTarget.setLevel(effectiveLevel); + } + } +} diff --git a/src/test/java/com/teragrep/rsm_01/ErrorCallbackImplTest.java b/src/test/java/com/teragrep/rsm_01/ErrorCallbackImplTest.java new file mode 100644 index 0000000..69403f2 --- /dev/null +++ b/src/test/java/com/teragrep/rsm_01/ErrorCallbackImplTest.java @@ -0,0 +1,113 @@ +/* + * Record Schema Mapping Library for Java RSM-01 + * Copyright (C) 2021-2024 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.rsm_01; + +import com.sun.jna.Pointer; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; + +public class ErrorCallbackImplTest { + + @BeforeAll + public static void log4jconfig() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + } + + @Test + public void errorCallbackImplTest() { + ErrorCallbackImpl callbackImpl = new ErrorCallbackImpl(); + Logger loggerForTarget = (Logger) LogManager.getLogger(ErrorCallbackImpl.class); + String s = "Something happened"; + String expectedLogMessages = "liblognorm error: Something happened"; + + final Appender appender = mock(Appender.class); + when(appender.getName()).thenReturn("Mock appender"); + when(appender.isStarted()).thenReturn(true); + final ArgumentCaptor logCaptor = ArgumentCaptor.forClass(LogEvent.class); + final Level effectiveLevel = loggerForTarget.getLevel(); // Save the initial logger state + // Attach our test appender and make sure the messages will be logged + loggerForTarget.addAppender(appender); + loggerForTarget.setLevel(Level.DEBUG); + try { + // invoke callback + callbackImpl.invoke(Pointer.NULL, s, s.length()); + // Assert that the expected log messages are seen + verify(appender, times(1)).append(logCaptor.capture()); + Arrays.stream(new String[] { + expectedLogMessages + } + ) + .forEach( + expectedLogMessage -> Assertions + .assertEquals( + logCaptor.getValue().getMessage().getFormattedMessage(), expectedLogMessage + ) + ); + } + finally { + // Restore logger state in case this affects other tests + loggerForTarget.removeAppender(appender); + loggerForTarget.setLevel(effectiveLevel); + } + } +} diff --git a/src/test/java/com/teragrep/rsm_01/JavaLognormImplTest.java b/src/test/java/com/teragrep/rsm_01/JavaLognormImplTest.java index ce7bdbb..052955a 100644 --- a/src/test/java/com/teragrep/rsm_01/JavaLognormImplTest.java +++ b/src/test/java/com/teragrep/rsm_01/JavaLognormImplTest.java @@ -45,13 +45,25 @@ */ package com.teragrep.rsm_01; +import org.apache.logging.log4j.core.config.Configurator; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.nio.file.Path; +import java.nio.file.Paths; + import static org.junit.jupiter.api.Assertions.*; class JavaLognormImplTest { + @BeforeAll + public static void log4jconfig() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2Error.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + } + @Test public void versionTest() { String s = new JavaLognorm.LibraryInformation().liblognormVersionCheck(); diff --git a/src/test/java/com/teragrep/rsm_01/LognormFactoryTest.java b/src/test/java/com/teragrep/rsm_01/LognormFactoryTest.java index ec1185d..7b168a9 100644 --- a/src/test/java/com/teragrep/rsm_01/LognormFactoryTest.java +++ b/src/test/java/com/teragrep/rsm_01/LognormFactoryTest.java @@ -45,15 +45,26 @@ */ package com.teragrep.rsm_01; +import org.apache.logging.log4j.core.config.Configurator; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class LognormFactoryTest { + @BeforeAll + public static void log4jconfig() { + // log4j2 configuration + Path log4j2Config = Paths.get("src/test/resources/log4j2Error.properties"); + Configurator.reconfigure(log4j2Config.toUri()); + } + @Test public void setCtxOptsTest() { assertDoesNotThrow(() -> { diff --git a/src/test/resources/log4j2.properties b/src/test/resources/log4j2.properties new file mode 100644 index 0000000..b81780e --- /dev/null +++ b/src/test/resources/log4j2.properties @@ -0,0 +1,6 @@ +appender.console.type = Console +appender.console.name = ConsoleLogger +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n +rootLogger.level = DEBUG +rootLogger.appenderRef.stdout.ref = ConsoleLogger \ No newline at end of file diff --git a/src/test/resources/log4j2Error.properties b/src/test/resources/log4j2Error.properties new file mode 100644 index 0000000..debf200 --- /dev/null +++ b/src/test/resources/log4j2Error.properties @@ -0,0 +1,6 @@ +appender.console.type = Console +appender.console.name = ConsoleLogger +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n +rootLogger.level = ERROR +rootLogger.appenderRef.stdout.ref = ConsoleLogger \ No newline at end of file