| @ -1,233 +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 com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.ObjectInputStream; | |||
| import java.util.Collections; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Set; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import java.util.concurrent.atomic.AtomicReference; | |||
| import java.util.logging.Level; | |||
| import java.util.logging.Logger; | |||
| /** | |||
| * Manager for loading metadata for alternate formats and short numbers. We also declare some | |||
| * constants for phone number metadata loading, to more easily maintain all three types of metadata | |||
| * together. | |||
| * TODO: Consider managing phone number metadata loading here too. | |||
| */ | |||
| final class MetadataManager { | |||
| static final String MULTI_FILE_PHONE_NUMBER_METADATA_FILE_PREFIX = | |||
| "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto"; | |||
| static final String SINGLE_FILE_PHONE_NUMBER_METADATA_FILE_NAME = | |||
| "/com/google/i18n/phonenumbers/data/SingleFilePhoneNumberMetadataProto"; | |||
| private static final String ALTERNATE_FORMATS_FILE_PREFIX = | |||
| "/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto"; | |||
| private static final String SHORT_NUMBER_METADATA_FILE_PREFIX = | |||
| "/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto"; | |||
| static final MetadataLoader DEFAULT_METADATA_LOADER = new MetadataLoader() { | |||
| @Override | |||
| public InputStream loadMetadata(String metadataFileName) { | |||
| return MetadataManager.class.getResourceAsStream(metadataFileName); | |||
| } | |||
| }; | |||
| private static final Logger logger = Logger.getLogger(MetadataManager.class.getName()); | |||
| // A mapping from a country calling code to the alternate formats for that country calling code. | |||
| private static final ConcurrentHashMap<Integer, PhoneMetadata> alternateFormatsMap = | |||
| new ConcurrentHashMap<Integer, PhoneMetadata>(); | |||
| // A mapping from a region code to the short number metadata for that region code. | |||
| private static final ConcurrentHashMap<String, PhoneMetadata> shortNumberMetadataMap = | |||
| new ConcurrentHashMap<String, PhoneMetadata>(); | |||
| // The set of country calling codes for which there are alternate formats. For every country | |||
| // calling code in this set there should be metadata linked into the resources. | |||
| private static final Set<Integer> alternateFormatsCountryCodes = | |||
| AlternateFormatsCountryCodeSet.getCountryCodeSet(); | |||
| // The set of region codes for which there are short number metadata. For every region code in | |||
| // this set there should be metadata linked into the resources. | |||
| private static final Set<String> shortNumberMetadataRegionCodes = | |||
| ShortNumbersRegionCodeSet.getRegionCodeSet(); | |||
| private MetadataManager() {} | |||
| static PhoneMetadata getAlternateFormatsForCountry(int countryCallingCode) { | |||
| if (!alternateFormatsCountryCodes.contains(countryCallingCode)) { | |||
| return null; | |||
| } | |||
| return getMetadataFromMultiFilePrefix(countryCallingCode, alternateFormatsMap, | |||
| ALTERNATE_FORMATS_FILE_PREFIX, DEFAULT_METADATA_LOADER); | |||
| } | |||
| static PhoneMetadata getShortNumberMetadataForRegion(String regionCode) { | |||
| if (!shortNumberMetadataRegionCodes.contains(regionCode)) { | |||
| return null; | |||
| } | |||
| return getMetadataFromMultiFilePrefix(regionCode, shortNumberMetadataMap, | |||
| SHORT_NUMBER_METADATA_FILE_PREFIX, DEFAULT_METADATA_LOADER); | |||
| } | |||
| static Set<String> getSupportedShortNumberRegions() { | |||
| return Collections.unmodifiableSet(shortNumberMetadataRegionCodes); | |||
| } | |||
| /** | |||
| * @param key the lookup key for the provided map, typically a region code or a country calling | |||
| * code | |||
| * @param map the map containing mappings of already loaded metadata from their {@code key}. If | |||
| * this {@code key}'s metadata isn't already loaded, it will be added to this map after | |||
| * loading | |||
| * @param filePrefix the prefix of the file to load metadata from | |||
| * @param metadataLoader the metadata loader used to inject alternative metadata sources | |||
| */ | |||
| static <T> PhoneMetadata getMetadataFromMultiFilePrefix(T key, | |||
| ConcurrentHashMap<T, PhoneMetadata> map, String filePrefix, MetadataLoader metadataLoader) { | |||
| PhoneMetadata metadata = map.get(key); | |||
| if (metadata != null) { | |||
| return metadata; | |||
| } | |||
| // We assume key.toString() is well-defined. | |||
| String fileName = filePrefix + "_" + key; | |||
| List<PhoneMetadata> metadataList = getMetadataFromSingleFileName(fileName, metadataLoader); | |||
| if (metadataList.size() > 1) { | |||
| logger.log(Level.WARNING, "more than one metadata in file " + fileName); | |||
| } | |||
| metadata = metadataList.get(0); | |||
| PhoneMetadata oldValue = map.putIfAbsent(key, metadata); | |||
| return (oldValue != null) ? oldValue : metadata; | |||
| } | |||
| // Loader and holder for the metadata maps loaded from a single file. | |||
| static class SingleFileMetadataMaps { | |||
| static SingleFileMetadataMaps load(String fileName, MetadataLoader metadataLoader) { | |||
| List<PhoneMetadata> metadataList = getMetadataFromSingleFileName(fileName, metadataLoader); | |||
| Map<String, PhoneMetadata> regionCodeToMetadata = new HashMap<String, PhoneMetadata>(); | |||
| Map<Integer, PhoneMetadata> countryCallingCodeToMetadata = | |||
| new HashMap<Integer, PhoneMetadata>(); | |||
| for (PhoneMetadata metadata : metadataList) { | |||
| String regionCode = metadata.getId(); | |||
| if (PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode)) { | |||
| // regionCode belongs to a non-geographical entity. | |||
| countryCallingCodeToMetadata.put(metadata.getCountryCode(), metadata); | |||
| } else { | |||
| regionCodeToMetadata.put(regionCode, metadata); | |||
| } | |||
| } | |||
| return new SingleFileMetadataMaps(regionCodeToMetadata, countryCallingCodeToMetadata); | |||
| } | |||
| // A map from a region code to the PhoneMetadata for that region. | |||
| // For phone number metadata, the region code "001" is excluded, since that is used for the | |||
| // non-geographical phone number entities. | |||
| private final Map<String, PhoneMetadata> regionCodeToMetadata; | |||
| // A map from a country calling code to the PhoneMetadata for that country calling code. | |||
| // Examples of the country calling codes include 800 (International Toll Free Service) and 808 | |||
| // (International Shared Cost Service). | |||
| // For phone number metadata, only the non-geographical phone number entities' country calling | |||
| // codes are present. | |||
| private final Map<Integer, PhoneMetadata> countryCallingCodeToMetadata; | |||
| private SingleFileMetadataMaps(Map<String, PhoneMetadata> regionCodeToMetadata, | |||
| Map<Integer, PhoneMetadata> countryCallingCodeToMetadata) { | |||
| this.regionCodeToMetadata = Collections.unmodifiableMap(regionCodeToMetadata); | |||
| this.countryCallingCodeToMetadata = Collections.unmodifiableMap(countryCallingCodeToMetadata); | |||
| } | |||
| PhoneMetadata get(String regionCode) { | |||
| return regionCodeToMetadata.get(regionCode); | |||
| } | |||
| PhoneMetadata get(int countryCallingCode) { | |||
| return countryCallingCodeToMetadata.get(countryCallingCode); | |||
| } | |||
| } | |||
| // Manages the atomic reference lifecycle of a SingleFileMetadataMaps encapsulation. | |||
| static SingleFileMetadataMaps getSingleFileMetadataMaps( | |||
| AtomicReference<SingleFileMetadataMaps> ref, String fileName, MetadataLoader metadataLoader) { | |||
| SingleFileMetadataMaps maps = ref.get(); | |||
| if (maps != null) { | |||
| return maps; | |||
| } | |||
| maps = SingleFileMetadataMaps.load(fileName, metadataLoader); | |||
| ref.compareAndSet(null, maps); | |||
| return ref.get(); | |||
| } | |||
| private static List<PhoneMetadata> getMetadataFromSingleFileName(String fileName, | |||
| MetadataLoader metadataLoader) { | |||
| InputStream source = metadataLoader.loadMetadata(fileName); | |||
| if (source == null) { | |||
| // Sanity check; this would only happen if we packaged jars incorrectly. | |||
| throw new IllegalStateException("missing metadata: " + fileName); | |||
| } | |||
| PhoneMetadataCollection metadataCollection = loadMetadataAndCloseInput(source); | |||
| List<PhoneMetadata> metadataList = metadataCollection.getMetadataList(); | |||
| if (metadataList.size() == 0) { | |||
| // Sanity check; this should not happen since we build with non-empty metadata. | |||
| throw new IllegalStateException("empty metadata: " + fileName); | |||
| } | |||
| return metadataList; | |||
| } | |||
| /** | |||
| * Loads and returns the metadata from the given stream and closes the stream. | |||
| * | |||
| * @param source the non-null stream from which metadata is to be read | |||
| * @return the loaded metadata | |||
| */ | |||
| private static PhoneMetadataCollection loadMetadataAndCloseInput(InputStream source) { | |||
| ObjectInputStream ois = null; | |||
| try { | |||
| try { | |||
| ois = new ObjectInputStream(source); | |||
| } catch (IOException e) { | |||
| throw new RuntimeException("cannot load/parse metadata", e); | |||
| } | |||
| PhoneMetadataCollection metadataCollection = new PhoneMetadataCollection(); | |||
| try { | |||
| metadataCollection.readExternal(ois); | |||
| } catch (IOException e) { | |||
| throw new RuntimeException("cannot load/parse metadata", e); | |||
| } | |||
| return metadataCollection; | |||
| } finally { | |||
| try { | |||
| if (ois != null) { | |||
| // This will close all underlying streams as well, including source. | |||
| ois.close(); | |||
| } else { | |||
| source.close(); | |||
| } | |||
| } catch (IOException e) { | |||
| logger.log(Level.WARNING, "error closing input stream (ignored)", e); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @ -1,39 +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; | |||
| /** | |||
| * A source for phone metadata for all regions. | |||
| */ | |||
| interface MetadataSource { | |||
| /** | |||
| * Gets phone metadata for a region. | |||
| * @param regionCode the region code. | |||
| * @return the phone metadata for that region, or null if there is none. | |||
| */ | |||
| PhoneMetadata getMetadataForRegion(String regionCode); | |||
| /** | |||
| * Gets phone metadata for a non-geographical region. | |||
| * @param countryCallingCode the country calling code. | |||
| * @return the phone metadata for that region, or null if there is none. | |||
| */ | |||
| PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode); | |||
| } | |||
| @ -1,86 +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 java.util.List; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| /** | |||
| * Implementation of {@link MetadataSource} that reads from multiple resource files. | |||
| */ | |||
| final class MultiFileMetadataSourceImpl implements MetadataSource { | |||
| // The prefix of the binary files containing phone number metadata for different regions. | |||
| // This enables us to set up with different metadata, such as for testing. | |||
| private final String phoneNumberMetadataFilePrefix; | |||
| // The {@link MetadataLoader} used to inject alternative metadata sources. | |||
| private final MetadataLoader metadataLoader; | |||
| // A mapping from a region code to the phone number metadata for that region code. | |||
| // Unlike the mappings for alternate formats and short number metadata, the phone number metadata | |||
| // is loaded from a non-statically determined file prefix; therefore this map is bound to the | |||
| // instance and not static. | |||
| private final ConcurrentHashMap<String, PhoneMetadata> geographicalRegions = | |||
| new ConcurrentHashMap<String, PhoneMetadata>(); | |||
| // A mapping from a country calling code for a non-geographical entity to the phone number | |||
| // metadata for that country calling code. Examples of the country calling codes include 800 | |||
| // (International Toll Free Service) and 808 (International Shared Cost Service). | |||
| // Unlike the mappings for alternate formats and short number metadata, the phone number metadata | |||
| // is loaded from a non-statically determined file prefix; therefore this map is bound to the | |||
| // instance and not static. | |||
| private final ConcurrentHashMap<Integer, PhoneMetadata> nonGeographicalRegions = | |||
| new ConcurrentHashMap<Integer, PhoneMetadata>(); | |||
| // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. | |||
| // @VisibleForTesting | |||
| MultiFileMetadataSourceImpl(String phoneNumberMetadataFilePrefix, MetadataLoader metadataLoader) { | |||
| this.phoneNumberMetadataFilePrefix = phoneNumberMetadataFilePrefix; | |||
| this.metadataLoader = metadataLoader; | |||
| } | |||
| // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. | |||
| MultiFileMetadataSourceImpl(MetadataLoader metadataLoader) { | |||
| this(MetadataManager.MULTI_FILE_PHONE_NUMBER_METADATA_FILE_PREFIX, metadataLoader); | |||
| } | |||
| @Override | |||
| public PhoneMetadata getMetadataForRegion(String regionCode) { | |||
| return MetadataManager.getMetadataFromMultiFilePrefix(regionCode, geographicalRegions, | |||
| phoneNumberMetadataFilePrefix, metadataLoader); | |||
| } | |||
| @Override | |||
| public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) { | |||
| if (!isNonGeographical(countryCallingCode)) { | |||
| // The given country calling code was for a geographical region. | |||
| return null; | |||
| } | |||
| return MetadataManager.getMetadataFromMultiFilePrefix(countryCallingCode, nonGeographicalRegions, | |||
| phoneNumberMetadataFilePrefix, metadataLoader); | |||
| } | |||
| // A country calling code is non-geographical if it only maps to the non-geographical region code, | |||
| // i.e. "001". | |||
| private boolean isNonGeographical(int countryCallingCode) { | |||
| List<String> regionCodes = | |||
| CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap().get(countryCallingCode); | |||
| return (regionCodes.size() == 1 | |||
| && PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCodes.get(0))); | |||
| } | |||
| } | |||
| @ -1,65 +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 java.util.concurrent.atomic.AtomicReference; | |||
| /** | |||
| * Implementation of {@link MetadataSource} that reads from a single resource file. | |||
| */ | |||
| final class SingleFileMetadataSourceImpl implements MetadataSource { | |||
| // The name of the binary file containing phone number metadata for different regions. | |||
| // This enables us to set up with different metadata, such as for testing. | |||
| private final String phoneNumberMetadataFileName; | |||
| // The {@link MetadataLoader} used to inject alternative metadata sources. | |||
| private final MetadataLoader metadataLoader; | |||
| private final AtomicReference<MetadataManager.SingleFileMetadataMaps> phoneNumberMetadataRef = | |||
| new AtomicReference<MetadataManager.SingleFileMetadataMaps>(); | |||
| // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. | |||
| // @VisibleForTesting | |||
| SingleFileMetadataSourceImpl(String phoneNumberMetadataFileName, MetadataLoader metadataLoader) { | |||
| this.phoneNumberMetadataFileName = phoneNumberMetadataFileName; | |||
| this.metadataLoader = metadataLoader; | |||
| } | |||
| // It is assumed that metadataLoader is not null. Checks should happen before passing it in here. | |||
| SingleFileMetadataSourceImpl(MetadataLoader metadataLoader) { | |||
| this(MetadataManager.SINGLE_FILE_PHONE_NUMBER_METADATA_FILE_NAME, metadataLoader); | |||
| } | |||
| @Override | |||
| public PhoneMetadata getMetadataForRegion(String regionCode) { | |||
| return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef, | |||
| phoneNumberMetadataFileName, metadataLoader).get(regionCode); | |||
| } | |||
| @Override | |||
| public PhoneMetadata getMetadataForNonGeographicalRegion(int countryCallingCode) { | |||
| // A country calling code is non-geographical if it only maps to the non-geographical region | |||
| // code, i.e. "001". If this is not true of the given country calling code, then we will return | |||
| // null here. If not for the atomic reference, such as if we were loading in multiple stages, we | |||
| // would check that the passed in country calling code was indeed non-geographical to avoid | |||
| // loading costs for a null result. Here though we do not check this since the entire data must | |||
| // be loaded anyway if any of it is needed at some point in the life cycle of this class. | |||
| return MetadataManager.getSingleFileMetadataMaps(phoneNumberMetadataRef, | |||
| phoneNumberMetadataFileName, metadataLoader).get(countryCallingCode); | |||
| } | |||
| } | |||
| @ -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")); | |||
| } | |||
| } | |||
| } | |||
| @ -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")); | |||
| } | |||
| } | |||
| } | |||
| @ -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")); | |||
| } | |||
| } | |||
| } | |||