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