Path: blob/master/test/jdk/java/util/Locale/LocaleCmdOverrides.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.HashMap;24import java.util.Map;25import java.util.Objects;26import java.lang.management.ManagementFactory;27import java.lang.management.RuntimeMXBean;28import java.util.List;2930/*31* @test32* @modules java.management33* @summary verify that overriddes on the command line affect *.display and *.format properties34* @run main/othervm35* LocaleCmdOverrides36* @run main/othervm -Duser.language=XX37* -Duser.country=X138* -Duser.script=X239* -Duser.variant=X340* LocaleCmdOverrides41* @run main/othervm -Duser.language=XX -Duser.language.display=YY42* -Duser.country=X1 -Duser.country.display=Y143* -Duser.script=X2 -Duser.script.display=Y244* -Duser.variant=X3 -Duser.variant.display=Y345* LocaleCmdOverrides46* @run main/othervm -Duser.language=XX -Duser.language.display=YY -Duser.language.format=ZZ47* -Duser.country=X1 -Duser.country.display=Y1 -Duser.country.format=Z148* -Duser.script=X2 -Duser.script.display=Y2 -Duser.script.format=Z249* -Duser.variant=X3 -Duser.variant.display=Y3 -Duser.variant.format=Z350* LocaleCmdOverrides51* @run main/othervm -Duser.language=XX -Duser.language.format=ZZ52* -Duser.country=X1 -Duser.country.format=Z153* -Duser.script=X2 -Duser.script.format=Z254* -Duser.variant=X3 -Duser.variant.format=Z355* LocaleCmdOverrides56* @run main/othervm -Duser.language=XX -Duser.language.display=XX57* -Duser.country=X1 -Duser.country.display=X158* -Duser.script=X2 -Duser.script.display=X259* -Duser.variant=X3 -Duser.variant.display=X360* LocaleCmdOverrides61* @run main/othervm -Duser.language=XX -Duser.language.display=XX -Duser.language.format=XX62* -Duser.country=X1 -Duser.country.display=X1 -Duser.country.format=X163* -Duser.script=X2 -Duser.script.display=X2 -Duser.script.format=X264* -Duser.variant=X3 -Duser.variant.display=X3 -Duser.variant.format=X365* LocaleCmdOverrides66* @run main/othervm -Duser.language=XX -Duser.language.format=X167* -Duser.country.format=X168* -Duser.script.format=X269* -Duser.variant.format=X370* LocaleCmdOverrides71*/72public class LocaleCmdOverrides {7374// Language, country, script, variant7576public static void main(String[] args) {77Map<String, String> props = commandLineDefines();78System.out.printf("props: %s%n", props);79test("user.language", props);80test("user.country", props);81test("user.script", props);82test("user.variant", props);83}8485/*86* Check each of the properties for a given basename.87*/88static void test(String baseName, Map<String, String> args) {89validateArg(baseName,"", args);90validateArg(baseName,".display", args);91validateArg(baseName,".format", args);92}9394// If an argument is -D defined, the corresponding property must be equal95static void validateArg(String name, String ext, Map<String, String> args) {96String extName = name.concat(ext);97String arg = args.get(extName);98String prop = System.getProperty(extName);99if (arg == null && prop == null) {100System.out.printf("No values for %s%n", extName);101} else {102System.out.printf("validateArg %s: arg: %s, prop: %s%n", extName, arg, prop);103}104105if (arg != null) {106if (!Objects.equals(arg, prop)) {107throw new RuntimeException(extName + ": -D value should match property: "108+ arg + " != " + prop);109}110} else if (prop != null) {111// no command line arg for extName and some value for prop112// Check that if a property is not overridden then it is not equal to the base113if (ext != null && !ext.isEmpty()) {114String value = System.getProperty(name);115if (Objects.equals(value, prop)) {116throw new RuntimeException(extName + " property should not be equals to "117+ name + " property: " + prop);118}119}120}121}122123/**124* Extract the -D arguments from the command line and return a map of key, value.125* @return a map of key, values defined by -D on the command line.126*/127static HashMap<String, String> commandLineDefines() {128HashMap<String, String> props = new HashMap<>();129RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();130List<String> args = runtime.getInputArguments();131System.out.printf("args: %s%n", args);132for (String arg : args) {133if (arg.startsWith("-Duser.")) {134String[] kv = arg.substring(2).split("=");135switch (kv.length) {136case 1:137props.put(kv[0], "");138break;139case 2:140props.put(kv[0], kv[1]);141break;142default:143throw new IllegalArgumentException("Illegal property syntax: " + arg);144}145}146}147return props;148}149}150151152