Skip to content

Commit 0cd4c91

Browse files
committed
Add test rule and property for better handling JNA if not available or in need of custom extension.
1 parent 9c03f22 commit 0cd4c91

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

byte-buddy-agent/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<packages.list>net.bytebuddy.agent,net.bytebuddy.agent.utility.nullability</packages.list>
1919
<native.compiler.32>i686-w64-mingw32-gcc</native.compiler.32>
2020
<native.compiler.64>x86_64-w64-mingw32-gcc</native.compiler.64>
21+
<net.bytebuddy.test.jnapath/>
2122
</properties>
2223

2324
<name>Byte Buddy agent</name>
@@ -89,6 +90,15 @@
8990
</resource>
9091
</resources>
9192
<plugins>
93+
<!-- Allow for specifying a custom library path for JNA. -->
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-surefire-plugin</artifactId>
97+
<version>${version.plugin.surefire}</version>
98+
<configuration combine.children="append">
99+
<argLine>-Djna.library.path=${net.bytebuddy.test.jnapath}</argLine>
100+
</configuration>
101+
</plugin>
92102
<!-- Create manifest file which is required for creating an OSGi bundle. -->
93103
<plugin>
94104
<groupId>org.apache.maven.plugins</groupId>

byte-buddy-agent/src/test/java/net/bytebuddy/agent/VirtualMachineAttachmentTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package net.bytebuddy.agent;
22

33
import net.bytebuddy.dynamic.ClassFileLocator;
4+
import net.bytebuddy.test.utility.JnaRule;
45
import org.junit.After;
56
import org.junit.Before;
7+
import org.junit.Rule;
68
import org.junit.Test;
9+
import org.junit.rules.TestRule;
710

811
import java.io.File;
912
import java.io.FileOutputStream;
@@ -20,6 +23,9 @@ public class VirtualMachineAttachmentTest {
2023

2124
private static final String FOO = "foo";
2225

26+
@Rule
27+
public TestRule jnaRule = new JnaRule();
28+
2329
private File agent;
2430

2531
@Before
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.bytebuddy.test.utility;
2+
3+
import com.sun.jna.Library;
4+
import com.sun.jna.Native;
5+
import com.sun.jna.Platform;
6+
import org.junit.rules.TestRule;
7+
import org.junit.runner.Description;
8+
import org.junit.runners.model.Statement;
9+
10+
import java.util.logging.Logger;
11+
12+
public class JnaRule implements TestRule {
13+
14+
private final boolean available;
15+
16+
@SuppressWarnings("deprecation")
17+
public JnaRule() {
18+
boolean available;
19+
try {
20+
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
21+
available = true;
22+
} catch (Throwable ignored) {
23+
available = false;
24+
}
25+
this.available = available;
26+
}
27+
28+
public Statement apply(Statement base, Description description) {
29+
return available
30+
? base
31+
: new NoOpStatement();
32+
}
33+
34+
private static class NoOpStatement extends Statement {
35+
36+
public void evaluate() {
37+
Logger.getLogger("net.bytebuddy").info("Omitting test case: JNA not available");
38+
}
39+
}
40+
41+
public interface CLibrary extends Library {
42+
43+
@SuppressWarnings("unused")
44+
void printf(String format, Object... args);
45+
}
46+
}

0 commit comments

Comments
 (0)