DefaultProperties.java revision 16009:5445b9413d9d
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 java.io.File;
25
26import com.sun.media.sound.JDK13Services;
27
28/**
29 * @test
30 * @bug 4776511
31 * @summary RFE: Setting the default MixerProvider. Test the retrieving and
32 *          parsing of properties. This is a part of the test for 4776511.
33 * @run main/othervm DefaultProperties
34 * @modules java.desktop/com.sun.media.sound
35 */
36public class DefaultProperties {
37
38    private static final Class[] lineTypeClasses = {
39        javax.sound.midi.Receiver.class,
40        javax.sound.midi.Transmitter.class,
41        javax.sound.midi.Sequencer.class,
42        javax.sound.midi.Synthesizer.class,
43    };
44
45    public static void main(String[] args) throws Exception {
46        boolean allOk = true;
47        File file = new File(System.getProperty("test.src", "."), "testdata");
48        System.setProperty("java.home", file.getCanonicalPath());
49
50        for (int i = 0; i < lineTypeClasses.length; i++) {
51            Class cls = lineTypeClasses[i];
52            String propertyName = cls.getName();
53            String result;
54            String provClassName;
55            String instanceName;
56
57            // properties file, both provider class name and instance name
58            provClassName = "xyz";
59            instanceName = "123";
60            result = JDK13Services.getDefaultProviderClassName(cls);
61            if (! provClassName.equals(result)) {
62                out("type " + cls + " failed: provider class should be '" +
63                    provClassName + "' but is '" + result + "'!");
64                allOk = false;
65            }
66            result = JDK13Services.getDefaultInstanceName(cls);
67            if (! instanceName.equals(result)) {
68                out("type " + cls + " failed: instance name should be '" +
69                    instanceName + "' but is '" + result + "'!");
70                allOk = false;
71            }
72
73            // system property, provider class name only, no trailing hash
74            provClassName = "abc";
75            System.setProperty(propertyName, provClassName);
76            result = JDK13Services.getDefaultProviderClassName(cls);
77            if (! provClassName.equals(result)) {
78                out("type " + cls + " failed: provider class should be '" +
79                    provClassName + "' but is '" + result + "'!");
80                allOk = false;
81            }
82            result = JDK13Services.getDefaultInstanceName(cls);
83            if (result != null) {
84                out("type " + cls + " failed: instance name should be " +
85                    "null but is '" + result + "'!");
86                allOk = false;
87            }
88
89            // system property, provider class name only, trailing hash
90            provClassName = "def";
91            System.setProperty(propertyName, provClassName + "#");
92            result = JDK13Services.getDefaultProviderClassName(cls);
93            if (! provClassName.equals(result)) {
94                out("type " + cls + " failed: provider class should be '" +
95                    provClassName + "' but is '" + result + "'!");
96                allOk = false;
97            }
98            result = JDK13Services.getDefaultInstanceName(cls);
99            if (result != null) {
100                out("type " + cls + " failed: instance name should be " +
101                    "null but is '" + result + "'!");
102                allOk = false;
103            }
104
105            // system property, instance name only
106            instanceName = "ghi";
107            System.setProperty(propertyName, "#" + instanceName);
108            result = JDK13Services.getDefaultProviderClassName(cls);
109            if (result != null) {
110                out("type " + cls + " failed: provider class should be " +
111                    "null but is '" + result + "'!");
112                allOk = false;
113            }
114            result = JDK13Services.getDefaultInstanceName(cls);
115            if (! instanceName.equals(result)) {
116                out("type " + cls + " failed: instance name should be '" +
117                    instanceName + "' but is '" + result + "'!");
118                allOk = false;
119            }
120
121            // system property, both provider class and instance name
122            provClassName = "jkl";
123            instanceName = "mno";
124            System.setProperty(propertyName, provClassName + "#" + instanceName);
125            result = JDK13Services.getDefaultProviderClassName(cls);
126            if (! provClassName.equals(result)) {
127                out("type " + cls + "failed: provider class should be '" +
128                    provClassName + "' but is '" + result + "'!");
129                allOk = false;
130            }
131            result = JDK13Services.getDefaultInstanceName(cls);
132            if (! instanceName.equals(result)) {
133                out("type " + cls + "failed: instance name should be '" +
134                    instanceName + "' but is '" + result + "'!");
135                allOk = false;
136            }
137
138            // system property, empty
139            System.setProperty(propertyName, "");
140            result = JDK13Services.getDefaultProviderClassName(cls);
141            if (result != null) {
142                out("type " + cls + " failed: provider class should be " +
143                    "null but is '" + result + "'!");
144                allOk = false;
145            }
146            result = JDK13Services.getDefaultInstanceName(cls);
147            if (result != null) {
148                out("type " + cls + "failed: instance name should be " +
149                    "null but is '" + result + "'!");
150                allOk = false;
151            }
152        }
153        if (! allOk) {
154            throw new Exception("Test failed");
155        } else {
156            out("Test passed");
157        }
158    }
159
160    private static void out(String message) {
161        System.out.println(message);
162    }
163}
164
165