1 /*
   2  * Copyright (c) 2006, 2016, 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.awt.Button;
  25 import java.awt.Dialog;
  26 import java.awt.Frame;
  27 import java.awt.Panel;
  28 import java.awt.TextArea;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.ActionListener;
  31 
  32 /**
  33  * @test
  34  * @bug 6411624
  35  * @summary Tests that MidiSystem.getReceiver() & MidiSystem.getTransmitter
  36  *          doesn't return sequencer
  37  * @build bug6411624
  38  * @run main/manual Test6411624
  39  */
  40 public class Test6411624 {
  41 
  42    private static void init() throws Exception {
  43         //*** Create instructions for the user here ***
  44 
  45         String[] instructions =
  46         {
  47          "This test should only be run on solaris or linux system",
  48          "WITHOUT audio card installed (to test on SunRay set",
  49          "incorrect $AUDIODEV value).",
  50          "If you system does not meet this conditions, press PASS.",
  51          "To run the test follow these instructions:",
  52          "1. Open a terminal window.",
  53          "2. Type \"cd " + System.getProperty("test.classes") + "\".",
  54          "3. Type \"" + System.getProperty("java.home") + "/bin/java bug6411624\".",
  55          "4. Follow the instructions shown in the terminal window.",
  56          "If you see \"All tests sucessfully passed\", press PASS else press FAIL."
  57        };
  58 
  59       Sysout.createDialog( );
  60       Sysout.printInstructions( instructions );
  61 
  62     }
  63 
  64  /*****************************************************
  65      Standard Test Machinery Section
  66       DO NOT modify anything in this section -- it's a
  67       standard chunk of code which has all of the
  68       synchronisation necessary for the test harness.
  69       By keeping it the same in all tests, it is easier
  70       to read and understand someone else's test, as
  71       well as insuring that all tests behave correctly
  72       with the test harness.
  73      There is a section following this for test-defined
  74       classes
  75    ******************************************************/
  76    private static boolean theTestPassed = false;
  77    private static boolean testGeneratedInterrupt = false;
  78    private static String failureMessage = "";
  79 
  80    private static Thread mainThread = null;
  81 
  82    private static int sleepTime = 300000;
  83 
  84    public static void main( String args[] ) throws Exception
  85     {
  86       mainThread = Thread.currentThread();
  87       try
  88        {
  89          init();
  90        }
  91       catch( TestPassedException e )
  92        {
  93          //The test passed, so just return from main and harness will
  94          // interepret this return as a pass
  95          return;
  96        }
  97       //At this point, neither test passed nor test failed has been
  98       // called -- either would have thrown an exception and ended the
  99       // test, so we know we have multiple threads.
 100 
 101       //Test involves other threads, so sleep and wait for them to
 102       // called pass() or fail()
 103       try
 104        {
 105          Thread.sleep( sleepTime );
 106          //Timed out, so fail the test
 107          throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 108        }
 109       catch (InterruptedException e)
 110        {
 111          if( ! testGeneratedInterrupt ) throw e;
 112 
 113          //reset flag in case hit this code more than once for some reason (just safety)
 114          testGeneratedInterrupt = false;
 115          if ( theTestPassed == false )
 116           {
 117             throw new RuntimeException( failureMessage );
 118           }
 119        }
 120 
 121     }//main
 122 
 123    public static synchronized void setTimeoutTo( int seconds )
 124     {
 125       sleepTime = seconds * 1000;
 126     }
 127 
 128    public static synchronized void pass()
 129     {
 130       Sysout.println( "The test passed." );
 131       Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 132       //first check if this is executing in main thread
 133       if ( mainThread == Thread.currentThread() )
 134        {
 135          //Still in the main thread, so set the flag just for kicks,
 136          // and throw a test passed exception which will be caught
 137          // and end the test.
 138          theTestPassed = true;
 139          throw new TestPassedException();
 140        }
 141       //pass was called from a different thread, so set the flag and interrupt
 142       // the main thead.
 143       theTestPassed = true;
 144       testGeneratedInterrupt = true;
 145       mainThread.interrupt();
 146     }//pass()
 147 
 148    public static synchronized void fail()
 149     {
 150       //test writer didn't specify why test failed, so give generic
 151       fail( "it just plain failed! :-)" );
 152     }
 153 
 154    public static synchronized void fail( String whyFailed )
 155     {
 156       Sysout.println( "The test failed: " + whyFailed );
 157       Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 158       //check if this called from main thread
 159       if ( mainThread == Thread.currentThread() )
 160        {
 161          //If main thread, fail now 'cause not sleeping
 162          throw new RuntimeException( whyFailed );
 163        }
 164       theTestPassed = false;
 165       testGeneratedInterrupt = true;
 166       failureMessage = whyFailed;
 167       mainThread.interrupt();
 168     }//fail()
 169 
 170  }// class Orient
 171 
 172 //This exception is used to exit from any level of call nesting
 173 // when it's determined that the test has passed, and immediately
 174 // end the test.
 175 class TestPassedException extends RuntimeException
 176  {
 177  }
 178 
 179 //*********** End Standard Test Machinery Section **********
 180 
 181 
 182 //************ Begin classes defined for the test ****************
 183 
 184 // make listeners in a class defined here, and instantiate them in init()
 185 
 186 /* Example of a class which may be written as part of a test
 187 class NewClass implements anInterface
 188  {
 189    static int newVar = 0;
 190 
 191    public void eventDispatched(AWTEvent e)
 192     {
 193       //Counting events to see if we get enough
 194       eventCount++;
 195 
 196       if( eventCount == 20 )
 197        {
 198          //got enough events, so pass
 199 
 200          Orient.pass();
 201        }
 202       else if( tries == 20 )
 203        {
 204          //tried too many times without getting enough events so fail
 205 
 206          Orient.fail();
 207        }
 208 
 209     }// eventDispatched()
 210 
 211  }// NewClass class
 212 
 213 */
 214 
 215 
 216 //************** End classes defined for the test *******************
 217 
 218 
 219 
 220 
 221 /****************************************************
 222  Standard Test Machinery
 223  DO NOT modify anything below -- it's a standard
 224   chunk of code whose purpose is to make user
 225   interaction uniform, and thereby make it simpler
 226   to read and understand someone else's test.
 227  ****************************************************/
 228 
 229 /**
 230  This is part of the standard test machinery.
 231  It creates a dialog (with the instructions), and is the interface
 232   for sending text messages to the user.
 233  To print the instructions, send an array of strings to Sysout.createDialog
 234   WithInstructions method.  Put one line of instructions per array entry.
 235  To display a message for the tester to see, simply call Sysout.println
 236   with the string to be displayed.
 237  This mimics System.out.println but works within the test harness as well
 238   as standalone.
 239  */
 240 
 241 class Sysout
 242  {
 243    private static TestDialog dialog;
 244 
 245    public static void createDialogWithInstructions( String[] instructions )
 246     {
 247       dialog = new TestDialog( new Frame(), "Instructions" );
 248       dialog.printInstructions( instructions );
 249       dialog.show();
 250       println( "Any messages for the tester will display here." );
 251     }
 252 
 253    public static void createDialog( )
 254     {
 255       dialog = new TestDialog( new Frame(), "Instructions" );
 256       String[] defInstr = { "Instructions will appear here. ", "" } ;
 257       dialog.printInstructions( defInstr );
 258       dialog.show();
 259       println( "Any messages for the tester will display here." );
 260     }
 261 
 262 
 263    public static void printInstructions( String[] instructions )
 264     {
 265       dialog.printInstructions( instructions );
 266     }
 267 
 268 
 269    public static void println( String messageIn )
 270     {
 271       dialog.displayMessage( messageIn );
 272     }
 273 
 274  }// Sysout  class
 275 
 276 /**
 277   This is part of the standard test machinery.  It provides a place for the
 278    test instructions to be displayed, and a place for interactive messages
 279    to the user to be displayed.
 280   To have the test instructions displayed, see Sysout.
 281   To have a message to the user be displayed, see Sysout.
 282   Do not call anything in this dialog directly.
 283   */
 284 class TestDialog extends Dialog implements ActionListener
 285  {
 286 
 287    TextArea instructionsText;
 288    TextArea messageText;
 289    int maxStringLength = 80;
 290    Panel  buttonP = new Panel();
 291    Button passB = new Button( "pass" );
 292    Button failB = new Button( "fail" );
 293 
 294    //DO NOT call this directly, go through Sysout
 295    public TestDialog( Frame frame, String name )
 296     {
 297       super( frame, name );
 298       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 299       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 300       add( "North", instructionsText );
 301 
 302       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 303       add("Center", messageText);
 304 
 305       passB = new Button( "pass" );
 306       passB.setActionCommand( "pass" );
 307       passB.addActionListener( this );
 308       buttonP.add( "East", passB );
 309 
 310       failB = new Button( "fail" );
 311       failB.setActionCommand( "fail" );
 312       failB.addActionListener( this );
 313       buttonP.add( "West", failB );
 314 
 315       add( "South", buttonP );
 316       pack();
 317 
 318       show();
 319     }// TestDialog()
 320 
 321    //DO NOT call this directly, go through Sysout
 322    public void printInstructions( String[] instructions )
 323     {
 324       //Clear out any current instructions
 325       instructionsText.setText( "" );
 326 
 327       //Go down array of instruction strings
 328 
 329       String printStr, remainingStr;
 330       for( int i=0; i < instructions.length; i++ )
 331        {
 332          //chop up each into pieces maxSringLength long
 333          remainingStr = instructions[ i ];
 334          while( remainingStr.length() > 0 )
 335           {
 336             //if longer than max then chop off first max chars to print
 337             if( remainingStr.length() >= maxStringLength )
 338              {
 339                //Try to chop on a word boundary
 340                int posOfSpace = remainingStr.
 341                   lastIndexOf( ' ', maxStringLength - 1 );
 342 
 343                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 344 
 345                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 346                remainingStr = remainingStr.substring( posOfSpace + 1 );
 347              }
 348             //else just print
 349             else
 350              {
 351                printStr = remainingStr;
 352                remainingStr = "";
 353              }
 354 
 355             instructionsText.append( printStr + "\n" );
 356 
 357           }// while
 358 
 359        }// for
 360 
 361     }//printInstructions()
 362 
 363    //DO NOT call this directly, go through Sysout
 364    public void displayMessage( String messageIn )
 365     {
 366       messageText.append( messageIn + "\n" );
 367     }
 368 
 369    //catch presses of the passed and failed buttons.
 370    //simply call the standard pass() or fail() static methods of
 371    //DialogOrient
 372    public void actionPerformed( ActionEvent e )
 373     {
 374       if( e.getActionCommand() == "pass" )
 375        {
 376          Test6411624.pass();
 377        }
 378       else
 379        {
 380          Test6411624.fail();
 381        }
 382     }
 383 
 384  }// TestDialog  class