1 /* 2 * Copyright (C) 2012 The Libphonenumber Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.i18n.phonenumbers; 18 19 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 20 import java.util.concurrent.ConcurrentHashMap; 21 import junit.framework.TestCase; 22 23 /** 24 * Some basic tests to check that metadata can be correctly loaded. 25 */ 26 public class MetadataManagerTest extends TestCase { 27 public void testAlternateFormatsLoadCorrectly() { 28 // We should have some data for Germany. 29 PhoneMetadata germanyMetadata = MetadataManager.getAlternateFormatsForCountry(49); 30 assertNotNull(germanyMetadata); 31 assertTrue(germanyMetadata.numberFormats().size() > 0); 32 } 33 34 public void testAlternateFormatsFailsGracefully() throws Exception { 35 PhoneMetadata noAlternateFormats = MetadataManager.getAlternateFormatsForCountry(999); 36 assertNull(noAlternateFormats); 37 } 38 39 public void testShortNumberMetadataLoadCorrectly() throws Exception { 40 // We should have some data for France. 41 PhoneMetadata franceMetadata = MetadataManager.getShortNumberMetadataForRegion("FR"); 42 assertNotNull(franceMetadata); 43 assertTrue(franceMetadata.hasShortCode()); 44 } 45 46 public void testShortNumberMetadataFailsGracefully() throws Exception { 47 PhoneMetadata noShortNumberMetadata = MetadataManager.getShortNumberMetadataForRegion("XXX"); 48 assertNull(noShortNumberMetadata); 49 } 50 51 public void testGetMetadataFromMultiFilePrefix_regionCode() { 52 ConcurrentHashMap<String, PhoneMetadata> map = new ConcurrentHashMap<String, PhoneMetadata>(); 53 PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix("CA", map, 54 "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting", 55 MetadataManager.DEFAULT_METADATA_LOADER); 56 assertEquals(metadata, map.get("CA")); 57 } 58 59 public void testGetMetadataFromMultiFilePrefix_countryCallingCode() { 60 ConcurrentHashMap<Integer, PhoneMetadata> map = new ConcurrentHashMap<Integer, PhoneMetadata>(); 61 PhoneMetadata metadata = MetadataManager.getMetadataFromMultiFilePrefix(800, map, 62 "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting", 63 MetadataManager.DEFAULT_METADATA_LOADER); 64 assertEquals(metadata, map.get(800)); 65 } 66 67 public void testGetMetadataFromMultiFilePrefix_missingMetadataFileThrowsRuntimeException() { 68 // In normal usage we should never get a state where we are asking to load metadata that doesn't 69 // exist. However if the library is packaged incorrectly in the jar, this could happen and the 70 // best we can do is make sure the exception has the file name in it. 71 try { 72 MetadataManager.getMetadataFromMultiFilePrefix("XX", 73 new ConcurrentHashMap<String, PhoneMetadata>(), "no/such/file", 74 MetadataManager.DEFAULT_METADATA_LOADER); 75 fail("expected exception"); 76 } catch (RuntimeException e) { 77 assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_XX")); 78 } 79 try { 80 MetadataManager.getMetadataFromMultiFilePrefix(123, 81 new ConcurrentHashMap<Integer, PhoneMetadata>(), "no/such/file", 82 MetadataManager.DEFAULT_METADATA_LOADER); 83 fail("expected exception"); 84 } catch (RuntimeException e) { 85 assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_123")); 86 } 87 } 88 } 89