< prev index next >
test/jdk/com/sun/tools/jextract/JextractToolProviderTest.java
Print this page
@@ -21,18 +21,20 @@
* questions.
*/
import org.testng.annotations.Test;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.foreign.annotations.NativeHeader;
import java.foreign.annotations.NativeLocation;
import java.foreign.memory.Pointer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
+import java.util.stream.Stream;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
@@ -45,29 +47,30 @@
* @run testng/othervm -Duser.language=en JextractToolProviderTest
*/
public class JextractToolProviderTest extends JextractToolRunner {
@Test
public void testHelp() {
- checkFailure(null); // no options
- checkSuccess(null, "--help");
- checkSuccess(null, "-h");
- checkSuccess(null, "-?");
+ run().checkFailure(); // no options
+ run("--help").checkSuccess();
+ run("-h").checkSuccess();
+ run("-?").checkSuccess();
}
// error for non-existent header file
@Test
public void testNonExistentHeader() {
- checkFailure("Cannot open header file", "--dry-run",
- getInputFilePath("non_existent.h").toString());
+ run("--dry-run", getInputFilePath("non_existent.h").toString())
+ .checkFailure()
+ .checkContainsOutput("Cannot open header file");
}
@Test
public void testDryRun() {
// only dry-run, don't produce any output
Path simpleJar = getOutputFilePath("simple.jar");
deleteFile(simpleJar);
- checkSuccess(null, "--dry-run", getInputFilePath("simple.h").toString());
+ run("--dry-run", getInputFilePath("simple.h").toString()).checkSuccess();
try {
assertFalse(Files.isRegularFile(simpleJar));
} finally {
deleteFile(simpleJar);
}
@@ -76,12 +79,12 @@
@Test
public void testOutputFileOption() {
// simple output file check
Path simpleJar = getOutputFilePath("simple.jar");
deleteFile(simpleJar);
- checkSuccess(null, "-o", simpleJar.toString(),
- getInputFilePath("simple.h").toString());
+ run("-o", simpleJar.toString(),
+ getInputFilePath("simple.h").toString()).checkSuccess();
try {
assertTrue(Files.isRegularFile(simpleJar));
} finally {
deleteFile(simpleJar);
}
@@ -90,11 +93,11 @@
@Test
public void testOutputClass() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
+ run("-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check NativeHeader annotation
NativeHeader header = cls.getAnnotation(NativeHeader.class);
assertNotNull(header);
@@ -110,11 +113,11 @@
private void testTargetPackage(String targetPkgOption) {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, targetPkgOption, "com.acme", "-o", helloJar.toString(), helloH.toString());
+ run(targetPkgOption, "com.acme", "-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("com.acme.hello", helloJar);
// check NativeHeader annotation
NativeHeader header = cls.getAnnotation(NativeHeader.class);
assertNotNull(header);
@@ -138,29 +141,22 @@
}
private void testPackageMapping(String pkgMapOption) {
Path worldJar = getOutputFilePath("world.jar");
deleteFile(worldJar);
- Path mytypesJar = getOutputFilePath("mytypes.jar");
- deleteFile(mytypesJar);
-
Path worldH = getInputFilePath("world.h");
Path include = getInputFilePath("include");
- // generate jar for mytypes.h
- checkSuccess(null, "-t", "com.acme", "-o", mytypesJar.toString(),
- include.resolve("mytypes.h").toString());
// world.h include mytypes.h, use appropriate package for stuff from mytypes.h
- checkSuccess(null, "-I", include.toString(), pkgMapOption, include.toString() + "=com.acme",
- "-o", worldJar.toString(), worldH.toString());
+ run("-I", include.toString(), pkgMapOption, include.toString() + "=com.acme",
+ "-o", worldJar.toString(), worldH.toString()).checkSuccess();
try {
- Class<?> cls = loadClass("world", worldJar, mytypesJar);
+ Class<?> cls = loadClass("world", worldJar);
Method m = findFirstMethod(cls, "distance");
Class<?>[] params = m.getParameterTypes();
assertEquals(params[0].getName(), "com.acme.mytypes$Point");
} finally {
deleteFile(worldJar);
- deleteFile(mytypesJar);
}
}
@Test
public void testPackageDirMappingOption() {
@@ -172,33 +168,38 @@
testPackageMapping("--package-map");
}
@Test
public void test_no_input_files() {
- String err = "No input files";
- checkFailure(err, "-L", "foo");
+ run("-L", "foo")
+ .checkContainsOutput("No input files")
+ .checkFailure();
}
@Test
public void test_option_L_without_l() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
Path linkDir = getInputFilePath("libs");
String warning = "WARNING: -L option specified without any -l option";
- checkSuccess(warning, "-L", linkDir.toString(), "-o", helloJar.toString(), helloH.toString());
+ run("-L", linkDir.toString(), "-o", helloJar.toString(), helloH.toString())
+ .checkContainsOutput(warning)
+ .checkSuccess();
}
@Test
public void test_option_rpath_without_l() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
Path rpathDir = getInputFilePath("libs");
String warning = "WARNING: -rpath option specified without any -l option";
try {
- checkSuccess(warning, "-rpath", rpathDir.toString(), "-o", helloJar.toString(), helloH.toString());
+ run("-rpath", rpathDir.toString(), "-o", helloJar.toString(), helloH.toString())
+ .checkContainsOutput(warning)
+ .checkSuccess();
} finally {
deleteFile(helloJar);
}
}
@@ -208,13 +209,15 @@
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
Path rpathDir = getInputFilePath("libs");
String warning = "WARNING: -infer-rpath used in conjunction with explicit -rpath paths";
try {
- checkSuccess(warning, "-rpath", rpathDir.toString(),
+ run("-rpath", rpathDir.toString(),
"-infer-rpath",
- "-o", helloJar.toString(), helloH.toString());
+ "-o", helloJar.toString(), helloH.toString())
+ .checkContainsOutput(warning)
+ .checkSuccess();
} finally {
deleteFile(helloJar);
}
}
@@ -223,13 +226,14 @@
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
String warning = "WARNING: -infer-rpath option specified without any -L option";
try {
- checkSuccess(warning,
- "-infer-rpath",
- "-o", helloJar.toString(), helloH.toString());
+ run("-infer-rpath",
+ "-o", helloJar.toString(), helloH.toString())
+ .checkContainsOutput(warning)
+ .checkSuccess();
} finally {
deleteFile(helloJar);
}
}
@@ -238,25 +242,26 @@
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
String warning = "WARNING: Some library names could not be resolved";
try {
- checkSuccess(warning,
- "-L", "nonExistent",
+ run("-L", "nonExistent",
"-l", "nonExistent",
- "-o", helloJar.toString(), helloH.toString());
+ "-o", helloJar.toString(), helloH.toString())
+ .checkContainsOutput(warning)
+ .checkSuccess();
} finally {
deleteFile(helloJar);
}
}
@Test
public void test_option_l() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, "-l", "hello", "-o", helloJar.toString(), helloH.toString());
+ run("-l", "hello", "-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check that NativeHeader annotation captures -l value
NativeHeader header = cls.getAnnotation(NativeHeader.class);
assertNotNull(header);
@@ -273,12 +278,12 @@
public void test_option_l_and_rpath() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
Path rpathDir = getInputFilePath("libs");
- checkSuccess(null, "-l", "hello", "-rpath", rpathDir.toString(),
- "-o", helloJar.toString(), helloH.toString());
+ run("-l", "hello", "-rpath", rpathDir.toString(),
+ "-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check that NativeHeader annotation captures -l and -rpath values
NativeHeader header = cls.getAnnotation(NativeHeader.class);
assertNotNull(header);
@@ -295,11 +300,11 @@
public void testUnionDeclaration() {
Path uniondeclJar = getOutputFilePath("uniondecl.jar");
deleteFile(uniondeclJar);
Path uniondeclH = getInputFilePath("uniondecl.h");
try {
- checkSuccess(null, "-o", uniondeclJar.toString(), uniondeclH.toString());
+ run("-o", uniondeclJar.toString(), uniondeclH.toString()).checkSuccess();
Class<?> unionCls = loadClass("uniondecl", uniondeclJar);
assertNotNull(unionCls);
boolean found = Arrays.stream(unionCls.getClasses()).
map(Class::getSimpleName).
filter(n -> n.equals("IntOrFloat")).
@@ -322,11 +327,11 @@
public void testAnonymousEnum() {
Path anonenumJar = getOutputFilePath("anonenum.jar");
deleteFile(anonenumJar);
Path anonenumH = getInputFilePath("anonenum.h");
try {
- checkSuccess(null, "-o", anonenumJar.toString(), anonenumH.toString());
+ run("-o", anonenumJar.toString(), anonenumH.toString()).checkSuccess();
Class<?> anonenumCls = loadClass("anonenum", anonenumJar);
assertNotNull(anonenumCls);
testEnumConstGetters(anonenumCls, List.of("RED", "GREEN", "BLUE"));
testEnumConstGetters(anonenumCls, List.of(
"Java", "C", "CPP", "Python", "Ruby"));
@@ -357,11 +362,11 @@
@Test
public void testExcludeSymbols() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
+ run("-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void func()"
assertNotNull(findMethod(cls, "func", Object[].class));
assertNotNull(findMethod(cls, "func2", Object[].class));
@@ -373,11 +378,12 @@
} finally {
deleteFile(helloJar);
}
// try with --exclude-symbols" this time.
- checkSuccess(null, "--exclude-symbols", "junk.*", "-o", helloJar.toString(), helloH.toString());
+ run("--exclude-symbols", "junk.*", "-o", helloJar.toString(), helloH.toString())
+ .checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void func()"
assertNotNull(findMethod(cls, "func", Object[].class));
assertNotNull(findMethod(cls, "func2", Object[].class));
@@ -394,11 +400,11 @@
@Test
public void testIncludeSymbols() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
+ run("-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void func()"
assertNotNull(findMethod(cls, "func", Object[].class));
assertNotNull(findMethod(cls, "func2", Object[].class));
@@ -410,11 +416,11 @@
} finally {
deleteFile(helloJar);
}
// try with --include-symbols" this time.
- checkSuccess(null, "--include-symbols", "junk.*", "-o", helloJar.toString(), helloH.toString());
+ run("--include-symbols", "junk.*", "-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void junk()"
assertNotNull(findMethod(cls, "junk", Object[].class));
assertNotNull(findMethod(cls, "junk2", Object[].class));
@@ -431,11 +437,11 @@
@Test
public void testNoLocations() {
Path simpleJar = getOutputFilePath("simple.jar");
deleteFile(simpleJar);
Path simpleH = getInputFilePath("simple.h");
- checkSuccess(null, "--no-locations", "-o", simpleJar.toString(), simpleH.toString());
+ run("--no-locations", "-o", simpleJar.toString(), simpleH.toString()).checkSuccess();
try {
Class<?> simpleCls = loadClass("simple", simpleJar);
Method func = findFirstMethod(simpleCls, "func");
assertFalse(func.isAnnotationPresent(NativeLocation.class));
Class<?> anonymousCls = loadClass("simple$anonymous", simpleJar);
@@ -448,11 +454,11 @@
@Test
public void testIncludeExcludeSymbols() {
Path helloJar = getOutputFilePath("hello.jar");
deleteFile(helloJar);
Path helloH = getInputFilePath("hello.h");
- checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
+ run("-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void func()"
assertNotNull(findMethod(cls, "func", Object[].class));
assertNotNull(findMethod(cls, "func2", Object[].class));
@@ -464,12 +470,12 @@
} finally {
deleteFile(helloJar);
}
// try with --include-symbols" this time.
- checkSuccess(null, "--include-symbols", "junk.*", "--exclude-symbols", "junk3",
- "-o", helloJar.toString(), helloH.toString());
+ run("--include-symbols", "junk.*", "--exclude-symbols", "junk3",
+ "-o", helloJar.toString(), helloH.toString()).checkSuccess();
try {
Class<?> cls = loadClass("hello", helloJar);
// check a method for "void junk()"
assertNotNull(findMethod(cls, "junk", Object[].class));
assertNotNull(findMethod(cls, "junk2", Object[].class));
@@ -488,11 +494,11 @@
public void testNestedStructsUnions() {
Path nestedJar = getOutputFilePath("nested.jar");
deleteFile(nestedJar);
Path nestedH = getInputFilePath("nested.h");
try {
- checkSuccess(null, "-o", nestedJar.toString(), nestedH.toString());
+ run("-o", nestedJar.toString(), nestedH.toString()).checkSuccess();
Class<?> headerCls = loadClass("nested", nestedJar);
assertNotNull(headerCls);
Class<?> fooCls = loadClass("nested$Foo", nestedJar);
assertNotNull(fooCls);
@@ -575,11 +581,11 @@
public void testAnonymousStructTypeGlobalVar() {
Path elaboratedTypeJar = getOutputFilePath("elaboratedtype.jar");
deleteFile(elaboratedTypeJar);
Path elaboratedTypeH = getInputFilePath("elaboratedtype.h");
try {
- checkSuccess(null, "-o", elaboratedTypeJar.toString(), elaboratedTypeH.toString());
+ run("-o", elaboratedTypeJar.toString(), elaboratedTypeH.toString()).checkSuccess();
Class<?> headerCls = loadClass("elaboratedtype", elaboratedTypeJar);
assertNotNull(findGlobalVariableGet(headerCls, "point"));
assertNotNull(findGlobalVariableGet(headerCls, "long_or_int"));
assertNotNull(findMethod(headerCls, "func", Pointer.class));
} finally {
@@ -589,11 +595,11 @@
private void testBuiltinInclude(String name) {
Path fileJar = getOutputFilePath(name + "inc.jar");
deleteFile(fileJar);
Path fileH = getInputFilePath(name + "inc.h");
- checkSuccess(null, "-o", fileJar.toString(), fileH.toString());
+ run("-o", fileJar.toString(), fileH.toString()).checkSuccess();
deleteFile(fileJar);
}
@Test
public void testBuiltinHeader() {
@@ -604,11 +610,11 @@
@Test
public void testGlobalFuncPointerCallback() {
Path globalFuncPointerJar = getOutputFilePath("globalFuncPointer.jar");
deleteFile(globalFuncPointerJar);
Path globalFuncPointerH = getInputFilePath("globalFuncPointer.h");
- checkSuccess(null, "-o", globalFuncPointerJar.toString(), globalFuncPointerH.toString());
+ run("-o", globalFuncPointerJar.toString(), globalFuncPointerH.toString()).checkSuccess();
Class<?> callbackCls = loadClass("globalFuncPointer$FI1", globalFuncPointerJar);
Method callback = findFirstMethod(callbackCls, "fn");
assertNotNull(callback);
assertTrue(callback.isVarArgs());
deleteFile(globalFuncPointerJar);
@@ -617,11 +623,11 @@
@Test
public void testFuncPtrTypedef() {
Path funcPtrTypedefJar = getOutputFilePath("funcPtrTypedef.jar");
deleteFile(funcPtrTypedefJar);
Path funcPtrTypedefH = getInputFilePath("funcPtrTypedef.h");
- checkSuccess(null, "-o", funcPtrTypedefJar.toString(), funcPtrTypedefH.toString());
+ run("-o", funcPtrTypedefJar.toString(), funcPtrTypedefH.toString()).checkSuccess();
// force parsing of class, method
Class<?> headerCls = loadClass("funcPtrTypedef", funcPtrTypedefJar);
Method getter = findFirstMethod(headerCls, "my_function$get");
assertNotNull(getter);
assertNotNull(getter.getGenericParameterTypes());
@@ -631,11 +637,11 @@
@Test
public void testDuplicatedecls() {
Path duplicatedeclsJar = getOutputFilePath("duplicatedecls.jar");
deleteFile(duplicatedeclsJar);
Path duplicatedeclsH = getInputFilePath("duplicatedecls.h");
- checkSuccess(null, "-o", duplicatedeclsJar.toString(), duplicatedeclsH.toString());
+ run("-o", duplicatedeclsJar.toString(), duplicatedeclsH.toString()).checkSuccess();
// load the class to make sure no duplicate methods generated in it
Class<?> headerCls = loadClass("duplicatedecls", duplicatedeclsJar);
assertNotNull(headerCls);
deleteFile(duplicatedeclsJar);
}
< prev index next >