Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/DisposeStressTest/DisposeStressTest.java
38828 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25test26@bug 4051487 4145670 806202127@summary Tests that disposing of an empty Frame or a Frame with a MenuBar28while it is being created does not crash the VM.29@author dpm area=Threads30@run applet/timeout=7200 DisposeStressTest.html31*/3233// Note there is no @ in front of test above. This is so that the34// harness will not mistake this file as a test file. It should35// only see the html file as a test file. (the harness runs all36// valid test files, so it would run this test twice if this file37// were valid as well as the html file.)38// Also, note the area= after Your Name in the author tag. Here, you39// should put which functional area the test falls in. See the40// AWT-core home page -> test areas and/or -> AWT team for a list of41// areas.42// Note also the 'DisposeStressTest.html' in the run tag. This should43// be changed to the name of the test.444546/**47* DisposeStressTest.java48*49* summary:50*/5152import java.applet.Applet;53import java.awt.*;545556//Automated tests should run as applet tests if possible because they57// get their environments cleaned up, including AWT threads, any58// test created threads, and any system resources used by the test59// such as file descriptors. (This is normally not a problem as60// main tests usually run in a separate VM, however on some platforms61// such as the Mac, separate VMs are not possible and non-applet62// tests will cause problems). Also, you don't have to worry about63// synchronisation stuff in Applet tests they way you do in main64// tests...656667public class DisposeStressTest extends Applet68{69//Declare things used in the test, like buttons and labels here7071public void init()72{73//Create instructions for the user here, as well as set up74// the environment -- set the layout manager, add buttons,75// etc.7677this.setLayout (new BorderLayout ());7879String[] instructions =80{81"This is an AUTOMATIC test",82"simply wait until it is done"83};84Sysout.createDialog( );85Sysout.printInstructions( instructions );8687}//End init()8889public void start ()90{91for (int i = 0; i < 1000; i++) {92Frame f = new Frame();93f.setBounds(10, 10, 10, 10);94f.show();95f.dispose();9697Frame f2 = new Frame();98f2.setBounds(10, 10, 100, 100);99MenuBar bar = new MenuBar();100Menu menu = new Menu();101menu.add(new MenuItem("foo"));102bar.add(menu);103f2.setMenuBar(bar);104f2.show();105f2.dispose();106}107}// start()108109}// class DisposeStressTest110111112/****************************************************113Standard Test Machinery114DO NOT modify anything below -- it's a standard115chunk of code whose purpose is to make user116interaction uniform, and thereby make it simpler117to read and understand someone else's test.118****************************************************/119120/**121This is part of the standard test machinery.122It creates a dialog (with the instructions), and is the interface123for sending text messages to the user.124To print the instructions, send an array of strings to Sysout.createDialog125WithInstructions method. Put one line of instructions per array entry.126To display a message for the tester to see, simply call Sysout.println127with the string to be displayed.128This mimics System.out.println but works within the test harness as well129as standalone.130*/131132class Sysout133{134private static TestDialog dialog;135136public static void createDialogWithInstructions( String[] instructions )137{138dialog = new TestDialog( new Frame(), "Instructions" );139dialog.printInstructions( instructions );140dialog.show();141println( "Any messages for the tester will display here." );142}143144public static void createDialog( )145{146dialog = new TestDialog( new Frame(), "Instructions" );147String[] defInstr = { "Instructions will appear here. ", "" } ;148dialog.printInstructions( defInstr );149dialog.show();150println( "Any messages for the tester will display here." );151}152153154public static void printInstructions( String[] instructions )155{156dialog.printInstructions( instructions );157}158159160public static void println( String messageIn )161{162dialog.displayMessage( messageIn );163}164165}// Sysout class166167/**168This is part of the standard test machinery. It provides a place for the169test instructions to be displayed, and a place for interactive messages170to the user to be displayed.171To have the test instructions displayed, see Sysout.172To have a message to the user be displayed, see Sysout.173Do not call anything in this dialog directly.174*/175class TestDialog extends Dialog176{177178TextArea instructionsText;179TextArea messageText;180int maxStringLength = 80;181182//DO NOT call this directly, go through Sysout183public TestDialog( Frame frame, String name )184{185super( frame, name );186int scrollBoth = TextArea.SCROLLBARS_BOTH;187instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );188add( "North", instructionsText );189190messageText = new TextArea( "", 5, maxStringLength, scrollBoth );191add("South", messageText);192193pack();194195show();196}// TestDialog()197198//DO NOT call this directly, go through Sysout199public void printInstructions( String[] instructions )200{201//Clear out any current instructions202instructionsText.setText( "" );203204//Go down array of instruction strings205206String printStr, remainingStr;207for( int i=0; i < instructions.length; i++ )208{209//chop up each into pieces maxSringLength long210remainingStr = instructions[ i ];211while( remainingStr.length() > 0 )212{213//if longer than max then chop off first max chars to print214if( remainingStr.length() >= maxStringLength )215{216//Try to chop on a word boundary217int posOfSpace = remainingStr.218lastIndexOf( ' ', maxStringLength - 1 );219220if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;221222printStr = remainingStr.substring( 0, posOfSpace + 1 );223remainingStr = remainingStr.substring( posOfSpace + 1 );224}225//else just print226else227{228printStr = remainingStr;229remainingStr = "";230}231232instructionsText.append( printStr + "\n" );233234}// while235236}// for237238}//printInstructions()239240//DO NOT call this directly, go through Sysout241public void displayMessage( String messageIn )242{243messageText.append( messageIn + "\n" );244}245246}// TestDialog class247248249