Path: blob/master/test/jdk/javax/sound/midi/SysexMessage/Exceptions.java
41153 views
/*1* Copyright (c) 2019, 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.SysexMessage;2526import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;2728/**29* @test30* @bug 822144531* @summary Checks exceptions thrown by javax.sound.midi.SysexMessage class32*/33public final class Exceptions {3435public static void main(final String[] args) throws Exception {36testInvalidMidiDataException();37testIndexOutOfBoundsException();38testNullPointerException();39}4041private static void testInvalidMidiDataException() {42try {43// data should conatins a status byte44new SysexMessage(new byte[0], 0);45throw new RuntimeException("Expected exception is not thrown");46} catch (final InvalidMidiDataException ignored) {47// ok48}49try {50// length is zero, no space for the status byte51new SysexMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);52throw new RuntimeException("Expected exception is not thrown");53} catch (final InvalidMidiDataException ignored) {54// ok55}56try {57// status should conatins a status byte (0xF0 or 0xF7)58new SysexMessage(0, new byte[0], 2);59throw new RuntimeException("Expected exception is not thrown");60} catch (final InvalidMidiDataException ignored) {61// ok62}63SysexMessage sysexMessage = new SysexMessage();64try {65// data should conatins a status byte66sysexMessage.setMessage(new byte[0], 0);67throw new RuntimeException("Expected exception is not thrown");68} catch (final InvalidMidiDataException ignored) {69// ok70}71try {72// length is zero, no space for the status byte73sysexMessage.setMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);74throw new RuntimeException("Expected exception is not thrown");75} catch (final InvalidMidiDataException ignored) {76// ok77}78try {79// data should conatins a status byte (0xF0 or 0xF7)80sysexMessage.setMessage(new byte[]{0}, 0);81throw new RuntimeException("Expected exception is not thrown");82} catch (final InvalidMidiDataException ignored) {83// ok84}85try {86// status should conatins a status byte (0xF0 or 0xF7)87sysexMessage.setMessage(0, new byte[0], 0);88throw new RuntimeException("Expected exception is not thrown");89} catch (final InvalidMidiDataException ignored) {90// ok91}92}9394private static void testIndexOutOfBoundsException() throws Exception {95// length is bigger than data96try {97new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);98throw new RuntimeException("Expected exception is not thrown");99} catch (final IndexOutOfBoundsException ignored) {100// ok101}102try {103new SysexMessage(0xF0, new byte[0], 2);104throw new RuntimeException("Expected exception is not thrown");105} catch (final IndexOutOfBoundsException ignored) {106// ok107}108SysexMessage sysexMessage = new SysexMessage();109try {110sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);111throw new RuntimeException("Expected exception is not thrown");112} catch (final IndexOutOfBoundsException ignored) {113// ok114}115try {116sysexMessage.setMessage(0xF0, new byte[0], 2);117throw new RuntimeException("Expected exception is not thrown");118} catch (final IndexOutOfBoundsException ignored) {119// ok120}121122// length is negative123try {124new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);125throw new RuntimeException("Expected exception is not thrown");126} catch (final IndexOutOfBoundsException ignored) {127// ok128}129try {130new SysexMessage(0xF0, new byte[0], -1);131throw new RuntimeException("Expected exception is not thrown");132} catch (final IndexOutOfBoundsException ignored) {133// ok134}135sysexMessage = new SysexMessage();136try {137sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);138throw new RuntimeException("Expected exception is not thrown");139} catch (final IndexOutOfBoundsException ignored) {140// ok141}142try {143sysexMessage.setMessage(0xF0, new byte[0], -1);144throw new RuntimeException("Expected exception is not thrown");145} catch (final IndexOutOfBoundsException ignored) {146// ok147}148}149150private static void testNullPointerException() throws Exception {151try {152new SysexMessage(null, 0);153throw new RuntimeException("Expected exception is not thrown");154} catch (final NullPointerException ignored) {155// ok156}157try {158new SysexMessage(SYSTEM_EXCLUSIVE, null, 2);159throw new RuntimeException("Expected exception is not thrown");160} catch (final NullPointerException ignored) {161// ok162}163SysexMessage sysexMessage = new SysexMessage();164try {165sysexMessage.setMessage(null, 0);166throw new RuntimeException("Expected exception is not thrown");167} catch (final NullPointerException ignored) {168// ok169}170sysexMessage = new SysexMessage();171try {172sysexMessage.setMessage(SYSTEM_EXCLUSIVE, null, 2);173throw new RuntimeException("Expected exception is not thrown");174} catch (final NullPointerException ignored) {175// ok176}177}178}179180181