1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   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
  61         ArrayList<String> jextrOpts = new ArrayList<>();
  62         jextrOpts.add("-C-nostdinc");
  63         jextrOpts.add("-I");
  64         jextrOpts.add(inputDir.toString());
  65         jextrOpts.add("-o");
  66         jextrOpts.add(jarPath.toString());
  67         jextrOpts.add("--src-dump-dir");
  68         jextrOpts.add(outputDir.toString());
  69         jextrOpts.add("-t");
  70         jextrOpts.add(pkgName);
  71         jextrOpts.add("-l");
  72         jextrOpts.add("srcgentest");
  73         jextrOpts.add(inputDir + File.separator + "srcgentest.h");
  74 
  75         int result = JEXTRACT.run(System.out, System.err, jextrOpts.toArray(String[]::new));
  76         if (result != 0) {
  77             throw new RuntimeException(JEXTRACT.name() + " returns non-zero value");
  78         }
  79 
  80         // delete .jar file generated by jextract
  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 }