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 8157096
  27  * @summary Check that marshalling of xjc generated class doesn't throw
  28  *          ClassCast exception.
  29  * @modules java.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 ensure that
  73      *    ClassCastException is not thrown
  74      */
  75     @Test
  76     public void marshallClassCastExceptionTest() throws Exception {
  77         JAXBContext jaxbContext;
  78         Marshaller marshaller;
  79         URLClassLoader jaxbContextClassLoader;
  80         // Generate java classes by xjc
  81         runXjc(XSD_FILENAME);
  82         // Compile xjc generated java files
  83         compileXjcGeneratedClasses();
  84 
  85         // Create JAXB context based on xjc generated package.
  86         // Need to create URL class loader ot make compiled classes discoverable
  87         // by JAXB context
  88         jaxbContextClassLoader = URLClassLoader.newInstance(new URL[] {testWorkDirUrl});
  89         jaxbContext = JAXBContext.newInstance( TEST_PACKAGE, jaxbContextClassLoader);
  90 
  91         // Create instance of Xjc generated data type.
  92         // Java classes were compiled during the test execution hence reflection
  93         // is needed here
  94         Class classLongListClass = jaxbContextClassLoader.loadClass(TEST_CLASS);
  95         Object objectLongListClass = classLongListClass.newInstance();
  96         // Get 'getIn' method object
  97         Method getInMethod = classLongListClass.getMethod( GET_LIST_METHOD, (Class [])null );
  98         // Invoke 'getIn' method
  99         List<Long> inList = (List<Long>)getInMethod.invoke(objectLongListClass);
 100         // Add values into the jaxb object list
 101         inList.add(Long.valueOf(0));
 102         inList.add(Long.valueOf(43));
 103         inList.add(Long.valueOf(1000000123));
 104 
 105         // Marshall constructed complex type variable to standard output.
 106         // In case of failure the ClassCastException will be thrown
 107         marshaller = jaxbContext.createMarshaller();
 108         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 109         marshaller.marshal(objectLongListClass, System.out);
 110     }
 111 
 112     // Compile schema file into java classes definitions
 113     void runXjc(String xsdFileName) throws Exception {
 114         // Prepare process builder to run schemagen tool and save its output
 115         JDKToolLauncher xjcLauncher = JDKToolLauncher.createUsingTestJDK("xjc");
 116         xjcLauncher.addToolArg(xsdFileName);
 117         System.out.println("Executing xjc command: " + Arrays.asList(xjcLauncher.getCommand()));
 118         ProcessBuilder pb = new ProcessBuilder(xjcLauncher.getCommand());
 119         // Set xjc work directory with the input java file
 120         pb.directory(testWorkDir.toFile());
 121         pb.inheritIO();
 122         Process p = pb.start();
 123         p.waitFor();
 124         p.destroy();
 125     }
 126 
 127     // Compile java classes with javac tool
 128     void compileXjcGeneratedClasses() throws Exception {
 129         JDKToolLauncher javacLauncher = JDKToolLauncher.createUsingTestJDK("javac");
 130         javacLauncher.addToolArg("--add-modules");
 131         javacLauncher.addToolArg("java.xml.bind");
 132         javacLauncher.addToolArg(xjcResultDir.resolve("ObjectFactory.java").toString());
 133         javacLauncher.addToolArg(xjcResultDir.resolve("TypesLongList.java").toString());
 134         javacLauncher.addToolArg(xjcResultDir.resolve("package-info.java").toString());
 135         System.out.println("Compiling xjc generated classes: " + Arrays.asList(javacLauncher.getCommand()));
 136         ProcessBuilder pb = new ProcessBuilder(javacLauncher.getCommand());
 137         pb.inheritIO();
 138         pb.directory(testWorkDir.toFile());
 139         Process p = pb.start();
 140         p.waitFor();
 141         p.destroy();
 142     }
 143 
 144     // Test schema filename
 145     static final String XSD_FILENAME = "testSchema.xsd";
 146     // Package of java classes generated by xjc
 147     static final String TEST_PACKAGE = "testns_package";
 148     // Name of generated java class
 149     static final String TEST_CLASS = TEST_PACKAGE+".TypesLongList";
 150     // Method to get the list from xjc generated class
 151     static final String GET_LIST_METHOD = "getIn";
 152     // Test working directory
 153     Path testWorkDir;
 154     // Test working directory URL
 155     URL testWorkDirUrl;
 156     // Directory with test src
 157     Path testSrcDir;
 158     // Directory with java files generated by xjc
 159     Path xjcResultDir;
 160 }