Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tests for the log outputs of liblognorm error and debug callbacks #14

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,21 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.5</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -575,6 +587,8 @@
<exclude>src/test/resources/sample.rulebase</exclude>
<exclude>src/test/resources/sample2.rulebase</exclude>
<exclude>src/test/resources/sample3.rulebase</exclude>
<exclude>src/test/resources/log4j2.properties</exclude>
<exclude>src/test/resources/log4j2Error.properties</exclude>
</excludes>
</configuration>
<executions>
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/teragrep/rsm_01/DebugCallback.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*
*
* 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);
}
60 changes: 60 additions & 0 deletions src/main/java/com/teragrep/rsm_01/DebugCallbackImpl.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*
*
* 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);
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/teragrep/rsm_01/ErrorCallback.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*
*
* 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);

}
60 changes: 60 additions & 0 deletions src/main/java/com/teragrep/rsm_01/ErrorCallbackImpl.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*
*
* 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);
}
}
32 changes: 0 additions & 32 deletions src/main/java/com/teragrep/rsm_01/LibJavaLognorm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/teragrep/rsm_01/LognormFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading