|
1 /* |
|
2 * Copyright (c) 2014, 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 |
|
27 * @summary schemagen fails to generate xsd for enum types |
|
28 * @run main/othervm GenerateEnumSchema |
|
29 */ |
|
30 import java.io.BufferedReader; |
|
31 import java.io.File; |
|
32 import java.io.FileNotFoundException; |
|
33 import java.io.IOException; |
|
34 import java.io.InputStreamReader; |
|
35 import java.nio.file.Paths; |
|
36 import java.util.Scanner; |
|
37 |
|
38 public class GenerateEnumSchema { |
|
39 |
|
40 private static final String SCHEMA_OUTPUT_FILENAME = "schema1.xsd"; |
|
41 private static final File schemaOutputFile = new File(SCHEMA_OUTPUT_FILENAME); |
|
42 |
|
43 public static void main(String[] args) throws Exception, IOException { |
|
44 //Check schema generation for class type |
|
45 runSchemaGen("TestClassType.java"); |
|
46 checkIfSchemaGenerated(); |
|
47 checkSchemaContent("<xs:complexType name=\"testClassType\">"); |
|
48 checkSchemaContent("<xs:element name=\"a\" type=\"xs:int\"/>"); |
|
49 schemaOutputFile.delete(); |
|
50 //Check schema generation for enum type |
|
51 runSchemaGen("TestEnumType.java"); |
|
52 checkIfSchemaGenerated(); |
|
53 checkSchemaContent("<xs:simpleType name=\"testEnumType\">"); |
|
54 checkSchemaContent("<xs:enumeration value=\"ONE\"/>"); |
|
55 checkSchemaContent("<xs:enumeration value=\"TWO\"/>"); |
|
56 checkSchemaContent("<xs:enumeration value=\"THREE\"/>"); |
|
57 schemaOutputFile.delete(); |
|
58 } |
|
59 |
|
60 private static void checkIfSchemaGenerated() { |
|
61 if (!schemaOutputFile.exists()) { |
|
62 throw new RuntimeException("FAIL:" + SCHEMA_OUTPUT_FILENAME + " was not generated by schemagen tool"); |
|
63 } |
|
64 } |
|
65 |
|
66 private static void checkSchemaContent(String exp_token) throws FileNotFoundException { |
|
67 System.out.print("Check if generated schema contains '" + exp_token + "' string: "); |
|
68 try (Scanner scanner = new Scanner(schemaOutputFile)) { |
|
69 if (scanner.findWithinHorizon(exp_token, 0) != null) { |
|
70 System.out.println("OK"); |
|
71 return; |
|
72 } |
|
73 } |
|
74 System.out.println("FAIL"); |
|
75 throw new RuntimeException("The '" + exp_token + "' is not found in generated schema"); |
|
76 |
|
77 } |
|
78 |
|
79 private static String getClassFilePath(String filename) { |
|
80 String testSrc = System.getProperty("test.src"); |
|
81 if (testSrc == null) { |
|
82 testSrc = "."; |
|
83 } |
|
84 return Paths.get(testSrc).resolve(filename).toString(); |
|
85 } |
|
86 |
|
87 private static String getSchemagen() { |
|
88 String javaHome = System.getProperty("java.home"); |
|
89 if (javaHome.endsWith("jre")) { |
|
90 javaHome = new File(javaHome).getParent(); |
|
91 } |
|
92 String schemagen = javaHome + File.separator + "bin" + File.separator + "schemagen"; |
|
93 if (System.getProperty("os.name").startsWith("Windows")) { |
|
94 schemagen = schemagen.concat(".exe"); |
|
95 } |
|
96 return schemagen; |
|
97 } |
|
98 |
|
99 private static void logOutput(Process p) throws IOException { |
|
100 BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
101 String s = r.readLine(); |
|
102 while (s != null) { |
|
103 System.out.println(s.trim()); |
|
104 s = r.readLine(); |
|
105 } |
|
106 } |
|
107 |
|
108 private static void runSchemaGen(String classFile) { |
|
109 String schemagen = getSchemagen(); |
|
110 |
|
111 try { |
|
112 System.out.println("Call to schemagen: " + schemagen + " " + classFile); |
|
113 String[] schemagen_args = { |
|
114 schemagen, |
|
115 getClassFilePath(classFile) |
|
116 }; |
|
117 |
|
118 ProcessBuilder pb = new ProcessBuilder(schemagen_args); |
|
119 pb.redirectErrorStream(true); |
|
120 Process p = pb.start(); |
|
121 logOutput(p); |
|
122 int result = p.waitFor(); |
|
123 p.destroy(); |
|
124 |
|
125 if (result != 0) { |
|
126 throw new RuntimeException("schemagen failed"); |
|
127 } |
|
128 } catch (IOException | InterruptedException e) { |
|
129 System.err.println("Can't run schemagen tool. Exception:"); |
|
130 e.printStackTrace(System.err); |
|
131 throw new RuntimeException("Error launching schemagen tool"); |
|
132 } |
|
133 } |
|
134 } |