jdk/test/javax/xml/bind/xjc/8032884/XjcOptionalPropertyTest.java
author mchung
Wed, 10 Aug 2016 15:51:25 -0700
changeset 40261 86a49ba76f52
parent 38427 73d309f504f6
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers Reviewed-by: alanb, ksrini, henryjen, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     1
/*
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     4
 *
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     8
 *
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    13
 * accompanied this code).
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    14
 *
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    18
 *
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    21
 * questions.
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    22
 */
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    23
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    24
/*
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    25
 * @test
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    26
 * @bug 8032884 8072579
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    27
 * @summary Globalbindings optionalProperty="primitive" does not work when minOccurs=0
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    28
 * @library /lib/testlibrary
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    29
 * @modules java.xml.bind
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    30
 * @run testng/othervm XjcOptionalPropertyTest
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    31
 */
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    32
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    33
import java.io.IOException;
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    34
import java.lang.reflect.Method;
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    35
import java.net.URL;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    36
import java.net.URLClassLoader;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    37
import java.nio.file.Files;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    38
import java.nio.file.Path;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    39
import java.nio.file.Paths;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    40
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    41
import java.util.Arrays;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    42
import jdk.testlibrary.JDKToolLauncher;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    43
import org.testng.Assert;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    44
import org.testng.annotations.BeforeTest;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    45
import org.testng.annotations.Test;
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    46
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    47
public class XjcOptionalPropertyTest {
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    48
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    49
    @Test
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    50
    public void optionalPropertyTest() throws Exception {
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    51
        runXjc();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    52
        compileXjcGeneratedClasses();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    53
        URLClassLoader testClassLoader;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    54
        testClassLoader = URLClassLoader.newInstance(new URL[]{testWorkDirUrl});
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    55
        Class fooClass = testClassLoader.loadClass(CLASS_TO_TEST);
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    56
        Object foo = fooClass.newInstance();
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    57
        Method[] methods = foo.getClass().getMethods();
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    58
        System.out.println("Found [" + methods.length + "] methods");
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    59
        for (int i = 0; i < methods.length; i++) {
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    60
            Method method = methods[i];
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    61
            if (method.getName().equals("setFoo")) {
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    62
                System.out.println("Checking method [" + method.getName() + "]");
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    63
                Class[] parameterTypes = method.getParameterTypes();
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    64
                Assert.assertEquals(parameterTypes.length, 1);
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    65
                Assert.assertTrue(parameterTypes[0].isPrimitive());
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    66
                break;
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    67
            }
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    68
        }
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    69
    }
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    70
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    71
    @BeforeTest
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    72
    public void setUp() throws IOException {
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    73
        // Create test directory inside scratch
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    74
        testWorkDir = Paths.get(System.getProperty("user.dir", "."));
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    75
        // Save its URL
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    76
        testWorkDirUrl = testWorkDir.toUri().toURL();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    77
        // Get test source directory path
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    78
        testSrcDir = Paths.get(System.getProperty("test.src", "."));
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    79
        // Get path of xjc result folder
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    80
        xjcResultDir = testWorkDir.resolve(TEST_PACKAGE);
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    81
        // Copy schema document file to scratch directory
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    82
        Files.copy(testSrcDir.resolve(XSD_FILENAME), testWorkDir.resolve(XSD_FILENAME), REPLACE_EXISTING);
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    83
    }
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    84
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    85
    // Compile schema file into java classes definitions
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    86
    void runXjc() throws Exception {
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    87
        // Prepare process builder to run schemagen tool and save its output
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    88
        JDKToolLauncher xjcLauncher = JDKToolLauncher.createUsingTestJDK("xjc");
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    89
        xjcLauncher.addToolArg(XSD_FILENAME);
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    90
        System.out.println("Executing xjc command: " + Arrays.asList(xjcLauncher.getCommand()));
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    91
        ProcessBuilder pb = new ProcessBuilder(xjcLauncher.getCommand());
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    92
        // Set xjc work directory with the input java file
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    93
        pb.directory(testWorkDir.toFile());
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    94
        pb.inheritIO();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    95
        Process p = pb.start();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    96
        p.waitFor();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
    97
        p.destroy();
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    98
    }
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
    99
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   100
    // Compile java classes with javac tool
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   101
    void compileXjcGeneratedClasses() throws Exception {
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   102
        JDKToolLauncher javacLauncher = JDKToolLauncher.createUsingTestJDK("javac");
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38427
diff changeset
   103
        javacLauncher.addToolArg("--add-modules");
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   104
        javacLauncher.addToolArg("java.xml.bind");
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   105
        javacLauncher.addToolArg(xjcResultDir.resolve("Foo.java").toString());
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   106
        System.out.println("Compiling xjc generated class: " + Arrays.asList(javacLauncher.getCommand()));
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   107
        ProcessBuilder pb = new ProcessBuilder(javacLauncher.getCommand());
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   108
        pb.inheritIO();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   109
        pb.directory(testWorkDir.toFile());
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   110
        Process p = pb.start();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   111
        p.waitFor();
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   112
        p.destroy();
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
   113
    }
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
   114
38427
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   115
    // Test schema filename
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   116
    static final String XSD_FILENAME = "optional-property-schema.xsd";
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   117
    // Test package with generated class
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   118
    static final String TEST_PACKAGE = "anamespace";
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   119
    // Name of generated java class
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   120
    static final String CLASS_TO_TEST = TEST_PACKAGE+".Foo";
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   121
    // Test working directory
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   122
    Path testWorkDir;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   123
    // Test working directory URL
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   124
    URL testWorkDirUrl;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   125
    // Directory with test src
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   126
    Path testSrcDir;
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   127
    // Directory with java files generated by xjc
73d309f504f6 8072579: XjcOptionalPropertyTest.java creates files in test.src
aefimov
parents: 37779
diff changeset
   128
    Path xjcResultDir;
23713
d3927a03a94e 8032884: Globalbindings optionalProperty=primitive does not work when minOccurs=0
mkos
parents:
diff changeset
   129
}