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.IOException;
  25 import java.io.PrintWriter;
  26 import java.io.StringWriter;
  27 import java.lang.reflect.Field;
  28 import java.lang.reflect.Method;
  29 import java.net.URL;
  30 import java.net.URLClassLoader;
  31 import java.nio.file.FileVisitResult;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.nio.file.SimpleFileVisitor;
  36 import java.nio.file.attribute.BasicFileAttributes;
  37 import java.util.spi.ToolProvider;
  38 
  39 import static org.testng.Assert.assertEquals;
  40 import static org.testng.Assert.assertNotEquals;
  41 import static org.testng.Assert.assertNotNull;
  42 import static org.testng.Assert.assertTrue;
  43 import static org.testng.Assert.fail;
  44 
  45 class JextractToolRunner {
  46     private static final ToolProvider JEXTRACT_TOOL = ToolProvider.findFirst("jextract")
  47             .orElseThrow(() ->
  48                     new RuntimeException("jextract tool not found")
  49             );
  50 
  51     private final Path inputDir;
  52     private final Path outputDir;
  53 
  54     protected JextractToolRunner() {
  55         this(null, null);
  56     }
  57 
  58     protected JextractToolRunner(Path input, Path output) {
  59         inputDir = (input != null) ? input :
  60             Paths.get(System.getProperty("test.src", "."));
  61         outputDir = (output != null) ? output :
  62             Paths.get(System.getProperty("test.classes", "."));
  63     }
  64 
  65     protected Path getInputFilePath(String fileName) {
  66         return inputDir.resolve(fileName).toAbsolutePath();
  67     }
  68 
  69     protected Path getOutputFilePath(String fileName) {
  70         return outputDir.resolve(fileName).toAbsolutePath();
  71     }
  72 
  73     protected static int checkJextract(String expected, String... options) {
  74         StringWriter writer = new StringWriter();
  75         PrintWriter pw = new PrintWriter(writer);
  76 
  77         int result = JEXTRACT_TOOL.run(pw, pw, options);
  78         String output = writer.toString();
  79         System.err.println(output);
  80         if (expected != null) {
  81             if (!output.contains(expected)) {
  82                 throw new AssertionError("Output does not contain " + expected);
  83             }
  84         }
  85 
  86         return result;
  87     }
  88 
  89     protected static void checkSuccess(String expected, String... options) {
  90         int result = checkJextract(expected, options);
  91         assertEquals(result, 0, "Sucess excepted, failed: " + result);
  92     }
  93 
  94     protected static void checkFailure(String expected, String... options) {
  95         int result = checkJextract(expected, options);
  96         assertNotEquals(result, 0, "Failure excepted, succeeded!");
  97     }
  98 
  99     protected static void deleteFile(Path path) {
 100         try {
 101             Files.delete(path);
 102         } catch (IOException ioExp) {
 103             System.err.println(ioExp);
 104         }
 105     }
 106 
 107     protected static void deleteDir(Path path) {
 108         try {
 109             Files.walkFileTree(path, new SimpleFileVisitor<>() {
 110                 @Override
 111                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 112                    deleteFile(file);
 113                    return FileVisitResult.CONTINUE;
 114                 }
 115 
 116                 @Override
 117                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 118                    deleteFile(dir);
 119                    return FileVisitResult.CONTINUE;
 120                 }
 121             });
 122         } catch (IOException ioExp) {
 123             System.err.println(ioExp);
 124         }
 125     }
 126 
 127     protected static Class<?> loadClass(String className, Path...paths) {
 128         try {
 129             URL[] urls = new URL[paths.length];
 130             for (int i = 0; i < paths.length; i++) {
 131                 urls[i] = paths[i].toUri().toURL();
 132             }
 133             URLClassLoader ucl = new URLClassLoader(urls, null);
 134             return Class.forName(className, false, ucl);
 135         } catch (RuntimeException re) {
 136             throw re;
 137         } catch (Exception e) {
 138             throw new RuntimeException(e);
 139         }
 140     }
 141 
 142     protected static Field findField(Class<?> cls, String name) {
 143         try {
 144             return cls.getField(name);
 145         } catch (Exception e) {
 146             System.err.println(e);
 147             return null;
 148         }
 149     }
 150 
 151     protected static Method findMethod(Class<?> cls, String name, Class<?>... argTypes) {
 152         try {
 153             return cls.getMethod(name, argTypes);
 154         } catch (Exception e) {
 155             System.err.println(e);
 156             return null;
 157         }
 158     }
 159 
 160     protected static Method findStructFieldGet(Class<?> cls, String name) {
 161         return findMethod(cls, name + "$get");
 162     }
 163 
 164     protected static Method findGlobalVariableGet(Class<?> cls, String name) {
 165         return findMethod(cls, name + "$get");
 166     }
 167 
 168     protected static Method findEnumConstGet(Class<?> cls, String name) {
 169         return findMethod(cls, name);
 170     }
 171 
 172     protected static Method findFirstMethod(Class<?> cls, String name) {
 173         try {
 174             for (Method m : cls.getMethods()) {
 175                 if (name.equals(m.getName())) {
 176                     return m;
 177                 }
 178             }
 179             return null;
 180         } catch (Exception e) {
 181             System.err.println(e);
 182             return null;
 183         }
 184     }
 185 
 186     protected Field checkIntField(Class<?> cls, String name, int value) {
 187         Field field = findField(cls, name);
 188         assertNotNull(field);
 189         assertEquals(field.getType(), int.class);
 190         try {
 191             assertEquals((int)field.get(null), value);
 192         } catch (Exception exp) {
 193             System.err.println(exp);
 194             assertTrue(false, "should not reach here");
 195         }
 196         return field;
 197     }
 198 
 199     protected Class<?> findClass(Class<?>[] clz, String name) {
 200         for (Class<?> cls: clz) {
 201             if (cls.getSimpleName().equals(name)) {
 202                 return cls;
 203             }
 204         }
 205         return null;
 206     }
 207 
 208     protected Method checkMethod(Class<?> cls, String name, Class<?> returnType, Class<?>... args) {
 209         try {
 210             Method m = cls.getDeclaredMethod(name, args);
 211             assertTrue(m.getReturnType() == returnType);
 212             return m;
 213         } catch (NoSuchMethodException nsme) {
 214             fail("Expect method " + name);
 215         }
 216         return null;
 217     }
 218 }