use of jdk.testlibrary.JDKToolLauncher in project jdk8u_jdk by JetBrains.
the class GenerateEnumSchema method runSchemaGen.
private static void runSchemaGen(String classFile, Path runDir) {
try {
JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen");
sgl.addToolArg(classFile);
System.out.println("Executing: " + Arrays.asList(sgl.getCommand()));
ProcessBuilder pb = new ProcessBuilder(sgl.getCommand());
pb.directory(runDir.toFile());
pb.redirectErrorStream(true);
pb.inheritIO();
Process p = pb.start();
int result = p.waitFor();
p.destroy();
if (result != 0) {
throw new RuntimeException("schemagen failed");
}
} catch (IOException | InterruptedException e) {
System.err.println("Can't run schemagen tool. Exception:");
e.printStackTrace(System.err);
throw new RuntimeException("Error launching schemagen tool");
}
}
use of jdk.testlibrary.JDKToolLauncher in project jdk8u_jdk by JetBrains.
the class SchemagenErrorReporting method schemagenErrorReporting.
@Test
public void schemagenErrorReporting() throws Exception {
//schemagen tool output file name
final String SCHEMA_FILE = "schema1.xsd";
//Schemagen input java file with not compilable source
final String CLASS_FILE = "InputWithError.java";
//Test working, src directories and test output file
Path testWorkDir, testSrcDir, testOutput;
//Prepare test environment
//Create test directory inside scratch
testWorkDir = Paths.get(System.getProperty("user.dir", ".")).resolve("SchemagenErrorReporting");
//Get test source directory
testSrcDir = Paths.get(System.getProperty("test.src", "."));
//Set test output file path
testOutput = testWorkDir.resolve("stdErrContent");
//Create test directory inside scratch directory
Files.createDirectory(testWorkDir);
//Copy java source from test.src to the test directory
Files.copy(testSrcDir.resolve(CLASS_FILE), testWorkDir.resolve(CLASS_FILE), StandardCopyOption.REPLACE_EXISTING);
//Prepare process builder to run schemagen tool and save its output
JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen");
sgl.addToolArg(CLASS_FILE);
System.out.println("Executing: " + Arrays.asList(sgl.getCommand()));
ProcessBuilder pb = new ProcessBuilder(sgl.getCommand());
//Set schemagen work directory with the input java file
pb.directory(testWorkDir.toFile());
//Redirect schemagen output to file
pb.redirectError(testOutput.toFile());
Process p = pb.start();
int result = p.waitFor();
p.destroy();
//Read schemagen output from the file
String stdErrContent = Files.lines(testOutput).collect(Collectors.joining(System.lineSeparator(), System.lineSeparator(), ""));
System.out.println("Schemagen return value:" + result);
System.out.println("Error output:" + stdErrContent);
//Check test results:
//Schemagen finished with non-0 return value
Assert.assertNotEquals(result, 0);
//Schemagen output contains compile error message
Assert.assertTrue(stdErrContent.contains("InputWithError.java:28: error"));
}
Aggregations