1 /*
  2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8071929
 27  * @summary Test obsolete ISO3166-1 alpha-2 country codes should not be retrieved.
 28  * ISO3166-1 alpha-2, ISO3166-1 alpha-3, ISO3166-3 country codes
 29  * from overloaded getISOCountries(Iso3166 type) are retrieved correctly.
 30  */
 31 import java.util.HashSet;
 32 import java.util.List;
 33 import java.util.Locale;
 34 import java.util.Locale.IsoCountryCode;
 35 import java.util.Set;
 36 import java.util.stream.Collectors;
 37 
 38 public class Bug8071929 {
 39 
 40     private static final List<String> ISO3166_1_ALPHA2_OBSOLETE_CODES = List.of("AN", "BU", "CS",
 41             "NT", "SF", "TP", "YU", "ZR");
 42 
 43     private static final Set<String> ISO3166_3EXPECTED = Set.of(
 44             "AIDJ", "ANHH", "BQAQ", "BUMM", "BYAA", "CSHH", "CSXX", "CTKI", "DDDE",
 45             "DYBJ", "FQHH", "FXFR", "GEHH", "HVBF", "JTUM", "MIUM", "NHVU", "NQAQ",
 46             "NTHH", "PCHH", "PUUM", "PZPA", "RHZW", "SKIN", "SUHH", "TPTL", "VDVN",
 47             "WKUM", "YDYE", "YUCS", "ZRCD");
 48 
 49     private static final Set<String> ISO3166_1_ALPHA3_EXPECTED
 50             = Set.of("ABW", "AFG", "AGO", "AIA", "ALA", "ALB", "AND",
 51                     "ARE", "ARG", "ARM", "ASM", "ATA", "ATF", "ATG",
 52                     "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BES", "BFA",
 53                     "BGD", "BGR", "BHR", "BHS", "BIH", "BLM", "BLR", "BLZ",
 54                     "BMU", "BOL", "BRA", "BRB", "BRN", "BTN", "BVT", "BWA", "CAF", "CAN",
 55                     "CCK", "CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COK", "COL",
 56                     "COM", "CPV", "CRI", "CUB", "CUW", "CXR", "CYM", "CYP", "CZE", "DEU",
 57                     "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", "ESH", "ESP",
 58                     "EST", "ETH", "FIN", "FJI", "FLK", "FRA", "FRO", "FSM", "GAB", "GBR",
 59                     "GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNB", "GNQ",
 60                     "GRC", "GRD", "GRL", "GTM", "GUF", "GUM", "GUY", "HKG", "HMD", "HND",
 61                     "HRV", "HTI", "HUN", "IDN", "IMN", "IND", "IOT", "IRL", "IRN", "IRQ",
 62                     "ISL", "ISR", "ITA", "JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ",
 63                     "KHM", "KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBR", "LBY", "LCA",
 64                     "LIE", "LKA", "LSO", "LTU", "LUX", "LVA", "MAC", "MAF", "MAR", "MCO",
 65                     "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", "MLT", "MMR", "MNE",
 66                     "MNG", "MNP", "MOZ", "MRT", "MSR", "MTQ", "MUS", "MWI", "MYS", "MYT",
 67                     "NAM", "NCL", "NER", "NFK", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL",
 68                     "NRU", "NZL", "OMN", "PAK", "PAN", "PCN", "PER", "PHL", "PLW", "PNG",
 69                     "POL", "PRI", "PRK", "PRT", "PRY", "PSE", "PYF", "QAT", "REU", "ROU",
 70                     "RUS", "RWA", "SAU", "SDN", "SEN", "SGP", "SGS", "SHN", "SJM", "SLB",
 71                     "SLE", "SLV", "SMR", "SOM", "SPM", "SRB", "SSD", "STP", "SUR", "SVK",
 72                     "SVN", "SWE", "SWZ", "SXM", "SYC", "SYR", "TCA", "TCD", "TGO", "THA",
 73                     "TJK", "TKL", "TKM", "TLS", "TON", "TTO", "TUN", "TUR", "TUV", "TWN",
 74                     "TZA", "UGA", "UKR", "UMI", "URY", "USA", "UZB", "VAT", "VCT", "VEN",
 75                     "VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "YEM", "ZAF", "ZMB", "ZWE");
 76 
 77     /**
 78      * This method checks that obsolete ISO3166-1 alpha-2 country codes are not
 79      * retrieved in output of getISOCountries() method.
 80      */
 81     private static void checkISO3166_1_Alpha2ObsoleteCodes() {
 82         Set<String> unexpectedCodes = ISO3166_1_ALPHA2_OBSOLETE_CODES.stream().
 83                 filter(Set.of(Locale.getISOCountries())::contains).collect(Collectors.toSet());
 84         if (!unexpectedCodes.isEmpty()) {
 85             throw new RuntimeException("Obsolete ISO3166-1 alpha2 two letter"
 86                     + " country Codes " + unexpectedCodes + " in output of getISOCountries() method");
 87         }
 88     }
 89 
 90     /**
 91      * This method checks that ISO3166-3 country codes which are PART3 of
 92      * IsoCountryCode enum, are retrieved correctly.
 93      */
 94     private static void checkISO3166_3Codes() {
 95         Set<String> iso3166_3Codes = Locale.getISOCountries(IsoCountryCode.PART3);
 96         if (!iso3166_3Codes.equals(ISO3166_3EXPECTED)) {
 97             reportDifference(iso3166_3Codes, ISO3166_3EXPECTED);
 98         }
 99     }
100 
101     /**
102      * This method checks that ISO3166-1 alpha-3 country codes which are
103      * PART1_ALPHA3 of IsoCountryCode enum, are retrieved correctly.
104      */
105     private static void checkISO3166_1_Alpha3Codes() {
106         Set<String> iso3166_1_Alpha3Codes = Locale.getISOCountries(IsoCountryCode.PART1_ALPHA3);
107         if (!iso3166_1_Alpha3Codes.equals(ISO3166_1_ALPHA3_EXPECTED)) {
108             reportDifference(iso3166_1_Alpha3Codes, ISO3166_1_ALPHA3_EXPECTED);
109         }
110     }
111 
112     /**
113      * This method checks that ISO3166-1 alpha-2 country codes, which are
114      * PART1_ALPHA2 of IsoCountryCode enum, are retrieved correctly.
115      */
116     private static void checkISO3166_1_Alpha2Codes() {
117         Set<String> iso3166_1_Alpha2Codes = Locale.getISOCountries(IsoCountryCode.PART1_ALPHA2);
118         Set<String> ISO3166_1_ALPHA2_EXPECTED = Set.of(Locale.getISOCountries());
119         if (!iso3166_1_Alpha2Codes.equals(ISO3166_1_ALPHA2_EXPECTED)) {
120             reportDifference(iso3166_1_Alpha2Codes, ISO3166_1_ALPHA2_EXPECTED);
121         }
122     }
123 
124     private static void reportDifference(Set<String> retrievedCountrySet, Set<String> expectedCountrySet) {
125         Set<String> retrievedSet = new HashSet<>(retrievedCountrySet);
126         Set<String> expectedSet = new HashSet<>(expectedCountrySet);
127         retrievedSet.removeAll(expectedCountrySet);
128         expectedSet.removeAll(retrievedCountrySet);
129         if ((retrievedSet.size() > 0) && (expectedSet.size() > 0)) {
130             throw new RuntimeException("Retrieved country codes set contains extra codes "
131                     + retrievedSet + " and missing codes " + expectedSet);
132         }
133         if (retrievedSet.size() > 0) {
134             throw new RuntimeException("Retrieved country codes set contains extra codes "
135                     + retrievedSet);
136         }
137         if (expectedSet.size() > 0) {
138             throw new RuntimeException("Retrieved country codes set is missing codes "
139                     + expectedSet);
140         }
141     }
142 
143     public static void main(String[] args) {
144         checkISO3166_1_Alpha2ObsoleteCodes();
145         checkISO3166_1_Alpha2Codes();
146         checkISO3166_1_Alpha3Codes();
147         checkISO3166_3Codes();
148     }
149 }