Browse Source

Update library to v2.1

pull/567/head
Shaopeng Jia 16 years ago
committed by Mihaela Rosca
parent
commit
e4e52ec2d2
9 changed files with 795 additions and 258 deletions
  1. +529
    -161
      java/resources/com/google/i18n/phonenumbers/src/PhoneNumberMetaData.xml
  2. BIN
      java/resources/com/google/i18n/phonenumbers/src/generated_files/PhoneNumberMetadataProto
  3. +48
    -1
      java/resources/com/google/i18n/phonenumbers/test/PhoneNumberMetaDataForTesting.xml
  4. BIN
      java/resources/com/google/i18n/phonenumbers/test/generated_files/PhoneNumberMetadataProtoForTesting
  5. +26
    -18
      java/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java
  6. +21
    -13
      java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  7. +17
    -22
      java/src/com/google/i18n/phonenumbers/phonemetadata.proto
  8. +91
    -1
      java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
  9. +63
    -42
      java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java

+ 529
- 161
java/resources/com/google/i18n/phonenumbers/src/PhoneNumberMetaData.xml
File diff suppressed because it is too large
View File


BIN
java/resources/com/google/i18n/phonenumbers/src/generated_files/PhoneNumberMetadataProto View File


+ 48
- 1
java/resources/com/google/i18n/phonenumbers/test/PhoneNumberMetaDataForTesting.xml View File

@ -16,7 +16,8 @@
MetaData on Phone Number Plan and formatting rules. This file is used
solely for the purpose of unittesting, so data in this file is not
necessarily consistent with that of PhoneNumberMetaData.xml
necessarily consistent with that of
../src/PhoneNumberMetaData.xml
-->
<phoneNumberMetadata>
@ -233,6 +234,52 @@
</premiumRate>
</territory>
<!-- Korea (Rep. of) -->
<!-- http://www.itu.int/oth/T0202000072/en -->
<!-- http://en.wikipedia.org/wiki/%2B82 -->
<!-- http://www.kcc.go.kr/user.do?mode=view&page=P02030300&dc=K02030300&boardId=1074&boardSeq=2349 -->
<!-- http://www.kcc.go.kr/user.do?mode=view&page=P02030300&dc=K02030300&boardId=1074&boardSeq=2240 -->
<!-- http://www.telecentro.co.kr/sub/index.php?job=detail&ebcf_id=faq&page=1&mid=0503&eb_seq=36 -->
<!-- Exceptions :
internationalPrefix
0031, 0033, 0071, 0073 - Special services of KT and DACOM, ignorable
nationalPrefix
1[4-6]XX-YYYY - Country-wide common number services, display as it is without hyphens -->
<territory id="KR" countryCode="82" internationalPrefix="00(?:[124-68]|[37]\d{2})"
nationalPrefix="0" nationalPrefixForParsing="0(?:8[1-46-8]|85\d{2})?"
nationalPrefixFormattingRule="$NP$FG">
<availableFormats>
<numberFormat leadingDigits="1(?:0|1[19]|[69]9|5(?:44|59|8))|[57]0"
pattern="(\d{2})(\d{4})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="1(?:[169][2-8]|[78]|5(?:[1-3]|4[56]))|[68]0|[3-9][1-9][2-9]"
pattern="(\d{2})(\d{3})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="1312"
pattern="(\d{3})(\d)(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="131[13-9]"
pattern="(\d{3})(\d{2})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="13[2-9]"
pattern="(\d{3})(\d{3})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="30"
pattern="(\d{2})(\d{2})(\d{3})(\d{4})">$1-$2-$3-$4</numberFormat>
<numberFormat leadingDigits="2(?:[26]|3(?:01|1[45]|2[17-9]|39|4|6[67]|7[078]))"
pattern="(\d)(\d{4})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="2(?:3(?:0[02-9]|1[0-36-9]|2[02-6]|3[0-8]|6[0-589]|7[1-69]|[589])|[457-9])"
pattern="(\d)(\d{3})(\d{4})">$1-$2-$3</numberFormat>
<numberFormat leadingDigits="21(?:[0-247-9]|3[124]|6[1269])"
pattern="(\d)(\d{3})">$1-$2</numberFormat>
<numberFormat leadingDigits="21(?:3[035-9]|6[03-578])"
pattern="(\d)(\d{4})">$1-$2</numberFormat>
<numberFormat leadingDigits="[3-9][1-9]1(?:[0-247-9]|3[124]|6[1269])"
pattern="(\d{2})(\d{3})">$1-$2</numberFormat>
<numberFormat leadingDigits="[3-9][1-9]1(?:3[035-9]|6[03-578])"
pattern="(\d{2})(\d{4})">$1-$2</numberFormat>
</availableFormats>
<generalDesc>
<nationalNumberPattern>[1-79]\d{3,9}|8\d{8}</nationalNumberPattern>
<possibleNumberPattern>\d{4,10}</possibleNumberPattern>
</generalDesc>
</territory>
<!-- Mexico -->
<territory id="MX" countryCode="52" internationalPrefix="00"
nationalPrefix="01" nationalPrefixForParsing="01|04[45](\d{10})"


