1 package org.unicode.cldr.unittest; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.util.Collection; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 import java.util.Set; 10 11 import org.unicode.cldr.draft.FileUtilities; 12 import org.unicode.cldr.util.CLDRConfig; 13 import org.unicode.cldr.util.CLDRFile; 14 import org.unicode.cldr.util.CldrUtility; 15 import org.unicode.cldr.util.SupplementalDataInfo; 16 17 import com.google.common.base.Splitter; 18 import com.google.common.collect.ImmutableSet; 19 import com.google.common.collect.ImmutableSetMultimap; 20 import com.google.common.collect.Multimap; 21 import com.google.common.collect.TreeMultimap; 22 import com.ibm.icu.impl.Row.R2; 23 import com.ibm.icu.util.ICUUncheckedIOException; 24 25 public class TestUnContainment extends TestFmwkPlus { 26 static CLDRConfig testInfo = CLDRConfig.getInstance(); 27 private static final SupplementalDataInfo SUPPLEMENTAL_DATA_INFO = testInfo.getSupplementalDataInfo(); 28 Map<String, R2<List<String>, String>> regionToInfo = SUPPLEMENTAL_DATA_INFO 29 .getLocaleAliasInfo() 30 .get("territory"); 31 32 private static final Set<String> NOT_CLDR_TERRITORY_CODES = ImmutableSet.of("830"); // Channel Islands 33 private static final Set<String> KNOWN_CONTAINMENT_EXCEPTIONS = ImmutableSet.of("AQ","680"); // Antarctica, Sark 34 35 final Multimap<String, String> UnChildToParent; 36 { 37 Multimap<String, String> _UnChildToParent = TreeMultimap.create(); 38 Splitter tab = Splitter.on('\t').trimResults(); try(BufferedReader unCodes = CldrUtility.getUTF8Data("external/UnCodes.txt");)39 try (BufferedReader unCodes = CldrUtility.getUTF8Data("external/UnCodes.txt");) { 40 for (String line : FileUtilities.in(unCodes)) { 41 List<String> items = tab.splitToList(line); 42 if (line.isEmpty() || line.startsWith("Global Code")) { 43 continue; 44 } 45 String parent = null; 46 for (int i = 0; i < 10; i += 2) { 47 String region = items.get(i); 48 if (!region.isEmpty()) { 49 region = unToCldrCode(region); 50 if (parent != null && region != null){ 51 _UnChildToParent.put(region, parent); 52 } 53 if (region != null) { 54 parent = region; 55 } 56 } 57 if (i == 6) { 58 ++i; // hack because last two are out of order 59 } 60 } 61 } 62 UnChildToParent = ImmutableSetMultimap.copyOf(_UnChildToParent); 63 } catch (IOException e) { 64 throw new ICUUncheckedIOException(e); 65 } 66 } 67 main(String[] args)68 public static void main(String[] args) { 69 new TestUnContainment().run(args); 70 } 71 name(Collection<String> codes)72 private String name(Collection<String> codes) { 73 StringBuilder result = new StringBuilder(); 74 for (String code : codes) { 75 if (result.length() != 0) { 76 result.append(", "); 77 } 78 result.append(name(code)); 79 } 80 return result.toString(); 81 } 82 name(String code)83 private String name(String code) { 84 String name = testInfo.getEnglish().getName(CLDRFile.TERRITORY_NAME, code); 85 return name + " (" + code + ")"; 86 } 87 unToCldrCode(String code)88 private String unToCldrCode(String code) { 89 90 if (NOT_CLDR_TERRITORY_CODES.contains(code)) { 91 return null; 92 } 93 94 R2<List<String>, String> codeInfo = regionToInfo.get(code); 95 if (codeInfo != null) { 96 if (codeInfo.get0() != null && !codeInfo.get0().isEmpty()) { 97 code = codeInfo.get0().get(0); 98 } 99 } 100 return code; 101 } 102 TestContainment()103 public void TestContainment() { 104 105 /* 106 CLDR 107 <group type="001" contains="019 002 150 142 009"/> <!--World --> 108 <group type="001" contains="EU EZ UN" status="grouping"/> <!--European Union, Eurozone, United Nations --> 109 <group type="001" contains="QU" status="deprecated"/> <!--European Union --> 110 <group type="011" contains="BF BJ CI CV GH GM GN GW LR ML MR NE NG SH SL SN TG"/> <!--Western Africa --> 111 */ 112 for (Entry<String, Collection<String>> entry : UnChildToParent.asMap().entrySet()) { 113 Collection<String> unParents = entry.getValue(); 114 String unChild = entry.getKey(); 115 //System.out.println(name(unParents) + "\t" + name(unChild)); 116 for (String unParent : unParents) { 117 Set<String> children = SUPPLEMENTAL_DATA_INFO.getContained(unParent); 118 if (children != null && children.contains(unChild)) { 119 continue; 120 } 121 // See CLDR ticket 10187 for rationalization on the known containment exceptions. 122 if (KNOWN_CONTAINMENT_EXCEPTIONS.contains(unChild)) { 123 continue; 124 } 125 msg("UN containment doesn't match CLDR for " + name(unParent) 126 + ": cldr children " + children 127 + " don't contain UN " + name(unChild), ERR, true, true); 128 } 129 } 130 } 131 } 132