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