Path: blob/master/test/jdk/javax/sound/midi/MidiSystem/6411624/Test6411624.java
41161 views
/*1* Copyright (c) 2006, 2016, 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*/2223import java.awt.Button;24import java.awt.Dialog;25import java.awt.Frame;26import java.awt.Panel;27import java.awt.TextArea;28import java.awt.event.ActionEvent;29import java.awt.event.ActionListener;3031/**32* @test33* @bug 641162434* @summary Tests that MidiSystem.getReceiver() & MidiSystem.getTransmitter35* doesn't return sequencer36* @build bug641162437* @run main/manual Test641162438*/39public class Test6411624 {4041private static void init() throws Exception {42//*** Create instructions for the user here ***4344String[] instructions =45{46"This test should only be run on solaris or linux system",47"WITHOUT audio card installed (to test on SunRay set",48"incorrect $AUDIODEV value).",49"If you system does not meet this conditions, press PASS.",50"To run the test follow these instructions:",51"1. Open a terminal window.",52"2. Type \"cd " + System.getProperty("test.classes") + "\".",53"3. Type \"" + System.getProperty("java.home") + "/bin/java bug6411624\".",54"4. Follow the instructions shown in the terminal window.",55"If you see \"All tests sucessfully passed\", press PASS else press FAIL."56};5758Sysout.createDialog( );59Sysout.printInstructions( instructions );6061}6263/*****************************************************64Standard Test Machinery Section65DO NOT modify anything in this section -- it's a66standard chunk of code which has all of the67synchronisation necessary for the test harness.68By keeping it the same in all tests, it is easier69to read and understand someone else's test, as70well as insuring that all tests behave correctly71with the test harness.72There is a section following this for test-defined73classes74******************************************************/75private static boolean theTestPassed = false;76private static boolean testGeneratedInterrupt = false;77private static String failureMessage = "";7879private static Thread mainThread = null;8081private static int sleepTime = 300000;8283public static void main( String args[] ) throws Exception84{85mainThread = Thread.currentThread();86try87{88init();89}90catch( TestPassedException e )91{92//The test passed, so just return from main and harness will93// interepret this return as a pass94return;95}96//At this point, neither test passed nor test failed has been97// called -- either would have thrown an exception and ended the98// test, so we know we have multiple threads.99100//Test involves other threads, so sleep and wait for them to101// called pass() or fail()102try103{104Thread.sleep( sleepTime );105//Timed out, so fail the test106throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );107}108catch (InterruptedException e)109{110if( ! testGeneratedInterrupt ) throw e;111112//reset flag in case hit this code more than once for some reason (just safety)113testGeneratedInterrupt = false;114if ( theTestPassed == false )115{116throw new RuntimeException( failureMessage );117}118}119120}//main121122public static synchronized void setTimeoutTo( int seconds )123{124sleepTime = seconds * 1000;125}126127public static synchronized void pass()128{129Sysout.println( "The test passed." );130Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );131//first check if this is executing in main thread132if ( mainThread == Thread.currentThread() )133{134//Still in the main thread, so set the flag just for kicks,135// and throw a test passed exception which will be caught136// and end the test.137theTestPassed = true;138throw new TestPassedException();139}140//pass was called from a different thread, so set the flag and interrupt141// the main thead.142theTestPassed = true;143testGeneratedInterrupt = true;144mainThread.interrupt();145}//pass()146147public static synchronized void fail()148{149//test writer didn't specify why test failed, so give generic150fail( "it just plain failed! :-)" );151}152153public static synchronized void fail( String whyFailed )154{155Sysout.println( "The test failed: " + whyFailed );156Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );157//check if this called from main thread158if ( mainThread == Thread.currentThread() )159{160//If main thread, fail now 'cause not sleeping161throw new RuntimeException( whyFailed );162}163theTestPassed = false;164testGeneratedInterrupt = true;165failureMessage = whyFailed;166mainThread.interrupt();167}//fail()168169}// class Orient170171//This exception is used to exit from any level of call nesting172// when it's determined that the test has passed, and immediately173// end the test.174class TestPassedException extends RuntimeException175{176}177178//*********** End Standard Test Machinery Section **********179180181//************ Begin classes defined for the test ****************182183// make listeners in a class defined here, and instantiate them in init()184185/* Example of a class which may be written as part of a test186class NewClass implements anInterface187{188static int newVar = 0;189190public void eventDispatched(AWTEvent e)191{192//Counting events to see if we get enough193eventCount++;194195if( eventCount == 20 )196{197//got enough events, so pass198199Orient.pass();200}201else if( tries == 20 )202{203//tried too many times without getting enough events so fail204205Orient.fail();206}207208}// eventDispatched()209210}// NewClass class211212*/213214215//************** End classes defined for the test *******************216217218219220/****************************************************221Standard Test Machinery222DO NOT modify anything below -- it's a standard223chunk of code whose purpose is to make user224interaction uniform, and thereby make it simpler225to read and understand someone else's test.226****************************************************/227228/**229This is part of the standard test machinery.230It creates a dialog (with the instructions), and is the interface231for sending text messages to the user.232To print the instructions, send an array of strings to Sysout.createDialog233WithInstructions method. Put one line of instructions per array entry.234To display a message for the tester to see, simply call Sysout.println235with the string to be displayed.236This mimics System.out.println but works within the test harness as well237as standalone.238*/239240class Sysout241{242private static TestDialog dialog;243244public static void createDialogWithInstructions( String[] instructions )245{246dialog = new TestDialog( new Frame(), "Instructions" );247dialog.printInstructions( instructions );248dialog.show();249println( "Any messages for the tester will display here." );250}251252public static void createDialog( )253{254dialog = new TestDialog( new Frame(), "Instructions" );255String[] defInstr = { "Instructions will appear here. ", "" } ;256dialog.printInstructions( defInstr );257dialog.show();258println( "Any messages for the tester will display here." );259}260261262public static void printInstructions( String[] instructions )263{264dialog.printInstructions( instructions );265}266267268public static void println( String messageIn )269{270dialog.displayMessage( messageIn );271}272273}// Sysout class274275/**276This is part of the standard test machinery. It provides a place for the277test instructions to be displayed, and a place for interactive messages278to the user to be displayed.279To have the test instructions displayed, see Sysout.280To have a message to the user be displayed, see Sysout.281Do not call anything in this dialog directly.282*/283class TestDialog extends Dialog implements ActionListener284{285286TextArea instructionsText;287TextArea messageText;288int maxStringLength = 80;289Panel buttonP = new Panel();290Button passB = new Button( "pass" );291Button failB = new Button( "fail" );292293//DO NOT call this directly, go through Sysout294public TestDialog( Frame frame, String name )295{296super( frame, name );297int scrollBoth = TextArea.SCROLLBARS_BOTH;298instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );299add( "North", instructionsText );300301messageText = new TextArea( "", 5, maxStringLength, scrollBoth );302add("Center", messageText);303304passB = new Button( "pass" );305passB.setActionCommand( "pass" );306passB.addActionListener( this );307buttonP.add( "East", passB );308309failB = new Button( "fail" );310failB.setActionCommand( "fail" );311failB.addActionListener( this );312buttonP.add( "West", failB );313314add( "South", buttonP );315pack();316317show();318}// TestDialog()319320//DO NOT call this directly, go through Sysout321public void printInstructions( String[] instructions )322{323//Clear out any current instructions324instructionsText.setText( "" );325326//Go down array of instruction strings327328String printStr, remainingStr;329for( int i=0; i < instructions.length; i++ )330{331//chop up each into pieces maxSringLength long332remainingStr = instructions[ i ];333while( remainingStr.length() > 0 )334{335//if longer than max then chop off first max chars to print336if( remainingStr.length() >= maxStringLength )337{338//Try to chop on a word boundary339int posOfSpace = remainingStr.340lastIndexOf( ' ', maxStringLength - 1 );341342if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;343344printStr = remainingStr.substring( 0, posOfSpace + 1 );345remainingStr = remainingStr.substring( posOfSpace + 1 );346}347//else just print348else349{350printStr = remainingStr;351remainingStr = "";352}353354instructionsText.append( printStr + "\n" );355356}// while357358}// for359360}//printInstructions()361362//DO NOT call this directly, go through Sysout363public void displayMessage( String messageIn )364{365messageText.append( messageIn + "\n" );366}367368//catch presses of the passed and failed buttons.369//simply call the standard pass() or fail() static methods of370//DialogOrient371public void actionPerformed( ActionEvent e )372{373if( e.getActionCommand() == "pass" )374{375Test6411624.pass();376}377else378{379Test6411624.fail();380}381}382383}// TestDialog class384385386