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 * @modules javax.xml.bind 30 * @library /lib/testlibrary 31 * @run testng/othervm JaxbMarshallTest 32 */ 33 34 import java.io.IOException; 35 import java.lang.reflect.Method; 36 import java.net.URL; 37 import java.net.URLClassLoader; 38 import java.nio.file.Files; 39 import java.nio.file.Path; 40 import java.nio.file.Paths; 41 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 42 import java.util.Arrays; 43 import java.util.List; 44 import javax.xml.bind.JAXBContext; 45 import javax.xml.bind.Marshaller; 46 import jdk.testlibrary.JDKToolLauncher; 47 import org.testng.annotations.BeforeTest; 48 import org.testng.annotations.Test; 49 50 public class JaxbMarshallTest { 51 52 @BeforeTest 53 public void setUp() throws IOException { 54 //Create test directory inside scratch 55 testWorkDir = Paths.get(System.getProperty("user.dir", ".")); 56 //Save its URL 57 testWorkDirUrl = testWorkDir.toUri().toURL(); 58 //Get test source directory path 59 testSrcDir = Paths.get(System.getProperty("test.src", ".")); 60 //Get path of xjc result folder 61 xjcResultDir = testWorkDir.resolve(TEST_PACKAGE); 62 //Copy schema document file to scratch directory 63 Files.copy(testSrcDir.resolve(XSD_FILENAME), testWorkDir.resolve(XSD_FILENAME), REPLACE_EXISTING); 64 } 65 66 67 /* 68 * Test does the following steps to reproduce problem reported by 8145039: 69 * 1. Copy test schema to JTREG scratch folder 70 * 2. Run xjc on test schema file 71 * 3. Compile generated java files with test javac 72 * 4. Marshall the new list instance to reproduce reported ClassCastException 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 }