1 /*
   2  * Copyright (c) 2012, 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 /*
  25  * @test
  26  * @bug 7073852
  27  * @summary Support multiple scripts for digits and decimal symbols per locale
  28  */
  29 
  30 import java.text.*;
  31 import java.util.*;
  32 
  33 public class MultipleNumberScriptTest {
  34 
  35     static Locale[] locales = {
  36         new Locale("ar"),
  37         new Locale("ar", "EG"),
  38         new Locale("ar", "DZ"),
  39         Locale.forLanguageTag("ar-EG-u-nu-arab"),
  40         Locale.forLanguageTag("ar-EG-u-nu-latn"),
  41         Locale.forLanguageTag("ar-DZ-u-nu-arab"),
  42         Locale.forLanguageTag("ar-DZ-u-nu-latn"),
  43         Locale.forLanguageTag("ee"),
  44         Locale.forLanguageTag("ee-GH"),
  45         Locale.forLanguageTag("ee-GH-u-nu-latn"),
  46         new Locale("th", "TH", "TH"),
  47         Locale.forLanguageTag("th-TH"),
  48         Locale.forLanguageTag("th-TH-u-nu-thai"),
  49         Locale.forLanguageTag("th-TH-u-nu-hoge"),
  50     };
  51 
  52     // expected numbering system for each locale
  53     static String[] expectedNumSystem = {
  54         "latn", // ar
  55         "latn", // ar-EG
  56         "latn", // ar-DZ
  57         "arab", // ar-EG-u-nu-arab
  58         "latn", // ar-EG-u-nu-latn
  59         "arab", // ar-DZ-u-nu-arab
  60         "latn", // ar-DZ-u-nu-latn
  61         "latn", // ee
  62         "latn", // ee-GH
  63         "latn", // ee-GH-u-nu-latn
  64         "thai", // th_TH_TH
  65         "latn", // th-TH
  66         "thai", // th-TH-u-nu-thai
  67         "latn", // th-TH-u-nu-hoge (invalid)
  68     };
  69 
  70     public static void main(String[] args) {
  71         int num = 123456;
  72 
  73         for (int i = 0; i < locales.length; i++) {
  74             NumberFormat nf = NumberFormat.getIntegerInstance(locales[i]);
  75             String formatted = nf.format(num);
  76             System.out.printf("%s is %s in %s locale (expected in %s script).\n",
  77                 num, formatted, locales[i], expectedNumSystem[i]);
  78             if (!checkResult(formatted, expectedNumSystem[i])) {
  79                 throw new RuntimeException("test failed. expected number system was not returned.");
  80             }
  81         }
  82     }
  83 
  84     static boolean checkResult(String formatted, String numSystem) {
  85         switch (numSystem) {
  86             case "arab":
  87                 return formatted.charAt(0) == '\u0661';
  88             case "latn":
  89                 return formatted.charAt(0) == '1';
  90             case "thai":
  91                 return formatted.charAt(0) == '\u0e51';
  92             default:
  93                 return false;
  94         }
  95     }
  96 }