17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Method;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.net.URL;
30 import java.net.URLClassLoader;
31 import java.nicl.metadata.Header;
32 import java.nicl.metadata.LibraryDependencies;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.nio.file.Files;
36 import java.util.Arrays;
37 import java.util.Optional;
38 import java.util.spi.ToolProvider;
39 import org.testng.annotations.Test;
40
41 import static org.testng.Assert.assertEquals;
42 import static org.testng.Assert.assertNotEquals;
43 import static org.testng.Assert.assertNotNull;
44 import static org.testng.Assert.assertNull;
45 import static org.testng.Assert.assertTrue;
46 import static org.testng.Assert.assertFalse;
47
48 /*
49 * @test
50 * @modules jdk.jextract
51 * @build JextractToolProviderTest
52 * @run testng/othervm -Duser.language=en JextractToolProviderTest
53 */
54 public class JextractToolProviderTest {
55 private static final ToolProvider JEXTRACT_TOOL = ToolProvider.findFirst("jextract")
56 .orElseThrow(() ->
359 filter(n -> n.equals("IntOrFloat")).
360 findFirst().isPresent();
361 assertTrue(found, "uniondecl.IntOrFloat not found");
362 } finally {
363 deleteFile(uniondeclJar);
364 }
365 }
366
367 private void checkIntField(Class<?> cls, String name, int value) {
368 Field field = findField(cls, name);
369 assertNotNull(field);
370 assertEquals(field.getType(), int.class);
371 try {
372 assertEquals((int)field.get(null), value);
373 } catch (Exception exp) {
374 System.err.println(exp);
375 assertTrue(false, "should not reach here");
376 }
377 }
378
379 @Test
380 public void testAnonymousEnum() {
381 Path anonenumJar = getOutputFilePath("anonenum.jar");
382 deleteFile(anonenumJar);
383 Path anonenumH = getInputFilePath("anonenum.h");
384 try {
385 checkSuccess(null, "-o", anonenumJar.toString(), anonenumH.toString());
386 Class<?> anonenumCls = loadClass("anonenum", anonenumJar);
387 assertNotNull(anonenumCls);
388 // the nested type for anonymous enum has name starting with "enum__anonymous_at"
389 // followed by full path name of header file + line + column numbers. Any non-ident
390 // char replaced by "_". But we test only the start pattern here.
391 Optional<Class<?>> optEnumCls = Arrays.stream(anonenumCls.getClasses()).
392 filter(c -> c.getSimpleName().startsWith("enum__anonymous_at")).
393 findFirst();
394 assertTrue(optEnumCls.isPresent());
395 Class<?> enumCls = optEnumCls.get();
396 checkIntField(enumCls, "RED", 0xff0000);
397 checkIntField(enumCls, "GREEN", 0x00ff00);
398 checkIntField(enumCls, "BLUE", 0x0000ff);
399 } finally {
400 deleteFile(anonenumJar);
401 }
402 }
403
404 @Test
405 public void testExcludeSymbols() {
406 Path helloJar = getOutputFilePath("hello.jar");
407 deleteFile(helloJar);
408 Path helloH = getInputFilePath("hello.h");
409 checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
410 try {
411 Class<?> cls = loadClass("hello", helloJar);
412 // check a method for "void func()"
413 assertNotNull(findMethod(cls, "func", Object[].class));
414 // check a method for "void junk()"
415 assertNotNull(findMethod(cls, "junk", Object[].class));
416 } finally {
417 deleteFile(helloJar);
418 }
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Method;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.net.URL;
30 import java.net.URLClassLoader;
31 import java.nicl.metadata.Header;
32 import java.nicl.metadata.LibraryDependencies;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.nio.file.Files;
36 import java.util.Arrays;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.spi.ToolProvider;
40 import org.testng.annotations.Test;
41
42 import static org.testng.Assert.assertEquals;
43 import static org.testng.Assert.assertNotEquals;
44 import static org.testng.Assert.assertNotNull;
45 import static org.testng.Assert.assertNull;
46 import static org.testng.Assert.assertTrue;
47 import static org.testng.Assert.assertFalse;
48
49 /*
50 * @test
51 * @modules jdk.jextract
52 * @build JextractToolProviderTest
53 * @run testng/othervm -Duser.language=en JextractToolProviderTest
54 */
55 public class JextractToolProviderTest {
56 private static final ToolProvider JEXTRACT_TOOL = ToolProvider.findFirst("jextract")
57 .orElseThrow(() ->
360 filter(n -> n.equals("IntOrFloat")).
361 findFirst().isPresent();
362 assertTrue(found, "uniondecl.IntOrFloat not found");
363 } finally {
364 deleteFile(uniondeclJar);
365 }
366 }
367
368 private void checkIntField(Class<?> cls, String name, int value) {
369 Field field = findField(cls, name);
370 assertNotNull(field);
371 assertEquals(field.getType(), int.class);
372 try {
373 assertEquals((int)field.get(null), value);
374 } catch (Exception exp) {
375 System.err.println(exp);
376 assertTrue(false, "should not reach here");
377 }
378 }
379
380 private Class<?> findClass(Class<?>[] clz, String name) {
381 for (Class<?> cls: clz) {
382 if (cls.getSimpleName().equals(name)) {
383 return cls;
384 }
385 }
386 return null;
387 }
388
389 private void testEnumValue(Class<?> enumCls, Map<String, Integer> values) {
390 values.entrySet().stream().
391 forEach(e -> checkIntField(enumCls, e.getKey(), e.getValue()));
392 }
393
394 @Test
395 public void testAnonymousEnum() {
396 Path anonenumJar = getOutputFilePath("anonenum.jar");
397 deleteFile(anonenumJar);
398 Path anonenumH = getInputFilePath("anonenum.h");
399 try {
400 checkSuccess(null, "-o", anonenumJar.toString(), anonenumH.toString());
401 Class<?> anonenumCls = loadClass("anonenum", anonenumJar);
402 assertNotNull(anonenumCls);
403 checkIntField(anonenumCls, "RED", 0xff0000);
404 checkIntField(anonenumCls, "GREEN", 0x00ff00);
405 checkIntField(anonenumCls, "BLUE", 0x0000ff);
406
407 Class<?> enumClz[] = anonenumCls.getClasses();
408 assert(enumClz.length >= 3);
409
410 Class<?> enumCls = findClass(enumClz, "codetype_t");
411 assertNotNull(enumCls);
412 testEnumValue(enumCls, Map.of(
413 "Java", 0,
414 "C", 1,
415 "CPP", 2,
416 "Python", 3,
417 "Ruby", 4));
418
419 enumCls = findClass(enumClz, "SIZE");
420 assertNotNull(enumCls);
421 testEnumValue(enumCls, Map.of(
422 "XS", 0,
423 "S", 1,
424 "M", 2,
425 "L", 3,
426 "XL", 4,
427 "XXL", 5));
428
429 enumCls = findClass(enumClz, "temp");
430 assertNotNull(enumCls);
431 testEnumValue(enumCls, Map.of(
432 "ONE", 1,
433 "TWO", 2));
434
435 enumCls = findClass(enumClz, "temp_t");
436 assertNull(enumCls);
437 } finally {
438 deleteFile(anonenumJar);
439 }
440 }
441
442 @Test
443 public void testExcludeSymbols() {
444 Path helloJar = getOutputFilePath("hello.jar");
445 deleteFile(helloJar);
446 Path helloH = getInputFilePath("hello.h");
447 checkSuccess(null, "-o", helloJar.toString(), helloH.toString());
448 try {
449 Class<?> cls = loadClass("hello", helloJar);
450 // check a method for "void func()"
451 assertNotNull(findMethod(cls, "func", Object[].class));
452 // check a method for "void junk()"
453 assertNotNull(findMethod(cls, "junk", Object[].class));
454 } finally {
455 deleteFile(helloJar);
456 }
|