1 /*
   2  * Copyright (c) 2014, 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 8032884 8072579
  27  * @summary Globalbindings optionalProperty="primitive" does not work when minOccurs=0
  28  * @library /lib/testlibrary
  29  * @modules java.xml.bind
  30  * @run testng/othervm XjcOptionalPropertyTest
  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 jdk.testlibrary.JDKToolLauncher;
  43 import org.testng.Assert;
  44 import org.testng.annotations.BeforeTest;
  45 import org.testng.annotations.Test;
  46 
  47 public class XjcOptionalPropertyTest {
  48 
  49     @Test
  50     public void optionalPropertyTest() throws Exception {
  51         runXjc();
  52         compileXjcGeneratedClasses();
  53         URLClassLoader testClassLoader;
  54         testClassLoader = URLClassLoader.newInstance(new URL[]{testWorkDirUrl});
  55         Class fooClass = testClassLoader.loadClass(CLASS_TO_TEST);
  56         Object foo = fooClass.newInstance();
  57         Method[] methods = foo.getClass().getMethods();
  58         System.out.println("Found [" + methods.length + "] methods");
  59         for (int i = 0; i < methods.length; i++) {
  60             Method method = methods[i];
  61             if (method.getName().equals("setFoo")) {
  62                 System.out.println("Checking method [" + method.getName() + "]");
  63                 Class[] parameterTypes = method.getParameterTypes();
  64                 Assert.assertEquals(parameterTypes.length, 1);
  65                 Assert.assertTrue(parameterTypes[0].isPrimitive());
  66                 break;
  67             }
  68         }
  69     }
  70 
  71     @BeforeTest
  72     public void setUp() throws IOException {
  73         // Create test directory inside scratch
  74         testWorkDir = Paths.get(System.getProperty("user.dir", "."));
  75         // Save its URL
  76         testWorkDirUrl = testWorkDir.toUri().toURL();
  77         // Get test source directory path
  78         testSrcDir = Paths.get(System.getProperty("test.src", "."));
  79         // Get path of xjc result folder
  80         xjcResultDir = testWorkDir.resolve(TEST_PACKAGE);
  81         // Copy schema document file to scratch directory
  82         Files.copy(testSrcDir.resolve(XSD_FILENAME), testWorkDir.resolve(XSD_FILENAME), REPLACE_EXISTING);
  83     }
  84 
  85     // Compile schema file into java classes definitions
  86     void runXjc() throws Exception {
  87         // Prepare process builder to run schemagen tool and save its output
  88         JDKToolLauncher xjcLauncher = JDKToolLauncher.createUsingTestJDK("xjc");
  89         xjcLauncher.addToolArg(XSD_FILENAME);
  90         System.out.println("Executing xjc command: " + Arrays.asList(xjcLauncher.getCommand()));
  91         ProcessBuilder pb = new ProcessBuilder(xjcLauncher.getCommand());
  92         // Set xjc work directory with the input java file
  93         pb.directory(testWorkDir.toFile());
  94         pb.inheritIO();
  95         Process p = pb.start();
  96         p.waitFor();
  97         p.destroy();
  98     }
  99 
 100     // Compile java classes with javac tool
 101     void compileXjcGeneratedClasses() throws Exception {
 102         JDKToolLauncher javacLauncher = JDKToolLauncher.createUsingTestJDK("javac");
 103         javacLauncher.addToolArg("-addmods");
 104         javacLauncher.addToolArg("java.xml.bind");
 105         javacLauncher.addToolArg(xjcResultDir.resolve("Foo.java").toString());
 106         System.out.println("Compiling xjc generated class: " + Arrays.asList(javacLauncher.getCommand()));
 107         ProcessBuilder pb = new ProcessBuilder(javacLauncher.getCommand());
 108         pb.inheritIO();
 109         pb.directory(testWorkDir.toFile());
 110         Process p = pb.start();
 111         p.waitFor();
 112         p.destroy();
 113     }
 114 
 115     // Test schema filename
 116     static final String XSD_FILENAME = "optional-property-schema.xsd";
 117     // Test package with generated class
 118     static final String TEST_PACKAGE = "anamespace";
 119     // Name of generated java class
 120     static final String CLASS_TO_TEST = TEST_PACKAGE+".Foo";
 121     // Test working directory
 122     Path testWorkDir;
 123     // Test working directory URL
 124     URL testWorkDirUrl;
 125     // Directory with test src
 126     Path testSrcDir;
 127     // Directory with java files generated by xjc
 128     Path xjcResultDir;
 129 }