diff options
Diffstat (limited to 'libphonenumber/test/com/google')
53 files changed, 563 insertions, 241 deletions
diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java b/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java index 0c2188e8..a68d45fe 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java @@ -30,11 +30,11 @@ public class CountryCodeToRegionCodeMapForTesting { // country/region represented by that country code. In the case of multiple // countries sharing a calling code, such as the NANPA countries, the one // indicated with "isMainCountryForCode" in the metadata should be first. - static Map<Integer, List<String>> getCountryCodeToRegionCodeMap() { - // The capacity is set to 37 as there are 28 different entries, + public static Map<Integer, List<String>> getCountryCodeToRegionCodeMap() { + // The capacity is set to 38 as there are 29 different entries, // and this offers a load factor of roughly 0.75. Map<Integer, List<String>> countryCodeToRegionCodeMap = - new HashMap<Integer, List<String>>(37); + new HashMap<Integer, List<String>>(38); ArrayList<String> listWithRegionCode; @@ -86,6 +86,10 @@ public class CountryCodeToRegionCodeMapForTesting { listWithRegionCode.add("BR"); countryCodeToRegionCodeMap.put(55, listWithRegionCode); + listWithRegionCode = new ArrayList<String>(1); + listWithRegionCode.add("CO"); + countryCodeToRegionCodeMap.put(57, listWithRegionCode); + listWithRegionCode = new ArrayList<String>(3); listWithRegionCode.add("AU"); listWithRegionCode.add("CC"); diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java index ebea7b7d..e98470bf 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java @@ -19,15 +19,15 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; - -import junit.framework.TestCase; - +import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider; +import com.google.i18n.phonenumbers.metadata.source.RegionMetadataSource; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import junit.framework.TestCase; /** * Verifies all of the example numbers in the metadata are valid and of the correct type. If no @@ -37,10 +37,14 @@ import java.util.logging.Logger; */ public class ExampleNumbersTest extends TestCase { private static final Logger logger = Logger.getLogger(ExampleNumbersTest.class.getName()); - private PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); - private ShortNumberInfo shortNumberInfo = ShortNumberInfo.getInstance(); - private List<PhoneNumber> invalidCases = new ArrayList<PhoneNumber>(); - private List<PhoneNumber> wrongTypeCases = new ArrayList<PhoneNumber>(); + private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); + private final ShortNumberInfo shortNumberInfo = ShortNumberInfo.getInstance(); + private final RegionMetadataSource shortNumberMetadataSource = + DefaultMetadataDependenciesProvider.getInstance().getShortNumberMetadataSource(); + + private final List<PhoneNumber> invalidCases = new ArrayList<>(); + private final List<PhoneNumber> wrongTypeCases = new ArrayList<>(); + private final Set<String> shortNumberSupportedRegions = ShortNumbersRegionCodeSet.getRegionCodeSet(); /** * @param exampleNumberRequestedType type we are requesting an example number for @@ -55,14 +59,14 @@ public class ExampleNumbersTest extends TestCase { if (exampleNumber != null) { if (!phoneNumberUtil.isValidNumber(exampleNumber)) { invalidCases.add(exampleNumber); - logger.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString()); + logger.log(Level.SEVERE, "Failed validation for " + exampleNumber); } else { // We know the number is valid, now we check the type. PhoneNumberType exampleNumberType = phoneNumberUtil.getNumberType(exampleNumber); if (!possibleExpectedTypes.contains(exampleNumberType)) { wrongTypeCases.add(exampleNumber); logger.log(Level.SEVERE, "Wrong type for " - + exampleNumber.toString() + + exampleNumber + ": got " + exampleNumberType); logger.log(Level.WARNING, "Expected types: "); for (PhoneNumberType type : possibleExpectedTypes) { @@ -74,7 +78,7 @@ public class ExampleNumbersTest extends TestCase { } } - public void testFixedLine() throws Exception { + public void testFixedLine() { Set<PhoneNumberType> fixedLineTypes = EnumSet.of(PhoneNumberType.FIXED_LINE, PhoneNumberType.FIXED_LINE_OR_MOBILE); checkNumbersValidAndCorrectType(PhoneNumberType.FIXED_LINE, fixedLineTypes); @@ -82,7 +86,7 @@ public class ExampleNumbersTest extends TestCase { assertEquals(0, wrongTypeCases.size()); } - public void testMobile() throws Exception { + public void testMobile() { Set<PhoneNumberType> mobileTypes = EnumSet.of(PhoneNumberType.MOBILE, PhoneNumberType.FIXED_LINE_OR_MOBILE); checkNumbersValidAndCorrectType(PhoneNumberType.MOBILE, mobileTypes); @@ -90,56 +94,56 @@ public class ExampleNumbersTest extends TestCase { assertEquals(0, wrongTypeCases.size()); } - public void testTollFree() throws Exception { + public void testTollFree() { Set<PhoneNumberType> tollFreeTypes = EnumSet.of(PhoneNumberType.TOLL_FREE); checkNumbersValidAndCorrectType(PhoneNumberType.TOLL_FREE, tollFreeTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testPremiumRate() throws Exception { + public void testPremiumRate() { Set<PhoneNumberType> premiumRateTypes = EnumSet.of(PhoneNumberType.PREMIUM_RATE); checkNumbersValidAndCorrectType(PhoneNumberType.PREMIUM_RATE, premiumRateTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testVoip() throws Exception { + public void testVoip() { Set<PhoneNumberType> voipTypes = EnumSet.of(PhoneNumberType.VOIP); checkNumbersValidAndCorrectType(PhoneNumberType.VOIP, voipTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testPager() throws Exception { + public void testPager() { Set<PhoneNumberType> pagerTypes = EnumSet.of(PhoneNumberType.PAGER); checkNumbersValidAndCorrectType(PhoneNumberType.PAGER, pagerTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testUan() throws Exception { + public void testUan() { Set<PhoneNumberType> uanTypes = EnumSet.of(PhoneNumberType.UAN); checkNumbersValidAndCorrectType(PhoneNumberType.UAN, uanTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testVoicemail() throws Exception { + public void testVoicemail() { Set<PhoneNumberType> voicemailTypes = EnumSet.of(PhoneNumberType.VOICEMAIL); checkNumbersValidAndCorrectType(PhoneNumberType.VOICEMAIL, voicemailTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testSharedCost() throws Exception { + public void testSharedCost() { Set<PhoneNumberType> sharedCostTypes = EnumSet.of(PhoneNumberType.SHARED_COST); checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes); assertEquals(0, invalidCases.size()); assertEquals(0, wrongTypeCases.size()); } - public void testCanBeInternationallyDialled() throws Exception { + public void testCanBeInternationallyDialled() { for (String regionCode : phoneNumberUtil.getSupportedRegions()) { PhoneNumber exampleNumber = null; PhoneNumberDesc desc = @@ -153,41 +157,41 @@ public class ExampleNumbersTest extends TestCase { } if (exampleNumber != null && phoneNumberUtil.canBeInternationallyDialled(exampleNumber)) { wrongTypeCases.add(exampleNumber); - logger.log(Level.SEVERE, "Number " + exampleNumber.toString() + logger.log(Level.SEVERE, "Number " + exampleNumber + " should not be internationally diallable"); } } assertEquals(0, wrongTypeCases.size()); } - public void testGlobalNetworkNumbers() throws Exception { + public void testGlobalNetworkNumbers() { for (Integer callingCode : phoneNumberUtil.getSupportedGlobalNetworkCallingCodes()) { PhoneNumber exampleNumber = phoneNumberUtil.getExampleNumberForNonGeoEntity(callingCode); assertNotNull("No example phone number for calling code " + callingCode, exampleNumber); if (!phoneNumberUtil.isValidNumber(exampleNumber)) { invalidCases.add(exampleNumber); - logger.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString()); + logger.log(Level.SEVERE, "Failed validation for " + exampleNumber); } } assertEquals(0, invalidCases.size()); } - public void testEveryRegionHasAnExampleNumber() throws Exception { + public void testEveryRegionHasAnExampleNumber() { for (String regionCode : phoneNumberUtil.getSupportedRegions()) { PhoneNumber exampleNumber = phoneNumberUtil.getExampleNumber(regionCode); assertNotNull("No example number found for region " + regionCode, exampleNumber); } } - public void testEveryRegionHasAnInvalidExampleNumber() throws Exception { + public void testEveryRegionHasAnInvalidExampleNumber() { for (String regionCode : phoneNumberUtil.getSupportedRegions()) { PhoneNumber exampleNumber = phoneNumberUtil.getInvalidExampleNumber(regionCode); assertNotNull("No invalid example number found for region " + regionCode, exampleNumber); } } - public void testEveryTypeHasAnExampleNumber() throws Exception { + public void testEveryTypeHasAnExampleNumber() { for (PhoneNumberUtil.PhoneNumberType type : PhoneNumberUtil.PhoneNumberType.values()) { if (type == PhoneNumberType.UNKNOWN) { continue; @@ -198,8 +202,8 @@ public class ExampleNumbersTest extends TestCase { } public void testShortNumbersValidAndCorrectCost() throws Exception { - List<String> invalidStringCases = new ArrayList<String>(); - for (String regionCode : shortNumberInfo.getSupportedRegions()) { + List<String> invalidStringCases = new ArrayList<>(); + for (String regionCode : shortNumberSupportedRegions) { String exampleShortNumber = shortNumberInfo.getExampleShortNumber(regionCode); if (!shortNumberInfo.isValidShortNumberForRegion( phoneNumberUtil.parse(exampleShortNumber, regionCode), regionCode)) { @@ -211,7 +215,7 @@ public class ExampleNumbersTest extends TestCase { PhoneNumber phoneNumber = phoneNumberUtil.parse(exampleShortNumber, regionCode); if (!shortNumberInfo.isValidShortNumber(phoneNumber)) { invalidCases.add(phoneNumber); - logger.log(Level.SEVERE, "Failed validation for " + phoneNumber.toString()); + logger.log(Level.SEVERE, "Failed validation for " + phoneNumber); } for (ShortNumberInfo.ShortNumberCost cost : ShortNumberInfo.ShortNumberCost.values()) { @@ -236,9 +240,8 @@ public class ExampleNumbersTest extends TestCase { public void testEmergency() throws Exception { int wrongTypeCounter = 0; - for (String regionCode : shortNumberInfo.getSupportedRegions()) { - PhoneNumberDesc desc = - MetadataManager.getShortNumberMetadataForRegion(regionCode).getEmergency(); + for (String regionCode : shortNumberSupportedRegions) { + PhoneNumberDesc desc = shortNumberMetadataSource.getMetadataForRegion(regionCode).getEmergency(); if (desc.hasExampleNumber()) { String exampleNumber = desc.getExampleNumber(); PhoneNumber phoneNumber = phoneNumberUtil.parse(exampleNumber, regionCode); @@ -258,9 +261,8 @@ public class ExampleNumbersTest extends TestCase { public void testCarrierSpecificShortNumbers() throws Exception { int wrongTagCounter = 0; - for (String regionCode : shortNumberInfo.getSupportedRegions()) { - PhoneNumberDesc desc = - MetadataManager.getShortNumberMetadataForRegion(regionCode).getCarrierSpecific(); + for (String regionCode : shortNumberSupportedRegions) { + PhoneNumberDesc desc = shortNumberMetadataSource.getMetadataForRegion(regionCode).getCarrierSpecific(); if (desc.hasExampleNumber()) { String exampleNumber = desc.getExampleNumber(); PhoneNumber carrierSpecificNumber = phoneNumberUtil.parse(exampleNumber, regionCode); @@ -276,9 +278,8 @@ public class ExampleNumbersTest extends TestCase { public void testSmsServiceShortNumbers() throws Exception { int wrongTagCounter = 0; - for (String regionCode : shortNumberInfo.getSupportedRegions()) { - PhoneNumberDesc desc = - MetadataManager.getShortNumberMetadataForRegion(regionCode).getSmsServices(); + for (String regionCode : shortNumberSupportedRegions) { + PhoneNumberDesc desc = shortNumberMetadataSource.getMetadataForRegion(regionCode).getSmsServices(); if (desc.hasExampleNumber()) { String exampleNumber = desc.getExampleNumber(); PhoneNumber smsServiceNumber = phoneNumberUtil.parse(exampleNumber, regionCode); diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java deleted file mode 100644 index 91b2f395..00000000 --- a/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2012 The Libphonenumber Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.i18n.phonenumbers; - -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import java.util.concurrent.ConcurrentHashMap; -import junit.framework.TestCase; - -/** - * Some basic tests to check that metadata can be correctly loaded. - */ -public class MetadataManagerTest extends TestCase { - public void testAlternateFormatsLoadCorrectly() { - // We should have some data for Germany. - PhoneMetadata germanyMetadata = MetadataManager.getAlternateFormatsForCountry(49); - assertNotNull(germanyMetadata); - assertTrue(germanyMetadata.getNumberFormatCount() > 0); - } - - public void testAlternateFormatsFailsGracefully() throws Exception { - PhoneMetadata noAlternateFormats = MetadataManager.getAlternateFormatsForCountry(999); - assertNull(noAlternateFormats); - } - - public void testShortNumberMetadataLoadCorrectly() throws Exception { - // We should have some data for France. - PhoneMetadata franceMetadata = MetadataManager.getShortNumberMetadataForRegion("FR"); - assertNotNull(franceMetadata); - assertTrue(franceMetadata.hasShortCode()); - } - - public void testShortNumberMetadataFailsGracefully() throws Exception { - PhoneMetadata noShortNumberMetadata = MetadataManager.getShortNumberMetadataForRegion("XXX"); - assertNull(noShortNumberMetadata); - } - - public void testGetMetadataFromMultiFilePrefix_regionCode() { - ConcurrentHashMap<String, PhoneMetadata> map = new ConcurrentHashMap<String, PhoneMetadata>(); - PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix("CA", map, - "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting", - MetadataManager.DEFAULT_METADATA_LOADER); - assertEquals(metadata, map.get("CA")); - } - - public void testGetMetadataFromMultiFilePrefix_countryCallingCode() { - ConcurrentHashMap<Integer, PhoneMetadata> map = new ConcurrentHashMap<Integer, PhoneMetadata>(); - PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix(800, map, - "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting", - MetadataManager.DEFAULT_METADATA_LOADER); - assertEquals(metadata, map.get(800)); - } - - public void testGetMetadataFromMultiFilePrefix_missingMetadataFileThrowsRuntimeException() { - // In normal usage we should never get a state where we are asking to load metadata that doesn't - // exist. However if the library is packaged incorrectly in the jar, this could happen and the - // best we can do is make sure the exception has the file name in it. - try { - MetadataManager.getMetadataFromMultiFilePrefix("XX", - new ConcurrentHashMap<String, PhoneMetadata>(), "no/such/file", - MetadataManager.DEFAULT_METADATA_LOADER); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_XX")); - } - try { - MetadataManager.getMetadataFromMultiFilePrefix(123, - new ConcurrentHashMap<Integer, PhoneMetadata>(), "no/such/file", - MetadataManager.DEFAULT_METADATA_LOADER); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_123")); - } - } -} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java deleted file mode 100644 index 7c9b0faf..00000000 --- a/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2015 The Libphonenumber Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.i18n.phonenumbers; - -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import junit.framework.TestCase; - -/** - * Unit tests for MultiFileMetadataSourceImpl.java. - */ -public class MultiFileMetadataSourceImplTest extends TestCase { - private static final MultiFileMetadataSourceImpl SOURCE = - new MultiFileMetadataSourceImpl(MetadataManager.DEFAULT_METADATA_LOADER); - private static final MultiFileMetadataSourceImpl MISSING_FILE_SOURCE = - new MultiFileMetadataSourceImpl("no/such/file", MetadataManager.DEFAULT_METADATA_LOADER); - - public void testGeoPhoneNumberMetadataLoadCorrectly() { - // We should have some data for the UAE. - PhoneMetadata uaeMetadata = SOURCE.getMetadataForRegion("AE"); - assertEquals(uaeMetadata.getCountryCode(), 971); - assertTrue(uaeMetadata.hasGeneralDesc()); - } - - public void testGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception { - try { - MISSING_FILE_SOURCE.getMetadataForRegion("AE"); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file")); - } - } - - public void testNonGeoPhoneNumberMetadataLoadCorrectly() { - // We should have some data for international toll-free numbers. - PhoneMetadata intlMetadata = SOURCE.getMetadataForNonGeographicalRegion(800); - assertEquals(intlMetadata.getId(), "001"); - assertTrue(intlMetadata.hasGeneralDesc()); - } - - public void testNonGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception { - try { - MISSING_FILE_SOURCE.getMetadataForNonGeographicalRegion(800); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file")); - } - } -} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 641bd77d..6a466e43 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -16,6 +16,8 @@ package com.google.i18n.phonenumbers; +import static org.junit.Assert.assertThrows; + import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; import com.google.i18n.phonenumbers.PhoneNumberUtil.ValidationResult; @@ -25,9 +27,13 @@ import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; +import com.google.i18n.phonenumbers.metadata.source.MetadataSource; import java.util.ArrayList; import java.util.List; import java.util.Set; +import org.junit.Assert; +import org.junit.function.ThrowingRunnable; +import org.mockito.Mockito; /** * Unit tests for PhoneNumberUtil.java @@ -55,6 +61,8 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { new PhoneNumber().setCountryCode(1).setNationalNumber(2423570000L); private static final PhoneNumber BS_NUMBER = new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L); + private static final PhoneNumber CO_FIXED_LINE = + new PhoneNumber().setCountryCode(57).setNationalNumber(6012345678L); // Note that this is the same as the example number for DE in the metadata. private static final PhoneNumber DE_NUMBER = new PhoneNumber().setCountryCode(49).setNationalNumber(30123456L); @@ -119,6 +127,11 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { private static final PhoneNumber UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT = new PhoneNumber().setCountryCode(2).setNationalNumber(12345L); + private final MetadataSource mockedMetadataSource = Mockito.mock(MetadataSource.class); + private final PhoneNumberUtil phoneNumberUtilWithMissingMetadata = + new PhoneNumberUtil(mockedMetadataSource, + CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap()); + public void testGetSupportedRegions() { assertTrue(phoneUtil.getSupportedRegions().size() > 0); } @@ -799,6 +812,8 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { public void testFormatNumberForMobileDialing() { // Numbers are normally dialed in national format in-country, and international format from // outside the country. + assertEquals("6012345678", + phoneUtil.formatNumberForMobileDialing(CO_FIXED_LINE, RegionCode.CO, false)); assertEquals("030123456", phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.DE, false)); assertEquals("+4930123456", @@ -3160,4 +3175,38 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertFalse(phoneUtil.isMobileNumberPortableRegion(RegionCode.AE)); assertFalse(phoneUtil.isMobileNumberPortableRegion(RegionCode.BS)); } + + public void testGetMetadataForRegionForNonGeoEntity_shouldBeNull() { + assertNull(phoneUtil.getMetadataForRegion(RegionCode.UN001)); + } + + public void testGetMetadataForRegionForUnknownRegion_shouldBeNull() { + assertNull(phoneUtil.getMetadataForRegion(RegionCode.ZZ)); + } + + public void testGetMetadataForNonGeographicalRegionForGeoRegion_shouldBeNull() { + assertNull(phoneUtil.getMetadataForNonGeographicalRegion(/* countryCallingCode = */ 1)); + } + + public void testGetMetadataForRegionForMissingMetadata() { + assertThrows( + MissingMetadataException.class, + new ThrowingRunnable() { + @Override + public void run() { + phoneNumberUtilWithMissingMetadata.getMetadataForRegion(RegionCode.US); + } + }); + } + + public void testGetMetadataForNonGeographicalRegionForMissingMetadata() { + assertThrows( + MissingMetadataException.class, + new ThrowingRunnable() { + @Override + public void run() { + phoneNumberUtilWithMissingMetadata.getMetadataForNonGeographicalRegion(800); + } + }); + } } diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java b/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java index 32169f6a..20fc1212 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java @@ -37,6 +37,7 @@ final class RegionCode { static final String CH = "CH"; static final String CL = "CL"; static final String CN = "CN"; + static final String CO = "CO"; static final String CS = "CS"; static final String CX = "CX"; static final String DE = "DE"; diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java deleted file mode 100644 index 664fc52d..00000000 --- a/libphonenumber/test/com/google/i18n/phonenumbers/SingleFileMetadataSourceImplTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2015 The Libphonenumber Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.i18n.phonenumbers; - -import junit.framework.TestCase; - -/** - * Unit tests for SingleFileMetadataSourceImpl.java. - * - * <p> - * We do not package single file metadata files, so it is only possible to test failures here. - */ -public class SingleFileMetadataSourceImplTest extends TestCase { - private static final SingleFileMetadataSourceImpl MISSING_FILE_SOURCE = - new SingleFileMetadataSourceImpl("no/such/file", MetadataManager.DEFAULT_METADATA_LOADER); - - public void testGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception { - try { - MISSING_FILE_SOURCE.getMetadataForRegion("AE"); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file")); - } - } - - public void testNonGeoPhoneNumberMetadataLoadFromMissingFileThrowsException() throws Exception { - try { - MISSING_FILE_SOURCE.getMetadataForNonGeographicalRegion(800); - fail("expected exception"); - } catch (RuntimeException e) { - assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file")); - } - } -} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java b/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java index 51360d71..5dfb56e9 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java @@ -16,6 +16,9 @@ package com.google.i18n.phonenumbers; +import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider; +import com.google.i18n.phonenumbers.metadata.source.MetadataSourceImpl; +import com.google.i18n.phonenumbers.metadata.source.MultiFileModeFileNameProvider; import junit.framework.TestCase; /** @@ -33,15 +36,20 @@ import junit.framework.TestCase; * @author Shaopeng Jia */ public class TestMetadataTestCase extends TestCase { + private static final String TEST_METADATA_FILE_PREFIX = "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting"; - /** An instance of PhoneNumberUtil that uses test metadata. */ + /** + * An instance of PhoneNumberUtil that uses test metadata. + */ protected final PhoneNumberUtil phoneUtil; public TestMetadataTestCase() { - phoneUtil = new PhoneNumberUtil(new MultiFileMetadataSourceImpl(TEST_METADATA_FILE_PREFIX, - MetadataManager.DEFAULT_METADATA_LOADER), + phoneUtil = new PhoneNumberUtil( + new MetadataSourceImpl(new MultiFileModeFileNameProvider(TEST_METADATA_FILE_PREFIX), + DefaultMetadataDependenciesProvider.getInstance().getMetadataLoader(), + DefaultMetadataDependenciesProvider.getInstance().getMetadataParser()), CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap()); } diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 Binary files differindex ac762db9..3749cffd 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 Binary files differindex 82a8364e..85a516c0 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 Binary files differindex 24f6ff3e..a02d0c5f 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD Binary files differindex c201182e..38a89be2 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE Binary files differindex a4b965e4..06a87ce8 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM Binary files differindex cb84cce2..f04b0a33 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO Binary files differindex 649be0b0..36bfe0b1 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR Binary files differindex 1d244d93..f734f82e 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU Binary files differindex f93cc81c..967b9fa5 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB Binary files differindex 59e3737e..6f40a0d6 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR Binary files differindex 28a45b7a..94915a1a 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS Binary files differindex c5cdbfa5..fd9ae5e7 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY Binary files differindex b004fd7b..0584116d 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA Binary files differindex 08350420..970d83b8 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC Binary files differindex a16aa16d..c7e2d589 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN Binary files differindex f362e470..0a918b19 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CO b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CO Binary files differnew file mode 100644 index 00000000..9fdc0a87 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CO diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX Binary files differindex 50f33900..f80f054d 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE Binary files differindex 3bee3616..1beef43c 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR Binary files differindex f200d980..9727be52 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB Binary files differindex c6839a6b..f5ede480 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG Binary files differindex 03f9f5da..390aafc0 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU Binary files differdeleted file mode 100644 index 8e7a77b1..00000000 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU +++ /dev/null diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT Binary files differindex 1d5d920a..f95cb405 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP Binary files differindex c6f8d98c..a5450776 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR Binary files differindex 17851cc2..279f0824 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX Binary files differindex fa8d8c4f..02acf106 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ Binary files differindex d4283c64..e3b91cf6 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL Binary files differindex 0b3d761b..185b0df9 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE Binary files differindex 00ecf8de..3f2f9266 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RU b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RU Binary files differindex 15ed3310..e49aa0e5 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RU +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RU diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE Binary files differindex a14912a4..662dd2e7 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG Binary files differindex bf336608..81e910b5 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA Binary files differindex 8b266466..b6284c33 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US Binary files differindex 88f9ab1b..adc28d54 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_UZ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_UZ Binary files differindex 758c8b5a..c1fb79bf 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_UZ +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_UZ diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT Binary files differindex 20d167d5..80fd0a10 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/internal/GeoEntityUtilityTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/internal/GeoEntityUtilityTest.java new file mode 100644 index 00000000..300c5f53 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/internal/GeoEntityUtilityTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.internal; + +import junit.framework.TestCase; + +public class GeoEntityUtilityTest extends TestCase { + + public void test_isGeoEntity_shouldReturnTrueForCountryRegionCode() { + assertTrue(GeoEntityUtility.isGeoEntity("DE")); + } + + public void test_isGeoEntity_shouldReturnFalseForWorldRegionCode() { + assertFalse(GeoEntityUtility.isGeoEntity("001")); + } + + public void test_isGeoEntity_shouldReturnTrueForCountryCallingCode() { + assertTrue(GeoEntityUtility.isGeoEntity(41)); + } + + public void test_isGeoEntity_shouldReturnFalseForInternationalSharedCostServiceCallingCode() { + assertFalse(GeoEntityUtility.isGeoEntity(808)); + } + + public void test_isGeoEntity_shouldReturnFalseForNonExistingCountryCallingCode() { + assertFalse(GeoEntityUtility.isGeoEntity(111111111)); + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/PhoneMetadataCollectionUtil.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/PhoneMetadataCollectionUtil.java new file mode 100644 index 00000000..57fab191 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/PhoneMetadataCollectionUtil.java @@ -0,0 +1,21 @@ +package com.google.i18n.phonenumbers.metadata; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; + +public class PhoneMetadataCollectionUtil { + + public static InputStream toInputStream(PhoneMetadataCollection metadata) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); + metadata.writeExternal(objectOutputStream); + objectOutputStream.flush(); + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + objectOutputStream.close(); + return inputStream; + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/init/MetadataParserTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/init/MetadataParserTest.java new file mode 100644 index 00000000..22c52e29 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/init/MetadataParserTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.init; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertThrows; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.metadata.PhoneMetadataCollectionUtil; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import junit.framework.TestCase; +import org.junit.function.ThrowingRunnable; + +public final class MetadataParserTest extends TestCase { + + private static final MetadataParser metadataParser = MetadataParser.newStrictParser(); + + public void test_parse_shouldThrowExceptionForNullInput() { + assertThrows( + IllegalArgumentException.class, + new ThrowingRunnable() { + @Override + public void run() { + metadataParser.parse(null); + } + }); + } + + public void test_parse_shouldThrowExceptionForEmptyInput() { + final InputStream emptyInput = new ByteArrayInputStream(new byte[0]); + + assertThrows( + IllegalStateException.class, + new ThrowingRunnable() { + @Override + public void run() { + metadataParser.parse(emptyInput); + } + }); + } + + public void test_parse_shouldThrowExceptionForInvalidInput() { + final InputStream invalidInput = new ByteArrayInputStream("Some random input".getBytes(UTF_8)); + + assertThrows( + IllegalStateException.class, + new ThrowingRunnable() { + @Override + public void run() { + metadataParser.parse(invalidInput); + } + }); + } + + public void test_parse_shouldParseValidInput() throws IOException { + InputStream input = PhoneMetadataCollectionUtil.toInputStream( + PhoneMetadataCollection.newBuilder() + .addMetadata(PhoneMetadata.newBuilder().setId("id").build())); + + Collection<PhoneMetadata> actual = metadataParser.parse(input); + + assertEquals(1, actual.size()); + } + + public void test_parse_shouldReturnEmptyCollectionForNullInput() { + Collection<PhoneMetadata> actual = MetadataParser.newLenientParser().parse(null); + + assertTrue(actual.isEmpty()); + } +}
\ No newline at end of file diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/BlockingMetadataBootstrappingGuardTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/BlockingMetadataBootstrappingGuardTest.java new file mode 100644 index 00000000..c2917708 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/BlockingMetadataBootstrappingGuardTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.source; + +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.i18n.phonenumbers.MetadataLoader; +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.metadata.PhoneMetadataCollectionUtil; +import com.google.i18n.phonenumbers.metadata.init.MetadataParser; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.function.ThrowingRunnable; +import org.mockito.Mockito; + +public class BlockingMetadataBootstrappingGuardTest extends TestCase { + + private static final String PHONE_METADATA_FILE = "some metadata file"; + private static final PhoneMetadataCollection PHONE_METADATA = + PhoneMetadataCollection.newBuilder() + .addMetadata(PhoneMetadata.newBuilder().setId("id").build()); + + private final MetadataLoader metadataLoader = Mockito.mock(MetadataLoader.class); + private final MetadataContainer metadataContainer = Mockito.mock(MetadataContainer.class); + + private BlockingMetadataBootstrappingGuard<MetadataContainer> bootstrappingGuard; + + @Override + public void setUp() throws IOException { + when(metadataLoader.loadMetadata(PHONE_METADATA_FILE)) + .thenReturn(PhoneMetadataCollectionUtil.toInputStream(PHONE_METADATA)); + bootstrappingGuard = + new BlockingMetadataBootstrappingGuard<>( + metadataLoader, MetadataParser.newStrictParser(), metadataContainer); + } + + public void test_getOrBootstrap_shouldInvokeBootstrappingOnlyOnce() { + bootstrappingGuard.getOrBootstrap(PHONE_METADATA_FILE); + bootstrappingGuard.getOrBootstrap(PHONE_METADATA_FILE); + + verify(metadataLoader, times(1)).loadMetadata(PHONE_METADATA_FILE); + } + + public void test_getOrBootstrap_shouldIncludeFileNameInExceptionOnFailure() { + when(metadataLoader.loadMetadata(PHONE_METADATA_FILE)).thenReturn(null); + + ThrowingRunnable throwingRunnable = + new ThrowingRunnable() { + @Override + public void run() { + bootstrappingGuard.getOrBootstrap(PHONE_METADATA_FILE); + } + }; + + IllegalStateException exception = assertThrows(IllegalStateException.class, throwingRunnable); + Assert.assertTrue(exception.getMessage().contains(PHONE_METADATA_FILE)); + } + + public void test_getOrBootstrap_shouldInvokeBootstrappingOnlyOnceWhenThreadsCallItAtTheSameTime() + throws InterruptedException { + ExecutorService executorService = Executors.newFixedThreadPool(2); + + List<BootstrappingRunnable> runnables = new ArrayList<>(); + runnables.add(new BootstrappingRunnable()); + runnables.add(new BootstrappingRunnable()); + executorService.invokeAll(runnables); + + verify(metadataLoader, times(1)).loadMetadata(PHONE_METADATA_FILE); + } + + private class BootstrappingRunnable implements Callable<MetadataContainer> { + + @Override + public MetadataContainer call() { + return bootstrappingGuard.getOrBootstrap(PHONE_METADATA_FILE); + } + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/CompositeMetadataContainerTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/CompositeMetadataContainerTest.java new file mode 100644 index 00000000..75b66c49 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/CompositeMetadataContainerTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.source; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.internal.GeoEntityUtility; +import junit.framework.TestCase; + +public class CompositeMetadataContainerTest extends TestCase { + + private static final String REGION_CODE = "US"; + private static final Integer COUNTRY_CODE = 1; + private static final PhoneMetadata PHONE_METADATA_WITH_REGION_CODE = + PhoneMetadata.newBuilder().setId(REGION_CODE).setCountryCode(COUNTRY_CODE); + private static final PhoneMetadata PHONE_METADATA_WITH_COUNTRY_CODE = + PhoneMetadata.newBuilder() + .setId(GeoEntityUtility.REGION_CODE_FOR_NON_GEO_ENTITIES) + .setCountryCode(COUNTRY_CODE); + + private CompositeMetadataContainer metadataContainer; + + @Override + public void setUp() { + metadataContainer = new CompositeMetadataContainer(); + } + + public void test_getMetadataBy_shouldReturnNullForNonExistingRegionCode() { + assertNull(metadataContainer.getMetadataBy(REGION_CODE)); + } + + public void test_getMetadataBy_shouldReturnMetadataForExistingRegionCode() { + metadataContainer.accept(PHONE_METADATA_WITH_REGION_CODE); + + assertSame(PHONE_METADATA_WITH_REGION_CODE, metadataContainer.getMetadataBy(REGION_CODE)); + } + + public void test_getMetadataBy_shouldReturnNullForNonExistingCountryCode() { + assertNull(metadataContainer.getMetadataBy(COUNTRY_CODE)); + } + + public void test_getMetadataBy_shouldReturnMetadataForExistingCountryCode() { + metadataContainer.accept(PHONE_METADATA_WITH_COUNTRY_CODE); + + assertSame(PHONE_METADATA_WITH_COUNTRY_CODE, metadataContainer.getMetadataBy(COUNTRY_CODE)); + } + + public void test_getMetadataBy_shouldReturnNullForExistingCountryCodeOfGeoRegion() { + metadataContainer.accept(PHONE_METADATA_WITH_REGION_CODE); + + assertNull(metadataContainer.getMetadataBy(COUNTRY_CODE)); + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MapBackedMetadataContainerTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MapBackedMetadataContainerTest.java new file mode 100644 index 00000000..21fbea64 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MapBackedMetadataContainerTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.source; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import junit.framework.TestCase; + +public class MapBackedMetadataContainerTest extends TestCase { + + private static final String REGION_CODE = "US"; + private static final Integer COUNTRY_CODE = 41; + private static final PhoneMetadata PHONE_METADATA = + PhoneMetadata.newBuilder().setId(REGION_CODE).setCountryCode(COUNTRY_CODE); + + public void test_getMetadataBy_shouldReturnNullForNullRegionCode() { + assertNull(MapBackedMetadataContainer.byRegionCode().getMetadataBy(null)); + } + + public void test_getMetadataBy_shouldReturnNullForNonExistingRegionCode() { + assertNull(MapBackedMetadataContainer.byRegionCode().getMetadataBy(REGION_CODE)); + } + + public void test_getMetadataBy_shouldReturnMetadataForExistingRegionCode() { + MapBackedMetadataContainer<String> metadataContainer = + MapBackedMetadataContainer.byRegionCode(); + + metadataContainer.accept(PHONE_METADATA); + + assertSame(PHONE_METADATA, metadataContainer.getMetadataBy(REGION_CODE)); + } + + public void test_getMetadataBy_shouldReturnNullForNullCountryCode() { + assertNull(MapBackedMetadataContainer.byCountryCallingCode().getMetadataBy(null)); + } + + public void test_getMetadataBy_shouldReturnNullForNonExistingCountryCode() { + assertNull(MapBackedMetadataContainer.byCountryCallingCode().getMetadataBy(COUNTRY_CODE)); + } + + public void test_getMetadataBy_shouldReturnMetadataForExistingCountryCode() { + MapBackedMetadataContainer<Integer> metadataContainer = + MapBackedMetadataContainer.byCountryCallingCode(); + + metadataContainer.accept(PHONE_METADATA); + + assertSame(PHONE_METADATA, metadataContainer.getMetadataBy(COUNTRY_CODE)); + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MultiFileModeFileNameProviderTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MultiFileModeFileNameProviderTest.java new file mode 100644 index 00000000..c7ad7ddc --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/MultiFileModeFileNameProviderTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.source; + +import static org.junit.Assert.assertThrows; + +import junit.framework.TestCase; +import org.junit.function.ThrowingRunnable; + +public final class MultiFileModeFileNameProviderTest extends TestCase { + + private final PhoneMetadataFileNameProvider metadataFileNameProvider = + new MultiFileModeFileNameProvider("some/file"); + + public void test_getFor_shouldAppendKeyToTheBase() { + String metadataFileName = metadataFileNameProvider.getFor("key1"); + + assertEquals("some/file_key1", metadataFileName); + } + + public void test_getFor_shouldThrowExceptionForNonAlphanumericKey() { + assertThrows( + IllegalArgumentException.class, + new ThrowingRunnable() { + @Override + public void run() { + metadataFileNameProvider.getFor("\tkey1\n"); + } + }); + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/SingleFileModeFileNameProviderTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/SingleFileModeFileNameProviderTest.java new file mode 100644 index 00000000..21d3bf75 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/metadata/source/SingleFileModeFileNameProviderTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.metadata.source; + +import junit.framework.TestCase; + +public final class SingleFileModeFileNameProviderTest extends TestCase { + + private final PhoneMetadataFileNameProvider metadataFileNameProvider = + new SingleFileModeFileNameProvider("some/file"); + + public void test_getFor_shouldReturnTheFileNameBase() { + String metadataFileName = metadataFileNameProvider.getFor("key1"); + + assertEquals("some/file", metadataFileName); + } +} |