phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
internationalPrefix));
}
public void testMaybeExtractCountryCode() {
PhoneNumber number = new PhoneNumber();
PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US);
// Note that for the US, the IDD is 011.
try {
String phoneNumber = "011112-3456789";
String strippedNumber = "123456789";
int countryCallingCode = 1;
StringBuilder numberToFill = new StringBuilder();
assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
countryCallingCode,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_NUMBER_WITH_IDD, number.getCountryCodeSource());
// Should strip and normalize national significant number.
assertEquals("Did not strip off the country calling code correctly.",
strippedNumber,
numberToFill.toString());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "+6423456789";
int countryCallingCode = 64;
StringBuilder numberToFill = new StringBuilder();
assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
countryCallingCode,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "+80012345678";
int countryCallingCode = 800;
StringBuilder numberToFill = new StringBuilder();
assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
countryCallingCode,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "2345-6789";
StringBuilder numberToFill = new StringBuilder();
assertEquals(
"Should not have extracted a country calling code - no international prefix present.",
0,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "0119991123456789";
StringBuilder numberToFill = new StringBuilder();
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number);
fail("Should have thrown an exception, no valid country calling code present.");
} catch (NumberParseException e) {
// Expected.
assertEquals("Wrong error type stored in exception.",
NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
e.getErrorType());
}
number.clear();
try {
String phoneNumber = "(1 610) 619 4466";
int countryCallingCode = 1;
StringBuilder numberToFill = new StringBuilder();
assertEquals("Should have extracted the country calling code of the region passed in",
countryCallingCode,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN,
number.getCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "(1 610) 619 4466";
int countryCallingCode = 1;
StringBuilder numberToFill = new StringBuilder();
assertEquals("Should have extracted the country calling code of the region passed in",
countryCallingCode,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false,
number));
assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "(1 610) 619 446";
StringBuilder numberToFill = new StringBuilder();
assertEquals("Should not have extracted a country calling code - invalid number after " +
"extraction of uncertain country calling code.",
0,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false,
number));
assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
number.clear();
try {
String phoneNumber = "(1 610) 619";
StringBuilder numberToFill = new StringBuilder();
assertEquals("Should not have extracted a country calling code - too short number both " +
"before and after extraction of uncertain country calling code.",
0,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
number));
assertEquals("Did not figure out CountryCodeSource correctly",
CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource());
} catch (NumberParseException e) {
fail("Should not have thrown an exception: " + e.toString());
}
}