Skip to content

Commit

Permalink
Add test rule and property for better handling JNA if not available o…
Browse files Browse the repository at this point in the history
…r in need of custom extension.
  • Loading branch information
raphw committed Sep 20, 2022
1 parent 9c03f22 commit 0cd4c91
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions byte-buddy-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<packages.list>net.bytebuddy.agent,net.bytebuddy.agent.utility.nullability</packages.list>
<native.compiler.32>i686-w64-mingw32-gcc</native.compiler.32>
<native.compiler.64>x86_64-w64-mingw32-gcc</native.compiler.64>
<net.bytebuddy.test.jnapath/>
</properties>

<name>Byte Buddy agent</name>
Expand Down Expand Up @@ -89,6 +90,15 @@
</resource>
</resources>
<plugins>
<!-- Allow for specifying a custom library path for JNA. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<configuration combine.children="append">
<argLine>-Djna.library.path=${net.bytebuddy.test.jnapath}</argLine>
</configuration>
</plugin>
<!-- Create manifest file which is required for creating an OSGi bundle. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.bytebuddy.agent;

import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.test.utility.JnaRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -20,6 +23,9 @@ public class VirtualMachineAttachmentTest {

private static final String FOO = "foo";

@Rule
public TestRule jnaRule = new JnaRule();

private File agent;

@Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.bytebuddy.test.utility;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.logging.Logger;

public class JnaRule implements TestRule {

private final boolean available;

@SuppressWarnings("deprecation")
public JnaRule() {
boolean available;
try {
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
available = true;
} catch (Throwable ignored) {
available = false;
}
this.available = available;
}

public Statement apply(Statement base, Description description) {
return available
? base
: new NoOpStatement();
}

private static class NoOpStatement extends Statement {

public void evaluate() {
Logger.getLogger("net.bytebuddy").info("Omitting test case: JNA not available");
}
}

public interface CLibrary extends Library {

@SuppressWarnings("unused")
void printf(String format, Object... args);
}
}

0 comments on commit 0cd4c91

Please sign in to comment.