Browse Source

Java: remove obsolete code that treated numbers where we had no metadata as valid.

pull/567/head
Lara Scheidegger 11 years ago
committed by Mihaela Rosca
parent
commit
945b904470
5 changed files with 5 additions and 52 deletions
  1. +1
    -24
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  2. +1
    -13
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  3. BIN
      java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC
  4. BIN
      java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX
  5. +3
    -15
      resources/PhoneNumberMetadataForTesting.xml

+ 1
- 24
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -1981,9 +1981,7 @@ public class PhoneNumberUtil {
}
private PhoneNumberType getNumberTypeHelper(String nationalNumber, PhoneMetadata metadata) {
PhoneNumberDesc generalNumberDesc = metadata.getGeneralDesc();
if (!generalNumberDesc.hasNationalNumberPattern() ||
!isNumberMatchingDesc(nationalNumber, generalNumberDesc)) {
if (!isNumberMatchingDesc(nationalNumber, metadata.getGeneralDesc())) {
return PhoneNumberType.UNKNOWN;
}
@ -2114,16 +2112,7 @@ public class PhoneNumberUtil {
// match that of the region code.
return false;
}
PhoneNumberDesc generalNumDesc = metadata.getGeneralDesc();
String nationalSignificantNumber = getNationalSignificantNumber(number);
// For regions where we don't have metadata for PhoneNumberDesc, we treat any number passed in
// as a valid number if its national significant number is between the minimum and maximum
// lengths defined by ITU for a national significant number.
if (!generalNumDesc.hasNationalNumberPattern()) {
int numberLength = nationalSignificantNumber.length();
return numberLength > MIN_LENGTH_FOR_NSN && numberLength <= MAX_LENGTH_FOR_NSN;
}
return getNumberTypeHelper(nationalSignificantNumber, metadata) != PhoneNumberType.UNKNOWN;
}
@ -2382,18 +2371,6 @@ public class PhoneNumberUtil {
// Metadata cannot be null because the country calling code is valid.
PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode);
PhoneNumberDesc generalNumDesc = metadata.getGeneralDesc();
// Handling case of numbers with no metadata.
if (!generalNumDesc.hasNationalNumberPattern()) {
logger.log(Level.FINER, "Checking if number is possible with incomplete metadata.");
int numberLength = nationalNumber.length();
if (numberLength < MIN_LENGTH_FOR_NSN) {
return ValidationResult.TOO_SHORT;
} else if (numberLength > MAX_LENGTH_FOR_NSN) {
return ValidationResult.TOO_LONG;
} else {
return ValidationResult.IS_POSSIBLE;
}
}
Pattern possibleNumberPattern =
regexCache.getPatternForRegex(generalNumDesc.getPossibleNumberPattern());
return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber);


+ 1
- 13
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -1314,18 +1314,6 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG,
phoneUtil.isPossibleNumberWithReason(INTERNATIONAL_TOLL_FREE_TOO_LONG));
// Try with number that we don't have metadata for.
PhoneNumber adNumber = new PhoneNumber();
adNumber.setCountryCode(376).setNationalNumber(12345L);
assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
phoneUtil.isPossibleNumberWithReason(adNumber));
adNumber.setCountryCode(376).setNationalNumber(1L);
assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT,
phoneUtil.isPossibleNumberWithReason(adNumber));
adNumber.setCountryCode(376).setNationalNumber(123456789012345678L);
assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG,
phoneUtil.isPossibleNumberWithReason(adNumber));
}
public void testIsNotPossibleNumber() {
@ -2350,7 +2338,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase {
assertEquals("+37612345", phoneUtil.format(adNumber, PhoneNumberFormat.E164));
assertEquals("12345", phoneUtil.format(adNumber, PhoneNumberFormat.NATIONAL));
assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber));
assertTrue(phoneUtil.isValidNumber(adNumber));
assertFalse(phoneUtil.isValidNumber(adNumber));
// Test dialing a US number from within Andorra.
assertEquals("00 1 650 253 0000",


BIN
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC View File


BIN
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX View File


+ 3
- 15
resources/PhoneNumberMetadataForTesting.xml View File

@ -223,14 +223,8 @@
<!-- Cocos Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
recognised by the library. However, we don't want all numbers to be considered valid for
it, so we set at least a general description that allows our test geocoding numbers to be
valid only for Australia. -->
recognised by the library. -->
<territory id="CC" countryCode="61">
<generalDesc>
<nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
<possibleNumberPattern>\d{2,14}</possibleNumberPattern>
</generalDesc>
</territory>
<!-- China -->
@ -251,18 +245,12 @@
</numberFormat>
</availableFormats>
</territory>
<!-- Christmas Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
recognised by the library. However, we don't want all numbers to be considered valid for
it, so we set at least a general description that allows our test geocoding numbers to be
valid only for Australia. -->
recognised by the library. -->
<territory id="CX" countryCode="61">
<generalDesc>
<nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
<possibleNumberPattern>\d{2,14}</possibleNumberPattern>
</generalDesc>
</territory>
<!-- Germany -->


Loading…
Cancel
Save