1 /*
   2  * Copyright (c) 2019, 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.SysexMessage;
  26 
  27 import static javax.sound.midi.SysexMessage.SYSTEM_EXCLUSIVE;
  28 
  29 /**
  30  * @test
  31  * @bug 8221445
  32  * @summary Checks exceptions thrown by javax.sound.midi.SysexMessage class
  33  */
  34 public final class Exceptions {
  35 
  36     public static void main(final String[] args) throws Exception {
  37         testInvalidMidiDataException();
  38         testIndexOutOfBoundsException();
  39         testNullPointerException();
  40     }
  41 
  42     private static void testInvalidMidiDataException() {
  43         try {
  44             // data should conatins a status byte
  45             new SysexMessage(new byte[0], 0);
  46             throw new RuntimeException("Expected exception is not thrown");
  47         } catch (final InvalidMidiDataException ignored) {
  48             // ok
  49         }
  50         try {
  51             // length is zero, no space for the status byte
  52             new SysexMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);
  53             throw new RuntimeException("Expected exception is not thrown");
  54         } catch (final InvalidMidiDataException ignored) {
  55             // ok
  56         }
  57         try {
  58             // status should conatins a status byte (0xF0 or 0xF7)
  59             new SysexMessage(0, new byte[0], 2);
  60             throw new RuntimeException("Expected exception is not thrown");
  61         } catch (final InvalidMidiDataException ignored) {
  62             // ok
  63         }
  64         SysexMessage sysexMessage = new SysexMessage();
  65         try {
  66             // data should conatins a status byte
  67             sysexMessage.setMessage(new byte[0], 0);
  68             throw new RuntimeException("Expected exception is not thrown");
  69         } catch (final InvalidMidiDataException ignored) {
  70             // ok
  71         }
  72         try {
  73             // length is zero, no space for the status byte
  74             sysexMessage.setMessage(new byte[]{(byte) (SYSTEM_EXCLUSIVE)}, 0);
  75             throw new RuntimeException("Expected exception is not thrown");
  76         } catch (final InvalidMidiDataException ignored) {
  77             // ok
  78         }
  79         try {
  80             // data should conatins a status byte (0xF0 or 0xF7)
  81             sysexMessage.setMessage(new byte[]{0}, 0);
  82             throw new RuntimeException("Expected exception is not thrown");
  83         } catch (final InvalidMidiDataException ignored) {
  84             // ok
  85         }
  86         try {
  87             // status should conatins a status byte (0xF0 or 0xF7)
  88             sysexMessage.setMessage(0, new byte[0], 0);
  89             throw new RuntimeException("Expected exception is not thrown");
  90         } catch (final InvalidMidiDataException ignored) {
  91             // ok
  92         }
  93     }
  94 
  95     private static void testIndexOutOfBoundsException() throws Exception {
  96         // length is bigger than data
  97         try {
  98             new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);
  99             throw new RuntimeException("Expected exception is not thrown");
 100         } catch (final IndexOutOfBoundsException ignored) {
 101             // ok
 102         }
 103         try {
 104             new SysexMessage(0xF0, new byte[0], 2);
 105             throw new RuntimeException("Expected exception is not thrown");
 106         } catch (final IndexOutOfBoundsException ignored) {
 107             // ok
 108         }
 109         SysexMessage sysexMessage = new SysexMessage();
 110         try {
 111             sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, 2);
 112             throw new RuntimeException("Expected exception is not thrown");
 113         } catch (final IndexOutOfBoundsException ignored) {
 114             // ok
 115         }
 116         try {
 117             sysexMessage.setMessage(0xF0, new byte[0], 2);
 118             throw new RuntimeException("Expected exception is not thrown");
 119         } catch (final IndexOutOfBoundsException ignored) {
 120             // ok
 121         }
 122 
 123         // length is negative
 124         try {
 125             new SysexMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);
 126             throw new RuntimeException("Expected exception is not thrown");
 127         } catch (final IndexOutOfBoundsException ignored) {
 128             // ok
 129         }
 130         try {
 131             new SysexMessage(0xF0, new byte[0], -1);
 132             throw new RuntimeException("Expected exception is not thrown");
 133         } catch (final IndexOutOfBoundsException ignored) {
 134             // ok
 135         }
 136         sysexMessage = new SysexMessage();
 137         try {
 138             sysexMessage.setMessage(new byte[]{(byte) (0xF0 & 0xFF)}, -1);
 139             throw new RuntimeException("Expected exception is not thrown");
 140         } catch (final IndexOutOfBoundsException ignored) {
 141             // ok
 142         }
 143         try {
 144             sysexMessage.setMessage(0xF0, new byte[0], -1);
 145             throw new RuntimeException("Expected exception is not thrown");
 146         } catch (final IndexOutOfBoundsException ignored) {
 147             // ok
 148         }
 149     }
 150 
 151     private static void testNullPointerException() throws Exception {
 152         try {
 153             new SysexMessage(null, 0);
 154             throw new RuntimeException("Expected exception is not thrown");
 155         } catch (final NullPointerException ignored) {
 156             // ok
 157         }
 158         try {
 159             new SysexMessage(SYSTEM_EXCLUSIVE, null, 2);
 160             throw new RuntimeException("Expected exception is not thrown");
 161         } catch (final NullPointerException ignored) {
 162             // ok
 163         }
 164         SysexMessage sysexMessage = new SysexMessage();
 165         try {
 166             sysexMessage.setMessage(null, 0);
 167             throw new RuntimeException("Expected exception is not thrown");
 168         } catch (final NullPointerException ignored) {
 169             // ok
 170         }
 171         sysexMessage = new SysexMessage();
 172         try {
 173             sysexMessage.setMessage(SYSTEM_EXCLUSIVE, null, 2);
 174             throw new RuntimeException("Expected exception is not thrown");
 175         } catch (final NullPointerException ignored) {
 176             // ok
 177         }
 178     }
 179 }