1 package org.unicode.cldr.unittest;
2 
3 import java.util.Collections;
4 import java.util.EnumSet;
5 import java.util.HashSet;
6 import java.util.LinkedHashSet;
7 import java.util.Set;
8 
9 import org.unicode.cldr.util.CLDRConfig;
10 import org.unicode.cldr.util.Level;
11 import org.unicode.cldr.util.Organization;
12 import org.unicode.cldr.util.StandardCodes;
13 
14 import com.google.common.collect.Sets;
15 import com.ibm.icu.dev.util.CollectionUtilities;
16 
17 public class TestCLDRLocaleCoverage extends TestFmwkPlus {
18     private static StandardCodes sc = StandardCodes.make();
19 
main(String[] args)20     public static void main(String[] args) {
21         new TestCLDRLocaleCoverage().run(args);
22     }
23 
24     /**
25      * Test whether there are any locales for the organization CLDR
26      */
TestCLDROrganizationPresence()27     public void TestCLDROrganizationPresence() {
28         Set<String> cldrLocales = sc.getLocaleCoverageLocales(
29             Organization.cldr, EnumSet.of(Level.MODERN));
30         assertNotNull("Expected CLDR modern locales not to be null",
31             cldrLocales);
32         assertTrue("Expected locales for CLDR, but found none.",
33             cldrLocales != null && !cldrLocales.isEmpty());
34     }
35 
36     /**
37      * Tests that cldr is a superset.
38      */
TestCldrSuperset()39     public void TestCldrSuperset() {
40         checkCldrLocales(Organization.google, ERR);
41         checkCldrLocales(Organization.apple, ERR);
42         checkCldrLocales(Organization.microsoft, WARN);
43     }
44 
checkCldrLocales(Organization organization, int warningLevel)45     private void checkCldrLocales(Organization organization, int warningLevel) {
46         // use a union, so that items can be higher
47         EnumSet<Level> modernModerate = EnumSet.of(Level.MODERATE, Level.MODERN);
48 
49         Set<String> orgLocalesModerate = sc.getLocaleCoverageLocales(organization, modernModerate);
50         Set<String> cldrLocalesModerate = sc.getLocaleCoverageLocales(Organization.cldr, modernModerate);
51         Set<String> failures = checkCldrLocalesSuperset(modernModerate, cldrLocalesModerate, organization, orgLocalesModerate, warningLevel,
52             Collections.emptySet());
53 
54         EnumSet<Level> modernSet = EnumSet.of(Level.MODERN);
55         Set<String> orgLocalesModern = sc.getLocaleCoverageLocales(organization, modernSet);
56         Set<String> cldrLocalesModern = sc.getLocaleCoverageLocales(Organization.cldr, modernSet);
57         failures = new HashSet<>(failures);
58         failures.add("to"); // ok to be moderate
59         checkCldrLocalesSuperset(modernSet, cldrLocalesModern, organization, orgLocalesModern, warningLevel, failures);
60     }
61 
checkCldrLocalesSuperset(Set<Level> level, Set<String> cldrLocales, Organization organization, Set<String> orgLocales, int warningLevel, Set<String> skip)62     private Set<String> checkCldrLocalesSuperset(Set<Level> level, Set<String> cldrLocales, Organization organization, Set<String> orgLocales, int warningLevel,
63         Set<String> skip) {
64         if (!cldrLocales.containsAll(orgLocales)) {
65             Set<String> diff2 = new LinkedHashSet<>(Sets.difference(orgLocales, cldrLocales));
66             diff2.removeAll(skip);
67             if (!diff2.isEmpty()) {
68                 String diffString = diff2.toString();
69                 String levelString = CollectionUtilities.join(level, "+");
70                 for (String localeId : diff2) {
71                     diffString += "\n\t" + localeId + "\t" + CLDRConfig.getInstance().getEnglish().getName(localeId);
72                 }
73                 msg("The following " + organization.displayName + " " + levelString + " locales were absent from the "
74                     + Organization.cldr.displayName + " " + levelString + " locales:" + diffString,
75                     warningLevel, true, true);
76             }
77             return diff2;
78         }
79         return Collections.EMPTY_SET;
80     }
81 }
82