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.Objects;
  38 import java.util.spi.ToolProvider;
  39 
  40 import static org.testng.Assert.assertEquals;
  41 import static org.testng.Assert.assertNotEquals;
  42 import static org.testng.Assert.assertNotNull;
  43 import static org.testng.Assert.assertTrue;
  44 import static org.testng.Assert.fail;
  45 
  46 class JextractToolRunner {
  47     private static final ToolProvider JEXTRACT_TOOL = ToolProvider.findFirst("jextract")
  48             .orElseThrow(() ->
  49                     new RuntimeException("jextract tool not found")
  50             );
  51 
  52     private final Path inputDir;
  53     private final Path outputDir;
  54 
  55     protected JextractToolRunner() {
  56         this(null, null);
  57     }
  58 
  59     protected JextractToolRunner(Path input, Path output) {
  60         inputDir = (input != null) ? input :
  61             Paths.get(System.getProperty("test.src", "."));
  62         outputDir = (output != null) ? output :
  63             Paths.get(System.getProperty("test.classes", "."));
  64     }
  65 
  66     protected Path getInputFilePath(String fileName) {
  67         return inputDir.resolve(fileName).toAbsolutePath();
  68     }
  69 
  70     protected Path getOutputFilePath(String fileName) {
  71         return outputDir.resolve(fileName).toAbsolutePath();
  72     }
  73 
  74     protected static class JextractResult {
  75         private int exitCode;
  76         private String output;
  77 
  78         JextractResult(int exitCode, String output) {
  79             this.exitCode = exitCode;
  80             this.output = output;



  81         }
  82 
  83         protected JextractResult checkSuccess() {
  84             assertEquals(exitCode, 0, "Sucess excepted, failed: " + exitCode);
  85             return this;
  86         }
  87 
  88         protected JextractResult checkFailure() {
  89             assertNotEquals(exitCode, 0, "Failure excepted, succeeded!");
  90             return this;
  91         }
  92 
  93         protected JextractResult checkContainsOutput(String expected) {
  94             Objects.requireNonNull(expected);
  95             assertTrue(output.contains(expected), "Output does not contain string: " + expected);
  96             return this;
  97         }
  98 
  99         protected JextractResult checkMatchesOutput(String regex) {
 100             Objects.requireNonNull(regex);
 101             assertTrue(output.matches(regex), "Output does not match regex: " + regex);
 102             return this;
 103         }
 104     }
 105 
 106     protected static JextractResult run(String... options) {
 107         StringWriter writer = new StringWriter();
 108         PrintWriter pw = new PrintWriter(writer);
 109 
 110         int result = JEXTRACT_TOOL.run(pw, pw, options);
 111         String output = writer.toString();
 112         System.err.println(output);
 113         return new JextractResult(result, output);
 114     }
 115 
 116     protected static void deleteFile(Path path) {
 117         try {
 118             Files.delete(path);
 119         } catch (IOException ioExp) {
 120             System.err.println(ioExp);
 121         }
 122     }
 123 
 124     protected static void deleteDir(Path path) {
 125         try {
 126             Files.walkFileTree(path, new SimpleFileVisitor<>() {
 127                 @Override
 128                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 129                    deleteFile(file);
 130                    return FileVisitResult.CONTINUE;
 131                 }
 132 
 133                 @Override
 134                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 135                    deleteFile(dir);
 136                    return FileVisitResult.CONTINUE;
 137                 }
 138             });
 139         } catch (IOException ioExp) {
 140             System.err.println(ioExp);
 141         }
 142     }
 143 
 144     protected static Class<?> loadClass(String className, Path...paths) {
 145         try {
 146             URL[] urls = new URL[paths.length];
 147             for (int i = 0; i < paths.length; i++) {
 148                 urls[i] = paths[i].toUri().toURL();
 149             }
 150             URLClassLoader ucl = new URLClassLoader(urls, null);
 151             return Class.forName(className, false, ucl);
 152         } catch (RuntimeException re) {
 153             throw re;
 154         } catch (Exception e) {
 155             throw new RuntimeException(e);
 156         }
 157     }
 158 
 159     protected static Field findField(Class<?> cls, String name) {
 160         try {
 161             return cls.getField(name);
 162         } catch (Exception e) {
 163             System.err.println(e);
 164             return null;
 165         }
 166     }
 167 
 168     protected static Method findMethod(Class<?> cls, String name, Class<?>... argTypes) {
 169         try {
 170             return cls.getMethod(name, argTypes);
 171         } catch (Exception e) {
 172             System.err.println(e);
 173             return null;
 174         }
 175     }
 176 
 177     protected static Method findStructFieldGet(Class<?> cls, String name) {
 178         return findMethod(cls, name + "$get");
 179     }
 180 
 181     protected static Method findGlobalVariableGet(Class<?> cls, String name) {
 182         return findMethod(cls, name + "$get");
 183     }
 184 
 185     protected static Method findEnumConstGet(Class<?> cls, String name) {
 186         return findMethod(cls, name);
 187     }
 188 
 189     protected static Method findFirstMethod(Class<?> cls, String name) {
 190         try {
 191             for (Method m : cls.getMethods()) {
 192                 if (name.equals(m.getName())) {
 193                     return m;
 194                 }
 195             }
 196             return null;
 197         } catch (Exception e) {
 198             System.err.println(e);
 199             return null;
 200         }
 201     }
 202 
 203     protected Field checkIntField(Class<?> cls, String name, int value) {
 204         Field field = findField(cls, name);
 205         assertNotNull(field);
 206         assertEquals(field.getType(), int.class);
 207         try {
 208             assertEquals((int)field.get(null), value);
 209         } catch (Exception exp) {
 210             System.err.println(exp);
 211             assertTrue(false, "should not reach here");
 212         }
 213         return field;
 214     }
 215 
 216     protected Class<?> findClass(Class<?>[] clz, String name) {
 217         for (Class<?> cls: clz) {
 218             if (cls.getSimpleName().equals(name)) {
 219                 return cls;
 220             }
 221         }
 222         return null;
 223     }
 224 
 225     protected Method checkMethod(Class<?> cls, String name, Class<?> returnType, Class<?>... args) {
 226         try {
 227             Method m = cls.getDeclaredMethod(name, args);
 228             assertTrue(m.getReturnType() == returnType);
 229             return m;
 230         } catch (NoSuchMethodException nsme) {
 231             fail("Expect method " + name);
 232         }
 233         return null;
 234     }
 235 }
--- EOF ---