1 /* 2 * Copyright (c) 2014, 2015, 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 /* 25 * @test 26 * @bug 8046817 8073357 8076139 27 * @summary schemagen fails to generate xsd for enum types. 28 * Check that order of Enum values is preserved. 29 * @library /lib/testlibrary 30 * @run testng/othervm GenerateEnumSchema 31 */ 32 import java.io.BufferedReader; 33 import java.io.IOException; 34 import java.io.InputStreamReader; 35 import java.nio.file.Files; 36 import java.nio.file.Path; 37 import java.nio.file.Paths; 38 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 39 import java.util.Arrays; 40 import java.util.regex.Pattern; 41 import java.util.stream.Collectors; 42 import jdk.testlibrary.JDKToolLauncher; 43 import org.testng.Assert; 44 import org.testng.annotations.BeforeTest; 45 import org.testng.annotations.DataProvider; 46 import org.testng.annotations.Test; 47 48 public class GenerateEnumSchema { 49 50 @DataProvider 51 //test case name, input file for schemagen, regexp for checking schema content 52 public static Object[][] schemagenGenerationData() { 53 return new Object[][] { 54 {"Class", "TestClassType.java", 55 ".+?name=\"testClassType\".+"}, 56 {"Enum", "TestEnumType.java", 57 ".+?FIRST.+?ONE.+?TWO.+?THREE.+?FOUR.+?FIVE.+?SIX.+?LAST.+"}, 58 }; 59 } 60 61 @BeforeTest 62 public void setUp() throws IOException { 63 //Create test directory inside scratch 64 testWorkDir = Paths.get(System.getProperty("user.dir", ".")) 65 .resolve("GenerateEnumSchema"); 66 testSrcDir = Paths.get(System.getProperty("test.src", ".")); 67 //Create test work directory inside scratch directory 68 Files.createDirectory(testWorkDir); 69 } 70 71 @Test(dataProvider="schemagenGenerationData") 72 public void schemangenGenerationTestCase(String shortTestName, 73 String inputFileName, String regexp) throws IOException { 74 //Create test case directory 75 Path testCaseDir = testWorkDir.resolve(shortTestName); 76 Files.createDirectories(testCaseDir); 77 //Copy java source from test.src to the test case dir 78 Files.copy(testSrcDir.resolve(inputFileName), testCaseDir.resolve(inputFileName), REPLACE_EXISTING); 79 //Run schemagen 80 runSchemaGen(inputFileName, testCaseDir); 81 //Check if schema file generated 82 Assert.assertTrue(Files.exists(testCaseDir.resolve(SCHEMA_FILE))); 83 //Read schema content from file 84 String content = Files.lines(testCaseDir.resolve(SCHEMA_FILE)).collect(Collectors.joining("")); 85 System.out.println("Generated schema: " + content); 86 //Check if schema contains expected content 87 Assert.assertTrue(Pattern.matches(regexp, content)); 88 } 89 90 private static void runSchemaGen(String classFile, Path runDir) { 91 try { 92 JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen"); 93 sgl.addToolArg(classFile); 94 System.out.println("Executing: " + Arrays.asList(sgl.getCommand())); 95 ProcessBuilder pb = new ProcessBuilder(sgl.getCommand()); 96 pb.directory(runDir.toFile()); 97 pb.redirectErrorStream(true); 98 pb.inheritIO(); 99 Process p = pb.start(); 100 int result = p.waitFor(); 101 p.destroy(); 102 if (result != 0) { 103 throw new RuntimeException("schemagen failed"); 104 } 105 } catch (IOException | InterruptedException e) { 106 System.err.println("Can't run schemagen tool. Exception:"); 107 e.printStackTrace(System.err); 108 throw new RuntimeException("Error launching schemagen tool"); 109 } 110 } 111 112 //schemagen tool output file name 113 private static final String SCHEMA_FILE = "schema1.xsd"; 114 //Test working directory 115 Path testWorkDir; 116 //Directory with test src 117 Path testSrcDir; 118 }