diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CLibMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CLibMojo.java index 651f381685..1687a780ff 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CLibMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CLibMojo.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package org.eolang.maven; import java.io.File; @@ -6,17 +30,28 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import org.apache.commons.io.FilenameUtils; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.apache.commons.io.FilenameUtils; +/** + * Mojo that compiles C native libraries. + * + * @since 0.38 + */ @Mojo( name = "clib", defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true ) -public class CLibMojo extends SafeMojo { +public final class CLibMojo extends SafeMojo { + + /** + * The directory where the target libraries will be placed. + */ + private static final Path LIB_TARGET_DIR = Paths.get("Lib/native_clib"); + /** * The directory containing sources for using C from EO (e.g. system calls). * @checkstyle MemberNameCheck (8 lines) @@ -27,23 +62,21 @@ public class CLibMojo extends SafeMojo { defaultValue = "${project.basedir}/src/main/c/eo/lib" ) @SuppressWarnings("PMD.UnusedPrivateField") - private File cEoLibDir; - - private final Path LIB_TARGET_DIR = Paths.get("Lib/native_clib"); + private File cLibDir; @Override void exec() throws IOException { - Path target = this.targetDir.toPath().resolve(LIB_TARGET_DIR); + final Path target = this.targetDir.toPath().resolve(CLibMojo.LIB_TARGET_DIR); Files.createDirectories(target); try (DirectoryStream files = - Files.newDirectoryStream(cEoLibDir.toPath(), "*.c")) { + Files.newDirectoryStream(this.cLibDir.toPath(), "*.{c,cpp}")) { boolean contains = false; - for (Path source : files) { + for (final Path source : files) { contains = true; new NativeCLib( source, target.resolve(FilenameUtils.removeExtension(source.getFileName().toString())) - ).build(); + ).compile(); } if (!contains) { throw new IllegalStateException("There are no C sources in the directory"); diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/NativeCLib.java b/eo-maven-plugin/src/main/java/org/eolang/maven/NativeCLib.java index 6f4cc88ee7..c3469138eb 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/NativeCLib.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/NativeCLib.java @@ -1,17 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package org.eolang.maven; import com.yegor256.Jaxec; -import java.io.IOException; import java.nio.file.Path; -import org.eolang.maven.util.JniInfo; +import org.eolang.maven.util.CJniInfo; +/** + * Compiles C native libraries. + * + * @since 0.38 + */ public class NativeCLib { /** * The single C source for building shared library. - * @checkstyle MemberNameCheck (8 lines) */ private final Path source; + /** + * The path to the target file where the resulting native library will be compiled. + */ private final Path target; /** @@ -24,22 +54,25 @@ public NativeCLib(final Path source, final Path target) { this.target = target; } - public void build() { - final String cc = System.getenv("CC"); + /** + * Compiles C native libraries. + */ + public void compile() { + final String ccompiler = System.getenv("CC"); try { new Jaxec( - cc, - String.format("-I%s", JniInfo.COMMON_HEADER), - String.format("-I%s", JniInfo.PLATFORM_SPECIFIC_HEADER), - source.toString(), + ccompiler, + String.format("-I%s", CJniInfo.COMMON_HEADER), + String.format("-I%s", CJniInfo.OS_SPEC_HEADER), + this.source.toString(), "-shared", "-o", - target.toString() + this.target.toString() ).exec(); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException( "An error occurred while compiling the source code of the C native library", - e + ex ); } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/util/CJniInfo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/util/CJniInfo.java new file mode 100644 index 0000000000..4d955fa238 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/util/CJniInfo.java @@ -0,0 +1,87 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang.maven.util; + +import java.util.Locale; +import java.util.Map; +import org.cactoos.map.MapEntry; +import org.cactoos.map.MapOf; + +/** + * All the information you need to build native C library that can be used with JNI. + * + * @since 0.38 + */ +public final class CJniInfo { + /** + * OS name. + */ + private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); + + /** + * Correspondence of OS and directory with platform specific JVM headers. + */ + private static final Map OS_TO_DIRECTORY = new MapOf<>( + new MapEntry<>("linux", "linux"), + new MapEntry<>("mac", "darwin"), + new MapEntry<>("windows", "windows") + ); + + /** + * Java Home. + */ + private static final String JAVA_HOME = System.getProperty("java.home"); + + /** + * The jni.h header, that is common for all systems. + */ + public static final String COMMON_HEADER = String.format("%s/include", CJniInfo.JAVA_HOME); + + /** + * The jni_md.h header, that is platform-specific. + */ + public static final String OS_SPEC_HEADER = String.format( + "%s/%s", CJniInfo.COMMON_HEADER, specificIncludeDirName() + ); + + /** + * Ctor. + */ + private CJniInfo() { } + + /** + * The name of the directory that contains the platform-specific C header for JNI. + * @link Where to find jni_md.h + * @return The directory name. + */ + private static String specificIncludeDirName() { + for (final Map.Entry entry : CJniInfo.OS_TO_DIRECTORY.entrySet()) { + if (CJniInfo.OS_NAME.contains(entry.getKey())) { + return entry.getValue(); + } + } + throw new IllegalStateException("Unavailable OS for native C standard lib usage"); + } +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/util/JniInfo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/util/JniInfo.java deleted file mode 100644 index a2c9a42c5e..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/util/JniInfo.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.eolang.maven.util; - -import java.util.Locale; -import java.util.Map; -import org.cactoos.map.MapEntry; -import org.cactoos.map.MapOf; - -public final class JniInfo { - /** - * OS name. - */ - private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); - - private static final Map OS_TO_DIRECTORY = new MapOf<>( - new MapEntry<>("linux", "linux"), - new MapEntry<>("mac", "darwin"), - new MapEntry<>("windows", "windows") - ); - - private static final String JAVA_HOME = System.getProperty("java.home"); - - public static final String COMMON_HEADER = String.format("%s/include", JAVA_HOME); - - public static final String PLATFORM_SPECIFIC_HEADER = String.format( - "%s/%s", COMMON_HEADER, specificIncludeDirName() - ); - - private JniInfo() {} - - /** - * The name of the directory that contains the platform-specific C header for JNI. - * @link Where to find jni_md.h - */ - private static String specificIncludeDirName() { - for (Map.Entry entry : OS_TO_DIRECTORY.entrySet()) { - if (JniInfo.OS_NAME.contains(entry.getKey())) { - return entry.getValue(); - } - } - throw new IllegalStateException("Unavailable OS for native C standard lib usage"); - } -} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CLibMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CLibMojoTest.java index 00f1b6ced9..ba552f0bcd 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CLibMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CLibMojoTest.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package org.eolang.maven; import java.io.File; @@ -8,7 +32,17 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -class CLibMojoTest { +/** + * Test case for {@link org.eolang.maven.CLibMojo}. + * + * @since 0.38 + */ +final class CLibMojoTest { + /** + * {@link CLibMojo} mojo parameter that holds path to folder with C library sources. + */ + private static final String CLIB_PARAMETER = "cLibDir"; + @Test void executesWithoutErrors(@TempDir final Path temp) { Assertions.assertDoesNotThrow( @@ -18,13 +52,12 @@ void executesWithoutErrors(@TempDir final Path temp) { } @Test - void throwsExceptionOnEmptySourceDir(@TempDir final Path temp) throws IOException { - final Path empty = CLibMojoTest.createEmptyDirectory(temp, "empty"); + void throwsExceptionOnEmptySourceDir(@TempDir final Path temp) { Assertions.assertThrows( IllegalStateException.class, () -> new FakeMaven(temp).with( - "cEoLibDir", - new File(empty.toString()) + CLibMojoTest.CLIB_PARAMETER, + new File(temp.toString()) ).execute(CLibMojo.class), "An exception should be thrown due to absence of any files in given directory" ); @@ -32,13 +65,14 @@ void throwsExceptionOnEmptySourceDir(@TempDir final Path temp) throws IOExceptio @Test void throwsExceptionOnAppropriateSourceAbsence(@TempDir final Path temp) throws IOException { - final Path dir = CLibMojoTest.createEmptyDirectory(temp, "empty"); - Files.createFile(dir.resolve("source.java")); + final String source = "empty.java"; + new File(temp.toString(), source); + Files.write(temp.resolve(source), "".getBytes()); Assertions.assertThrows( IllegalStateException.class, () -> new FakeMaven(temp).with( - "cEoLibDir", - new File(dir.toString()) + CLibMojoTest.CLIB_PARAMETER, + new File(temp.toString()) ).execute(CLibMojo.class), "An exception should be thrown due to absence of C sources in directory" ); @@ -46,12 +80,13 @@ void throwsExceptionOnAppropriateSourceAbsence(@TempDir final Path temp) throws @Test void executesWithoutErrorsOnEmptyCSource(@TempDir final Path temp) throws IOException { - final Path dir = CLibMojoTest.createEmptyDirectory(temp, "empty"); - Files.createFile(dir.resolve("source.c")); + final String source = "empty.c"; + new File(temp.toString(), source); + Files.write(temp.resolve(source), "".getBytes()); Assertions.assertDoesNotThrow( () -> new FakeMaven(temp).with( - "cEoLibDir", - new File(dir.toString()) + CLibMojoTest.CLIB_PARAMETER, + new File(temp.toString()) ).execute(CLibMojo.class), "Exception shouldn't been thrown" ); @@ -59,13 +94,13 @@ void executesWithoutErrorsOnEmptyCSource(@TempDir final Path temp) throws IOExce @Test void executesWithoutErrorsOnValidCSource(@TempDir final Path temp) throws IOException { - final Path dir = CLibMojoTest.createEmptyDirectory(temp, "empty"); - Files.createFile(dir.resolve("source.c")); - Files.write(dir.resolve("source.c"), "void empty();".getBytes()); + final String source = "valid.c"; + new File(temp.toString(), source); + Files.write(temp.resolve(source), "void empty();".getBytes()); Assertions.assertDoesNotThrow( () -> new FakeMaven(temp).with( - "cEoLibDir", - new File(dir.toString()) + CLibMojoTest.CLIB_PARAMETER, + new File(temp.toString()) ).execute(CLibMojo.class), "Exception shouldn't been thrown" ); @@ -73,25 +108,16 @@ void executesWithoutErrorsOnValidCSource(@TempDir final Path temp) throws IOExce @Test void throwsExceptionOnInvalidCSource(@TempDir final Path temp) throws IOException { - final Path dir = CLibMojoTest.createEmptyDirectory(temp, "empty"); - Files.createFile(dir.resolve("source.c")); - Files.write(dir.resolve("source.c"), "INVLAID".getBytes()); + final String source = "invalid.c"; + new File(temp.toString(), source); + Files.write(temp.resolve(source), "INVLAID".getBytes()); Assertions.assertThrows( IllegalStateException.class, () -> new FakeMaven(temp).with( - "cEoLibDir", - new File(dir.toString()) + CLibMojoTest.CLIB_PARAMETER, + new File(temp.toString()) ).execute(CLibMojo.class), "Exception shouldn't been thrown" ); } - - private static Path createEmptyDirectory(Path dir, String name) throws IOException { - if (!Files.exists(dir) || !Files.isDirectory(dir)) { - throw new IllegalArgumentException("The specified path is not an existing directory."); - } - final Path empty = dir.resolve(name); - Files.createDirectories(empty); - return empty; - } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java index 958fd81b40..961fe610fb 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java @@ -246,7 +246,7 @@ public FakeMaven execute(final Class mojo) throws IO new File("../eo-runtime/src/main/rust/eo") ); this.params.putIfAbsent( - "cEoLibDir", + "cLibDir", new File("../eo-runtime/src/main/c/eo/lib") ); this.params.putIfAbsent("namesDir", this.generatedPath().resolve("names").toFile()); diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/JniStub.java b/eo-maven-plugin/src/test/java/org/eolang/maven/JniStub.java new file mode 100644 index 0000000000..97e1082c94 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/JniStub.java @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang.maven; + +/** + * Stub class to test JNI behaviour. + * + * @since 0.38 + */ +@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"}) +public final class JniStub { + /** + * Ctor. + */ + private JniStub() { } + + /** + * Returns it's parameter. + * @param param The parameter that will be returned. + * @return It's parameter. + */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") + public static native int returnParam(int param); +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/NativeCLibTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/NativeCLibTest.java index a3419ef101..1810e99c3f 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/NativeCLibTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/NativeCLibTest.java @@ -1,15 +1,42 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package org.eolang.maven; -import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import org.apache.commons.io.FilenameUtils; -import org.eolang.maven.clib.JniStub; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -class NativeCLibTest { +/** + * Test case for {@link org.eolang.maven.NativeCLib}. + * + * @since 0.38 + */ +final class NativeCLibTest { /** * Folder with source C files for tests. */ @@ -26,20 +53,23 @@ class NativeCLibTest { private static final String COMPILE_ERROR = "compile-error.c"; @Test - public void compilesCorrectSource(@TempDir final Path temp) { + void compilesCorrectSource(@TempDir final Path temp) { Assertions.assertDoesNotThrow( () -> new NativeCLib( - SRC.resolve(CORRECT_SOURCE), - temp.resolve(FilenameUtils.removeExtension(CORRECT_SOURCE)) - ).build(), + NativeCLibTest.SRC.resolve(NativeCLibTest.CORRECT_SOURCE), + temp.resolve(FilenameUtils.removeExtension(NativeCLibTest.CORRECT_SOURCE)) + ).compile(), "Exception shouldn't been thrown while compiling native library" ); } @Test - public void loadsCorrectSource(@TempDir final Path temp) { - String target = FilenameUtils.removeExtension(CORRECT_SOURCE); - new NativeCLib(SRC.resolve(CORRECT_SOURCE), temp.resolve(target)).build(); + void loadsCorrectSource(@TempDir final Path temp) { + final String target = FilenameUtils.removeExtension(NativeCLibTest.CORRECT_SOURCE); + new NativeCLib( + NativeCLibTest.SRC.resolve(NativeCLibTest.CORRECT_SOURCE), + temp.resolve(target) + ).compile(); Assertions.assertDoesNotThrow( () -> System.load(temp.resolve(target).toString()), "Exception shouldn't been thrown while loading native library" @@ -47,10 +77,13 @@ public void loadsCorrectSource(@TempDir final Path temp) { } @Test - public void runsCompiledCorrectSource(@TempDir final Path temp) { - int value = 10; - String target = FilenameUtils.removeExtension(CORRECT_SOURCE); - new NativeCLib(SRC.resolve(CORRECT_SOURCE), temp.resolve(target)).build(); + void runsCompiledCorrectSource(@TempDir final Path temp) { + final int value = 10; + final String target = FilenameUtils.removeExtension(NativeCLibTest.CORRECT_SOURCE); + new NativeCLib( + NativeCLibTest.SRC.resolve(NativeCLibTest.CORRECT_SOURCE), + temp.resolve(target) + ).compile(); System.load(temp.resolve(target).toString()); Assertions.assertDoesNotThrow( () -> JniStub.returnParam(value), @@ -64,14 +97,14 @@ public void runsCompiledCorrectSource(@TempDir final Path temp) { } @Test - public void failsOnSourceCompilationError(@TempDir final Path temp) { + void failsOnSourceCompilationError(@TempDir final Path temp) { Assertions.assertThrows( IllegalArgumentException.class, () -> new NativeCLib( - SRC.resolve(COMPILE_ERROR), - temp.resolve(FilenameUtils.removeExtension(COMPILE_ERROR)) - ).build(), + NativeCLibTest.SRC.resolve(NativeCLibTest.COMPILE_ERROR), + temp.resolve(FilenameUtils.removeExtension(NativeCLibTest.COMPILE_ERROR)) + ).compile(), "Exception shouldn't been thrown while compiling native library" ); } -} \ No newline at end of file +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/clib/JniStub.java b/eo-maven-plugin/src/test/java/org/eolang/maven/clib/JniStub.java deleted file mode 100644 index 7a04c361b6..0000000000 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/clib/JniStub.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.eolang.maven.clib; - -public final class JniStub { - public static native int returnParam(int param); -} diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/c-sources/correct.c b/eo-maven-plugin/src/test/resources/org/eolang/maven/c-sources/correct.c index f6a4673b78..6399e46356 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/c-sources/correct.c +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/c-sources/correct.c @@ -1,6 +1,6 @@ #include -JNIEXPORT jint JNICALL Java_org_eolang_maven_clib_JniStub_returnParam +JNIEXPORT jint JNICALL Java_org_eolang_maven_JniStub_returnParam (JNIEnv* env, jclass klass, jint param) { return param; } \ No newline at end of file