• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.unittest;
2 
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Locale;
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import java.util.Set;
14 import java.util.TreeMap;
15 
16 import org.unicode.cldr.util.CLDRConfig;
17 import org.unicode.cldr.util.CLDRPaths;
18 import org.unicode.cldr.util.ChainedMap;
19 import org.unicode.cldr.util.Pair;
20 import org.unicode.cldr.util.StandardCodes.LstrType;
21 import org.unicode.cldr.util.SupplementalDataInfo;
22 import org.unicode.cldr.util.Validity;
23 import org.unicode.cldr.util.Validity.Status;
24 import org.unicode.cldr.util.XMLFileReader;
25 import org.unicode.cldr.util.XPathParts;
26 
27 import com.ibm.icu.impl.Row.R2;
28 
29 public class TestSubdivisions extends TestFmwkPlus {
30     private static final String SUB_DIR = CLDRPaths.COMMON_DIRECTORY + "subdivisions/";
31     static final SupplementalDataInfo SDI = CLDRConfig.getInstance().getSupplementalDataInfo();
32 
main(String[] args)33     public static void main(String[] args) {
34         new TestSubdivisions().run(args);
35     }
36 
TestContainment()37     public void TestContainment() {
38         Set<String> containers = SDI.getContainersForSubdivisions();
39         assertNotNull("subdivision containers", containers);
40         Set<String> states = SDI.getContainedSubdivisions("US");
41 
42         assertRelation("US contains CA", true, states, TestFmwkPlus.CONTAINS, "usca");
43 
44         /*
45          * <subgroup type="BE" contains="WAL BRU VLG"/>
46          * <subgroup type="BE" subtype="WAL" contains="WLX WNA WHT WBR WLG"/>
47          * <subgroup type="BE" subtype="VLG" contains="VBR VWV VAN VLI VOV"/>
48          */
49         assertEquals("BE",
50             new HashSet<String>(Arrays.asList("bewal", "bebru", "bevlg")),
51             SDI.getContainedSubdivisions("BE"));
52         assertEquals("BE",
53             new HashSet<String>(Arrays.asList("bewlx", "bewna", "bewht", "bewbr", "bewlg")),
54             SDI.getContainedSubdivisions("bewal"));
55     }
56 
TestNames()57     public void TestNames() {
58         final Map<String, R2<List<String>, String>> subdivisionAliases = SDI.getLocaleAliasInfo().get("subdivision");
59         // <subdivisionAlias type="CN-71" replacement="TW" reason="overlong"/>
60         //        R2<List<String>, String> region = subdivisionAliases.get(value);
61         final Validity VALIDITY = Validity.getInstance();
62         Map<String, Status> deprecated = VALIDITY.getCodeToStatus(LstrType.subdivision);
63 
64         for (String file : new File(SUB_DIR).list()) {
65             if (!file.endsWith(".xml")) {
66                 continue;
67             }
68             checkSubdivisionFile(file, subdivisionAliases, deprecated);
69         }
70     }
71 
checkSubdivisionFile(String file, final Map<String, R2<List<String>, String>> subdivisionAliases, Map<String, Status> deprecated)72     private void checkSubdivisionFile(String file,
73         final Map<String, R2<List<String>, String>> subdivisionAliases,
74         Map<String, Status> deprecated) {
75         String lang = file.replace(".xml", "");
76 
77         List<Pair<String, String>> data = new ArrayList<>();
78         XMLFileReader.loadPathValues(SUB_DIR + file, data, true);
79         logln(file + "\t" + data.size());
80         ChainedMap.M4<String, String, String, Status> countryToNameToSubdivisions = ChainedMap.of(
81             new TreeMap<String, Object>(), new TreeMap<String, Object>(), new TreeMap<String, Object>(), Status.class);
82 
83         for (Pair<String, String> entry : data) {
84             // <subdivision type="AD-02">Canillo</subdivision>
85             XPathParts parts = XPathParts.getFrozenInstance(entry.getFirst());
86             if (!parts.getElement(-1).equals("subdivision")) {
87                 continue;
88             }
89             String name = entry.getSecond();
90             final String subdivision = parts.getAttributeValue(-1, "type");
91             String country = subdivision.substring(0, 2);
92 
93             // if there is an alias, we're ok, don't bother with it
94             R2<List<String>, String> subdivisionAlias = subdivisionAliases.get(subdivision);
95             if (subdivisionAlias != null) {
96                 // String countryName = CLDRConfig.getInstance().getEnglish().getName(CLDRFile.TERRITORY_NAME, country);
97                 // assertEquals("country " + country + " = subdivision " + subdivision, countryName, value);
98                 continue;
99             }
100             countryToNameToSubdivisions.put(country, name, subdivision, deprecated.get(subdivision));
101         }
102         // now look for uniqueness
103         LinkedHashSet<String> problemSet = new LinkedHashSet<>();
104         for (Entry<String, Map<String, Map<String, Status>>> entry1 : countryToNameToSubdivisions) {
105             String country = entry1.getKey().toUpperCase(Locale.ROOT);
106             for (Entry<String, Map<String, Status>> entry2 : entry1.getValue().entrySet()) {
107                 String name = entry2.getKey();
108                 Map<String, Status> subdivisionMap = entry2.getValue();
109                 if (subdivisionMap.size() == 1) {
110                     continue;
111                 }
112                 logln(lang + "," + country + "Name «" + name + "» for " + subdivisionMap.keySet());
113                 // we have multiple names.
114                 // remove the deprecated ones, but generate aliases
115                 problemSet.clear();
116                 for (Iterator<Entry<String, Status>> it = subdivisionMap.entrySet().iterator(); it.hasNext();) {
117                     Entry<String, Status> entry = it.next();
118                     if (entry.getValue() != Status.regular) { // if not deprecated
119                         problemSet.add(entry.getKey());
120                         it.remove();
121                     }
122                 }
123                 if (problemSet.size() < 2) {
124                     continue;
125                 }
126                 // warn about collisions
127                 errln(lang + "," + country + "Name collision for «" + name + "» in " + problemSet);
128 
129                 // show the possible aliases to add
130                 String first = problemSet.iterator().next();
131                 for (String deprecatedItem : subdivisionMap.keySet()) {
132                     warnln(lang + "," + country + "Consider adding: "
133                         + "<subdivisionAlias type=\"" + deprecatedItem
134                         + "\" replacement=\"" + first
135                         + "\" reason=\"deprecated\"/>");
136                 }
137             }
138         }
139     }
140 }
141