Path: blob/master/test/jdk/javax/sound/midi/Sequencer/SequencerState.java
41155 views
/*1* Copyright (c) 2003, 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.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.InputStream;2728import javax.sound.midi.InvalidMidiDataException;29import javax.sound.midi.MidiDevice;30import javax.sound.midi.MidiSystem;31import javax.sound.midi.Sequence;32import javax.sound.midi.Sequencer;3334/**35* @test36* @bug 491302737* @summary several Sequencer methods should specify behaviour on closed Sequencer38*/39public class SequencerState {4041private static boolean hasSequencer() {42try {43Sequencer seq = MidiSystem.getSequencer();44if (seq != null) {45seq.open();46seq.close();47return true;48}49} catch (Exception e) {}50System.out.println("No sequencer available! Cannot execute test.");51return false;52}535455public static void main(String[] args) throws Exception {56out("4913027: several Sequencer methods should should specify behaviour on closed Sequencer");57if (hasSequencer()) {58boolean passed = testAll();59if (passed) {60out("Test PASSED.");61} else {62throw new Exception("Test FAILED.");63}64}65}6667/**68* Execute the test on all available Sequencers.69*70* @return true if the test passed for all Sequencers, false otherwise71*/72private static boolean testAll() throws Exception {73boolean result = true;74MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();75for (int i = 0; i < devices.length; i++) {76MidiDevice device = MidiSystem.getMidiDevice(devices[i]);77if (device instanceof Sequencer) {78result &= testSequencer((Sequencer) device);79}80}81return result;82}8384/**85* Execute the test on the passed Sequencer.86*87* @return true if the test is passed this Sequencer, false otherwise88*/89private static boolean testSequencer(Sequencer seq) throws Exception {90boolean result = true;9192out("testing: " + seq);93/* test calls in closed state.94*/95if (seq.isOpen()) {96out("Sequencer is already open, cannot test!");97return result;98}99100try {101seq.start();102out("closed state: start() does not throw IllegalStateException!");103result = false;104} catch (IllegalStateException e) {105}106107try {108seq.stop();109out("closed state: stop() does not throw IllegalStateException!");110result = false;111} catch (IllegalStateException e) {112}113114try {115seq.startRecording();116out("closed state: startRecording() does not throw IllegalStateException!");117result = false;118} catch (IllegalStateException e) {119}120121try {122seq.stopRecording();123out("closed state: stopRecording() does not throw IllegalStateException!");124result = false;125} catch (IllegalStateException e) {126}127128Sequence sequence = createSequence();129if (sequence == null) {130out("created Sequence is null, cannot test!");131return result;132}133try {134seq.setSequence(sequence);135} catch (IllegalStateException e) {136out("closed state: setSequence(Sequence) throws IllegalStateException!");137result = false;138}139140InputStream inputStream = createSequenceInputStream();141if (inputStream == null) {142out("created InputStream is null, cannot test!");143return result;144}145try {146seq.setSequence(inputStream);147} catch (IllegalStateException e) {148out("closed state: setSequence(InputStream) throws IllegalStateException!");149result = false;150}151152try {153seq.getSequence();154} catch (IllegalStateException e) {155out("closed state: getSequence() throws IllegalStateException!");156result = false;157}158159/* test calls in open state.160*/161seq.open();162if (! seq.isOpen()) {163out("Sequencer is not open, cannot test!");164return result;165}166167try {168seq.start();169} catch (IllegalStateException e) {170out("open state: start() throws IllegalStateException!");171result = false;172}173174try {175seq.stop();176} catch (IllegalStateException e) {177out("open state: stop() throws IllegalStateException!");178result = false;179}180181try {182seq.startRecording();183} catch (IllegalStateException e) {184out("open state: startRecording() throws IllegalStateException!");185result = false;186}187188try {189seq.stopRecording();190} catch (IllegalStateException e) {191out("open state: stopRecording() throws IllegalStateException!");192result = false;193}194195sequence = createSequence();196if (sequence == null) {197out("created Sequence is null, cannot test!");198return result;199}200try {201seq.setSequence(sequence);202} catch (IllegalStateException e) {203out("open state: setSequence(Sequence) throws IllegalStateException!");204result = false;205}206207inputStream = createSequenceInputStream();208if (inputStream == null) {209out("created InputStream is null, cannot test!");210return result;211}212try {213seq.setSequence(inputStream);214} catch (IllegalStateException e) {215out("open state: setSequence(InputStream) throws IllegalStateException!");216result = false;217}218219try {220seq.getSequence();221} catch (IllegalStateException e) {222out("open state: getSequence() throws IllegalStateException!");223result = false;224}225226seq.close();227return result;228}229230/**231* Create a new Sequence for testing.232*233* @return a dummy Sequence, or null, if a problem occured while creating234* the Sequence235*/236private static Sequence createSequence() {237Sequence sequence = null;238try {239sequence = new Sequence(Sequence.PPQ, 480, 1);240} catch (InvalidMidiDataException e) {241// DO NOTHING242}243return sequence;244}245246/**247* Create a new InputStream containing a Sequence for testing.248*249* @return an InputStream containing a dummy Sequence, or null, if a problem250* occured while creating the InputStream251*/252private static InputStream createSequenceInputStream() {253ByteArrayOutputStream baos = new ByteArrayOutputStream();254Sequence sequence = createSequence();255if (sequence == null) {256return null;257}258try {259MidiSystem.write(sequence, 0, baos);260ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());261return bais;262} catch (IOException e) {263return null;264}265}266267268private static void out(String message) {269System.out.println(message);270}271}272273274