1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html#License
3/*
4 **********************************************************************
5 * Copyright (c) 2011-2016, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 * Author: John Emmons
9 * Created: April 8 - 2011
10 * Since: ICU 4.8
11 **********************************************************************
12 */
13
14package com.ibm.icu.dev.test.util;
15
16import java.util.List;
17import java.util.Set;
18
19import org.junit.Test;
20
21import com.ibm.icu.dev.test.TestFmwk;
22import com.ibm.icu.util.Region;
23import com.ibm.icu.util.Region.RegionType;
24
25/**
26 * @test
27 * @summary General test of Regions
28 */
29
30public class RegionTest extends TestFmwk {
31    String[][] knownRegions = {
32            //   Code  , Numeric , Parent, Type, Containing Continent
33            { "001", "001", null , "WORLD", null },
34            { "002", "002", "001", "CONTINENT", null },
35            { "003", "003", null,  "GROUPING", null },
36            { "005", "005", "019", "SUBCONTINENT", "019" },
37            { "009", "009", "001", "CONTINENT", null},
38            { "011", "011", "002", "SUBCONTINENT", "002" },
39            { "013", "013", "019", "SUBCONTINENT", "019" },
40            { "014", "014", "002", "SUBCONTINENT", "002" },
41            { "015", "015", "002", "SUBCONTINENT", "002" },
42            { "017", "017", "002", "SUBCONTINENT", "002" },
43            { "018", "018", "002", "SUBCONTINENT", "002" },
44            { "019", "019", "001", "CONTINENT", null },
45            { "021", "021", "019", "SUBCONTINENT", "019" },
46            { "029", "029", "019", "SUBCONTINENT", "019" },
47            { "030", "030", "142", "SUBCONTINENT", "142" },
48            { "034", "034", "142", "SUBCONTINENT", "142" },
49            { "035", "035", "142", "SUBCONTINENT", "142" },
50            { "039", "039", "150", "SUBCONTINENT", "150"},
51            { "053", "053", "009", "SUBCONTINENT", "009" },
52            { "054", "054", "009", "SUBCONTINENT", "009" },
53            { "057", "057", "009", "SUBCONTINENT", "009" },
54            { "061", "061", "009", "SUBCONTINENT", "009" },
55            { "142", "142", "001", "CONTINENT", null },
56            { "143", "143", "142", "SUBCONTINENT", "142" },
57            { "145", "145", "142", "SUBCONTINENT", "142" },
58            { "150", "150", "001", "CONTINENT", null },
59            { "151", "151", "150", "SUBCONTINENT", "150" },
60            { "154", "154", "150", "SUBCONTINENT", "150" },
61            { "155", "155", "150", "SUBCONTINENT", "150" },
62            { "419", "419", null,  "GROUPING" , null},
63            { "AC" , "-1" , "QO" , "TERRITORY", "009" },
64            { "AD" , "020", "039", "TERRITORY", "150" },
65            { "AE" , "784", "145", "TERRITORY", "142" },
66            { "AF" , "004", "034", "TERRITORY", "142" },
67            { "AG" , "028", "029", "TERRITORY", "019" },
68            { "AI" , "660", "029", "TERRITORY", "019" },
69            { "AL" , "008", "039", "TERRITORY", "150" },
70            { "AM" , "051", "145", "TERRITORY", "142" },
71            { "AN" , "530", null,  "DEPRECATED", null },
72            { "AO" , "024", "017", "TERRITORY", "002" },
73            { "AQ" , "010", "QO" , "TERRITORY", "009" },
74            { "AR" , "032", "005", "TERRITORY", "019" },
75            { "AS" , "016", "061", "TERRITORY", "009" },
76            { "AT" , "040", "155", "TERRITORY", "150" },
77            { "AU" , "036", "053", "TERRITORY", "009" },
78            { "AW" , "533", "029", "TERRITORY", "019" },
79            { "AX" , "248", "154", "TERRITORY", "150" },
80            { "AZ" , "031", "145", "TERRITORY", "142" },
81            { "BA" , "070", "039", "TERRITORY", "150" },
82            { "BB" , "052", "029", "TERRITORY", "019" },
83            { "BD" , "050", "034", "TERRITORY", "142" },
84            { "BE" , "056", "155", "TERRITORY", "150" },
85            { "BF" , "854", "011", "TERRITORY", "002" },
86            { "BG" , "100", "151", "TERRITORY", "150" },
87            { "BH" , "048", "145", "TERRITORY", "142" },
88            { "BI" , "108", "014", "TERRITORY", "002" },
89            { "BJ" , "204", "011", "TERRITORY", "002" },
90            { "BL" , "652", "029", "TERRITORY", "019" },
91            { "BM" , "060", "021", "TERRITORY", "019" },
92            { "BN" , "096", "035", "TERRITORY", "142" },
93            { "BO" , "068", "005", "TERRITORY", "019" },
94            { "BQ" , "535", "029", "TERRITORY", "019" },
95            { "BR" , "076", "005", "TERRITORY", "019" },
96            { "BS" , "044", "029", "TERRITORY", "019" },
97            { "BT" , "064", "034", "TERRITORY", "142" },
98            { "BU" , "104", "035", "TERRITORY", "142" },
99            { "BV" , "074", "QO" , "TERRITORY", "009" },
100            { "BW" , "072", "018", "TERRITORY", "002" },
101            { "BY" , "112", "151", "TERRITORY", "150" },
102            { "BZ" , "084", "013", "TERRITORY", "019" },
103            { "CA" , "124", "021", "TERRITORY", "019" },
104            { "CC" , "166", "QO" , "TERRITORY", "009" },
105            { "CD" , "180", "017", "TERRITORY", "002" },
106            { "CF" , "140", "017", "TERRITORY", "002" },
107            { "CG" , "178", "017", "TERRITORY", "002" },
108            { "CH" , "756", "155", "TERRITORY", "150" },
109            { "CI" , "384", "011", "TERRITORY", "002" },
110            { "CK" , "184", "061", "TERRITORY", "009" },
111            { "CL" , "152", "005", "TERRITORY", "019" },
112            { "CM" , "120", "017", "TERRITORY", "002" },
113            { "CN" , "156", "030", "TERRITORY", "142" },
114            { "CO" , "170", "005", "TERRITORY", "019" },
115            { "CP" , "-1" , "QO" , "TERRITORY", "009" },
116            { "CR" , "188", "013", "TERRITORY", "019" },
117            { "CU" , "192", "029", "TERRITORY", "019" },
118            { "CV" , "132", "011", "TERRITORY", "002" },
119            { "CW" , "531", "029", "TERRITORY", "019" },
120            { "CX" , "162", "QO" , "TERRITORY", "009" },
121            { "CY" , "196", "145", "TERRITORY", "142" },
122            { "CZ" , "203", "151", "TERRITORY", "150" },
123            { "DD" , "276", "155", "TERRITORY", "150" },
124            { "DE" , "276", "155", "TERRITORY", "150" },
125            { "DG" , "-1" , "QO" , "TERRITORY", "009" },
126            { "DJ" , "262", "014", "TERRITORY", "002" },
127            { "DK" , "208", "154", "TERRITORY", "150" },
128            { "DM" , "212", "029", "TERRITORY", "019" },
129            { "DO" , "214", "029", "TERRITORY", "019" },
130            { "DZ" , "012", "015", "TERRITORY", "002" },
131            { "EA" , "-1" , "015", "TERRITORY", "002" },
132            { "EC" , "218", "005", "TERRITORY", "019" },
133            { "EE" , "233", "154", "TERRITORY", "150" },
134            { "EG" , "818", "015", "TERRITORY", "002" },
135            { "EH" , "732", "015", "TERRITORY", "002" },
136            { "ER" , "232", "014", "TERRITORY", "002" },
137            { "ES" , "724", "039", "TERRITORY", "150" },
138            { "ET" , "231", "014", "TERRITORY", "002" },
139            { "EU" , "967", null,  "GROUPING", null },
140            { "FI" , "246", "154", "TERRITORY", "150" },
141            { "FJ" , "242", "054", "TERRITORY", "009" },
142            { "FK" , "238", "005", "TERRITORY", "019" },
143            { "FM" , "583", "057", "TERRITORY", "009" },
144            { "FO" , "234", "154", "TERRITORY", "150" },
145            { "FR" , "250", "155", "TERRITORY", "150" },
146            { "FX" , "250", "155", "TERRITORY", "150" },
147            { "GA" , "266", "017", "TERRITORY", "002" },
148            { "GB" , "826", "154", "TERRITORY", "150" },
149            { "GD" , "308", "029", "TERRITORY", "019" },
150            { "GE" , "268", "145", "TERRITORY", "142" },
151            { "GF" , "254", "005", "TERRITORY", "019" },
152            { "GG" , "831", "154", "TERRITORY", "150" },
153            { "GH" , "288", "011", "TERRITORY", "002" },
154            { "GI" , "292", "039", "TERRITORY", "150" },
155            { "GL" , "304", "021", "TERRITORY", "019" },
156            { "GM" , "270", "011", "TERRITORY", "002" },
157            { "GN" , "324", "011", "TERRITORY", "002" },
158            { "GP" , "312", "029", "TERRITORY", "019" },
159            { "GQ" , "226", "017", "TERRITORY", "002" },
160            { "GR" , "300", "039", "TERRITORY", "150" },
161            { "GS" , "239", "QO" , "TERRITORY", "009" },
162            { "GT" , "320", "013", "TERRITORY", "019" },
163            { "GU" , "316", "057", "TERRITORY", "009" },
164            { "GW" , "624", "011", "TERRITORY", "002" },
165            { "GY" , "328", "005", "TERRITORY", "019" },
166            { "HK" , "344", "030", "TERRITORY", "142" },
167            { "HM" , "334", "QO" , "TERRITORY", "009" },
168            { "HN" , "340", "013", "TERRITORY", "019" },
169            { "HR" , "191", "039", "TERRITORY", "150" },
170            { "HT" , "332", "029", "TERRITORY", "019" },
171            { "HU" , "348", "151", "TERRITORY", "150" },
172            { "IC" , "-1" , "015", "TERRITORY", "002" },
173            { "ID" , "360", "035", "TERRITORY", "142" },
174            { "IE" , "372", "154", "TERRITORY", "150" },
175            { "IL" , "376", "145", "TERRITORY", "142" },
176            { "IM" , "833", "154", "TERRITORY", "150" },
177            { "IN" , "356", "034", "TERRITORY", "142" },
178            { "IO" , "086", "QO" , "TERRITORY", "009" },
179            { "IQ" , "368", "145", "TERRITORY", "142" },
180            { "IR" , "364", "034", "TERRITORY", "142" },
181            { "IS" , "352", "154", "TERRITORY", "150" },
182            { "IT" , "380", "039", "TERRITORY", "150" },
183            { "JE" , "832", "154", "TERRITORY", "150" },
184            { "JM" , "388", "029", "TERRITORY", "019" },
185            { "JO" , "400", "145", "TERRITORY", "142" },
186            { "JP" , "392", "030", "TERRITORY", "142" },
187            { "KE" , "404", "014", "TERRITORY", "002" },
188            { "KG" , "417", "143", "TERRITORY", "142" },
189            { "KH" , "116", "035", "TERRITORY", "142" },
190            { "KI" , "296", "057", "TERRITORY", "009" },
191            { "KM" , "174", "014", "TERRITORY", "002" },
192            { "KN" , "659", "029", "TERRITORY", "019" },
193            { "KP" , "408", "030", "TERRITORY", "142" },
194            { "KR" , "410", "030", "TERRITORY", "142" },
195            { "KW" , "414", "145", "TERRITORY", "142" },
196            { "KY" , "136", "029", "TERRITORY", "019" },
197            { "KZ" , "398", "143", "TERRITORY", "142" },
198            { "LA" , "418", "035", "TERRITORY", "142" },
199            { "LB" , "422", "145", "TERRITORY", "142" },
200            { "LC" , "662", "029", "TERRITORY", "019" },
201            { "LI" , "438", "155", "TERRITORY", "150" },
202            { "LK" , "144", "034", "TERRITORY", "142" },
203            { "LR" , "430", "011", "TERRITORY", "002" },
204            { "LS" , "426", "018", "TERRITORY", "002" },
205            { "LT" , "440", "154", "TERRITORY", "150" },
206            { "LU" , "442", "155", "TERRITORY", "150" },
207            { "LV" , "428", "154", "TERRITORY", "150" },
208            { "LY" , "434", "015", "TERRITORY", "002" },
209            { "MA" , "504", "015", "TERRITORY", "002" },
210            { "MC" , "492", "155", "TERRITORY", "150" },
211            { "MD" , "498", "151", "TERRITORY", "150" },
212            { "ME" , "499", "039", "TERRITORY", "150" },
213            { "MF" , "663", "029", "TERRITORY", "019" },
214            { "MG" , "450", "014", "TERRITORY", "002" },
215            { "MH" , "584", "057", "TERRITORY", "009" },
216            { "MK" , "807", "039", "TERRITORY", "150" },
217            { "ML" , "466", "011", "TERRITORY", "002" },
218            { "MM" , "104", "035", "TERRITORY", "142" },
219            { "MN" , "496", "030", "TERRITORY", "142" },
220            { "MO" , "446", "030", "TERRITORY", "142" },
221            { "MP" , "580", "057", "TERRITORY", "009" },
222            { "MQ" , "474", "029", "TERRITORY", "019" },
223            { "MR" , "478", "011", "TERRITORY", "002" },
224            { "MS" , "500", "029", "TERRITORY", "019" },
225            { "MT" , "470", "039", "TERRITORY", "150" },
226            { "MU" , "480", "014", "TERRITORY", "002" },
227            { "MV" , "462", "034", "TERRITORY", "142" },
228            { "MW" , "454", "014", "TERRITORY", "002" },
229            { "MX" , "484", "013", "TERRITORY", "019"},
230            { "MY" , "458", "035", "TERRITORY", "142" },
231            { "MZ" , "508", "014", "TERRITORY", "002" },
232            { "NA" , "516", "018", "TERRITORY", "002" },
233            { "NC" , "540", "054", "TERRITORY", "009" },
234            { "NE" , "562", "011", "TERRITORY", "002" },
235            { "NF" , "574", "053", "TERRITORY", "009" },
236            { "NG" , "566", "011", "TERRITORY", "002" },
237            { "NI" , "558", "013", "TERRITORY", "019" },
238            { "NL" , "528", "155", "TERRITORY", "150" },
239            { "NO" , "578", "154", "TERRITORY", "150" },
240            { "NP" , "524", "034", "TERRITORY", "142" },
241            { "NR" , "520", "057", "TERRITORY", "009" },
242            { "NT" , "536", null , "DEPRECATED", null },
243            { "NU" , "570", "061", "TERRITORY", "009" },
244            { "NZ" , "554", "053", "TERRITORY", "009" },
245            { "OM" , "512", "145", "TERRITORY", "142" },
246            { "PA" , "591", "013", "TERRITORY", "019" },
247            { "PE" , "604", "005", "TERRITORY", "019" },
248            { "PF" , "258", "061", "TERRITORY", "009" },
249            { "PG" , "598", "054", "TERRITORY", "009" },
250            { "PH" , "608", "035", "TERRITORY", "142" },
251            { "PK" , "586", "034", "TERRITORY", "142" },
252            { "PL" , "616", "151", "TERRITORY", "150" },
253            { "PM" , "666", "021", "TERRITORY", "019" },
254            { "PN" , "612", "061", "TERRITORY", "009" },
255            { "PR" , "630", "029", "TERRITORY", "019" },
256            { "PS" , "275", "145", "TERRITORY", "142" },
257            { "PT" , "620", "039", "TERRITORY", "150" },
258            { "PW" , "585", "057", "TERRITORY", "009" },
259            { "PY" , "600", "005", "TERRITORY", "019" },
260            { "QA" , "634", "145", "TERRITORY", "142" },
261            { "QO" , "961", "009", "SUBCONTINENT", "009" },
262            { "QU" , "967", null,  "GROUPING", null },
263            { "RE" , "638", "014", "TERRITORY", "002" },
264            { "RO" , "642", "151", "TERRITORY", "150" },
265            { "RS" , "688", "039", "TERRITORY", "150" },
266            { "RU" , "643", "151", "TERRITORY", "150" },
267            { "RW" , "646", "014", "TERRITORY", "002" },
268            { "SA" , "682", "145", "TERRITORY", "142" },
269            { "SB" , "090", "054", "TERRITORY", "009" },
270            { "SC" , "690", "014", "TERRITORY", "002" },
271            { "SD" , "729", "015", "TERRITORY", "002" },
272            { "SE" , "752", "154", "TERRITORY", "150" },
273            { "SG" , "702", "035", "TERRITORY", "142" },
274            { "SH" , "654", "011", "TERRITORY", "002" },
275            { "SI" , "705", "039", "TERRITORY", "150" },
276            { "SJ" , "744", "154", "TERRITORY", "150" },
277            { "SK" , "703", "151", "TERRITORY", "150" },
278            { "SL" , "694", "011", "TERRITORY", "002" },
279            { "SM" , "674", "039", "TERRITORY", "150" },
280            { "SN" , "686", "011", "TERRITORY", "002" },
281            { "SO" , "706", "014", "TERRITORY", "002" },
282            { "SR" , "740", "005", "TERRITORY", "019" },
283            { "SS" , "728", "014", "TERRITORY", "002" },
284            { "ST" , "678", "017", "TERRITORY", "002" },
285            { "SU" , "810", null , "DEPRECATED" , null},
286            { "SV" , "222", "013", "TERRITORY", "019" },
287            { "SX" , "534", "029", "TERRITORY", "019" },
288            { "SY" , "760", "145", "TERRITORY", "142" },
289            { "SZ" , "748", "018", "TERRITORY", "002" },
290            { "TA" , "-1" , "QO", "TERRITORY", "009" },
291            { "TC" , "796", "029", "TERRITORY", "019" },
292            { "TD" , "148", "017", "TERRITORY", "002" },
293            { "TF" , "260", "QO" , "TERRITORY", "009" },
294            { "TG" , "768", "011", "TERRITORY", "002" },
295            { "TH" , "764", "035", "TERRITORY", "142" },
296            { "TJ" , "762", "143", "TERRITORY", "142" },
297            { "TK" , "772", "061", "TERRITORY", "009" },
298            { "TL" , "626", "035", "TERRITORY", "142" },
299            { "TM" , "795", "143", "TERRITORY", "142" },
300            { "TN" , "788", "015", "TERRITORY", "002" },
301            { "TO" , "776", "061", "TERRITORY", "009" },
302            { "TP" , "626", "035", "TERRITORY", "142" },
303            { "TR" , "792", "145", "TERRITORY", "142" },
304            { "TT" , "780", "029", "TERRITORY", "019" },
305            { "TV" , "798", "061", "TERRITORY", "009" },
306            { "TW" , "158", "030", "TERRITORY", "142" },
307            { "TZ" , "834", "014", "TERRITORY", "002" },
308            { "UA" , "804", "151", "TERRITORY", "150" },
309            { "UG" , "800", "014", "TERRITORY", "002" },
310            { "UM" , "581", "QO" , "TERRITORY", "009" },
311            { "US" , "840", "021", "TERRITORY", "019" },
312            { "UY" , "858", "005", "TERRITORY", "019" },
313            { "UZ" , "860", "143", "TERRITORY", "142" },
314            { "VA" , "336", "039", "TERRITORY", "150" },
315            { "VC" , "670", "029", "TERRITORY", "019" },
316            { "VE" , "862", "005", "TERRITORY", "019" },
317            { "VG" , "092", "029", "TERRITORY", "019" },
318            { "VI" , "850", "029", "TERRITORY", "019" },
319            { "VN" , "704", "035", "TERRITORY", "142" },
320            { "VU" , "548", "054", "TERRITORY", "009" },
321            { "WF" , "876", "061", "TERRITORY", "009" },
322            { "WS" , "882", "061", "TERRITORY", "009" },
323            { "YD" , "887", "145", "TERRITORY", "142" },
324            { "YE" , "887", "145", "TERRITORY", "142" },
325            { "YT" , "175", "014", "TERRITORY", "002" },
326            { "ZA" , "710", "018", "TERRITORY", "002" },
327            { "ZM" , "894", "014", "TERRITORY", "002" },
328            { "ZR" , "180", "017", "TERRITORY", "002" },
329            { "ZW" , "716", "014", "TERRITORY", "002" },
330            { "ZZ" , "999", null , "UNKNOWN", null }
331    };
332
333    /**
334     * Test for known regions.
335     */
336    @Test
337    public void TestKnownRegions() {
338
339        for (String [] rd : knownRegions ) {
340            try {
341                Region r = Region.getInstance(rd[0]);
342                int n = r.getNumericCode();
343                int e = Integer.valueOf(rd[1]).intValue();
344                if ( n != e ) {
345                    errln("Numeric code mismatch for region " + r.toString() + ". Expected: " + e + " Got:" + n);
346                }
347                if (r.getType() != Region.RegionType.valueOf(rd[3])) {
348                    errln("Expected region " + r.toString() + " to be of type " + rd[3] + ". Got:" + r.getType().toString());
349                }
350                int nc = Integer.valueOf(rd[1]).intValue();
351                if ( nc > 0 ) {
352                    Region ncRegion = Region.getInstance(nc);
353                    if ( !ncRegion.equals(r) && nc != 891 ) { // 891 is special case - CS and YU both deprecated codes for region 891
354                        errln("Creating region " + r.toString() + " by its numeric code returned a different region. Got: " + ncRegion.toString());
355                    }
356                }
357            } catch (IllegalArgumentException ex ) {
358                errln("Known region " + rd[0] + " was not recognized.");
359            }
360        }
361    }
362
363    @Test
364    public void TestGetInstanceString() {
365        String[][] testData = {
366                //  Input ID, Expected ID, Expected Type
367                { "DE", "DE", "TERRITORY" },  // Normal region
368                { "QU", "EU", "GROUPING" },   // Alias to a grouping
369                { "DD", "DE", "TERRITORY" },  // Alias to a deprecated region (East Germany) with single preferred value
370                { "276", "DE", "TERRITORY" }, // Numeric code for Germany
371                { "278", "DE", "TERRITORY" }, // Numeric code for East Germany (Deprecated)
372                { "SU", "SU", "DEPRECATED" }, // Alias to a deprecated region with multiple preferred values
373                { "AN", "AN", "DEPRECATED" }, // Deprecated region with multiple preferred values
374                { "SVK", "SK", "TERRITORY" }  // 3-letter code - Slovakia
375        };
376
377        try {
378            Region.getInstance(null);
379            errln("Calling Region.get(null) should have thrown a NullPointerException, but didn't.");
380        } catch ( NullPointerException ex ) {
381            // Do nothing - we're supposed to get here.
382        }
383
384        try {
385            Region.getInstance("BOGUS");
386            errln("Calling Region.get(BOGUS) should have thrown a IllegalArgumentException, but didn't.");
387        } catch ( IllegalArgumentException ex ) {
388            // Do nothing - we're supposed to get here.
389        }
390
391        for (String [] data : testData) {
392            String inputID = data[0];
393            String expectedID = data[1];
394            Region.RegionType expectedType = Region.RegionType.valueOf(data[2]);
395            Region r = Region.getInstance(inputID);
396            if ( !expectedID.equals(r.toString())) {
397                errln("Unexpected region ID for Region.getInstance(\"" + inputID + "\"); Expected: " + expectedID + " Got: " + r.toString());
398            }
399            if ( !expectedType.equals(r.getType())) {
400                errln("Unexpected region type for Region.getInstance(\"" + inputID + "\"); Expected: " + expectedType + " Got: " + r.getType());
401            }
402        }
403    }
404
405    @Test
406    public void TestGetInstanceInt() {
407        String[][] testData = {
408                //  Input ID, Expected ID, Expected Type
409                { "276", "DE", "TERRITORY" }, // Numeric code for Germany
410                { "278", "DE", "TERRITORY" }, // Numeric code for East Germany (Deprecated)
411                { "419", "419", "GROUPING" }, // Latin America
412                { "736", "SD", "TERRITORY" }, // Sudan (pre-2011) - changed numeric code after South Sudan split off
413                { "729", "SD", "TERRITORY" }, // Sudan (post-2011) - changed numeric code after South Sudan split off
414        };
415
416        try {
417            Region.getInstance(-123);
418            errln("Calling Region.get(-123) should have thrown a IllegalArgumentException, but didn't.");
419        } catch ( IllegalArgumentException ex ) {
420            // Do nothing - we're supposed to get here.
421        }
422        for (String [] data : testData) {
423            String inputID = data[0];
424            String expectedID = data[1];
425            Region.RegionType expectedType = Region.RegionType.valueOf(data[2]);
426            Region r = Region.getInstance(Integer.valueOf(inputID));
427            if ( !expectedID.equals(r.toString())) {
428                errln("Unexpected region ID for Region.getInstance(" + inputID + "); Expected: " + expectedID + " Got: " + r.toString());
429            }
430            if ( !expectedType.equals(r.getType())) {
431                errln("Unexpected region type for Region.getInstance(" + inputID + "); Expected: " + expectedType + " Got: " + r.getType());
432            }
433        }
434
435    }
436
437    @Test
438    public void TestGetContainedRegions() {
439        for (String [] rd : knownRegions ) {
440            try {
441                Region r = Region.getInstance(rd[0]);
442                if (r.getType() == Region.RegionType.GROUPING) {
443                    continue;
444                }
445                Set<Region> containedRegions = r.getContainedRegions();
446                for (Region cr : containedRegions ) {
447                    if ( cr.getContainingRegion() != r) {
448                        errln("Region: " + r.toString() + " contains region: " + cr.toString() + ". Expected containing region of this region to be the original region, but got: " +
449                                ( cr.getContainingRegion() == null ? "NULL" : cr.getContainingRegion().toString()));
450                    }
451                }
452            } catch (IllegalArgumentException ex ) {
453                errln("Known region " + rd[0] + " was not recognized.");
454            }
455        }
456    }
457
458    @Test
459    public void TestGetContainedRegionsWithType() {
460        for (String [] rd : knownRegions ) {
461            try {
462                Region r = Region.getInstance(rd[0]);
463                if ( r.getType() != Region.RegionType.CONTINENT ) {
464                    continue;
465                }
466                Set<Region> containedRegions = r.getContainedRegions(Region.RegionType.TERRITORY);
467                for (Region cr : containedRegions ) {
468                    if ( cr.getContainingRegion(Region.RegionType.CONTINENT) != r) {
469                        errln("Continent: " + r.toString() + " contains territory: " + cr.toString() + ". Expected containing continent of this region to be the original continent, but got: " +
470                                ( cr.getContainingRegion() == null ? "NULL" : cr.getContainingRegion().toString()));
471                    }
472                }
473            } catch (IllegalArgumentException ex ) {
474                errln("Known region " + rd[0] + " was not recognized.");
475            }
476        }
477    }
478
479    @Test
480    public void TestGetContainingRegionWithType() {
481        for (String [] rd : knownRegions ) {
482            try {
483                Region r = Region.getInstance(rd[0]);
484                Region c = r.getContainingRegion(Region.RegionType.CONTINENT);
485                if (rd[4] == null) {
486                    if ( c != null) {
487                        errln("Containing continent for " + r.toString() + " should have been NULL.  Got: " + c.toString());
488                    }
489                } else {
490                    Region p = Region.getInstance(rd[4]);
491                    if ( !p.equals(c)) {
492                        errln("Expected containing continent of region " + r.toString() + " to be " + p.toString() + ". Got: " + ( c == null ? "NULL" : c.toString()) );
493                    }
494                }
495            } catch (IllegalArgumentException ex ) {
496                errln("Known region " + rd[0] + " was not recognized.");
497            }
498        }
499    }
500
501    @Test
502    public void TestGetContainingRegion() {
503        for (String [] rd : knownRegions ) {
504            try {
505                Region r = Region.getInstance(rd[0]);
506                Region c = r.getContainingRegion();
507                if (rd[2] == null) {
508                    if ( c != null) {
509                        errln("Containing region for " + r.toString() + " should have been NULL.  Got: " + c.toString());
510                    }
511                } else {
512                    Region p = Region.getInstance(rd[2]);
513                    if ( !p.equals(c)) {
514                        errln("Expected containing region of region " + r.toString() + " to be " + p.toString() + ". Got: " + ( c == null ? "NULL" : c.toString()) );
515                    }
516                }
517            } catch (IllegalArgumentException ex ) {
518                errln("Known region " + rd[0] + " was not recognized.");
519            }
520        }
521    }
522
523    @Test
524    public void TestGetPreferredValues() {
525        String[][] testData = {
526                //  Input ID, Expected Preferred Values...
527                { "AN", "CW", "SX", "BQ" }, // Netherlands Antilles
528                { "CS", "RS", "ME" },     // Serbia & Montenegro
529                { "FQ", "AQ", "TF" },     // French Southern and Antarctic Territories
530                { "NT", "IQ", "SA" },     // Neutral Zone
531                { "PC", "FM", "MH", "MP", "PW" }, // Pacific Islands Trust Territory
532                { "SU", "RU", "AM", "AZ", "BY", "EE", "GE", "KZ", "KG", "LV", "LT", "MD", "TJ", "TM", "UA", "UZ" } // Soviet Union
533        };
534
535        for (String [] data : testData) {
536            String inputID = data[0];
537            try {
538                Region r = Region.getInstance(inputID);
539                List<Region> preferredValues = r.getPreferredValues();
540                for ( int i = 1 ; i < data.length ; i++ ) {
541                    try {
542                        Region pvr = Region.getInstance(data[i]);
543                        if ( !preferredValues.contains(pvr)) {
544                            errln("Region.getPreferredValues() for region:" + inputID + "should have contained: " + pvr.toString() + "but it didn't.");
545                        }
546                    } catch (IllegalArgumentException ex ) {
547                        errln("Known region " + data[i] + " was not recognized.");
548                    }
549                }
550            } catch (IllegalArgumentException ex ) {
551                errln("Known region " + inputID + " was not recognized.");
552            }
553        }
554    }
555
556    @Test
557    public void TestContains() {
558        for (String [] rd : knownRegions ) {
559            try {
560                Region r = Region.getInstance(rd[0]);
561                Region c = r.getContainingRegion();
562                while ( c != null ) {
563                    if ( !c.contains(r)) {
564                        errln("Region " + c.toString() + "should have contained: " + r.toString() + "but it didn't.");
565                    }
566                    c = c.getContainingRegion();
567                }
568            } catch (IllegalArgumentException ex ) {
569                errln("Known region " + rd[0] + " was not recognized.");
570            }
571        }
572    }
573
574    @Test
575    public void TestAvailableTerritories() {
576        // Test to make sure that the set of territories contained in World and the set of all available
577        // territories are one and the same.
578        Set<Region> availableTerritories = Region.getAvailable(RegionType.TERRITORY);
579        Region world = Region.getInstance("001");
580        Set<Region> containedInWorld = world.getContainedRegions(RegionType.TERRITORY);
581        if ( !availableTerritories.equals(containedInWorld) ) {
582            errln("Available territories and all territories contained in world should be the same set.\n" +
583                    "Available          = " + availableTerritories.toString() + "\n" +
584                    "Contained in World = " + containedInWorld.toString());
585        }
586    }
587}
588