Path: blob/master/test/jdk/javax/sound/midi/Sequencer/LoopIAE.java
41155 views
/*1* Copyright (c) 2004, 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 javax.sound.midi.InvalidMidiDataException;24import javax.sound.midi.MidiEvent;25import javax.sound.midi.MidiSystem;26import javax.sound.midi.MidiUnavailableException;27import javax.sound.midi.Sequence;28import javax.sound.midi.Sequencer;29import javax.sound.midi.ShortMessage;30import javax.sound.midi.Track;3132/**33* @test34* @bug 502554935* @summary Verify that setLoopEndPoint throws IAE36*/37public class LoopIAE {3839static ShortMessage MidiMsg3(int a, int b, int c) {40try {41ShortMessage msg = new ShortMessage();42msg.setMessage((byte)a,(byte)b,(byte)c);43return msg;44} catch(InvalidMidiDataException ex) {45throw new RuntimeException();46}47}4849static boolean failed = false;5051public static void main(String[] argv) throws Exception {52if (!hasSequencer()) {53return;54}55Sequencer sequencer = MidiSystem.getSequencer();56Sequence sequence = new Sequence(Sequence.PPQ, 240);57Track track = sequence.createTrack();5859track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));60track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));61track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),10*20));62track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),10*20 + 10));6364try {65sequencer.open();66sequencer.setSequence(sequence);67sequencer.setTempoInBPM(100);6869System.out.println("Setting loop end point to 1");70sequencer.setLoopEndPoint(1);71System.out.println(" -> effectively: "+sequencer.getLoopEndPoint());72System.out.println("Setting loop start point to 2 -- should throw IAE");73sequencer.setLoopStartPoint(2);74System.out.println(" -> effectively: "+sequencer.getLoopStartPoint());75System.out.println("No IllegalArgumentException was thrown!");76failed = true;77} catch (IllegalArgumentException iae) {78System.out.println("IAE was thrown correctly.");79} catch (MidiUnavailableException mue) {80System.out.println("MidiUnavailableException was thrown: " + mue);81System.out.println("Cannot execute test.");82} catch (InvalidMidiDataException imEx) {83System.out.println("InvalidMidiDataException was thrown.");84imEx.printStackTrace();85System.out.println("Cannot execute test.");86} finally {87if (sequencer != null && sequencer.isOpen()) {88sequencer.close();89}90}91if (failed) {92throw new Exception("Test FAILED!");93}94System.out.println("test passed.");95}9697static boolean hasSequencer() {98try {99Sequencer seq = MidiSystem.getSequencer();100if (seq != null) {101if (seq.isOpen()) {102seq.close();103}104return true;105}106} catch (Exception e) {107e.printStackTrace();108}109System.out.println("No sequencer available! Cannot execute test.");110return false;111}112}113114115