1 /*
   2  * Copyright (c) 2003, 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.io.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 
  29 import javax.sound.midi.InvalidMidiDataException;
  30 import javax.sound.midi.MidiDevice;
  31 import javax.sound.midi.MidiSystem;
  32 import javax.sound.midi.Sequence;
  33 import javax.sound.midi.Sequencer;
  34 
  35 /**
  36  * @test
  37  * @bug 4913027
  38  * @summary several Sequencer methods should specify behaviour on closed Sequencer
  39  */
  40 public class SequencerState {
  41 
  42     private static boolean hasSequencer() {
  43         try {
  44             Sequencer seq = MidiSystem.getSequencer();
  45             if (seq != null) {
  46                 seq.open();
  47                 seq.close();
  48                 return true;
  49             }
  50         } catch (Exception e) {}
  51         System.out.println("No sequencer available! Cannot execute test.");
  52         return false;
  53     }
  54 
  55 
  56     public static void main(String[] args) throws Exception {
  57         out("4913027: several Sequencer methods should should specify behaviour on closed Sequencer");
  58         if (hasSequencer()) {
  59             boolean passed = testAll();
  60             if (passed) {
  61                 out("Test PASSED.");
  62             } else {
  63                 throw new Exception("Test FAILED.");
  64             }
  65         }
  66     }
  67 
  68     /**
  69      * Execute the test on all available Sequencers.
  70      *
  71      * @return true if the test passed for all Sequencers, false otherwise
  72      */
  73     private static boolean testAll() throws Exception {
  74         boolean result = true;
  75         MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
  76         for (int i = 0; i < devices.length; i++) {
  77             MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
  78             if (device instanceof Sequencer) {
  79                 result &= testSequencer((Sequencer) device);
  80             }
  81         }
  82         return result;
  83     }
  84 
  85     /**
  86      * Execute the test on the passed Sequencer.
  87      *
  88      * @return true if the test is passed this Sequencer, false otherwise
  89      */
  90     private static boolean testSequencer(Sequencer seq) throws Exception {
  91         boolean result = true;
  92 
  93         out("testing: " + seq);
  94         /* test calls in closed state.
  95          */
  96         if (seq.isOpen()) {
  97             out("Sequencer is already open, cannot test!");
  98             return result;
  99         }
 100 
 101         try {
 102             seq.start();
 103             out("closed state: start() does not throw IllegalStateException!");
 104             result = false;
 105         } catch (IllegalStateException e) {
 106         }
 107 
 108         try {
 109             seq.stop();
 110             out("closed state: stop() does not throw IllegalStateException!");
 111             result = false;
 112         } catch (IllegalStateException e) {
 113         }
 114 
 115         try {
 116             seq.startRecording();
 117             out("closed state: startRecording() does not throw IllegalStateException!");
 118             result = false;
 119         } catch (IllegalStateException e) {
 120         }
 121 
 122         try {
 123             seq.stopRecording();
 124             out("closed state: stopRecording() does not throw IllegalStateException!");
 125             result = false;
 126         } catch (IllegalStateException e) {
 127         }
 128 
 129         Sequence sequence = createSequence();
 130         if (sequence == null) {
 131             out("created Sequence is null, cannot test!");
 132             return result;
 133         }
 134         try {
 135             seq.setSequence(sequence);
 136         } catch (IllegalStateException e) {
 137             out("closed state: setSequence(Sequence) throws IllegalStateException!");
 138             result = false;
 139         }
 140 
 141         InputStream inputStream = createSequenceInputStream();
 142         if (inputStream == null) {
 143             out("created InputStream is null, cannot test!");
 144             return result;
 145         }
 146         try {
 147             seq.setSequence(inputStream);
 148         } catch (IllegalStateException e) {
 149             out("closed state: setSequence(InputStream) throws IllegalStateException!");
 150             result = false;
 151         }
 152 
 153         try {
 154             seq.getSequence();
 155         } catch (IllegalStateException e) {
 156             out("closed state: getSequence() throws IllegalStateException!");
 157             result = false;
 158         }
 159 
 160         /* test calls in open state.
 161          */
 162         seq.open();
 163         if (! seq.isOpen()) {
 164             out("Sequencer is not open, cannot test!");
 165             return result;
 166         }
 167 
 168         try {
 169             seq.start();
 170         } catch (IllegalStateException e) {
 171             out("open state: start() throws IllegalStateException!");
 172             result = false;
 173         }
 174 
 175         try {
 176             seq.stop();
 177         } catch (IllegalStateException e) {
 178             out("open state: stop() throws IllegalStateException!");
 179             result = false;
 180         }
 181 
 182         try {
 183             seq.startRecording();
 184         } catch (IllegalStateException e) {
 185             out("open state: startRecording() throws IllegalStateException!");
 186             result = false;
 187         }
 188 
 189         try {
 190             seq.stopRecording();
 191         } catch (IllegalStateException e) {
 192             out("open state: stopRecording() throws IllegalStateException!");
 193             result = false;
 194         }
 195 
 196         sequence = createSequence();
 197         if (sequence == null) {
 198             out("created Sequence is null, cannot test!");
 199             return result;
 200         }
 201         try {
 202             seq.setSequence(sequence);
 203         } catch (IllegalStateException e) {
 204             out("open state: setSequence(Sequence) throws IllegalStateException!");
 205             result = false;
 206         }
 207 
 208         inputStream = createSequenceInputStream();
 209         if (inputStream == null) {
 210             out("created InputStream is null, cannot test!");
 211             return result;
 212         }
 213         try {
 214             seq.setSequence(inputStream);
 215         } catch (IllegalStateException e) {
 216             out("open state: setSequence(InputStream) throws IllegalStateException!");
 217             result = false;
 218         }
 219 
 220         try {
 221             seq.getSequence();
 222         } catch (IllegalStateException e) {
 223             out("open state: getSequence() throws IllegalStateException!");
 224             result = false;
 225         }
 226 
 227         seq.close();
 228         return result;
 229     }
 230 
 231     /**
 232      * Create a new Sequence for testing.
 233      *
 234      * @return a dummy Sequence, or null, if a problem occured while creating
 235      *         the Sequence
 236      */
 237     private static Sequence createSequence() {
 238         Sequence sequence = null;
 239         try {
 240             sequence = new Sequence(Sequence.PPQ, 480, 1);
 241         } catch (InvalidMidiDataException e) {
 242             // DO NOTHING
 243         }
 244         return sequence;
 245     }
 246 
 247     /**
 248      * Create a new InputStream containing a Sequence for testing.
 249      *
 250      * @return an InputStream containing a dummy Sequence, or null, if a problem
 251      *         occured while creating the InputStream
 252      */
 253     private static InputStream createSequenceInputStream() {
 254         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 255         Sequence sequence = createSequence();
 256         if (sequence == null) {
 257             return null;
 258         }
 259         try {
 260             MidiSystem.write(sequence, 0, baos);
 261             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 262             return bais;
 263         } catch (IOException e) {
 264             return null;
 265         }
 266     }
 267 
 268 
 269     private static void out(String message) {
 270         System.out.println(message);
 271     }
 272 }