1/*
2 * Copyright (c) 2003, 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 javax.sound.sampled.Control;
25import javax.sound.sampled.Line;
26import javax.sound.sampled.LineEvent;
27import javax.sound.sampled.LineListener;
28
29/**
30 * @test
31 * @bug 4672865
32 * @summary LineEvent.toString() throws unexpected NullPointerException
33 */
34public class LineInfoNPE {
35
36    static final int STATUS_PASSED = 0;
37    static final int STATUS_FAILED = 2;
38    static final int STATUS_TEMP = 95;
39
40    public static void main(String argv[]) throws Exception {
41        int testExitStatus = run(argv, System.out);
42        if (testExitStatus != STATUS_PASSED) {
43            throw new Exception("test FAILED!");
44        }
45    }
46
47    public static int run(String argv[], java.io.PrintStream out) {
48        int testResult = STATUS_PASSED;
49
50        out.println("\n==> Test for LineEvent class:");
51
52        Line testLine = new TestLine();
53        Line nullLine = null;
54
55        LineEvent.Type testLineEventType = LineEvent.Type.OPEN;
56        LineEvent.Type nullLineEventType = null;
57
58        LineEvent testedLineEvent = null;
59        out.println("\n>> LineEvent constructor for Line = null: ");
60        try {
61            testedLineEvent =
62                new LineEvent(nullLine,  // the source Line of this event
63                                testLineEventType, // LineEvent.Type - the event type
64                                (long) 1000 // position - the number processed of sample frames
65                                );
66            out.println(">  No any Exception was thrown!");
67            out.println(">  testedLineEvent.getType():");
68            try {
69                Line producedLine = testedLineEvent.getLine();
70                out.println(">   PASSED: producedLine = " + producedLine);
71            } catch (Throwable thrown) {
72                out.println("##  FAILED: unexpected Exception was thrown:");
73                thrown.printStackTrace(out);
74                testResult = STATUS_FAILED;
75            }
76            out.println(">  testedLineEvent.toString():");
77            try {
78                String producedString = testedLineEvent.toString();
79                out.println(">   PASSED: producedString = " + producedString);
80            } catch (Throwable thrown) {
81                out.println("##  FAILED: unexpected Exception was thrown:");
82                thrown.printStackTrace(out);
83                testResult = STATUS_FAILED;
84            }
85        } catch (IllegalArgumentException illegArgExcept) {
86            out.println(">   PASSED: expected IllegalArgumentException was thrown:");
87            illegArgExcept.printStackTrace(out);
88        } catch (NullPointerException nullPE) {
89            out.println(">   PASSED: expected NullPointerException was thrown:");
90            nullPE.printStackTrace(out);
91        } catch (Throwable thrown) {
92            out.println("##  FAILED: unexpected Exception was thrown:");
93            thrown.printStackTrace(out);
94            testResult = STATUS_FAILED;
95        }
96
97        out.println("\n>> LineEvent constructor for LineEvent.Type = null: ");
98        try {
99            testedLineEvent =
100                new LineEvent(testLine,  // the source Line of this event
101                                nullLineEventType, // LineEvent.Type - the event type
102                                (long) 1000 // position - the number processed of sample frames
103                                );
104            out.println(">  No any Exception was thrown!");
105            out.println(">  testedLineEvent.getType():");
106            try {
107                LineEvent.Type producedType = testedLineEvent.getType();
108                out.println(">   PASSED: producedType = " + producedType);
109            } catch (Throwable thrown) {
110                out.println("##  FAILED: unexpected Exception was thrown:");
111                thrown.printStackTrace(out);
112                testResult = STATUS_FAILED;
113            }
114            out.println(">  testedLineEvent.toString():");
115            try {
116                String producedString = testedLineEvent.toString();
117                out.println(">   PASSED: producedString = " + producedString);
118            } catch (Throwable thrown) {
119                out.println("##  FAILED: unexpected Exception was thrown:");
120                thrown.printStackTrace(out);
121                testResult = STATUS_FAILED;
122            }
123        } catch (IllegalArgumentException illegArgExcept) {
124            out.println(">   PASSED: expected IllegalArgumentException was thrown:");
125            illegArgExcept.printStackTrace(out);
126        } catch (NullPointerException nullPE) {
127            out.println(">   PASSED: expected NullPointerException was thrown:");
128            nullPE.printStackTrace(out);
129        } catch (Throwable thrown) {
130            out.println("##  FAILED: unexpected Exception was thrown:");
131            thrown.printStackTrace(out);
132            testResult = STATUS_FAILED;
133        }
134
135        if ( testResult == STATUS_FAILED ) {
136            out.println("\n==> test FAILED!");
137        } else {
138            out.println("\n==> test PASSED!");
139        }
140        return testResult;
141    }
142}    // end of test class
143
144class TestLine implements Line {
145
146    public void addLineListener(LineListener listener) {
147    }
148
149    public void close() {
150    }
151
152    public Control getControl(Control.Type control) {
153        return null;
154    }
155
156    public Control[] getControls() {
157        return new Control[0];
158    }
159
160    public Line.Info getLineInfo() {
161        return null;
162    }
163
164    public boolean isOpen() {
165        return false;
166    }
167
168    public boolean isControlSupported(Control.Type control) {
169        return false;
170    }
171
172    public void open() {
173    }
174
175    public void removeLineListener(LineListener listener) {
176    }
177}
178