8076139: [TEST_BUG] test/javax/xml/ws/8046817/GenerateEnumSchema.java creates files in test.src
Reviewed-by: igerasim
This commit is contained in:
parent
5109058c5d
commit
a643b89ee2
118
jdk/test/javax/xml/bind/jxc/8046817/GenerateEnumSchema.java
Normal file
118
jdk/test/javax/xml/bind/jxc/8046817/GenerateEnumSchema.java
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8046817 8073357 8076139
|
||||
* @summary schemagen fails to generate xsd for enum types.
|
||||
* Check that order of Enum values is preserved.
|
||||
* @library /lib/testlibrary
|
||||
* @run testng/othervm GenerateEnumSchema
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import jdk.testlibrary.JDKToolLauncher;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class GenerateEnumSchema {
|
||||
|
||||
@DataProvider
|
||||
//test case name, input file for schemagen, regexp for checking schema content
|
||||
public static Object[][] schemagenGenerationData() {
|
||||
return new Object[][] {
|
||||
{"Class", "TestClassType.java",
|
||||
".+?name=\"testClassType\".+"},
|
||||
{"Enum", "TestEnumType.java",
|
||||
".+?FIRST.+?ONE.+?TWO.+?THREE.+?FOUR.+?FIVE.+?SIX.+?LAST.+"},
|
||||
};
|
||||
}
|
||||
|
||||
@BeforeTest
|
||||
public void setUp() throws IOException {
|
||||
//Create test directory inside scratch
|
||||
testWorkDir = Paths.get(System.getProperty("user.dir", "."))
|
||||
.resolve("GenerateEnumSchema");
|
||||
testSrcDir = Paths.get(System.getProperty("test.src", "."));
|
||||
//Create test work directory inside scratch directory
|
||||
Files.createDirectory(testWorkDir);
|
||||
}
|
||||
|
||||
@Test(dataProvider="schemagenGenerationData")
|
||||
public void schemangenGenerationTestCase(String shortTestName,
|
||||
String inputFileName, String regexp) throws IOException {
|
||||
//Create test case directory
|
||||
Path testCaseDir = testWorkDir.resolve(shortTestName);
|
||||
Files.createDirectories(testCaseDir);
|
||||
//Copy java source from test.src to the test case dir
|
||||
Files.copy(testSrcDir.resolve(inputFileName), testCaseDir.resolve(inputFileName), REPLACE_EXISTING);
|
||||
//Run schemagen
|
||||
runSchemaGen(inputFileName, testCaseDir);
|
||||
//Check if schema file generated
|
||||
Assert.assertTrue(Files.exists(testCaseDir.resolve(SCHEMA_FILE)));
|
||||
//Read schema content from file
|
||||
String content = Files.lines(testCaseDir.resolve(SCHEMA_FILE)).collect(Collectors.joining(""));
|
||||
System.out.println("Generated schema: " + content);
|
||||
//Check if schema contains expected content
|
||||
Assert.assertTrue(Pattern.matches(regexp, content));
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
//schemagen tool output file name
|
||||
private static final String SCHEMA_FILE = "schema1.xsd";
|
||||
//Test working directory
|
||||
Path testWorkDir;
|
||||
//Directory with test src
|
||||
Path testSrcDir;
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
@ -1,157 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8046817 8073357
|
||||
* @summary schemagen fails to generate xsd for enum types.
|
||||
* Check that order of Enum values is preserved.
|
||||
* @run main/othervm GenerateEnumSchema
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GenerateEnumSchema {
|
||||
|
||||
private static final String SCHEMA_OUTPUT_FILENAME = "schema1.xsd";
|
||||
private static final File schemaOutputFile = new File(SCHEMA_OUTPUT_FILENAME);
|
||||
private static final String[] expectedEnums = {
|
||||
"\"FIRST\"", "\"ONE\"", "\"TWO\"", "\"THREE\"",
|
||||
"\"FOUR\"", "\"FIVE\"", "\"SIX\"", "\"LAST\""};
|
||||
private static String schemaContent = "";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
//Check schema generation for class type
|
||||
runSchemaGen("TestClassType.java");
|
||||
checkIfSchemaGenerated();
|
||||
readSchemaContent();
|
||||
checkSchemaContent("<xs:complexType name=\"testClassType\">");
|
||||
checkSchemaContent("<xs:element name=\"a\" type=\"xs:int\"/>");
|
||||
|
||||
//Check schema generation for enum type
|
||||
runSchemaGen("TestEnumType.java");
|
||||
checkIfSchemaGenerated();
|
||||
readSchemaContent();
|
||||
//Check if Enum type schema is generated
|
||||
checkSchemaContent("<xs:simpleType name=\"testEnumType\">");
|
||||
//Check the sequence of enum values order
|
||||
checkEnumOrder();
|
||||
schemaOutputFile.delete();
|
||||
}
|
||||
|
||||
// Check if schema file successfully generated by schemagen
|
||||
private static void checkIfSchemaGenerated() {
|
||||
if (!schemaOutputFile.exists()) {
|
||||
throw new RuntimeException("FAIL:" + SCHEMA_OUTPUT_FILENAME + " was not generated by schemagen tool");
|
||||
}
|
||||
}
|
||||
|
||||
//Read schema content from file
|
||||
private static void readSchemaContent() throws Exception {
|
||||
schemaContent = Files.lines(schemaOutputFile.toPath()).collect(Collectors.joining(""));
|
||||
}
|
||||
|
||||
// Check if schema file contains specific string
|
||||
private static void checkSchemaContent(String expContent) {
|
||||
System.out.print("Check if generated schema contains '" + expContent + "' string: ");
|
||||
if (schemaContent.contains(expContent)) {
|
||||
System.out.println("OK");
|
||||
return;
|
||||
}
|
||||
System.out.println("FAIL");
|
||||
throw new RuntimeException("The '" + expContent + "' is not found in generated schema");
|
||||
}
|
||||
|
||||
// Check if the generated schema contains all enum constants
|
||||
// and their order is preserved
|
||||
private static void checkEnumOrder() throws Exception {
|
||||
int prevElem = -1;
|
||||
for (String elem : expectedEnums) {
|
||||
int curElem = schemaContent.indexOf(elem);
|
||||
System.out.println(elem + " position = " + curElem);
|
||||
if (curElem < prevElem) {
|
||||
throw new RuntimeException("FAIL: Enum values order is incorrect or " + elem + " element is not found");
|
||||
}
|
||||
prevElem = curElem;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getClassFilePath(String filename) {
|
||||
String testSrc = System.getProperty("test.src");
|
||||
if (testSrc == null) {
|
||||
testSrc = ".";
|
||||
}
|
||||
return Paths.get(testSrc).resolve(filename).toString();
|
||||
}
|
||||
|
||||
private static String getSchemagen() {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String schemagen = javaHome + File.separator + "bin" + File.separator + "schemagen";
|
||||
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||
schemagen = schemagen.concat(".exe");
|
||||
}
|
||||
return schemagen;
|
||||
}
|
||||
|
||||
private static void logOutput(Process p) throws IOException {
|
||||
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
String s = r.readLine();
|
||||
while (s != null) {
|
||||
System.out.println(s.trim());
|
||||
s = r.readLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static void runSchemaGen(String classFile) {
|
||||
String schemagen = getSchemagen();
|
||||
|
||||
try {
|
||||
System.out.println("Call to schemagen: " + schemagen + " " + classFile);
|
||||
String[] schemagen_args = {
|
||||
schemagen,
|
||||
getClassFilePath(classFile)
|
||||
};
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(schemagen_args);
|
||||
pb.redirectErrorStream(true);
|
||||
Process p = pb.start();
|
||||
logOutput(p);
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user