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 8073872
  27  * @summary test that stackoverflow is not observable when element
  28  *          references containing class
  29  * @modules java.xml
  30  * @modules java.xml.bind
  31  * @compile Foo.java
  32  * @run testng/othervm SchemagenStackOverflow
  33  */
  34 
  35 import java.io.IOException;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.stream.Collectors;
  40 import javax.xml.bind.JAXBContext;
  41 import javax.xml.bind.SchemaOutputResolver;
  42 import javax.xml.transform.Result;
  43 import javax.xml.transform.stream.StreamResult;
  44 import org.testng.Assert;
  45 import org.testng.annotations.Test;
  46 
  47 public class SchemagenStackOverflow {
  48 
  49     @Test
  50     public void schemagenStackOverflowTest() throws Exception {
  51         // Create new instance of JAXB context
  52         JAXBContext context = JAXBContext.newInstance(Foo.class);
  53         context.generateSchema(new TestOutputResolver());
  54 
  55         // Read schema content from file
  56         String content = Files.lines(resultSchemaFile).collect(Collectors.joining(""));
  57         System.out.println("Generated schema content:" + content);
  58 
  59         // Check if schema was generated: check class and list object names
  60         Assert.assertTrue(content.contains("name=\"Foo\""));
  61         Assert.assertTrue(content.contains("name=\"fooObject\""));
  62     }
  63 
  64     // Schemagen output resolver
  65     class TestOutputResolver extends SchemaOutputResolver {
  66         @Override
  67         public Result createOutput(String namespaceUri, String fileName)
  68                 throws IOException {
  69             return new StreamResult(resultSchemaFile.toFile());
  70         }
  71     }
  72 
  73     // Schemagen output file name and path
  74     static final String SCHEMA_FILENAME = "generatedSchema.xsd";
  75     Path resultSchemaFile = Paths.get(System.getProperty("user.dir", "."))
  76             .resolve(SCHEMA_FILENAME);
  77 
  78 }