1/*
2 * Copyright (c) 2001, 2016, 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
24import java.io.ByteArrayInputStream;
25
26import javax.sound.sampled.AudioFormat;
27import javax.sound.sampled.AudioInputStream;
28import javax.sound.sampled.AudioSystem;
29import javax.sound.sampled.Clip;
30import javax.sound.sampled.DataLine;
31import javax.sound.sampled.FloatControl;
32
33/**
34 * @test
35 * @bug 4479444
36 * @summary Verify that the error string of Clip.open() is meaningful
37 */
38public class ClipOpenBug {
39
40    public static void main(String args[]) throws Exception {
41        boolean res = true;
42        try {
43            AudioInputStream ais = new AudioInputStream(
44                    new ByteArrayInputStream(new byte[2000]),
45                    new AudioFormat(8000.0f, 8, 1, false, false), 2000); //
46            AudioFormat format = ais.getFormat();
47            DataLine.Info info = new DataLine.Info(Clip.class, format,
48                                                   ((int) ais.getFrameLength()
49                                                            * format
50                                                           .getFrameSize()));
51            Clip clip = (Clip) AudioSystem.getLine(info);
52            clip.open();
53            FloatControl rateControl = (FloatControl) clip.getControl(
54                    FloatControl.Type.SAMPLE_RATE);
55            int c = 0;
56            while (c++ < 10) {
57                clip.stop();
58                clip.setFramePosition(0);
59                clip.start();
60                for (float frq = 22000; frq < 44100; frq = frq + 100) {
61                    try {
62                        Thread.currentThread().sleep(20);
63                    } catch (Exception e) {
64                        break;
65                    }
66                    rateControl.setValue(frq);
67                }
68            }
69        } catch (Exception ex) {
70            ex.printStackTrace();
71            res = ex.getMessage().indexOf(
72                    "This method should not have been invoked!") < 0;
73        }
74        if (res) {
75            System.out.println("Test passed");
76        } else {
77            System.out.println("Test failed");
78            throw new Exception("Test failed");
79        }
80    }
81}
82