1 /*
   2  * Copyright (c) 2014, 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 /*
  26   test
  27   @bug 4051487 4145670 8062021
  28   @summary Tests that disposing of an empty Frame or a Frame with a MenuBar
  29            while it is being created does not crash the VM.
  30   @author dpm area=Threads
  31   @run applet/timeout=7200 DisposeStressTest.html
  32 */
  33 
  34 // Note there is no @ in front of test above.  This is so that the
  35 //  harness will not mistake this file as a test file.  It should
  36 //  only see the html file as a test file. (the harness runs all
  37 //  valid test files, so it would run this test twice if this file
  38 //  were valid as well as the html file.)
  39 // Also, note the area= after Your Name in the author tag.  Here, you
  40 //  should put which functional area the test falls in.  See the
  41 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  42 //  areas.
  43 // Note also the 'DisposeStressTest.html' in the run tag.  This should
  44 //  be changed to the name of the test.
  45 
  46 
  47 /**
  48  * DisposeStressTest.java
  49  *
  50  * summary:
  51  */
  52 
  53 import java.applet.Applet;
  54 import java.awt.*;
  55 
  56 
  57 //Automated tests should run as applet tests if possible because they
  58 // get their environments cleaned up, including AWT threads, any
  59 // test created threads, and any system resources used by the test
  60 // such as file descriptors.  (This is normally not a problem as
  61 // main tests usually run in a separate VM, however on some platforms
  62 // such as the Mac, separate VMs are not possible and non-applet
  63 // tests will cause problems).  Also, you don't have to worry about
  64 // synchronisation stuff in Applet tests they way you do in main
  65 // tests...
  66 
  67 
  68 public class DisposeStressTest extends Applet
  69  {
  70    //Declare things used in the test, like buttons and labels here
  71 
  72    public void init()
  73     {
  74       //Create instructions for the user here, as well as set up
  75       // the environment -- set the layout manager, add buttons,
  76       // etc.
  77 
  78       this.setLayout (new BorderLayout ());
  79 
  80       String[] instructions =
  81        {
  82          "This is an AUTOMATIC test",
  83          "simply wait until it is done"
  84        };
  85       Sysout.createDialog( );
  86       Sysout.printInstructions( instructions );
  87 
  88     }//End  init()
  89 
  90    public void start ()
  91     {
  92         for (int i = 0; i < 1000; i++) {
  93             Frame f = new Frame();
  94             f.setBounds(10, 10, 10, 10);
  95             f.show();
  96             f.dispose();
  97 
  98             Frame f2 = new Frame();
  99             f2.setBounds(10, 10, 100, 100);
 100             MenuBar bar = new MenuBar();
 101             Menu menu = new Menu();
 102             menu.add(new MenuItem("foo"));
 103             bar.add(menu);
 104             f2.setMenuBar(bar);
 105             f2.show();
 106             f2.dispose();
 107         }
 108     }// start()
 109 
 110  }// class DisposeStressTest
 111 
 112 
 113 /****************************************************
 114  Standard Test Machinery
 115  DO NOT modify anything below -- it's a standard
 116   chunk of code whose purpose is to make user
 117   interaction uniform, and thereby make it simpler
 118   to read and understand someone else's test.
 119  ****************************************************/
 120 
 121 /**
 122  This is part of the standard test machinery.
 123  It creates a dialog (with the instructions), and is the interface
 124   for sending text messages to the user.
 125  To print the instructions, send an array of strings to Sysout.createDialog
 126   WithInstructions method.  Put one line of instructions per array entry.
 127  To display a message for the tester to see, simply call Sysout.println
 128   with the string to be displayed.
 129  This mimics System.out.println but works within the test harness as well
 130   as standalone.
 131  */
 132 
 133 class Sysout
 134  {
 135    private static TestDialog dialog;
 136 
 137    public static void createDialogWithInstructions( String[] instructions )
 138     {
 139       dialog = new TestDialog( new Frame(), "Instructions" );
 140       dialog.printInstructions( instructions );
 141       dialog.show();
 142       println( "Any messages for the tester will display here." );
 143     }
 144 
 145    public static void createDialog( )
 146     {
 147       dialog = new TestDialog( new Frame(), "Instructions" );
 148       String[] defInstr = { "Instructions will appear here. ", "" } ;
 149       dialog.printInstructions( defInstr );
 150       dialog.show();
 151       println( "Any messages for the tester will display here." );
 152     }
 153 
 154 
 155    public static void printInstructions( String[] instructions )
 156     {
 157       dialog.printInstructions( instructions );
 158     }
 159 
 160 
 161    public static void println( String messageIn )
 162     {
 163       dialog.displayMessage( messageIn );
 164     }
 165 
 166  }// Sysout  class
 167 
 168 /**
 169   This is part of the standard test machinery.  It provides a place for the
 170    test instructions to be displayed, and a place for interactive messages
 171    to the user to be displayed.
 172   To have the test instructions displayed, see Sysout.
 173   To have a message to the user be displayed, see Sysout.
 174   Do not call anything in this dialog directly.
 175   */
 176 class TestDialog extends Dialog
 177  {
 178 
 179    TextArea instructionsText;
 180    TextArea messageText;
 181    int maxStringLength = 80;
 182 
 183    //DO NOT call this directly, go through Sysout
 184    public TestDialog( Frame frame, String name )
 185     {
 186       super( frame, name );
 187       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 188       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 189       add( "North", instructionsText );
 190 
 191       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 192       add("South", messageText);
 193 
 194       pack();
 195 
 196       show();
 197     }// TestDialog()
 198 
 199    //DO NOT call this directly, go through Sysout
 200    public void printInstructions( String[] instructions )
 201     {
 202       //Clear out any current instructions
 203       instructionsText.setText( "" );
 204 
 205       //Go down array of instruction strings
 206 
 207       String printStr, remainingStr;
 208       for( int i=0; i < instructions.length; i++ )
 209        {
 210          //chop up each into pieces maxSringLength long
 211          remainingStr = instructions[ i ];
 212          while( remainingStr.length() > 0 )
 213           {
 214             //if longer than max then chop off first max chars to print
 215             if( remainingStr.length() >= maxStringLength )
 216              {
 217                //Try to chop on a word boundary
 218                int posOfSpace = remainingStr.
 219                   lastIndexOf( ' ', maxStringLength - 1 );
 220 
 221                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 222 
 223                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 224                remainingStr = remainingStr.substring( posOfSpace + 1 );
 225              }
 226             //else just print
 227             else
 228              {
 229                printStr = remainingStr;
 230                remainingStr = "";
 231              }
 232 
 233             instructionsText.append( printStr + "\n" );
 234 
 235           }// while
 236 
 237        }// for
 238 
 239     }//printInstructions()
 240 
 241    //DO NOT call this directly, go through Sysout
 242    public void displayMessage( String messageIn )
 243     {
 244       messageText.append( messageIn + "\n" );
 245     }
 246 
 247  }// TestDialog  class