Path: blob/master/test/jdk/javax/sound/midi/File/WriteRealTimeMessageNPE.java
41153 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 java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.InputStream;2627import javax.sound.midi.MidiEvent;28import javax.sound.midi.MidiSystem;29import javax.sound.midi.Sequence;30import javax.sound.midi.ShortMessage;31import javax.sound.midi.Track;3233/**34* @test35* @bug 504838136* @summary NPE when writing a sequence with a realtime MIDI message37*/38public class WriteRealTimeMessageNPE {3940public static void main(String args[]) throws Exception {41System.out.println("5048381: NullPointerException when saving a MIDI sequence");42boolean npeThrown = false;43boolean noEx = false;4445Sequence seq = new Sequence(Sequence.PPQ, 384, 1);46Track t = seq.getTracks()[0];47ShortMessage msg = new ShortMessage();48msg.setMessage(0xF8, 0, 0);49t.add(new MidiEvent(msg, 0));5051ByteArrayOutputStream out = new ByteArrayOutputStream();52try {53MidiSystem.write(seq, 0, out);54noEx = true;55} catch (NullPointerException npe) {56npeThrown = true;57System.out.println("## Failed: Threw unexpected NPE: "+npe);58throw new Exception("Test FAILED!");59} catch (Exception e) {60System.out.println("Threw unexpected Exception: "+e);61System.out.println("But at least did not throw NPE...");62}63if (noEx) {64InputStream is = new ByteArrayInputStream(out.toByteArray());65seq = MidiSystem.getSequence(is);66System.out.println("Sequence has "+seq.getTracks().length+" tracks.");67if (seq.getTracks().length > 0) {68System.out.println("Track 0 has "+seq.getTracks()[0].size()+" events.");69}70}71System.out.println("Test passed.");72}73}747576