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