Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/bind/jxc/8046817/GenerateEnumSchema.java
38862 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8046817 8073357 807613926* @summary schemagen fails to generate xsd for enum types.27* Check that order of Enum values is preserved.28* @library /lib/testlibrary29* @run testng/othervm GenerateEnumSchema30*/31import java.io.BufferedReader;32import java.io.IOException;33import java.io.InputStreamReader;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;38import java.util.Arrays;39import java.util.regex.Pattern;40import java.util.stream.Collectors;41import jdk.testlibrary.JDKToolLauncher;42import org.testng.Assert;43import org.testng.annotations.BeforeTest;44import org.testng.annotations.DataProvider;45import org.testng.annotations.Test;4647public class GenerateEnumSchema {4849@DataProvider50//test case name, input file for schemagen, regexp for checking schema content51public static Object[][] schemagenGenerationData() {52return new Object[][] {53{"Class", "TestClassType.java",54".+?name=\"testClassType\".+"},55{"Enum", "TestEnumType.java",56".+?FIRST.+?ONE.+?TWO.+?THREE.+?FOUR.+?FIVE.+?SIX.+?LAST.+"},57};58}5960@BeforeTest61public void setUp() throws IOException {62//Create test directory inside scratch63testWorkDir = Paths.get(System.getProperty("user.dir", "."))64.resolve("GenerateEnumSchema");65testSrcDir = Paths.get(System.getProperty("test.src", "."));66//Create test work directory inside scratch directory67Files.createDirectory(testWorkDir);68}6970@Test(dataProvider="schemagenGenerationData")71public void schemangenGenerationTestCase(String shortTestName,72String inputFileName, String regexp) throws IOException {73//Create test case directory74Path testCaseDir = testWorkDir.resolve(shortTestName);75Files.createDirectories(testCaseDir);76//Copy java source from test.src to the test case dir77Files.copy(testSrcDir.resolve(inputFileName), testCaseDir.resolve(inputFileName), REPLACE_EXISTING);78//Run schemagen79runSchemaGen(inputFileName, testCaseDir);80//Check if schema file generated81Assert.assertTrue(Files.exists(testCaseDir.resolve(SCHEMA_FILE)));82//Read schema content from file83String content = Files.lines(testCaseDir.resolve(SCHEMA_FILE)).collect(Collectors.joining(""));84System.out.println("Generated schema: " + content);85//Check if schema contains expected content86Assert.assertTrue(Pattern.matches(regexp, content));87}8889private static void runSchemaGen(String classFile, Path runDir) {90try {91JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen");92sgl.addToolArg(classFile);93System.out.println("Executing: " + Arrays.asList(sgl.getCommand()));94ProcessBuilder pb = new ProcessBuilder(sgl.getCommand());95pb.directory(runDir.toFile());96pb.redirectErrorStream(true);97pb.inheritIO();98Process p = pb.start();99int result = p.waitFor();100p.destroy();101if (result != 0) {102throw new RuntimeException("schemagen failed");103}104} catch (IOException | InterruptedException e) {105System.err.println("Can't run schemagen tool. Exception:");106e.printStackTrace(System.err);107throw new RuntimeException("Error launching schemagen tool");108}109}110111//schemagen tool output file name112private static final String SCHEMA_FILE = "schema1.xsd";113//Test working directory114Path testWorkDir;115//Directory with test src116Path testSrcDir;117}118119120