1/*
2 * Copyright (c) 2015, 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 8073519
27 * @summary test that schemagen tool reports errors during
28 * xsd generation process
29 * @library /lib/testlibrary
30 * @run testng/othervm SchemagenErrorReporting
31 */
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.nio.file.Paths;
35import java.nio.file.StandardCopyOption;
36import java.util.Arrays;
37import java.util.stream.Collectors;
38import jdk.testlibrary.JDKToolLauncher;
39import org.testng.Assert;
40import org.testng.annotations.Test;
41
42public class SchemagenErrorReporting {
43
44    @Test
45    public void schemagenErrorReporting() throws Exception {
46        //schemagen tool output file name
47        final String SCHEMA_FILE = "schema1.xsd";
48        //Schemagen input java file with not compilable source
49        final String CLASS_FILE = "InputWithError.java";
50        //Test working, src directories and test output file
51        Path testWorkDir, testSrcDir, testOutput;
52
53        //Prepare test environment
54        //Create test directory inside scratch
55        testWorkDir = Paths.get(System.getProperty("user.dir", "."))
56                .resolve("SchemagenErrorReporting");
57        //Get test source directory
58        testSrcDir = Paths.get(System.getProperty("test.src", "."));
59        //Set test output file path
60        testOutput = testWorkDir.resolve("stdErrContent");
61        //Create test directory inside scratch directory
62        Files.createDirectory(testWorkDir);
63        //Copy java source from test.src to the test directory
64        Files.copy(testSrcDir.resolve(CLASS_FILE), testWorkDir.resolve(CLASS_FILE),
65                StandardCopyOption.REPLACE_EXISTING);
66
67        //Prepare process builder to run schemagen tool and save its output
68        JDKToolLauncher sgl = JDKToolLauncher.createUsingTestJDK("schemagen");
69        sgl.addToolArg(CLASS_FILE);
70        System.out.println("Executing: " + Arrays.asList(sgl.getCommand()));
71        ProcessBuilder pb = new ProcessBuilder(sgl.getCommand());
72        //Set schemagen work directory with the input java file
73        pb.directory(testWorkDir.toFile());
74        //Redirect schemagen output to file
75        pb.redirectError(testOutput.toFile());
76        Process p = pb.start();
77        int result = p.waitFor();
78        p.destroy();
79
80        //Read schemagen output from the file
81        String stdErrContent = Files.lines(testOutput)
82                .collect(Collectors.joining(System.lineSeparator(), System.lineSeparator(), ""));
83        System.out.println("Schemagen return value:" + result);
84        System.out.println("Error output:" + stdErrContent);
85        //Check test results:
86        //Schemagen finished with non-0 return value
87        Assert.assertNotEquals(result, 0);
88        //Schemagen output contains compile error message
89        Assert.assertTrue(stdErrContent.contains("InputWithError.java:28: error"));
90    }
91}
92