< prev index next >

test/jdk/com/sun/tools/jextract/Runner.java

Print this page




  68  * @modules java.base/jdk.internal.org.objectweb.asm
  69  * @modules java.base/jdk.internal.org.objectweb.asm.tree
  70  * @modules java.base/jdk.internal.org.objectweb.asm.util
  71  * @modules jdk.jextract/com.sun.tools.jextract
  72  * @build InMemoryFileManager
  73  * @run testng Runner
  74  */
  75 public class Runner {
  76     private final Path nativeSrc;
  77     private final Path[] javaSrcFiles;
  78     private final Context ctx;
  79     private final String pkg;
  80 
  81     private InMemoryFileManager<StandardJavaFileManager> mfm;
  82     private ClassLoader expectedCL;
  83     private Map<String, byte[]> actualClz;
  84     private ClassLoader actualCL;
  85     private Object[][] clz_data;
  86 
  87     public Runner(Path nativeSrc, String pkg, Path[] javaSrcFiles) {
  88         this.ctx = new Context();
  89         this.nativeSrc = nativeSrc;
  90         this.pkg = pkg;
  91         this.javaSrcFiles = javaSrcFiles;
  92     }
  93 
  94     private Map<String, byte[]> extract(String pkg) throws IOException {
  95         if (!Files.isReadable(nativeSrc)) {
  96             throw new IllegalArgumentException("Cannot read the file: " + nativeSrc);
  97         }
  98         Path p = nativeSrc.toAbsolutePath();
  99         ctx.usePackageForFolder(p.getParent(), pkg);
 100         ctx.addSource(p);
 101         ctx.parse();
 102         return ctx.collectClasses(pkg);
 103     }
 104 
 105     private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
 106         JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
 107         StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
 108         InMemoryFileManager<StandardJavaFileManager> fm = new InMemoryFileManager<>(sfm);


 111         task.call();
 112         return fm;
 113     }
 114 
 115     @Test
 116     public void testJarManifest() throws IOException {
 117         // Get the jar
 118         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 119         ctx.collectJarFile(new JarOutputStream(bos), pkg);
 120 
 121         System.out.println("Jar built, verifying...");
 122         JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
 123 
 124         // List all classes in the jar
 125         Set<String> files = new HashSet<>();
 126         for (JarEntry e = jis.getNextJarEntry(); e != null; e = jis.getNextJarEntry()) {
 127             if (e.isDirectory()) {
 128                 continue;
 129             }
 130             String name = e.getName();

 131             if (! name.endsWith(".class")) {
 132                 // Should not have file not class files
 133                 System.err.println("Warning: unexpected file " + name);
 134             }
 135             name = name.substring(0, name.length() - 6);
 136             files.add(name.replace(File.separatorChar, '.'));
 137         }

 138 
 139         assertEquals(files, mfm.listClasses());
 140     }
 141 
 142     private void verifyNativeLocation(NativeLocation actual, NativeLocation expected) {
 143         // Only check the filename, not full path
 144         assertNotNull(actual);
 145         assertNotNull(expected);
 146         assertEquals(Paths.get(actual.file()).getFileName(),
 147                      Paths.get(expected.file()).getFileName());
 148         assertEquals(actual.line(), expected.line());
 149         assertEquals(actual.column(), expected.column());
 150     }
 151 
 152     private void verifyMethodAnnotation(Method actual, Method expected) {
 153         Annotation[] aa = actual.getAnnotations();
 154         Annotation[] ea = expected.getAnnotations();
 155 
 156         for (Annotation a: ea) {
 157             if (a instanceof NativeLocation) {




  68  * @modules java.base/jdk.internal.org.objectweb.asm
  69  * @modules java.base/jdk.internal.org.objectweb.asm.tree
  70  * @modules java.base/jdk.internal.org.objectweb.asm.util
  71  * @modules jdk.jextract/com.sun.tools.jextract
  72  * @build InMemoryFileManager
  73  * @run testng Runner
  74  */
  75 public class Runner {
  76     private final Path nativeSrc;
  77     private final Path[] javaSrcFiles;
  78     private final Context ctx;
  79     private final String pkg;
  80 
  81     private InMemoryFileManager<StandardJavaFileManager> mfm;
  82     private ClassLoader expectedCL;
  83     private Map<String, byte[]> actualClz;
  84     private ClassLoader actualCL;
  85     private Object[][] clz_data;
  86 
  87     public Runner(Path nativeSrc, String pkg, Path[] javaSrcFiles) {
  88         this.ctx = new Context(new String[0]);
  89         this.nativeSrc = nativeSrc;
  90         this.pkg = pkg;
  91         this.javaSrcFiles = javaSrcFiles;
  92     }
  93 
  94     private Map<String, byte[]> extract(String pkg) throws IOException {
  95         if (!Files.isReadable(nativeSrc)) {
  96             throw new IllegalArgumentException("Cannot read the file: " + nativeSrc);
  97         }
  98         Path p = nativeSrc.toAbsolutePath();
  99         ctx.usePackageForFolder(p.getParent(), pkg);
 100         ctx.addSource(p);
 101         ctx.parse();
 102         return ctx.collectClasses(pkg);
 103     }
 104 
 105     private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
 106         JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
 107         StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
 108         InMemoryFileManager<StandardJavaFileManager> fm = new InMemoryFileManager<>(sfm);


 111         task.call();
 112         return fm;
 113     }
 114 
 115     @Test
 116     public void testJarManifest() throws IOException {
 117         // Get the jar
 118         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 119         ctx.collectJarFile(new JarOutputStream(bos), pkg);
 120 
 121         System.out.println("Jar built, verifying...");
 122         JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
 123 
 124         // List all classes in the jar
 125         Set<String> files = new HashSet<>();
 126         for (JarEntry e = jis.getNextJarEntry(); e != null; e = jis.getNextJarEntry()) {
 127             if (e.isDirectory()) {
 128                 continue;
 129             }
 130             String name = e.getName();
 131             if (! name.endsWith(".properties")) {
 132                 if (! name.endsWith(".class")) {
 133                     // Should not have file not class files
 134                     System.err.println("Warning: unexpected file " + name);
 135                 }
 136                 name = name.substring(0, name.length() - 6);
 137                 files.add(name.replace(File.separatorChar, '.'));
 138             }
 139         }
 140 
 141         assertEquals(files, mfm.listClasses());
 142     }
 143 
 144     private void verifyNativeLocation(NativeLocation actual, NativeLocation expected) {
 145         // Only check the filename, not full path
 146         assertNotNull(actual);
 147         assertNotNull(expected);
 148         assertEquals(Paths.get(actual.file()).getFileName(),
 149                      Paths.get(expected.file()).getFileName());
 150         assertEquals(actual.line(), expected.line());
 151         assertEquals(actual.column(), expected.column());
 152     }
 153 
 154     private void verifyMethodAnnotation(Method actual, Method expected) {
 155         Annotation[] aa = actual.getAnnotations();
 156         Annotation[] ea = expected.getAnnotations();
 157 
 158         for (Annotation a: ea) {
 159             if (a instanceof NativeLocation) {


< prev index next >