< prev index next >

test/jdk/com/sun/tools/jextract/JextractToolRunner.java

Print this page




  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




  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


< prev index next >