1 /*
   2  * Copyright (c) 2018, 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.PrintWriter;
  25 import java.io.StringWriter;
  26 import java.nio.file.Paths;
  27 import java.nio.file.Files;
  28 import java.util.spi.ToolProvider;
  29 
  30 /*
  31  * @test
  32  * @modules jdk.jextract
  33  * @build JextractToolProviderTest
  34  * @run main JextractToolProviderTest
  35  */
  36 public class JextractToolProviderTest {
  37     private static final ToolProvider JEXTRACT_TOOL = ToolProvider.findFirst("jextract")
  38         .orElseThrow(() ->
  39             new RuntimeException("jextract tool not found")
  40         );
  41 
  42     private static String testSrcDir = System.getProperty("test.src", ".");
  43     private static String testClassesDir = System.getProperty("test.classes", ".");
  44 
  45     private static String getFilePath(String dir, String fileName) {
  46         return Paths.get(dir, fileName).toAbsolutePath().toString();
  47     }
  48 
  49     private static String getInputFilePath(String fileName) {
  50         return getFilePath(testSrcDir, fileName);
  51     }
  52 
  53     private static String getOutputFilePath(String fileName) {
  54         return getFilePath(testClassesDir, fileName);
  55     }
  56 
  57     private static int checkJextract(String expected, String... options) {
  58         StringWriter writer = new StringWriter();
  59         PrintWriter pw = new PrintWriter(writer);
  60 
  61         int result = JEXTRACT_TOOL.run(pw, pw, options);
  62         String output = writer.toString();
  63         System.err.println(output);
  64         if (expected != null) {
  65             if (!output.contains(expected)) {
  66                 throw new AssertionError("Output does not contain " + expected);
  67             }
  68         }
  69 
  70         return result;
  71     }
  72 
  73     private static void checkSuccess(String expected, String... options) {
  74         int result = checkJextract(null, options);
  75         if (result != 0) {
  76             throw new AssertionError("Sucess excepted, failed: " + result);
  77         }
  78     }
  79 
  80     private static void checkFailure(String expected, String... options) {
  81         int result = checkJextract(expected, options);
  82         if (result == 0) {
  83             throw new AssertionError("Failure excepted, succeeded!");
  84         }
  85     }
  86 
  87     public static void main(String[] args) throws Exception {
  88         checkFailure(null); // no options
  89         checkSuccess(null, "--help");
  90         checkSuccess(null, "-h");
  91         checkSuccess(null, "-?");
  92 
  93         // error for non-existent header file
  94         checkFailure("Cannot open header file", "--dry-run", getInputFilePath("non_existent.h"));
  95 
  96         // only dry-run, don't produce any output
  97         String simpleJar = getOutputFilePath("simple.jar");
  98         checkSuccess(null, "--dry-run", getInputFilePath("simple.h"));
  99         if (Files.isRegularFile(Paths.get(simpleJar))) {
 100             throw new AssertionError(simpleJar + "output file should not have been produced");
 101         }
 102         // simple output file check
 103         checkSuccess(null, "-o", simpleJar, getInputFilePath("simple.h"));
 104         if (!Files.isRegularFile(Paths.get(simpleJar))) {
 105             throw new AssertionError(simpleJar + "output file not produced");
 106         }
 107     }
 108 }