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