jdk/test/java/util/Locale/Bug7069824.java
author peytoia
Thu, 08 May 2014 08:22:25 +0900
changeset 24267 094b5d3c7159
parent 14009 21856a20cc1d
child 45838 c6223c3b4594
permissions -rw-r--r--
8042360: Subtag syntax check is incomplete in Locale.LanguageRange Reviewed-by: naoto, okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     1
/*
24267
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     4
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     8
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    13
 * accompanied this code).
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    14
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    18
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    21
 * questions.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    22
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    23
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    24
/*
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    25
 * @test
24267
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    26
 * @bug 7069824 8042360
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    27
 * @summary Verify implementation for Locale matching.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    28
 * @run main Bug7069824
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    29
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    30
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    31
import java.util.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    32
import java.util.Locale.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    33
import static java.util.Locale.FilteringMode.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    34
import static java.util.Locale.LanguageRange.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    35
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    36
public class Bug7069824 {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    37
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    38
    static boolean err = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    39
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    40
    public static void main(String[] args) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    41
        testLanguageRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    42
        testLocale();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    43
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    44
        if (err) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    45
            throw new RuntimeException("Failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    46
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    47
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    48
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    49
    private static void testLanguageRange() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    50
        System.out.println("Test LanguageRange class...");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    51
        testConstants();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    52
        testConstructors();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    53
        testMethods();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    54
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    55
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    56
    private static void testLocale() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    57
        System.out.println("Test Locale class...");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    58
        test_filter();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    59
        test_filterTags();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    60
        test_lookup();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    61
        test_lookupTag();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    62
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    63
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    64
    private static void testConstants() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    65
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    66
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    67
        if (MIN_WEIGHT != 0.0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    68
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    69
            System.err.println("    MIN_WEIGHT should be 0.0 but got "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    70
                + MIN_WEIGHT);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    71
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    72
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    73
        if (MAX_WEIGHT != 1.0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    74
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    75
            System.err.println("    MAX_WEIGHT should be 1.0 but got "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    76
                + MAX_WEIGHT);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    77
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    78
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    79
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    80
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    81
            System.err.println("  testConstants() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    82
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    83
            System.out.println("  testConstants() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    84
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    85
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    86
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    87
    private static void testConstructors() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    88
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    89
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    90
        LanguageRange lr;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    91
        String range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    92
        double weight;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    93
24267
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    94
        // Testcase for 8042360
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    95
        range = "en-Latn-1234567890";
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    96
        try {
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    97
            lr = new LanguageRange(range);
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    98
            error = true;
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
    99
            System.err.println("    IAE should be thrown for LanguageRange("
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
   100
                + range + ").");
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
   101
        }
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
   102
        catch (IllegalArgumentException ex) {
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
   103
        }
094b5d3c7159 8042360: Subtag syntax check is incomplete in Locale.LanguageRange
peytoia
parents: 14009
diff changeset
   104
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   105
        range = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   106
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   107
            lr = new LanguageRange(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   108
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   109
            System.err.println("    NPE should be thrown for LanguageRange("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   110
                + range + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   111
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   112
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   113
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   114
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   115
        range = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   116
        weight = 0.8;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   117
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   118
            lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   119
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   120
            System.err.println("    NPE should be thrown for LanguageRange("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   121
                + range + ", " + weight + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   122
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   123
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   124
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   125
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   126
        range = "elvish";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   127
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   128
            lr = new LanguageRange(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   129
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   130
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   131
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   132
            System.err.println("    " + ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   133
                + " should not be thrown for LanguageRange(" + range + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   134
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   135
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   136
        range = "de-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   137
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   138
            lr = new LanguageRange(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   139
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   140
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   141
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   142
            System.err.println("    " + ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   143
                + " should not be thrown for LanguageRange(" + range + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   144
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   145
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   146
        range = "ar";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   147
        weight = 0.8;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   148
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   149
            lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   150
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   151
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   152
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   153
            System.err.println("    " + ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   154
                + " should not be thrown for LanguageRange(" + range + ", "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   155
                + weight + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   156
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   157
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   158
        range = "ja";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   159
        weight = -0.8;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   160
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   161
            lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   162
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   163
            System.err.println("    IAE should be thrown for LanguageRange("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   164
                + range + ", " + weight + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   165
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   166
        catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   167
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   168
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   169
        range = "Elvish";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   170
        weight = 3.0;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   171
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   172
            lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   173
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   174
            System.err.println("    IAE should be thrown for LanguageRange("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   175
                + range + ", " + weight + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   176
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   177
        catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   178
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   179
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   180
        String[] illformedRanges = {"-ja", "ja--JP", "en-US-", "a4r", "ar*",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   181
            "ar-*EG", "", "abcdefghijklmn", "ja-J=", "ja-opqrstuvwxyz"};
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   182
        for (String r : illformedRanges) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   183
            try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   184
                lr = new LanguageRange(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   185
                error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   186
                System.err.println("    IAE should be thrown for LanguageRange("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   187
                    + r + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   188
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   189
            catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   190
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   191
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   192
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   193
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   194
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   195
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   196
            System.err.println("  testConstructors() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   197
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   198
            System.out.println("  testConstructors() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   199
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   200
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   201
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   202
    private static void testMethods() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   203
        test_getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   204
        test_getWeight();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   205
        test_equals();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   206
        test_parse();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   207
        test_mapEquivalents();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   208
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   209
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   210
    private static void test_getRange() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   211
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   212
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   213
        String range = "ja";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   214
        double weight = 0.5;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   215
        LanguageRange lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   216
        if (!lr.getRange().equals(range)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   217
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   218
             System.err.println("    LanguageRange.getRange() returned unexpected value. Expected: "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   219
                 + range + ", got: " + lr.getRange());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   220
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   221
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   222
        range = "en-US";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   223
        weight = 0.5;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   224
        lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   225
        if (!lr.getRange().equals(range.toLowerCase())) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   226
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   227
             System.err.println("    LanguageRange.getRange() returned unexpected value. Expected: "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   228
                 + range + ", got: " + lr.getRange());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   229
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   230
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   231
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   232
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   233
            System.err.println("  test_getRange() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   234
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   235
            System.out.println("  test_getRange() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   236
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   237
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   238
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   239
    private static void test_getWeight() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   240
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   241
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   242
        String range = "ja";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   243
        double weight = 0.5;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   244
        LanguageRange lr = new LanguageRange(range, weight);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   245
        if (lr.getWeight() != weight) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   246
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   247
             System.err.println("    LanguageRange.getWeight() returned unexpected value. Expected: "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   248
                 + weight + ", got: " + lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   249
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   250
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   251
        range = "ja";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   252
        weight = MAX_WEIGHT; // default
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   253
        lr = new LanguageRange(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   254
        if (!lr.getRange().equals(range) || lr.getWeight() != MAX_WEIGHT) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   255
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   256
             System.err.println("    LanguageRange.getWeight() returned unexpected value. Expected: "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   257
                 + weight + ", got: " + lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   258
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   259
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   260
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   261
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   262
            System.err.println("  test_getWeight() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   263
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   264
            System.out.println("  test_getWeight() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   265
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   266
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   267
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   268
    private static void test_equals() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   269
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   270
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   271
        LanguageRange lr1 = new LanguageRange("ja", 1.0);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   272
        LanguageRange lr2 = new LanguageRange("ja");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   273
        LanguageRange lr3 = new LanguageRange("ja", 0.1);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   274
        LanguageRange lr4 = new LanguageRange("en", 1.0);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   275
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   276
        if (!lr1.equals(lr2)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   277
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   278
            System.err.println("    LanguageRange(LR(ja, 1.0)).equals(LR(ja)) should return true.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   279
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   280
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   281
        if (lr1.equals(lr3)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   282
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   283
            System.err.println("    LanguageRange(LR(ja, 1.0)).equals(LR(ja, 0.1)) should return false.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   284
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   285
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   286
        if (lr1.equals(lr4)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   287
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   288
            System.err.println("    LanguageRange(LR(ja, 1.0)).equals(LR(en, 1.0)) should return false.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   289
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   290
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   291
        if (lr1.equals(null)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   292
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   293
            System.err.println("    LanguageRange(LR(ja, 1.0)).equals(null) should return false.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   294
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   295
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   296
        if (lr1.equals("")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   297
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   298
            System.err.println("    LanguageRange(LR(ja, 1.0)).equals(\"\") should return false.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   299
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   300
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   301
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   302
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   303
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   304
            System.err.println("  test_equals() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   305
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   306
            System.out.println("  test_equals() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   307
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   308
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   309
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   310
    private static void test_parse() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   311
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   312
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   313
        List<LanguageRange> list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   314
        String str = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   315
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   316
            list = LanguageRange.parse(str);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   317
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   318
            System.err.println("    NPE should be thrown for parse("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   319
                + str + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   320
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   321
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   322
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   323
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   324
        str = "";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   325
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   326
            list = LanguageRange.parse("");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   327
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   328
            System.err.println("    IAE should be thrown for parse("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   329
                + str + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   330
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   331
        catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   332
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   333
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   334
        str = "ja;q=3";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   335
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   336
            list = LanguageRange.parse(str);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   337
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   338
            System.err.println("IAE should be thrown for parse("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   339
                 + str + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   340
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   341
        catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   342
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   343
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   344
        str = "Accept-Language: fr-FX,de-DE;q=0.5, fr-tp-x-FOO;q=0.1,"
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   345
                  + "en-X-tp;q=0.6,en-FR;q=.7,de-de;q=0.8, iw;q=0.4, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   346
                  + "he;q=0.4, de-de;q=0.5,ja, in-tpp, in-tp;q=0.2";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   347
        ArrayList<LanguageRange> expected = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   348
        expected.add(new LanguageRange("fr-fx", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   349
        expected.add(new LanguageRange("fr-fr", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   350
        expected.add(new LanguageRange("ja", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   351
        expected.add(new LanguageRange("in-tpp", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   352
        expected.add(new LanguageRange("id-tpp", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   353
        expected.add(new LanguageRange("en-fr", 0.7));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   354
        expected.add(new LanguageRange("en-fx", 0.7));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   355
        expected.add(new LanguageRange("en-x-tp", 0.6));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   356
        expected.add(new LanguageRange("de-de", 0.5));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   357
        expected.add(new LanguageRange("de-dd", 0.5));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   358
        expected.add(new LanguageRange("iw", 0.4));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   359
        expected.add(new LanguageRange("he", 0.4));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   360
        expected.add(new LanguageRange("in-tp", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   361
        expected.add(new LanguageRange("id-tl", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   362
        expected.add(new LanguageRange("id-tp", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   363
        expected.add(new LanguageRange("in-tl", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   364
        expected.add(new LanguageRange("fr-tp-x-foo", 0.1));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   365
        expected.add(new LanguageRange("fr-tl-x-foo", 0.1));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   366
        List<LanguageRange> got = LanguageRange.parse(str);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   367
        if (!areEqual(expected, got)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   368
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   369
            System.err.println("    #1 parse() test failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   370
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   371
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   372
        str = "Accept-Language: hak-CN;q=0.8, no-bok-NO;q=0.9, no-nyn, cmn-CN;q=0.1";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   373
        expected = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   374
        expected.add(new LanguageRange("no-nyn", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   375
        expected.add(new LanguageRange("nn", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   376
        expected.add(new LanguageRange("no-bok-no", 0.9));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   377
        expected.add(new LanguageRange("nb-no", 0.9));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   378
        expected.add(new LanguageRange("hak-CN", 0.8));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   379
        expected.add(new LanguageRange("zh-hakka-CN", 0.8));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   380
        expected.add(new LanguageRange("i-hak-CN", 0.8));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   381
        expected.add(new LanguageRange("cmn-CN", 0.1));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   382
        expected.add(new LanguageRange("zh-cmn-CN", 0.1));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   383
        expected.add(new LanguageRange("zh-guoyu-CN", 0.1));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   384
        got = LanguageRange.parse(str);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   385
        if (!areEqual(expected, got)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   386
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   387
            System.err.println("    #2 parse() test failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   388
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   389
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   390
        str = "Accept-Language: rki;q=0.4, no-bok-NO;q=0.9, ccq;q=0.5";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   391
        expected = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   392
        expected.add(new LanguageRange("no-bok-no", 0.9));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   393
        expected.add(new LanguageRange("nb-no", 0.9));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   394
        expected.add(new LanguageRange("rki", 0.4));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   395
        expected.add(new LanguageRange("ybd", 0.4));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   396
        expected.add(new LanguageRange("ccq", 0.4));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   397
        got = LanguageRange.parse(str);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   398
        if (!areEqual(expected, got)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   399
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   400
            System.err.println("    #3 parse() test failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   401
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   402
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   403
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   404
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   405
            System.err.println("  test_parse() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   406
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   407
            System.out.println("  test_parse() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   408
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   409
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   410
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   411
    private static boolean areEqual(List<LanguageRange> expected,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   412
                                    List<LanguageRange> got) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   413
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   414
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   415
        int expectedSize = expected.size();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   416
        int actualSize = got.size();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   417
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   418
        if (expectedSize != actualSize) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   419
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   420
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   421
            System.err.println("  Expected size=" + expectedSize);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   422
            for (LanguageRange lr : expected) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   423
                System.err.println("    range=" + lr.getRange()
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   424
                    + ", weight=" + lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   425
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   426
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   427
            System.out.println("  Actual size=" + actualSize);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   428
            for (LanguageRange lr : got) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   429
                System.err.println("    range=" + lr.getRange()
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   430
                    + ", weight=" + lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   431
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   432
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   433
            for (int i = 0; i < expectedSize; i++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   434
                LanguageRange lr1 = expected.get(i);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   435
                LanguageRange lr2 = got.get(i);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   436
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   437
                if (!lr1.getRange().equals(lr2.getRange())
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   438
                    || lr1.getWeight() != lr2.getWeight()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   439
                    error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   440
                    System.err.println("  " + i + ": Expected: range=" + lr1.getRange()
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   441
                        + ", weight=" + lr1.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   442
                    System.err.println("  " + i + ": Actual:   range=" + lr2.getRange()
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   443
                        + ", weight=" + lr2.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   444
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   445
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   446
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   447
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   448
        return !error;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   449
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   450
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   451
    private static void test_mapEquivalents() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   452
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   453
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   454
        String ranges = "zh, zh-TW;q=0.8, ar;q=0.9, EN, zh-HK, ja-JP;q=0.2, es;q=0.4";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   455
        List<LanguageRange> priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   456
        HashMap<String, List<String>> map = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   457
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   458
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   459
            List<LanguageRange> list =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   460
                LanguageRange.mapEquivalents(priorityList, null);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   461
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   462
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   463
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   464
             System.err.println(ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   465
                 + " should not be thrown for mapEquivalents(priorityList, null).");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   466
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   467
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   468
        map = new HashMap<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   469
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   470
            List<LanguageRange> list =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   471
                LanguageRange.mapEquivalents(priorityList, map);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   472
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   473
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   474
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   475
             System.err.println(ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   476
                 + " should not be thrown for mapEquivalents(priorityList, empty map).");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   477
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   478
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   479
        ArrayList<String> equivalentList = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   480
        equivalentList.add("ja");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   481
        equivalentList.add("ja-Hira");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   482
        map.put("ja", equivalentList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   483
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   484
            List<LanguageRange> list = LanguageRange.mapEquivalents(null, map);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   485
             error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   486
             System.err.println("NPE should be thrown for mapEquivalents(null, map).");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   487
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   488
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   489
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   490
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   491
        map = new LinkedHashMap<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   492
        ArrayList<String> equivalentList1 = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   493
        equivalentList1.add("ja");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   494
        equivalentList1.add("ja-Hira");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   495
        map.put("ja", equivalentList1);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   496
        ArrayList<String> equivalentList2 = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   497
        equivalentList2.add("zh-Hans");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   498
        equivalentList2.add("zh-Hans-CN");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   499
        equivalentList2.add("zh-CN");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   500
        map.put("zh", equivalentList2);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   501
        ArrayList<String> equivalentList3 = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   502
        equivalentList3.add("zh-TW");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   503
        equivalentList3.add("zh-Hant");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   504
        map.put("zh-TW", equivalentList3);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   505
        map.put("es", null);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   506
        ArrayList<String> equivalentList4 = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   507
        map.put("en", equivalentList4);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   508
        ArrayList<String> equivalentList5 = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   509
        equivalentList5.add("de");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   510
        map.put("zh-HK", equivalentList5);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   511
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   512
        ArrayList<LanguageRange> expected = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   513
        expected.add(new LanguageRange("zh-hans", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   514
        expected.add(new LanguageRange("zh-hans-cn", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   515
        expected.add(new LanguageRange("zh-cn", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   516
        expected.add(new LanguageRange("de", 1.0));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   517
        expected.add(new LanguageRange("ar", 0.9));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   518
        expected.add(new LanguageRange("zh-tw", 0.8));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   519
        expected.add(new LanguageRange("zh-hant", 0.8));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   520
        expected.add(new LanguageRange("ja-jp", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   521
        expected.add(new LanguageRange("ja-hira-jp", 0.2));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   522
        List<LanguageRange> got =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   523
            LanguageRange.mapEquivalents(priorityList, map);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   524
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   525
        if (!areEqual(expected, got)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   526
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   527
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   528
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   529
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   530
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   531
            System.err.println("  test_mapEquivalents() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   532
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   533
            System.out.println("  test_mapEquivalents() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   534
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   535
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   536
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   537
    private static void test_filter() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   538
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   539
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   540
        String ranges = "ja-JP, fr-FR";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   541
        String tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   542
        FilteringMode mode = EXTENDED_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   543
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   544
        List<LanguageRange> priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   545
        List<Locale> tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   546
        String actualLocales =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   547
            showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   548
        String expectedLocales = "ja-JP-hepburn, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   549
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   550
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   551
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   552
            showErrorMessage("#1 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   553
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   554
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   555
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   556
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   557
        ranges = "ja-*-JP, fr-FR";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   558
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   559
        mode = EXTENDED_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   560
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   561
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   562
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   563
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   564
        expectedLocales = "ja-JP-hepburn, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   565
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   566
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   567
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   568
            showErrorMessage("#2 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   569
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   570
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   571
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   572
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   573
        ranges = "ja-*-JP, fr-FR, de-de;q=0.2";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   574
        tags = "de-DE, en, ja-JP-hepburn, de-de, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   575
        mode = AUTOSELECT_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   576
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   577
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   578
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   579
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   580
        expectedLocales = "ja-JP-hepburn, ja-Latn-JP, de-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   581
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   582
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   583
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   584
            showErrorMessage("#3 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   585
                             ranges, tags,expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   586
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   587
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   588
        ranges = "ja-JP, fr-FR, de-de;q=0.2";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   589
        tags = "de-DE, en, ja-JP-hepburn, de-de, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   590
        mode = AUTOSELECT_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   591
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   592
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   593
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   594
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   595
        expectedLocales = "ja-JP-hepburn, de-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   596
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   597
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   598
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   599
            showErrorMessage("#4 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   600
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   601
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   602
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   603
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   604
        ranges = "en;q=0.2, ja-*-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   605
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   606
        mode = IGNORE_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   607
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   608
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   609
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   610
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   611
        expectedLocales = "en";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   612
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   613
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   614
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   615
            showErrorMessage("#5 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   616
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   617
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   618
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   619
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   620
        ranges = "en;q=0.2, ja-*-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   621
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   622
        mode = MAP_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   623
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   624
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   625
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   626
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   627
        expectedLocales = "ja-JP-hepburn, en";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   628
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   629
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   630
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   631
            showErrorMessage("#6 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   632
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   633
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   634
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   635
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   636
        ranges = "en;q=0.2, ja-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   637
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   638
        mode = REJECT_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   639
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   640
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   641
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   642
        actualLocales = showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   643
        expectedLocales = "ja-JP-hepburn, en";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   644
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   645
        if (!expectedLocales.equals(actualLocales)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   646
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   647
            showErrorMessage("#7 filter(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   648
                             ranges, tags, expectedLocales, actualLocales);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   649
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   650
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   651
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   652
        ranges = "en;q=0.2, ja-*-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   653
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   654
        mode = REJECT_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   655
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   656
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   657
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   658
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   659
            actualLocales =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   660
                showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   661
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   662
            System.out.println("IAE should be thrown for filter("
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   663
                + mode + ").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   664
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   665
        catch (IllegalArgumentException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   666
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   667
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   668
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   669
        ranges = "en;q=0.2, ja-*-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   670
        tags = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   671
        mode = REJECT_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   672
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   673
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   674
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   675
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   676
            actualLocales =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   677
                showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   678
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   679
            System.out.println("NPE should be thrown for filter(tags=null).");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   680
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   681
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   682
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   683
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   684
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   685
        ranges = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   686
        tags = "de-DE, en, ja-JP-hepburn, fr, he, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   687
        mode = REJECT_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   688
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   689
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   690
            priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   691
            tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   692
            actualLocales =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   693
                showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   694
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   695
            System.out.println("NPE should be thrown for filter(ranges=null).");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   696
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   697
        catch (NullPointerException ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   698
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   699
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   700
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   701
        ranges = "en;q=0.2, ja-*-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   702
        tags = "";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   703
        mode = REJECT_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   704
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   705
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   706
        tagList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   707
        try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   708
            actualLocales =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   709
                showLocales(Locale.filter(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   710
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   711
        catch (Exception ex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   712
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   713
            System.out.println(ex
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   714
                + " should not be thrown for filter(" + ranges + ", \"\").");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   715
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   716
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   717
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   718
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   719
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   720
            System.out.println("  test_filter() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   721
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   722
            System.out.println("  test_filter() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   723
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   724
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   725
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   726
    private static void test_filterTags() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   727
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   728
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   729
        String ranges = "en;q=0.2, *;q=0.6, ja";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   730
        String tags = "de-DE, en, ja-JP-hepburn, fr-JP, he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   731
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   732
        List<LanguageRange> priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   733
        List<String> tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   734
        String actualTags =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   735
            showLanguageTags(Locale.filterTags(priorityList, tagList));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   736
        String expectedTags = tags;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   737
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   738
        if (!expectedTags.equals(actualTags)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   739
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   740
            showErrorMessage("#1 filterTags()",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   741
                             ranges, tags, expectedTags, actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   742
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   743
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   744
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   745
        ranges = "en;q=0.2, ja-JP, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   746
        tags = "de-DE, en, ja-JP-hepburn, fr, he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   747
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   748
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   749
        actualTags = showLanguageTags(Locale.filterTags(priorityList, tagList));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   750
        expectedTags = "ja-jp-hepburn, en";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   751
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   752
        if (!expectedTags.equals(actualTags)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   753
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   754
            showErrorMessage("#2 filterTags()",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   755
                             ranges, tags, expectedTags, actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   756
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   757
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   758
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   759
        ranges = "de-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   760
        tags = "de-DE, de-de, de-Latn-DE, de-Latf-DE, de-DE-x-goethe, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   761
               + "de-Latn-DE-1996, de-Deva-DE, de, de-x-DE, de-Deva";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   762
        FilteringMode mode = MAP_EXTENDED_RANGES;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   763
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   764
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   765
        actualTags = showLanguageTags(Locale.filterTags(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   766
        expectedTags = "de-de, de-de-x-goethe";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   767
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   768
        if (!expectedTags.equals(actualTags)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   769
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   770
            showErrorMessage("#3 filterTags(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   771
                             ranges, tags, expectedTags, actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   772
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   773
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   774
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   775
        ranges = "de-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   776
        tags = "de-DE, de-de, de-Latn-DE, de-Latf-DE, de-DE-x-goethe, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   777
               + "de-Latn-DE-1996, de-Deva-DE, de, de-x-DE, de-Deva";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   778
        mode = EXTENDED_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   779
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   780
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   781
        actualTags = showLanguageTags(Locale.filterTags(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   782
        expectedTags = "de-de, de-latn-de, de-latf-de, de-de-x-goethe, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   783
                       + "de-latn-de-1996, de-deva-de";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   784
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   785
        if (!expectedTags.equals(actualTags)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   786
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   787
            showErrorMessage("#4 filterTags(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   788
                             ranges, tags, expectedTags, actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   789
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   790
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   791
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   792
        ranges = "de-*-DE";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   793
        tags = "de-DE, de-de, de-Latn-DE, de-Latf-DE, de-DE-x-goethe, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   794
               + "de-Latn-DE-1996, de-Deva-DE, de, de-x-DE, de-Deva";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   795
        mode = EXTENDED_FILTERING;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   796
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   797
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   798
        actualTags = showLanguageTags(Locale.filterTags(priorityList, tagList, mode));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   799
        expectedTags = "de-de, de-latn-de, de-latf-de, de-de-x-goethe, "
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   800
                       + "de-latn-de-1996, de-deva-de";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   801
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   802
        if (!expectedTags.equals(actualTags)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   803
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   804
            showErrorMessage("#5 filterTags(" + mode + ")",
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   805
                             ranges, tags, expectedTags, actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   806
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   807
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   808
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   809
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   810
            System.out.println("  test_filterTags() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   811
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   812
            System.out.println("  test_filterTags() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   813
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   814
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   815
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   816
    private static void test_lookup() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   817
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   818
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   819
        String ranges = "en;q=0.2, *-JP;q=0.6, iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   820
        String tags = "de-DE, en, ja-JP-hepburn, fr-JP, he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   821
        List<LanguageRange> priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   822
        List<Locale> localeList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   823
        String actualLocale =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   824
            Locale.lookup(priorityList, localeList).toLanguageTag();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   825
        String expectedLocale ="he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   826
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   827
        if (!expectedLocale.equals(actualLocale)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   828
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   829
            showErrorMessage("#1 lookup()", ranges, tags, expectedLocale, actualLocale);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   830
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   831
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   832
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   833
        ranges = "en;q=0.2, *-JP;q=0.6, iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   834
        tags = "de-DE, he-IL, en, iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   835
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   836
        localeList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   837
        actualLocale = Locale.lookup(priorityList, localeList).toLanguageTag();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   838
        expectedLocale = "he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   839
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   840
        if (!expectedLocale.equals(actualLocale)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   841
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   842
            showErrorMessage("#2 lookup()", ranges, tags, expectedLocale, actualLocale);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   843
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   844
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   845
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   846
        ranges = "en;q=0.2, ja-*-JP-x-foo;q=0.6, iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   847
        tags = "de-DE, fr, en, ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   848
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   849
        localeList = generateLocales(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   850
        actualLocale = Locale.lookup(priorityList, localeList).toLanguageTag();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   851
        expectedLocale = "ja-Latn-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   852
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   853
        if (!expectedLocale.equals(actualLocale)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   854
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   855
            showErrorMessage("#3 lookup()", ranges, tags, expectedLocale, actualLocale);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   856
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   857
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   858
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   859
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   860
            System.out.println("  test_lookup() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   861
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   862
            System.out.println("  test_lookup() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   863
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   864
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   865
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   866
    private static void test_lookupTag() {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   867
        boolean error = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   868
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   869
        String ranges = "en, *";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   870
        String tags = "es, de, ja-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   871
        List<LanguageRange> priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   872
        List<String> tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   873
        String actualTag = Locale.lookupTag(priorityList, tagList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   874
        String expectedTag = null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   875
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   876
        if (actualTag != null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   877
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   878
            showErrorMessage("#1 lookupTag()", ranges, tags, expectedTag, actualTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   879
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   880
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   881
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   882
        ranges= "en;q=0.2, *-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   883
        tags = "de-DE, en, ja-JP-hepburn, fr-JP, en-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   884
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   885
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   886
        actualTag = Locale.lookupTag(priorityList, tagList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   887
        expectedTag = "fr-jp";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   888
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   889
        if (!expectedTag.equals(actualTag)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   890
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   891
            showErrorMessage("#2 lookupTag()", ranges, tags, expectedTag, actualTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   892
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   893
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   894
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   895
        ranges = "en;q=0.2, ar-MO, iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   896
        tags = "de-DE, he, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   897
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   898
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   899
        actualTag = Locale.lookupTag(priorityList, tagList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   900
        expectedTag = "he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   901
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   902
        if (!expectedTag.equals(actualTag)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   903
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   904
            showErrorMessage("#3 lookupTag()", ranges, tags, expectedTag, actualTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   905
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   906
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   907
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   908
        ranges = "en;q=0.2, ar-MO, he";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   909
        tags = "de-DE, iw, fr-JP";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   910
        priorityList = LanguageRange.parse(ranges);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   911
        tagList = generateLanguageTags(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   912
        actualTag = Locale.lookupTag(priorityList, tagList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   913
        expectedTag = "iw";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   914
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   915
        if (!expectedTag.equals(actualTag)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   916
            error = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   917
            showErrorMessage("#4 lookupTag()", ranges, tags, expectedTag, actualTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   918
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   919
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   920
        if (error) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   921
            err = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   922
            System.out.println("  test_lookupTag() failed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   923
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   924
            System.out.println("  test_lookupTag() passed.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   925
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   926
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   927
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   928
    private static List<Locale> generateLocales(String tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   929
        if (tags == null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   930
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   931
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   932
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   933
        List<Locale> localeList = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   934
        if (tags.equals("")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   935
            return localeList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   936
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   937
        String[] t = tags.split(", ");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   938
        for (String tag : t) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   939
            localeList.add(Locale.forLanguageTag(tag));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   940
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   941
        return localeList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   942
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   943
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   944
    private static List<String> generateLanguageTags(String tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   945
        List<String> tagList = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   946
        String[] t = tags.split(", ");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   947
        for (String tag : t) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   948
            tagList.add(tag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   949
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   950
        return tagList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   951
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   952
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   953
    private static String showPriorityList(List<LanguageRange> priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   954
        StringBuilder sb = new StringBuilder();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   955
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   956
        Iterator<LanguageRange> itr = priorityList.iterator();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   957
        LanguageRange lr;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   958
        if (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   959
            lr = itr.next();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   960
            sb.append(lr.getRange());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   961
            sb.append(";q=");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   962
            sb.append(lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   963
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   964
        while (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   965
            sb.append(", ");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   966
            lr = itr.next();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   967
            sb.append(lr.getRange());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   968
            sb.append(";q=");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   969
            sb.append(lr.getWeight());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   970
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   971
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   972
        return sb.toString();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   973
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   974
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   975
    private static String showLanguageTags(List<String> tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   976
        StringBuilder sb = new StringBuilder();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   977
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   978
        Iterator<String> itr = tags.iterator();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   979
        if (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   980
            sb.append(itr.next());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   981
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   982
        while (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   983
            sb.append(", ");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   984
            sb.append(itr.next());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   985
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   986
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   987
        return sb.toString().trim();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   988
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   989
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   990
    private static String showLocales(List<Locale> locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   991
        StringBuilder sb = new StringBuilder();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   992
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   993
        Iterator<Locale> itr = locales.iterator();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   994
        if (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   995
            sb.append(itr.next().toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   996
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   997
        while (itr.hasNext()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   998
            sb.append(", ");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   999
            sb.append(itr.next().toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1000
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1001
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1002
        return sb.toString().trim();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1003
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1004
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1005
    private static void showErrorMessage(String methodName,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1006
                                         String priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1007
                                         String tags,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1008
                                         String expectedTags,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1009
                                         String actualTags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1010
        System.out.println("\nIncorrect " + methodName + " result.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1011
        System.out.println("  Priority list  :  " + priorityList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1012
        System.out.println("  Language tags  :  " + tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1013
        System.out.println("  Expected value : " + expectedTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1014
        System.out.println("  Actual value   : " + actualTags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1015
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1016
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
  1017
}