test/jdk/java/util/Locale/LocaleCmdOverrides.java
changeset 49292 dde7eaaa3ddc
child 52420 b6f32c533faf
equal deleted inserted replaced
49291:ae041d4dd43e 49292:dde7eaaa3ddc
       
     1 /*
       
     2  * Copyright (c) 2018, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 import java.util.HashMap;
       
    27 import java.util.Map;
       
    28 import java.util.Objects;
       
    29 import java.lang.management.ManagementFactory;
       
    30 import java.lang.management.RuntimeMXBean;
       
    31 import java.util.List;
       
    32 
       
    33 /*
       
    34  * @test
       
    35  * @modules java.management
       
    36  * @summary verify that overriddes on the command line affect *.display and *.format properties
       
    37  * @run main/othervm
       
    38  *          LocaleCmdOverrides
       
    39  * @run main/othervm -Duser.language=XX
       
    40  *                   -Duser.country=X1
       
    41  *                   -Duser.script=X2
       
    42  *                   -Duser.variant=X3
       
    43  *          LocaleCmdOverrides
       
    44  * @run main/othervm -Duser.language=XX -Duser.language.display=YY
       
    45  *                   -Duser.country=X1 -Duser.country.display=Y1
       
    46  *                   -Duser.script=X2 -Duser.script.display=Y2
       
    47  *                   -Duser.variant=X3 -Duser.variant.display=Y3
       
    48  *          LocaleCmdOverrides
       
    49  * @run main/othervm -Duser.language=XX -Duser.language.display=YY -Duser.language.format=ZZ
       
    50  *                   -Duser.country=X1 -Duser.country.display=Y1 -Duser.country.format=Z1
       
    51  *                   -Duser.script=X2 -Duser.script.display=Y2 -Duser.script.format=Z2
       
    52  *                   -Duser.variant=X3 -Duser.variant.display=Y3 -Duser.variant.format=Z3
       
    53  *          LocaleCmdOverrides
       
    54  * @run main/othervm -Duser.language=XX -Duser.language.format=ZZ
       
    55  *                   -Duser.country=X1 -Duser.country.format=Z1
       
    56  *                   -Duser.script=X2 -Duser.script.format=Z2
       
    57  *                   -Duser.variant=X3 -Duser.variant.format=Z3
       
    58  *          LocaleCmdOverrides
       
    59  * @run main/othervm -Duser.language=XX -Duser.language.display=XX
       
    60  *                   -Duser.country=X1 -Duser.country.display=X1
       
    61  *                   -Duser.script=X2 -Duser.script.display=X2
       
    62  *                   -Duser.variant=X3 -Duser.variant.display=X3
       
    63  *          LocaleCmdOverrides
       
    64  * @run main/othervm -Duser.language=XX -Duser.language.display=XX -Duser.language.format=XX
       
    65  *                   -Duser.country=X1 -Duser.country.display=X1 -Duser.country.format=X1
       
    66  *                   -Duser.script=X2 -Duser.script.display=X2 -Duser.script.format=X2
       
    67  *                   -Duser.variant=X3 -Duser.variant.display=X3 -Duser.variant.format=X3
       
    68  *          LocaleCmdOverrides
       
    69  * @run main/othervm -Duser.language=XX -Duser.language.format=X1
       
    70  *                   -Duser.country.format=X1
       
    71  *                   -Duser.script.format=X2
       
    72  *                   -Duser.variant.format=X3
       
    73  *          LocaleCmdOverrides
       
    74  */
       
    75 public class LocaleCmdOverrides {
       
    76 
       
    77     // Language, country, script, variant
       
    78 
       
    79     public static void main(String[] args) {
       
    80         Map<String, String> props = commandLineDefines();
       
    81         System.out.printf("props: %s%n", props);
       
    82         test("user.language", props);
       
    83         test("user.country", props);
       
    84         test("user.script", props);
       
    85         test("user.variant", props);
       
    86     }
       
    87 
       
    88     /*
       
    89      * Check each of the properties for a given basename.
       
    90      */
       
    91     static void test(String baseName, Map<String, String> args) {
       
    92         validateArg(baseName,"",  args);
       
    93         validateArg(baseName,".display", args);
       
    94         validateArg(baseName,".format", args);
       
    95     }
       
    96 
       
    97     // If an argument is -D defined, the corresponding property must be equal
       
    98     static void validateArg(String name, String ext, Map<String, String> args) {
       
    99         String extName = name.concat(ext);
       
   100         String arg = args.get(extName);
       
   101         String prop = System.getProperty(extName);
       
   102         if (arg == null && prop == null) {
       
   103             System.out.printf("No values for %s%n", extName);
       
   104         } else {
       
   105             System.out.printf("validateArg %s: arg: %s, prop: %s%n", extName, arg, prop);
       
   106         }
       
   107 
       
   108         if (arg != null) {
       
   109             if (!Objects.equals(arg, prop)) {
       
   110                 throw new RuntimeException(extName + ": -D value should match property: "
       
   111                         + arg + " != " + prop);
       
   112             }
       
   113         } else if (prop != null) {
       
   114             // no command line arg for extName and some value for prop
       
   115             // Check that if a property is not overridden then it is not equal to the base
       
   116             if (ext != null && !ext.isEmpty()) {
       
   117                 String value = System.getProperty(name);
       
   118                 if (Objects.equals(value, prop)) {
       
   119                     throw new RuntimeException(extName + " property should not be equals to "
       
   120                             + name + " property: " + prop);
       
   121                 }
       
   122             }
       
   123         }
       
   124     }
       
   125 
       
   126     /**
       
   127      * Extract the -D arguments from the command line and return a map of key, value.
       
   128      * @return a map of key, values defined by -D on the command line.
       
   129      */
       
   130     static HashMap<String, String> commandLineDefines() {
       
   131         HashMap<String, String> props = new HashMap<>();
       
   132         RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
       
   133         List<String> args = runtime.getInputArguments();
       
   134         System.out.printf("args: %s%n", args);
       
   135         for (String arg : args) {
       
   136             if (arg.startsWith("-Duser.")) {
       
   137                 String[] kv = arg.substring(2).split("=");
       
   138                 switch (kv.length) {
       
   139                     case 1:
       
   140                         props.put(kv[0], "");
       
   141                         break;
       
   142                     case 2:
       
   143                         props.put(kv[0], kv[1]);
       
   144                         break;
       
   145                     default:
       
   146                         throw new IllegalArgumentException("Illegal property syntax: " + arg);
       
   147                 }
       
   148             }
       
   149         }
       
   150         return props;
       
   151     }
       
   152 }