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 javax.sound.midi.InvalidMidiDataException;
  25 import javax.sound.midi.MetaMessage;
  26 import javax.sound.midi.MidiEvent;
  27 import javax.sound.midi.Sequence;
  28 import javax.sound.midi.Track;
  29 
  30 /**
  31  * @test
  32  * @bug 6416024
  33  * @summary Tests that sequence correctly handle removing of EndOfTrack event
  34  * @run main bug6416024
  35  */
  36 public class bug6416024 {
  37 
  38     boolean test() {
  39         Sequence sequence = null;
  40         Track track = null;
  41         MidiEvent event = null;
  42 
  43         log("creating sequence...");
  44         try {
  45             sequence = new Sequence(Sequence.PPQ, 10);
  46             log("  - OK: " + sequence);
  47         } catch(InvalidMidiDataException e ) {
  48             log("  - FAILED: got exception");
  49             e.printStackTrace(System.out);
  50             return false;
  51         }
  52 
  53         log("creating track...");
  54         track = sequence.createTrack();
  55         log("  - OK: " + track);
  56         log("initial track size=" + track.size());
  57 
  58         log("removing all track events...");
  59         while (track.size() > 0) {
  60             try {
  61                 event = track.get(0);
  62                 log("  ..removing event " + event);
  63                 track.remove(event);
  64                 log("    - OK, track size=" + track.size());
  65             } catch (Exception e) {
  66                 log("  - FAILED: got exception");
  67                 e.printStackTrace(System.out);
  68                 return false;
  69             }
  70         }
  71 
  72         MetaMessage newMsg = new MetaMessage();
  73         MidiEvent newEvent = new MidiEvent(newMsg, 10);
  74         log("adding new event...");
  75         try {
  76             if (!track.add(newEvent)) {
  77                 log("event hasn't been added");
  78                 return false;
  79             }
  80             log("    - OK, track size=" + track.size());
  81         } catch (Exception e) {
  82             log("  - FAILED: got exception");
  83             e.printStackTrace(System.out);
  84             return false;
  85         }
  86 
  87         return true;
  88     }
  89 
  90     public static void main(String args[]) throws Exception {
  91         bug6416024 This = new bug6416024();
  92         if (This.test()) {
  93             log("Test passed sucessfully.");
  94         } else {
  95             log("Test FAILED!");
  96             delay(1000);
  97             throw new RuntimeException("Test failed!");
  98         }
  99     }
 100 
 101     // helper routines
 102     static long startTime = currentTimeMillis();
 103     static long currentTimeMillis() {
 104         //return System.nanoTime() / 1000000L;
 105         return System.currentTimeMillis();
 106     }
 107     static void log(String s) {
 108         long time = currentTimeMillis() - startTime;
 109         long ms = time % 1000;
 110         time /= 1000;
 111         long sec = time % 60;
 112         time /= 60;
 113         long min = time % 60;
 114         time /= 60;
 115         System.out.println(""
 116                 + (time < 10 ? "0" : "") + time
 117                 + ":" + (min < 10 ? "0" : "") + min
 118                 + ":" + (sec < 10 ? "0" : "") + sec
 119                 + "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms
 120                 + " (" + Thread.currentThread().getName() + ") " + s);
 121     }
 122     static void delay(int millis) {
 123         try {
 124             Thread.sleep(millis);
 125         } catch (InterruptedException e) {}
 126     }
 127 }