Path: blob/master/test/jdk/java/awt/Mixing/setComponentZOrder.java
41149 views
/*1* Copyright (c) 2007, 2018, 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*/2223/*24@test25@key headful26@bug 658953027@summary Mixing code should correctly handle insertion of components with setComponentZOrder.28@author antohny.petrov@...: area=awt.mixing29@run main setComponentZOrder30*/3132/**33* setComponentZOrder.java34*35* summary: Mixing code should correctly handle insertion of components with setComponentZOrder.36*/3738import java.awt.*;39import java.awt.event.*;404142public class setComponentZOrder43{4445//*** test-writer defined static variables go here ***464748private static void init()49{50try {51Container c = new Container();52Button b = new Button("b");53c.setComponentZOrder(b, 0);54} catch (ArrayIndexOutOfBoundsException e) {55e.printStackTrace();56fail("The setComponentZOrder method used to insert a component caused the mixing code to throw the exception: " + e);57}5859pass();6061}//End init()62636465/*****************************************************66* Standard Test Machinery Section67* DO NOT modify anything in this section -- it's a68* standard chunk of code which has all of the69* synchronisation necessary for the test harness.70* By keeping it the same in all tests, it is easier71* to read and understand someone else's test, as72* well as insuring that all tests behave correctly73* with the test harness.74* There is a section following this for test-75* classes76******************************************************/77private static boolean theTestPassed = false;78private static boolean testGeneratedInterrupt = false;79private static String failureMessage = "";8081private static Thread mainThread = null;8283private static int sleepTime = 300000;8485// Not sure about what happens if multiple of this test are86// instantiated in the same VM. Being static (and using87// static vars), it aint gonna work. Not worrying about88// it for now.89public static void main( String args[] ) throws InterruptedException90{91mainThread = Thread.currentThread();92try93{94init();95}96catch( TestPassedException e )97{98//The test passed, so just return from main and harness will99// interepret this return as a pass100return;101}102//At this point, neither test pass nor test fail has been103// called -- either would have thrown an exception and ended the104// test, so we know we have multiple threads.105106//Test involves other threads, so sleep and wait for them to107// called pass() or fail()108try109{110Thread.sleep( sleepTime );111//Timed out, so fail the test112throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );113}114catch (InterruptedException e)115{116//The test harness may have interrupted the test. If so, rethrow the exception117// so that the harness gets it and deals with it.118if( ! testGeneratedInterrupt ) throw e;119120//reset flag in case hit this code more than once for some reason (just safety)121testGeneratedInterrupt = false;122123if ( theTestPassed == false )124{125throw new RuntimeException( failureMessage );126}127}128129}//main130131public static synchronized void setTimeoutTo( int seconds )132{133sleepTime = seconds * 1000;134}135136public static synchronized void pass()137{138System.out.println( "The test passed." );139System.out.println( "The test is over, hit Ctl-C to stop Java VM" );140//first check if this is executing in main thread141if ( mainThread == Thread.currentThread() )142{143//Still in the main thread, so set the flag just for kicks,144// and throw a test passed exception which will be caught145// and end the test.146theTestPassed = true;147throw new TestPassedException();148}149theTestPassed = true;150testGeneratedInterrupt = true;151mainThread.interrupt();152}//pass()153154public static synchronized void fail()155{156//test writer didn't specify why test failed, so give generic157fail( "it just plain failed! :-)" );158}159160public static synchronized void fail( String whyFailed )161{162System.out.println( "The test failed: " + whyFailed );163System.out.println( "The test is over, hit Ctl-C to stop Java VM" );164//check if this called from main thread165if ( mainThread == Thread.currentThread() )166{167//If main thread, fail now 'cause not sleeping168throw new RuntimeException( whyFailed );169}170theTestPassed = false;171testGeneratedInterrupt = true;172failureMessage = whyFailed;173mainThread.interrupt();174}//fail()175176}// class setComponentZOrder177178//This exception is used to exit from any level of call nesting179// when it's determined that the test has passed, and immediately180// end the test.181class TestPassedException extends RuntimeException182{183}184185186