Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/bind/xjc/8145039/JaxbMarshallTest.java
38861 views
1
/*
2
* Copyright (c) 2016, 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 8145039
27
* @summary Check that marshalling of xjc generated class doesn't throw
28
* ClassCast exception.
29
* @library /lib/testlibrary
30
* @run testng/othervm JaxbMarshallTest
31
*/
32
33
import java.io.IOException;
34
import java.lang.reflect.Method;
35
import java.net.URL;
36
import java.net.URLClassLoader;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.nio.file.Paths;
40
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
41
import java.util.Arrays;
42
import java.util.List;
43
import javax.xml.bind.JAXBContext;
44
import javax.xml.bind.Marshaller;
45
import jdk.testlibrary.JDKToolLauncher;
46
import org.testng.annotations.BeforeTest;
47
import org.testng.annotations.Test;
48
49
public class JaxbMarshallTest {
50
51
@BeforeTest
52
public void setUp() throws IOException {
53
// Create test directory inside scratch
54
testWorkDir = Paths.get(System.getProperty("user.dir", "."));
55
// Save its URL
56
testWorkDirUrl = testWorkDir.toUri().toURL();
57
// Get test source directory path
58
testSrcDir = Paths.get(System.getProperty("test.src", "."));
59
// Get path of xjc result folder
60
xjcResultDir = testWorkDir.resolve(TEST_PACKAGE);
61
// Copy schema document file to scratch directory
62
Files.copy(testSrcDir.resolve(XSD_FILENAME), testWorkDir.resolve(XSD_FILENAME), REPLACE_EXISTING);
63
}
64
65
66
/*
67
* Test does the following steps to reproduce problem reported by 8145039:
68
* 1. Copy test schema to JTREG scratch folder
69
* 2. Run xjc on test schema file
70
* 3. Compile generated java files with test javac
71
* 4. Marshall the new list instance to ensure that
72
* ClassCastException is not thrown
73
*/
74
@Test
75
public void marshallClassCastExceptionTest() throws Exception {
76
JAXBContext jaxbContext;
77
Marshaller marshaller;
78
URLClassLoader jaxbContextClassLoader;
79
// Generate java classes by xjc
80
runXjc(XSD_FILENAME);
81
// Compile xjc generated java files
82
compileXjcGeneratedClasses();
83
84
// Create JAXB context based on xjc generated package.
85
// Need to create URL class loader ot make compiled classes discoverable
86
// by JAXB context
87
jaxbContextClassLoader = URLClassLoader.newInstance(new URL[] {testWorkDirUrl});
88
jaxbContext = JAXBContext.newInstance( TEST_PACKAGE, jaxbContextClassLoader);
89
90
// Create instance of Xjc generated data type.
91
// Java classes were compiled during the test execution hence reflection
92
// is needed here
93
Class classLongListClass = jaxbContextClassLoader.loadClass(TEST_CLASS);
94
Object objectLongListClass = classLongListClass.newInstance();
95
// Get 'getIn' method object
96
Method getInMethod = classLongListClass.getMethod( GET_LIST_METHOD, (Class [])null );
97
// Invoke 'getIn' method
98
List<Long> inList = (List<Long>)getInMethod.invoke(objectLongListClass);
99
// Add values into the jaxb object list
100
inList.add(Long.valueOf(0));
101
inList.add(Long.valueOf(43));
102
inList.add(Long.valueOf(1000000123));
103
104
// Marshall constructed complex type variable to standard output.
105
// In case of failure the ClassCastException will be thrown
106
marshaller = jaxbContext.createMarshaller();
107
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
108
marshaller.marshal(objectLongListClass, System.out);
109
}
110
111
// Compile schema file into java classes definitions
112
void runXjc(String xsdFileName) throws Exception {
113
// Prepare process builder to run schemagen tool and save its output
114
JDKToolLauncher xjcLauncher = JDKToolLauncher.createUsingTestJDK("xjc");
115
xjcLauncher.addToolArg(xsdFileName);
116
System.out.println("Executing xjc command: " + Arrays.asList(xjcLauncher.getCommand()));
117
ProcessBuilder pb = new ProcessBuilder(xjcLauncher.getCommand());
118
// Set xjc work directory with the input java file
119
pb.directory(testWorkDir.toFile());
120
pb.inheritIO();
121
Process p = pb.start();
122
p.waitFor();
123
p.destroy();
124
}
125
126
// Compile java classes with javac tool
127
void compileXjcGeneratedClasses() throws Exception {
128
JDKToolLauncher javacLauncher = JDKToolLauncher.createUsingTestJDK("javac");
129
javacLauncher.addToolArg(xjcResultDir.resolve("ObjectFactory.java").toString());
130
javacLauncher.addToolArg(xjcResultDir.resolve("TypesLongList.java").toString());
131
javacLauncher.addToolArg(xjcResultDir.resolve("package-info.java").toString());
132
System.out.println("Compiling xjc generated classes: " + Arrays.asList(javacLauncher.getCommand()));
133
ProcessBuilder pb = new ProcessBuilder(javacLauncher.getCommand());
134
pb.inheritIO();
135
pb.directory(testWorkDir.toFile());
136
Process p = pb.start();
137
p.waitFor();
138
p.destroy();
139
}
140
141
// Test schema filename
142
static final String XSD_FILENAME = "testSchema.xsd";
143
// Package of java classes generated by xjc
144
static final String TEST_PACKAGE = "testns_package";
145
// Name of generated java class
146
static final String TEST_CLASS = TEST_PACKAGE+".TypesLongList";
147
// Method to get the list from xjc generated class
148
static final String GET_LIST_METHOD = "getIn";
149
// Test working directory
150
Path testWorkDir;
151
// Test working directory URL
152
URL testWorkDirUrl;
153
// Directory with test src
154
Path testSrcDir;
155
// Directory with java files generated by xjc
156
Path xjcResultDir;
157
}
158
159