1 /*
   2  * Copyright (c) 2013, 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 import java.util.Vector;
  25 import javax.swing.text.html.HTML;
  26 import javax.swing.text.html.HTMLDocument;
  27 import javax.swing.text.html.HTMLEditorKit;
  28 import javax.swing.text.html.parser.DTD;
  29 import javax.swing.text.html.parser.Element;
  30 import sun.awt.SunToolkit;
  31 
  32 /*
  33  * @test
  34  * @bug 8017492
  35  * @modules java.desktop/sun.awt
  36  * @run main/othervm Test8017492
  37  * @summary Tests for OutOfMemoryError/NegativeArraySizeException
  38  * @author Sergey Malenkov
  39  */
  40 
  41 public class Test8017492 {
  42     public static void main(String[] args) throws Exception {
  43         Runnable task = new Runnable() {
  44             @Override
  45             public void run() {
  46                 try {
  47                     SunToolkit.createNewAppContext();
  48                     DTD dtd = DTD.getDTD("dtd");
  49                     dtd.elements = new Vector<Element>() {
  50                         @Override
  51                         public synchronized int size() {
  52                             return Integer.MAX_VALUE;
  53                         }
  54                     };
  55                     dtd.getElement("element");
  56                 }
  57                 catch (Exception exception) {
  58                     throw new Error("unexpected", exception);
  59                 }
  60             }
  61         };
  62         // run task with different AppContext
  63         Thread thread = new Thread(new ThreadGroup("$$$"), task);
  64         thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  65             @Override
  66             public void uncaughtException(Thread thread, Throwable throwable) {
  67                 throwable.printStackTrace();
  68                 System.exit(1);
  69             }
  70         });
  71         thread.start();
  72         thread.join();
  73         // add error handling
  74         HTMLDocument document = new HTMLDocument() {
  75             @Override
  76             public HTMLEditorKit.ParserCallback getReader(int pos) {
  77                 return getReader(pos, 0, 0, null);
  78             }
  79 
  80             @Override
  81             public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag) {
  82                 return new HTMLDocument.HTMLReader(pos, popDepth, pushDepth, insertTag) {
  83                     @Override
  84                     public void handleError(String error, int pos) {
  85                         throw new Error(error);
  86                     }
  87                 };
  88             }
  89         };
  90         // run parser
  91         new HTMLEditorKit().insertHTML(document, 0, "<html><body>text", 0, 0, null);
  92     }
  93 }