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.
   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.util.HashMap;
  25 import java.util.Map;
  26 import java.util.Objects;
  27 import java.lang.management.ManagementFactory;
  28 import java.lang.management.RuntimeMXBean;
  29 import java.util.List;
  30 
  31 /*
  32  * @test
  33  * @modules java.management
  34  * @summary verify that overriddes on the command line affect *.display and *.format properties
  35  * @run main/othervm
  36  *          LocaleCmdOverrides
  37  * @run main/othervm -Duser.language=XX
  38  *                   -Duser.country=X1
  39  *                   -Duser.script=X2
  40  *                   -Duser.variant=X3
  41  *          LocaleCmdOverrides
  42  * @run main/othervm -Duser.language=XX -Duser.language.display=YY
  43  *                   -Duser.country=X1 -Duser.country.display=Y1
  44  *                   -Duser.script=X2 -Duser.script.display=Y2
  45  *                   -Duser.variant=X3 -Duser.variant.display=Y3
  46  *          LocaleCmdOverrides
  47  * @run main/othervm -Duser.language=XX -Duser.language.display=YY -Duser.language.format=ZZ
  48  *                   -Duser.country=X1 -Duser.country.display=Y1 -Duser.country.format=Z1
  49  *                   -Duser.script=X2 -Duser.script.display=Y2 -Duser.script.format=Z2
  50  *                   -Duser.variant=X3 -Duser.variant.display=Y3 -Duser.variant.format=Z3
  51  *          LocaleCmdOverrides
  52  * @run main/othervm -Duser.language=XX -Duser.language.format=ZZ
  53  *                   -Duser.country=X1 -Duser.country.format=Z1
  54  *                   -Duser.script=X2 -Duser.script.format=Z2
  55  *                   -Duser.variant=X3 -Duser.variant.format=Z3
  56  *          LocaleCmdOverrides
  57  * @run main/othervm -Duser.language=XX -Duser.language.display=XX
  58  *                   -Duser.country=X1 -Duser.country.display=X1
  59  *                   -Duser.script=X2 -Duser.script.display=X2
  60  *                   -Duser.variant=X3 -Duser.variant.display=X3
  61  *          LocaleCmdOverrides
  62  * @run main/othervm -Duser.language=XX -Duser.language.display=XX -Duser.language.format=XX
  63  *                   -Duser.country=X1 -Duser.country.display=X1 -Duser.country.format=X1
  64  *                   -Duser.script=X2 -Duser.script.display=X2 -Duser.script.format=X2
  65  *                   -Duser.variant=X3 -Duser.variant.display=X3 -Duser.variant.format=X3
  66  *          LocaleCmdOverrides
  67  * @run main/othervm -Duser.language=XX -Duser.language.format=X1
  68  *                   -Duser.country.format=X1
  69  *                   -Duser.script.format=X2
  70  *                   -Duser.variant.format=X3
  71  *          LocaleCmdOverrides
  72  */
  73 public class LocaleCmdOverrides {
  74 
  75     // Language, country, script, variant
  76 
  77     public static void main(String[] args) {
  78         Map<String, String> props = commandLineDefines();
  79         System.out.printf("props: %s%n", props);
  80         test("user.language", props);
  81         test("user.country", props);
  82         test("user.script", props);
  83         test("user.variant", props);
  84     }
  85 
  86     /*
  87      * Check each of the properties for a given basename.
  88      */
  89     static void test(String baseName, Map<String, String> args) {
  90         validateArg(baseName,"",  args);
  91         validateArg(baseName,".display", args);
  92         validateArg(baseName,".format", args);
  93     }
  94 
  95     // If an argument is -D defined, the corresponding property must be equal
  96     static void validateArg(String name, String ext, Map<String, String> args) {
  97         String extName = name.concat(ext);
  98         String arg = args.get(extName);
  99         String prop = System.getProperty(extName);
 100         if (arg == null && prop == null) {
 101             System.out.printf("No values for %s%n", extName);
 102         } else {
 103             System.out.printf("validateArg %s: arg: %s, prop: %s%n", extName, arg, prop);
 104         }
 105 
 106         if (arg != null) {
 107             if (!Objects.equals(arg, prop)) {
 108                 throw new RuntimeException(extName + ": -D value should match property: "
 109                         + arg + " != " + prop);
 110             }
 111         } else if (prop != null) {
 112             // no command line arg for extName and some value for prop
 113             // Check that if a property is not overridden then it is not equal to the base
 114             if (ext != null && !ext.isEmpty()) {
 115                 String value = System.getProperty(name);
 116                 if (Objects.equals(value, prop)) {
 117                     throw new RuntimeException(extName + " property should not be equals to "
 118                             + name + " property: " + prop);
 119                 }
 120             }
 121         }
 122     }
 123 
 124     /**
 125      * Extract the -D arguments from the command line and return a map of key, value.
 126      * @return a map of key, values defined by -D on the command line.
 127      */
 128     static HashMap<String, String> commandLineDefines() {
 129         HashMap<String, String> props = new HashMap<>();
 130         RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 131         List<String> args = runtime.getInputArguments();
 132         System.out.printf("args: %s%n", args);
 133         for (String arg : args) {
 134             if (arg.startsWith("-Duser.")) {
 135                 String[] kv = arg.substring(2).split("=");
 136                 switch (kv.length) {
 137                     case 1:
 138                         props.put(kv[0], "");
 139                         break;
 140                     case 2:
 141                         props.put(kv[0], kv[1]);
 142                         break;
 143                     default:
 144                         throw new IllegalArgumentException("Illegal property syntax: " + arg);
 145                 }
 146             }
 147         }
 148         return props;
 149     }
 150 }