< prev index next >

test/jdk/com/sun/tools/jextract/test8221154/SrcGenTest.java

Print this page




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  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.io.File;
  25 import java.io.IOException;


  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;

  29 import java.util.ArrayList;
  30 import java.util.spi.ToolProvider;
  31 import org.testng.annotations.Test;
  32 import static org.testng.Assert.assertTrue;
  33 
  34 /*
  35  * @test
  36  * @bug 8221154
  37  * @summary jextract should generate java source files

  38  * @run testng SrcGenTest
  39  */
  40 public class SrcGenTest {
  41     private static final ToolProvider JEXTRACT = ToolProvider.findFirst("jextract")
  42             .orElseThrow(() ->
  43                     new RuntimeException("jextract tool not found")
  44             );
  45 
  46     private static final ToolProvider JAVAC = ToolProvider.findFirst("javac")
  47             .orElseThrow(() ->
  48                     new RuntimeException("javac tool not found")
  49             );
  50 
  51     @Test
  52     public void test() throws IOException {
  53         Path inputDir = Paths.get(System.getProperty("test.src", "."));
  54         Path outputDir = Paths.get(System.getProperty("test.classes", "."));
  55         inputDir = inputDir.toAbsolutePath();
  56         outputDir = outputDir.toAbsolutePath();
  57         String pkgName = "test8221154";
  58         Path jarPath = outputDir.resolve(pkgName + ".jar");
  59 
  60         // run jextract with --src-dump-dir option


  81         Files.delete(jarPath);
  82 
  83         Path pkgDir = outputDir.resolve(pkgName);
  84         // compile jextract generated java sources
  85         ArrayList<String> javacOpts = new ArrayList<>();
  86         javacOpts.add("-d");
  87         javacOpts.add(outputDir.toString());
  88         javacOpts.add(pkgDir + File.separator + "srcgentest.java");
  89         javacOpts.add(pkgDir + File.separator + "srcgentest_h.java");
  90         result = JAVAC.run(System.out, System.err, javacOpts.toArray(String[]::new));
  91         if (result != 0) {
  92             throw new RuntimeException(JAVAC.name() + " returns non-zero value");
  93         }
  94 
  95         // sanity checks for .class file existence
  96         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest.class")));
  97         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest$Point.class")));
  98         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest$Color.class")));
  99         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest_h.class")));
 100         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest_h$Color.class")));
















































 101     }
 102 }


   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  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.foreign.memory.Pointer;
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.lang.reflect.Method;
  28 import java.lang.reflect.Modifier;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Arrays;
  33 import java.util.ArrayList;
  34 import java.util.spi.ToolProvider;
  35 import org.testng.annotations.Test;
  36 import static org.testng.Assert.assertTrue;
  37 
  38 /*
  39  * @test
  40  * @bug 8221154
  41  * @summary jextract should generate java source files
  42  * @library ..
  43  * @run testng SrcGenTest
  44  */
  45 public class SrcGenTest extends JextractToolRunner {
  46     private static final ToolProvider JEXTRACT = ToolProvider.findFirst("jextract")
  47             .orElseThrow(() ->
  48                     new RuntimeException("jextract tool not found")
  49             );
  50 
  51     private static final ToolProvider JAVAC = ToolProvider.findFirst("javac")
  52             .orElseThrow(() ->
  53                     new RuntimeException("javac tool not found")
  54             );
  55 
  56     @Test
  57     public void test() throws IOException {
  58         Path inputDir = Paths.get(System.getProperty("test.src", "."));
  59         Path outputDir = Paths.get(System.getProperty("test.classes", "."));
  60         inputDir = inputDir.toAbsolutePath();
  61         outputDir = outputDir.toAbsolutePath();
  62         String pkgName = "test8221154";
  63         Path jarPath = outputDir.resolve(pkgName + ".jar");
  64 
  65         // run jextract with --src-dump-dir option


  86         Files.delete(jarPath);
  87 
  88         Path pkgDir = outputDir.resolve(pkgName);
  89         // compile jextract generated java sources
  90         ArrayList<String> javacOpts = new ArrayList<>();
  91         javacOpts.add("-d");
  92         javacOpts.add(outputDir.toString());
  93         javacOpts.add(pkgDir + File.separator + "srcgentest.java");
  94         javacOpts.add(pkgDir + File.separator + "srcgentest_h.java");
  95         result = JAVAC.run(System.out, System.err, javacOpts.toArray(String[]::new));
  96         if (result != 0) {
  97             throw new RuntimeException(JAVAC.name() + " returns non-zero value");
  98         }
  99 
 100         // sanity checks for .class file existence
 101         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest.class")));
 102         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest$Point.class")));
 103         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest$Color.class")));
 104         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest_h.class")));
 105         assertTrue(Files.isRegularFile(pkgDir.resolve("srcgentest_h$Color.class")));
 106 
 107         checkClasses(outputDir, pkgName);
 108     }
 109 
 110     private void checkClasses(Path outputDir, String pkgName) {
 111         Loader loader = classLoader(outputDir);
 112         Class<?> forwarderCls = loader.loadClass(pkgName + ".srcgentest_h");
 113         assertTrue(forwarderCls != null);
 114 
 115         // check "sum" method
 116         Method sumMethod = findFirstMethod(forwarderCls, "sum");
 117         assertTrue(sumMethod.getReturnType() == int.class);
 118         Class<?>[] sumParamTypes = sumMethod.getParameterTypes();
 119         assertTrue(sumParamTypes.length == 2);
 120         assertTrue(sumParamTypes[0] == int.class);
 121         assertTrue(sumParamTypes[1] == Object[].class);
 122         assertTrue(Modifier.isStatic(sumMethod.getModifiers()));
 123 
 124         // check "x_coord" method
 125         Method xCoordMethod = findFirstMethod(forwarderCls, "x_coord");
 126         assertTrue(xCoordMethod.getReturnType() == int.class);
 127         Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
 128         assertTrue(xCoordParamTypes.length == 1);
 129         assertTrue(xCoordParamTypes[0] == Pointer.class);
 130 
 131         // check "y_coord" method
 132         Method yCoordMethod = findFirstMethod(forwarderCls, "y_coord");
 133         assertTrue(yCoordMethod.getReturnType() == int.class);
 134         Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
 135         assertTrue(yCoordParamTypes.length == 1);
 136         assertTrue(yCoordParamTypes[0] == Pointer.class);
 137 
 138         // global variable "num" getter
 139         Method numGet = findGlobalVariableGet(forwarderCls, "num");
 140         assertTrue(numGet != null);
 141 
 142         // anonymous enum fields
 143         assertTrue(findField(forwarderCls, "R") != null);
 144         assertTrue(findField(forwarderCls, "G") != null);
 145         assertTrue(findField(forwarderCls, "B") != null);
 146 
 147         // enum interface class
 148         Class<?> colorCls = Arrays.stream(forwarderCls.getClasses())
 149             .filter(c -> c.getSimpleName().equals("Color")).findFirst().get();
 150         assertTrue(Modifier.isInterface(colorCls.getModifiers()));
 151         checkIntField(colorCls, "RED", 0);
 152         checkIntField(colorCls, "GREEN", 1);
 153         checkIntField(colorCls, "BLUE", 2);
 154     }
 155 }
< prev index next >