001package bradleyross.music.MidiRouter;
002import javax.sound.midi.MidiSystem;
003import javax.sound.midi.MidiDevice;
004import javax.sound.midi.MidiUnavailableException;
005/**
006 * Test whether MidiDevice.Info.getMidiDevice always gives the
007 * same object.
008 * 
009 * <p>Three MidiDevice objects are created from the same
010 *    MidiDevice.Info object.  The same object should be returned
011 *    in each case.  This is not true for the synthesizer and
012 *    sequencer.</p>
013 * <p>A MidiDevice.Info object was created from each of the MidiDevice
014 *    objects.  The hash code should be the same for each of the three
015 *    cases.  This appears to be the case.</p>
016 * <p>Question: If two identical MIDI keyboards are attached at the same time,
017 *    will they have different hash codes?</p> 
018 * @author Bradley Ross
019 *
020 */
021public class RelationshipTest implements Runnable {
022        /**
023         * Carries out test for a single MidiDevice.Info object.
024         * @param item object to be tested
025         * @throws MidiUnavailableException
026         */
027        protected void single(MidiDevice.Info item) throws MidiUnavailableException {
028                System.out.println("Name: " + item.getName());
029                System.out.println("Vendor: " + item.getVendor());
030                System.out.println("Version: " + item.getVersion());
031                System.out.println("Description: " + item.getDescription());
032                System.out.println("Hash: " + Integer.toString(item.hashCode()));
033                MidiDevice device1 = MidiSystem.getMidiDevice(item);
034                MidiDevice device2 = MidiSystem.getMidiDevice(item);
035                MidiDevice device3 = MidiSystem.getMidiDevice(item);
036                if (device1 == device2) {
037                        System.out.println("object 1 equals object 2");
038                } else {
039                        System.out.println("object 1 not equals object 2 - this appears to be bug");
040                }
041                if (device2 == device3) {
042                        System.out.println("object 2 equals object 3");
043                } else {
044                        System.out.println("object 2 not equals object 3 - this appears to be bug");
045                }
046                if (device1 == device3) {
047                        System.out.println("object 1 equals object 3");
048                } else {
049                        System.out.println("object 1 not equals object 3 - this appears to be bug");
050                }
051                System.out.println("Hash from object 1: " + 
052                                Integer.toString(device1.getDeviceInfo().hashCode()));
053                System.out.println("Hash from object 2: " + 
054                                Integer.toString(device2.getDeviceInfo().hashCode()));                          
055                System.out.println("Hash from object 3: " + 
056                                Integer.toString(device3.getDeviceInfo().hashCode()));
057        }
058        public void run() {
059                try {
060                        int counter = 0;
061                        MidiDevice.Info[] infoList = MidiSystem.getMidiDeviceInfo();
062                        for (MidiDevice.Info item : infoList) {
063                                System.out.println("*****  *****  *****");
064                                System.out.println("Device " + Integer.toString(counter));
065                                single(item);
066                                counter++;
067                        }
068                        System.out.println("*****  *****  *****");
069                        MidiDevice.Info synthesizer = MidiSystem.getSynthesizer().getDeviceInfo();
070                        System.out.println("Synthesizer");
071                        single(synthesizer);
072                        System.out.println("*****  *****  *****");
073                        MidiDevice.Info sequencer = MidiSystem.getSequencer().getDeviceInfo();
074                        System.out.println("Sequencer");
075                        single(sequencer);                      
076                } catch (MidiUnavailableException ex) {
077                        ex.printStackTrace();
078                }
079        }
080        /**
081         * Test driver.
082         * 
083         * @param args not used
084         */
085        public static void main(String[] args) {
086                RelationshipTest instance = new RelationshipTest();
087                System.out.println("Java version is " + System.clearProperty("java.version"));
088                System.out.println("*****  *****  *****  *****");
089                System.out.println("First run");
090                instance.run(); 
091                for (int i = 0;  i < 2; i++) {
092                        System.out.println("*****  *****  *****  *****  *****  *****  *****");
093                }
094                System.out.println("Second run");
095                instance.run();
096        }
097}