BIN
java/resources/com/google/i18n/phonenumbers/test/generated_files/PhoneNumberMetadataProtoForTesting View File


+ 26
- 18
java/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java View File

@ -30,6 +30,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -84,24 +85,32 @@ public class BuildMetadataProtoFromXml {
}
}
private static String validateRE(String regex) {
Pattern regexPattern = Pattern.compile(regex);
// return regex itself if it is of correct regex syntax
return regex;
}
private static PhoneMetadata loadCountryMetadata(String regionCode, Element element) {
PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder();
metadata.setId(regionCode);
metadata.setCountryCode(Integer.parseInt(element.getAttribute("countryCode")));
metadata.setInternationalPrefix(element.getAttribute("internationalPrefix"));
metadata.setInternationalPrefix(validateRE(element.getAttribute("internationalPrefix")));
if (element.hasAttribute("preferredInternationalPrefix")) {
String preferredInternationalPrefix = element.getAttribute("preferredInternationalPrefix");
metadata.setPreferredInternationalPrefix(preferredInternationalPrefix);
}
String nationalPrefix = "";
String nationalPrefixFormattingRule = "";
if (element.hasAttribute("nationalPrefix")) {
nationalPrefix = element.getAttribute("nationalPrefix");
metadata.setNationalPrefix(nationalPrefix);
metadata.setNationalPrefixFormattingRule(
getNationalPrefixFormattingRuleFromElement(element, nationalPrefix));
nationalPrefixFormattingRule =
validateRE(getNationalPrefixFormattingRuleFromElement(element, nationalPrefix));
if (element.hasAttribute("nationalPrefixForParsing")) {
metadata.setNationalPrefixForParsing(element.getAttribute("nationalPrefixForParsing"));
metadata.setNationalPrefixForParsing(
validateRE(element.getAttribute("nationalPrefixForParsing")));
if (element.hasAttribute("nationalPrefixTransformRule")) {
metadata.setNationalPrefixTransformRule(
element.getAttribute("nationalPrefixTransformRule"));
@ -122,17 +131,16 @@ public class BuildMetadataProtoFromXml {
Element numberFormatElement = (Element) numberFormatElements.item(i);
NumberFormat.Builder format = NumberFormat.newBuilder();
if (numberFormatElement.hasAttribute("nationalPrefixFormattingRule")) {
format.setNationalPrefixFormattingRule(
getNationalPrefixFormattingRuleFromElement(numberFormatElement, nationalPrefix));
format.setNationalPrefixFormattingRule(validateRE(
getNationalPrefixFormattingRuleFromElement(numberFormatElement, nationalPrefix)));
} else {
format.setNationalPrefixFormattingRule(metadata.getNationalPrefixFormattingRule());
format.setNationalPrefixFormattingRule(nationalPrefixFormattingRule);
}
if (numberFormatElement.hasAttribute("leadingDigits")) {
format.setLeadingDigits(numberFormatElement.getAttribute("leadingDigits"));
format.setLeadingDigits(validateRE(numberFormatElement.getAttribute("leadingDigits")));
}
format.setPattern(numberFormatElement.getAttribute("pattern"));
String formatValue = numberFormatElement.getFirstChild().getNodeValue();
format.setFormat(formatValue);
format.setPattern(validateRE(numberFormatElement.getAttribute("pattern")));
format.setFormat(validateRE(numberFormatElement.getFirstChild().getNodeValue()));
metadata.addNumberFormat(format.build());
}
}
@ -144,10 +152,10 @@ public class BuildMetadataProtoFromXml {
Element numberFormatElement = (Element) intlNumberFormatElements.item(i);
NumberFormat.Builder format = NumberFormat.newBuilder();
if (numberFormatElement.hasAttribute("leadingDigits")) {
format.setLeadingDigits(numberFormatElement.getAttribute("leadingDigits"));
format.setLeadingDigits(validateRE(numberFormatElement.getAttribute("leadingDigits")));
}
format.setPattern(numberFormatElement.getAttribute("pattern"));
format.setFormat(numberFormatElement.getFirstChild().getNodeValue());
format.setPattern(validateRE(numberFormatElement.getAttribute("pattern")));
format.setFormat(validateRE(numberFormatElement.getFirstChild().getNodeValue()));
metadata.addIntlNumberFormat(format.build());
}
}
@ -215,14 +223,14 @@ public class BuildMetadataProtoFromXml {
Element element = (Element) phoneNumberDescList.item(0);
NodeList possiblePattern = element.getElementsByTagName("possibleNumberPattern");
if (possiblePattern.getLength() > 0) {
numberDesc.setPossibleNumberPattern(possiblePattern.
item(0).getFirstChild().getNodeValue());
numberDesc.setPossibleNumberPattern(
validateRE(possiblePattern.item(0).getFirstChild().getNodeValue()));
}
NodeList validPattern = element.getElementsByTagName("nationalNumberPattern");
if (validPattern.getLength() > 0) {
numberDesc.setNationalNumberPattern(validPattern.
item(0).getFirstChild().getNodeValue());
numberDesc.setNationalNumberPattern(
validateRE(validPattern.item(0).getFirstChild().getNodeValue()));
}
if (!liteBuild) {


+ 21
- 13
java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -161,6 +161,7 @@ public class PhoneNumberUtil {
new ImmutableSet.Builder<Integer>()
.add(39) // Italy
.add(225) // Cte d'Ivoire
.add(227) // Niger
.add(228) // Togo
.add(240) // Equatorial Guinea
.add(241) // Gabon
@ -562,7 +563,7 @@ public class PhoneNumberUtil {
* supplied is used only for the resultant log message.
*/
private boolean isValidRegionCode(String regionCode, int countryCode, String number) {
if (regionCode == null || regionCode.equals("ZZ")) {
if (regionCode == null || regionCode.equalsIgnoreCase("ZZ")) {
LOGGER.log(Level.WARNING,
"Number " + number + "has invalid or missing country code (" + countryCode + ")");
return false;
@ -674,11 +675,12 @@ public class PhoneNumberUtil {
*/
public String formatOutOfCountryCallingNumber(PhoneNumber number,
String countryCallingFrom) {
if (countryCallingFrom == null || countryCallingFrom.equals("ZZ")) {
if (countryCallingFrom == null || countryCallingFrom.equalsIgnoreCase("ZZ")) {
LOGGER.log(Level.WARNING,
"Trying to format number from invalid region. International formatting applied.");
return format(number, PhoneNumberFormat.INTERNATIONAL);
}
countryCallingFrom = countryCallingFrom.toUpperCase();
int countryCode = number.getCountryCode();
if (countryCode == NANPA_COUNTRY_CODE && isNANPACountry(countryCallingFrom)) {
// For NANPA countries, return the national format for these countries but prefix it with the
@ -837,6 +839,7 @@ public class PhoneNumberUtil {
* does not contain such information.
*/
public PhoneNumber getExampleNumber(String regionCode) {
regionCode = regionCode.toUpperCase();
return getExampleNumberForType(regionCode, PhoneNumberType.FIXED_LINE);
}
@ -850,6 +853,7 @@ public class PhoneNumberUtil {
* does not contain such information.
*/
public PhoneNumber getExampleNumberForType(String regionCode, PhoneNumberType type) {
regionCode = regionCode.toUpperCase();
PhoneNumberDesc desc = getNumberDescByType(getMetadataForRegion(regionCode), type);
try {
if (desc.hasExampleNumber()) {
@ -1021,6 +1025,7 @@ public class PhoneNumberUtil {
* @return a boolean that indicates whether the number is of a valid pattern
*/
public boolean isValidNumberForRegion(PhoneNumber number, String regionCode) {
regionCode = regionCode.toUpperCase();
if (number.getCountryCode() != getCountryCodeForRegion(regionCode)) {
return false;
}
@ -1098,11 +1103,11 @@ public class PhoneNumberUtil {
* @return the country calling code for the country/region denoted by regionCode
*/
public int getCountryCodeForRegion(String regionCode) {
if (regionCode == null || regionCode.equals("ZZ")) {
if (regionCode == null || regionCode.equalsIgnoreCase("ZZ")) {
LOGGER.log(Level.SEVERE, "Invalid or missing country code provided.");
return 0;
}
PhoneMetadata metadata = getMetadataForRegion(regionCode);
PhoneMetadata metadata = getMetadataForRegion(regionCode.toUpperCase());
if (metadata == null) {
LOGGER.log(Level.SEVERE, "Unsupported country code provided.");
return 0;
@ -1117,6 +1122,7 @@ public class PhoneNumberUtil {
* @return true if regionCode is one of the countries under NANPA
*/
public boolean isNANPACountry(String regionCode) {
regionCode = regionCode.toUpperCase();
return nanpaCountries.contains(regionCode);
}
@ -1202,6 +1208,7 @@ public class PhoneNumberUtil {
* @return true if the number is possible
*/
public boolean isPossibleNumber(String number, String countryDialingFrom) {
countryDialingFrom = countryDialingFrom.toUpperCase();
try {
return isPossibleNumber(parse(number, countryDialingFrom));
} catch (NumberParseException e) {
@ -1225,6 +1232,7 @@ public class PhoneNumberUtil {
* specific country "as you type"
*/
public AsYouTypeFormatter getAsYouTypeFormatter(String regionCode) {
regionCode = regionCode.toUpperCase();
return new AsYouTypeFormatter(regionCode);
}
@ -1472,11 +1480,11 @@ public class PhoneNumberUtil {
*/
public PhoneNumber parse(String numberToParse, String defaultCountry)
throws NumberParseException {
if (defaultCountry == null || defaultCountry.equals("ZZ")) {
if (defaultCountry == null || defaultCountry.equalsIgnoreCase("ZZ")) {
throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
"No default country was supplied.");
}
return parseHelper(numberToParse, defaultCountry, false);
return parseHelper(numberToParse, defaultCountry.toUpperCase(), false);
}
/**
@ -1496,11 +1504,11 @@ public class PhoneNumberUtil {
*/
public PhoneNumber parseAndKeepRawInput(String numberToParse, String defaultCountry)
throws NumberParseException {
if (defaultCountry == null || defaultCountry.equals("ZZ")) {
if (defaultCountry == null || defaultCountry.equalsIgnoreCase("ZZ")) {
throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
"No default country was supplied.");
}
return parseHelper(numberToParse, defaultCountry, true);
return parseHelper(numberToParse, defaultCountry.toUpperCase(), true);
}
/**
@ -1588,11 +1596,6 @@ public class PhoneNumberUtil {
countryMetadata.getNationalPrefixTransformRule(),
validNumberPattern);
}
phoneNumber.setCountryCode(countryCode);
if (isLeadingZeroCountry(countryCode) &&
normalizedNationalNumber.charAt(0) == '0') {
phoneNumber.setItalianLeadingZero(true);
}
int lengthOfNationalNumber = normalizedNationalNumber.length();
if (lengthOfNationalNumber < MIN_LENGTH_FOR_NSN) {
throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,
@ -1604,6 +1607,11 @@ public class PhoneNumberUtil {
"The string supplied is too long to be a "
+ "phone number.");
}
phoneNumber.setCountryCode(countryCode);
if (isLeadingZeroCountry(countryCode) &&
normalizedNationalNumber.charAt(0) == '0') {
phoneNumber.setItalianLeadingZero(true);
}
phoneNumber.setNationalNumber(Long.parseLong(normalizedNationalNumber.toString()));
return phoneNumber.build();
}


+ 17
- 22
java/src/com/google/i18n/phonenumbers/phonemetadata.proto View File

@ -20,8 +20,11 @@
syntax = "proto2";
option cc_api_version = 1;
option java_api_version = 1;
option py_api_version = 1;
option java_package = "com.google.i18n.phonenumbers";
option optimize_for = LITE_RUNTIME;
package i18n.phonenumbers;
message NumberFormat {
@ -51,9 +54,18 @@ message NumberFormat {
// This field specifies how the national prefix ($NP) together with the first
// group ($FG) in the national significant number should be formatted in
// the NATIONAL format when a national prefix exists for a certain country.
// When present, it overrides the national_prefix_formatting_rule specified
// in PhoneNumberDesc. See the comment in PhoneNumberDesc for more information
// on how this rule is specified.
// For example, when this field contains "($NP$FG)", a number from Beijing,
// China (whose $NP = 0), which would by default be formatted without
// national prefix as 10 1234 5678 in NATIONAL format, will instead be
// formatted as (010) 1234 5678; to format it as (0)10 1234 5678, the field
// would contain "($NP)$FG". Note $FG should always be present in this field,
// but $NP can be omitted. For example, having "$FG" could indicate the
// number should be formatted in NATIONAL format without the national prefix.
// This is commonly used to override the rule from generalDesc.
//
// When this field is missing, a number will be formatted without national
// prefix in NATIONAL format. This field does not affect how a number
// is formatted in other formats, such as INTERNATIONAL.
optional string national_prefix_formatting_rule = 4;
}
@ -184,24 +196,7 @@ message PhoneMetadata {
// 15 (inserted after the area code of 343) is used.
repeated NumberFormat intl_number_format = 20;
// This field specifies how the national prefix ($NP) together with the first
// group ($FG) in the national significant number should be formatted in
// the NATIONAL format when a national prefix exists for a certain country.
// For example, when this field contains "($NP$FG)", a number from Beijing,
// China (whose $NP = 0), which would by default be formatted without
// national prefix as 10 1234 5678 in NATIONAL format, will instead be
// formatted as (010) 1234 5678; to format it as (0)10 1234 5678, the field
// would contain "($NP)$FG". Note $FG should always be present in this field,
// but $NP can be omitted. For example, having "$FG" could indicate the
// number should be formatted in NATIONAL format without the national prefix.
// This is commonly used to override the rule from generalDesc.
//
// When this field is missing, a number will be formatted without national
// prefix in NATIONAL format. This field does not affect how a number
// is formatted in other formats, such as INTERNATIONAL.
//
// This field applies to all numberFormats unless overridden by the
// national_prefix_formatting_rule in a specific numberFormat.
// Deprecated.
optional string national_prefix_formatting_rule = 21;
}


+ 91
- 1
java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java View File

@ -196,7 +196,7 @@ public class AsYouTypeFormatterTest extends TestCase {
}
public void testAsYouTypeFormatterGBTollFree() {
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("GB");
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("gb");
assertEquals("0", formatter.inputDigit('0'));
assertEquals("08", formatter.inputDigit('8'));
assertEquals("080", formatter.inputDigit('0'));
@ -282,4 +282,94 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("+54 9 11 2312 123\u2008", formatter.inputDigit('3'));
assertEquals("+54 9 11 2312 1234", formatter.inputDigit('4'));
}
public void testAsYouTypeFormatterKR() {
// +82 51 234 5678
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("KR");
assertEquals("+", formatter.inputDigit('+'));
assertEquals("+8", formatter.inputDigit('8'));
assertEquals("+82", formatter.inputDigit('2'));
assertEquals("+825", formatter.inputDigit('5'));
assertEquals("+8251", formatter.inputDigit('1'));
assertEquals("+82 512", formatter.inputDigit('2'));
assertEquals("+82 51-23\u2008-\u2008\u2008\u2008\u2008", formatter.inputDigit('3'));
assertEquals("+82 51-234-\u2008\u2008\u2008\u2008", formatter.inputDigit('4'));
assertEquals("+82 51-234-5\u2008\u2008\u2008", formatter.inputDigit('5'));
assertEquals("+82 51-234-56\u2008\u2008", formatter.inputDigit('6'));
assertEquals("+82 51-234-567\u2008", formatter.inputDigit('7'));
assertEquals("+82 51-234-5678", formatter.inputDigit('8'));
// +82 2 531 5678
formatter.clear();
assertEquals("+", formatter.inputDigit('+'));
assertEquals("+8", formatter.inputDigit('8'));
assertEquals("+82", formatter.inputDigit('2'));
assertEquals("+822", formatter.inputDigit('2'));
assertEquals("+8225", formatter.inputDigit('5'));
assertEquals("+82 253", formatter.inputDigit('3'));
assertEquals("+82 2-531-\u2008\u2008\u2008\u2008", formatter.inputDigit('1'));
assertEquals("+82 2-531-5\u2008\u2008\u2008", formatter.inputDigit('5'));
assertEquals("+82 2-531-56\u2008\u2008", formatter.inputDigit('6'));
assertEquals("+82 2-531-567\u2008", formatter.inputDigit('7'));
assertEquals("+82 2-531-5678", formatter.inputDigit('8'));
// +82 2 3665 5678
formatter.clear();
assertEquals("+", formatter.inputDigit('+'));
assertEquals("+8", formatter.inputDigit('8'));
assertEquals("+82", formatter.inputDigit('2'));
assertEquals("+822", formatter.inputDigit('2'));
assertEquals("+8223", formatter.inputDigit('3'));
assertEquals("+82 236", formatter.inputDigit('6'));
assertEquals("+82 2-366\u2008-\u2008\u2008\u2008\u2008", formatter.inputDigit('6'));
assertEquals("+82 2-3665-\u2008\u2008\u2008\u2008", formatter.inputDigit('5'));
assertEquals("+82 2-3665-5\u2008\u2008\u2008", formatter.inputDigit('5'));
assertEquals("+82 2-3665-56\u2008\u2008", formatter.inputDigit('6'));
assertEquals("+82 2-3665-567\u2008", formatter.inputDigit('7'));
assertEquals("+82 2-3665-5678", formatter.inputDigit('8'));
// 02-114 : This is too short to format. Checking that there are no side-effects.
formatter.clear();
assertEquals("0", formatter.inputDigit('0'));
assertEquals("02", formatter.inputDigit('2'));
assertEquals("021", formatter.inputDigit('1'));
assertEquals("0211", formatter.inputDigit('1'));
assertEquals("02114", formatter.inputDigit('4'));
// 02-1300
formatter.clear();
assertEquals("0", formatter.inputDigit('0'));
assertEquals("02", formatter.inputDigit('2'));
assertEquals("021", formatter.inputDigit('1'));
assertEquals("0213", formatter.inputDigit('3'));
assertEquals("02130", formatter.inputDigit('0'));
assertEquals("02-1300", formatter.inputDigit('0'));
// 011-456-7890
formatter.clear();
assertEquals("0", formatter.inputDigit('0'));
assertEquals("01", formatter.inputDigit('1'));
assertEquals("011", formatter.inputDigit('1'));
assertEquals("0114", formatter.inputDigit('4'));
assertEquals("01145", formatter.inputDigit('5'));
assertEquals("011-456-\u2008\u2008\u2008\u2008", formatter.inputDigit('6'));
assertEquals("011-456-7\u2008\u2008\u2008", formatter.inputDigit('7'));
assertEquals("011-456-78\u2008\u2008", formatter.inputDigit('8'));
assertEquals("011-456-789\u2008", formatter.inputDigit('9'));
assertEquals("011-456-7890", formatter.inputDigit('0'));
// 011-9876-7890
formatter.clear();
assertEquals("0", formatter.inputDigit('0'));
assertEquals("01", formatter.inputDigit('1'));
assertEquals("011", formatter.inputDigit('1'));
assertEquals("0119", formatter.inputDigit('9'));
assertEquals("01198", formatter.inputDigit('8'));
assertEquals("011-987\u2008-\u2008\u2008\u2008\u2008", formatter.inputDigit('7'));
assertEquals("011-9876-\u2008\u2008\u2008\u2008", formatter.inputDigit('6'));
assertEquals("011-9876-7\u2008\u2008\u2008", formatter.inputDigit('7'));
assertEquals("011-9876-78\u2008\u2008", formatter.inputDigit('8'));
assertEquals("011-9876-789\u2008", formatter.inputDigit('9'));
assertEquals("011-9876-7890", formatter.inputDigit('0'));
}
}

+ 63
- 42
java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -123,6 +123,7 @@ public class PhoneNumberUtilTest extends TestCase {
PhoneNumber deNumber =
PhoneNumber.newBuilder().setCountryCode(49).setNationalNumber(30123456).build();
assertEquals(deNumber, phoneUtil.getExampleNumber("DE"));
assertEquals(deNumber, phoneUtil.getExampleNumber("de"));
assertEquals(deNumber,
phoneUtil.getExampleNumberForType("DE",
@ -415,6 +416,8 @@ public class PhoneNumberUtilTest extends TestCase {
phoneUtil.formatOutOfCountryCallingNumber(arNumber2, "AU"));
assertEquals("011 15 8765-4321 ext. 1234",
phoneUtil.formatOutOfCountryCallingNumber(arNumber2, "AR"));
assertEquals("011 15 8765-4321 ext. 1234",
phoneUtil.formatOutOfCountryCallingNumber(arNumber2, "ar"));
}
public void testFormatOutOfCountryWithPreferredIntlPrefix() {
@ -687,6 +690,7 @@ public class PhoneNumberUtilTest extends TestCase {
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(2423232345L).build();
assertTrue(phoneUtil.isValidNumber(bsNumber1));
assertTrue(phoneUtil.isValidNumberForRegion(bsNumber1, "BS"));
assertTrue(phoneUtil.isValidNumberForRegion(bsNumber1, "bs"));
assertFalse(phoneUtil.isValidNumberForRegion(bsNumber1, "US"));
PhoneNumber bsNumber2 =
PhoneNumber.newBuilder(bsNumber1).setNationalNumber(2421232345L).build();
@ -739,6 +743,7 @@ public class PhoneNumberUtilTest extends TestCase {
public void testGetCountryCodeForRegion() {
assertEquals(1, phoneUtil.getCountryCodeForRegion("US"));
assertEquals(64, phoneUtil.getCountryCodeForRegion("NZ"));
assertEquals(64, phoneUtil.getCountryCodeForRegion("nz"));
assertEquals(0, phoneUtil.getCountryCodeForRegion(null));
assertEquals(0, phoneUtil.getCountryCodeForRegion("ZZ"));
// CS is already deprecated so the library doesn't support it.
@ -748,6 +753,7 @@ public class PhoneNumberUtilTest extends TestCase {
public void testIsNANPACountry() {
assertTrue(phoneUtil.isNANPACountry("US"));
assertTrue(phoneUtil.isNANPACountry("BS"));
assertTrue(phoneUtil.isNANPACountry("bs"));
}
public void testIsPossibleNumber() {
@ -771,6 +777,7 @@ public class PhoneNumberUtilTest extends TestCase {
assertTrue(phoneUtil.isPossibleNumber("(020) 7031 3000", "GB"));
assertTrue(phoneUtil.isPossibleNumber("7031 3000", "GB"));
assertTrue(phoneUtil.isPossibleNumber("3331 6005", "NZ"));
assertTrue(phoneUtil.isPossibleNumber("3331 6005", "nz"));
}
public void testIsPossibleNumberWithReason() {
@ -1032,22 +1039,11 @@ public class PhoneNumberUtilTest extends TestCase {
// National prefix attached.
assertEquals(nzNumber, phoneUtil.parse("033316005", "NZ"));
assertEquals(nzNumber, phoneUtil.parse("033316005", "nz"));
assertEquals(nzNumber, phoneUtil.parse("33316005", "NZ"));
// National prefix attached and some formatting present.
assertEquals(nzNumber, phoneUtil.parse("03-331 6005", "NZ"));
assertEquals(nzNumber, phoneUtil.parse("03 331 6005", "NZ"));
// Test case with alpha characters.
PhoneNumber tollfreeNumber =
PhoneNumber.newBuilder().setCountryCode(64).setNationalNumber(800332005L).build();
assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", "NZ"));
PhoneNumber premiumNumber =
PhoneNumber.newBuilder().setCountryCode(64).setNationalNumber(9003326005L).build();
assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", "NZ"));
// Not enough alpha characters for them to be considered intentional, so they are stripped.
assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", "NZ"));
// Testing international prefixes.
// Should strip country code.
@ -1057,11 +1053,38 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals(nzNumber, phoneUtil.parse("01164 3 331 6005", "US"));
assertEquals(nzNumber, phoneUtil.parse("+64 3 331 6005", "US"));
// Test for http://b/issue?id=2247493
PhoneNumber nzNumber2 =
PhoneNumber.newBuilder().setCountryCode(64).setNationalNumber(64123456L).build();
assertEquals(nzNumber2, phoneUtil.parse("64(0)64123456", "NZ"));
// Check that using a "/" is fine in a phone number.
PhoneNumber deNumber =
PhoneNumber.newBuilder().setCountryCode(49).setNationalNumber(12345678L).build();
assertEquals(deNumber, phoneUtil.parse("123/45678", "DE"));
// Check it doesn't use the '1' as a country code when parsing if the phone number was already
// possible.
PhoneNumber usNumber =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(1234567890L).build();
assertEquals(usNumber, phoneUtil.parse("123-456-7890", "US"));
}
public void testParseNumberWithAlphaCharacters() throws Exception {
// Test case with alpha characters.
PhoneNumber tollfreeNumber =
PhoneNumber.newBuilder().setCountryCode(64).setNationalNumber(800332005L).build();
assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", "NZ"));
PhoneNumber premiumNumber =
PhoneNumber.newBuilder().setCountryCode(64).setNationalNumber(9003326005L).build();
assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", "NZ"));
// Not enough alpha characters for them to be considered intentional, so they are stripped.
assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", "NZ"));
assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", "NZ"));
}
public void testParseWithInternationalPrefixes() throws Exception {
PhoneNumber usNumber =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(6503336000L).build();
assertEquals(usNumber, phoneUtil.parse("+1 (650) 333-6000", "NZ"));
@ -1075,12 +1098,9 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals(usNumber, phoneUtil.parse("0191-650-333-6000", "SG"));
// Calling the US number from Poland
assertEquals(usNumber, phoneUtil.parse("0~01-650-333-6000", "PL"));
// Check it doesn't use the '1' as a country code when parsing if the phone number was already
// possible.
PhoneNumber usNumber2 =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(1234567890L).build();
assertEquals(usNumber2, phoneUtil.parse("123-456-7890", "US"));
}
public void testParseWithLeadingZero() throws Exception {
PhoneNumber itNumber =
PhoneNumber.newBuilder().setCountryCode(39).setNationalNumber(236618300L)
.setItalianLeadingZero(true).build();
@ -1090,12 +1110,9 @@ public class PhoneNumberUtilTest extends TestCase {
PhoneNumber itNumber2 =
PhoneNumber.newBuilder().setCountryCode(39).setNationalNumber(312345678L).build();
assertEquals(itNumber2, phoneUtil.parse("312 345 678", "IT"));
}
// Check that using a "/" is fine in a phone number.
PhoneNumber deNumber =
PhoneNumber.newBuilder().setCountryCode(49).setNationalNumber(12345678L).build();
assertEquals(deNumber, phoneUtil.parse("123/45678", "DE"));
public void testParseNationalNumberArgentina() throws Exception {
// Test parsing mobile numbers of Argentina.
PhoneNumber arNumber =
PhoneNumber.newBuilder().setCountryCode(54).setNationalNumber(93435551212L).build();
@ -1122,7 +1139,9 @@ public class PhoneNumberUtilTest extends TestCase {
PhoneNumber.newBuilder().setCountryCode(54).setNationalNumber(2312340000L).build();
assertEquals(arNumber5, phoneUtil.parse("+54 23 1234 0000", "AR"));
assertEquals(arNumber5, phoneUtil.parse("023 1234 0000", "AR"));
}
public void testParseWithXInNumber() throws Exception {
// Test that having an 'x' in the phone number at the start is ok and that it just gets removed.
PhoneNumber arNumber6 =
PhoneNumber.newBuilder().setCountryCode(54).setNationalNumber(123456789L).build();
@ -1137,7 +1156,9 @@ public class PhoneNumberUtilTest extends TestCase {
// to 7 digits. This assumption is okay for now as all the countries where a carrier selection
// code is written in the form of xx have a national significant number of length larger than 7.
assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", "US"));
}
public void testParseNumbersMexico() throws Exception {
// Test parsing fixed-line numbers of Mexico.
PhoneNumber mxNumber =
PhoneNumber.newBuilder().setCountryCode(52).setNationalNumber(4499780001L).build();
@ -1151,24 +1172,6 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals(mxNumber2, phoneUtil.parse("+52 1 33 1234-5678", "MX"));
assertEquals(mxNumber2, phoneUtil.parse("044 (33) 1234-5678", "MX"));
assertEquals(mxNumber2, phoneUtil.parse("045 33 1234-5678", "MX"));
// Test that if a number has two extensions specified, we ignore the second.
PhoneNumber usWithTwoExtensionsNumber =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(2121231234L)
.setExtension("508").build();
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234",
"US"));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234",
"US"));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234",
"US"));
// Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before
// the # are an extension.
PhoneNumber usWithExtension =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(6451231234L)
.setExtension("910").build();
assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", "US"));
}
public void testFailedParseOnInvalidNumbers() {
@ -1317,6 +1320,24 @@ public class PhoneNumberUtilTest extends TestCase {
phoneUtil.parse("(800) 901-3355 ,extension 7246433", "US"));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", "US"));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", "US"));
// Test that if a number has two extensions specified, we ignore the second.
PhoneNumber usWithTwoExtensionsNumber =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(2121231234L)
.setExtension("508").build();
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234",
"US"));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234",
"US"));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234",
"US"));
// Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before
// the # are an extension.
PhoneNumber usWithExtension2 =
PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(6451231234L)
.setExtension("910").build();
assertEquals(usWithExtension2, phoneUtil.parse("+1 (645) 123 1234-910#", "US"));
}
public void testParseAndKeepRaw() throws Exception {
@ -1334,7 +1355,7 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals("+376 12345", phoneUtil.format(adNumber,
PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL));
assertEquals("+37612345", phoneUtil.format(adNumber,
PhoneNumberUtil.PhoneNumberFormat.E164));
PhoneNumberUtil.PhoneNumberFormat.E164));
assertEquals("12345", phoneUtil.format(adNumber,
PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN,


Loading…
Cancel
Save