Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/bind/xjc/8145039/JaxbMarshallTest.java
38861 views
/*1* Copyright (c) 2016, 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 814503926* @summary Check that marshalling of xjc generated class doesn't throw27* ClassCast exception.28* @library /lib/testlibrary29* @run testng/othervm JaxbMarshallTest30*/3132import java.io.IOException;33import java.lang.reflect.Method;34import java.net.URL;35import java.net.URLClassLoader;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;40import java.util.Arrays;41import java.util.List;42import javax.xml.bind.JAXBContext;43import javax.xml.bind.Marshaller;44import jdk.testlibrary.JDKToolLauncher;45import org.testng.annotations.BeforeTest;46import org.testng.annotations.Test;4748public class JaxbMarshallTest {4950@BeforeTest51public void setUp() throws IOException {52// Create test directory inside scratch53testWorkDir = Paths.get(System.getProperty("user.dir", "."));54// Save its URL55testWorkDirUrl = testWorkDir.toUri().toURL();56// Get test source directory path57testSrcDir = Paths.get(System.getProperty("test.src", "."));58// Get path of xjc result folder59xjcResultDir = testWorkDir.resolve(TEST_PACKAGE);60// Copy schema document file to scratch directory61Files.copy(testSrcDir.resolve(XSD_FILENAME), testWorkDir.resolve(XSD_FILENAME), REPLACE_EXISTING);62}636465/*66* Test does the following steps to reproduce problem reported by 8145039:67* 1. Copy test schema to JTREG scratch folder68* 2. Run xjc on test schema file69* 3. Compile generated java files with test javac70* 4. Marshall the new list instance to ensure that71* ClassCastException is not thrown72*/73@Test74public void marshallClassCastExceptionTest() throws Exception {75JAXBContext jaxbContext;76Marshaller marshaller;77URLClassLoader jaxbContextClassLoader;78// Generate java classes by xjc79runXjc(XSD_FILENAME);80// Compile xjc generated java files81compileXjcGeneratedClasses();8283// Create JAXB context based on xjc generated package.84// Need to create URL class loader ot make compiled classes discoverable85// by JAXB context86jaxbContextClassLoader = URLClassLoader.newInstance(new URL[] {testWorkDirUrl});87jaxbContext = JAXBContext.newInstance( TEST_PACKAGE, jaxbContextClassLoader);8889// Create instance of Xjc generated data type.90// Java classes were compiled during the test execution hence reflection91// is needed here92Class classLongListClass = jaxbContextClassLoader.loadClass(TEST_CLASS);93Object objectLongListClass = classLongListClass.newInstance();94// Get 'getIn' method object95Method getInMethod = classLongListClass.getMethod( GET_LIST_METHOD, (Class [])null );96// Invoke 'getIn' method97List<Long> inList = (List<Long>)getInMethod.invoke(objectLongListClass);98// Add values into the jaxb object list99inList.add(Long.valueOf(0));100inList.add(Long.valueOf(43));101inList.add(Long.valueOf(1000000123));102103// Marshall constructed complex type variable to standard output.104// In case of failure the ClassCastException will be thrown105marshaller = jaxbContext.createMarshaller();106marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);107marshaller.marshal(objectLongListClass, System.out);108}109110// Compile schema file into java classes definitions111void runXjc(String xsdFileName) throws Exception {112// Prepare process builder to run schemagen tool and save its output113JDKToolLauncher xjcLauncher = JDKToolLauncher.createUsingTestJDK("xjc");114xjcLauncher.addToolArg(xsdFileName);115System.out.println("Executing xjc command: " + Arrays.asList(xjcLauncher.getCommand()));116ProcessBuilder pb = new ProcessBuilder(xjcLauncher.getCommand());117// Set xjc work directory with the input java file118pb.directory(testWorkDir.toFile());119pb.inheritIO();120Process p = pb.start();121p.waitFor();122p.destroy();123}124125// Compile java classes with javac tool126void compileXjcGeneratedClasses() throws Exception {127JDKToolLauncher javacLauncher = JDKToolLauncher.createUsingTestJDK("javac");128javacLauncher.addToolArg(xjcResultDir.resolve("ObjectFactory.java").toString());129javacLauncher.addToolArg(xjcResultDir.resolve("TypesLongList.java").toString());130javacLauncher.addToolArg(xjcResultDir.resolve("package-info.java").toString());131System.out.println("Compiling xjc generated classes: " + Arrays.asList(javacLauncher.getCommand()));132ProcessBuilder pb = new ProcessBuilder(javacLauncher.getCommand());133pb.inheritIO();134pb.directory(testWorkDir.toFile());135Process p = pb.start();136p.waitFor();137p.destroy();138}139140// Test schema filename141static final String XSD_FILENAME = "testSchema.xsd";142// Package of java classes generated by xjc143static final String TEST_PACKAGE = "testns_package";144// Name of generated java class145static final String TEST_CLASS = TEST_PACKAGE+".TypesLongList";146// Method to get the list from xjc generated class147static final String GET_LIST_METHOD = "getIn";148// Test working directory149Path testWorkDir;150// Test working directory URL151URL testWorkDirUrl;152// Directory with test src153Path testSrcDir;154// Directory with java files generated by xjc155Path xjcResultDir;156}157158159