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