1 /*
   2  * Copyright (c) 2015, 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 java.util.Collection;
  25 import java.util.HashSet;
  26 
  27 import javax.sound.midi.MidiDevice;
  28 import javax.sound.midi.MidiDevice.Info;
  29 import javax.sound.midi.MidiSystem;
  30 import javax.sound.midi.MidiUnavailableException;
  31 import javax.sound.midi.spi.MidiDeviceProvider;
  32 
  33 import static java.util.ServiceLoader.load;
  34 
  35 /**
  36  * @test
  37  * @bug 8059743
  38  * @summary MidiDeviceProvider shouldn't returns incorrect results in case of
  39  *          some unknown MidiDevice.Info
  40  * @author Sergey Bylokhov
  41  */
  42 public final class FakeInfo {
  43 
  44     private static final class Fake extends Info {
  45 
  46         Fake() {
  47             super("a", "b", "c", "d");
  48         }
  49     }
  50 
  51     public static void main(final String[] args) {
  52         final Info fake = new Fake();
  53         // MidiSystem API
  54         try {
  55             MidiSystem.getMidiDevice(fake);
  56             throw new RuntimeException("IllegalArgumentException expected");
  57         } catch (final MidiUnavailableException e) {
  58             throw new RuntimeException("IllegalArgumentException expected", e);
  59         } catch (final IllegalArgumentException ignored) {
  60             // expected
  61         }
  62         // MidiDeviceProvider API
  63         final Collection<String> errors = new HashSet<>();
  64         for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) {
  65             try {
  66                 if (mdp.isDeviceSupported(fake)) {
  67                     throw new RuntimeException("fake is supported");
  68                 }
  69                 final MidiDevice device = mdp.getDevice(fake);
  70                 System.err.println("MidiDevice: " + device);
  71                 throw new RuntimeException("IllegalArgumentException expected");
  72             } catch (final IllegalArgumentException e) {
  73                 errors.add(e.getMessage());
  74             }
  75         }
  76         if (errors.size() != 1) {
  77             throw new RuntimeException("Wrong number of messages:" + errors);
  78         }
  79     }
  80 }