1 /* 2 * Copyright (C) 2011 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.android.i18n.phonenumbers.geocoding; 18 19 import junit.framework.TestCase; 20 21 import java.io.ByteArrayInputStream; 22 import java.io.ByteArrayOutputStream; 23 import java.io.IOException; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.util.Arrays; 27 import java.util.HashSet; 28 import java.util.Set; 29 import java.util.SortedMap; 30 import java.util.TreeMap; 31 import java.util.logging.Level; 32 import java.util.logging.Logger; 33 34 /** 35 * Unittests for MappingFileProvider.java 36 * 37 * @author Shaopeng Jia 38 */ 39 public class MappingFileProviderTest extends TestCase { 40 private final MappingFileProvider mappingProvider = new MappingFileProvider(); 41 private static final Logger LOGGER = Logger.getLogger(MappingFileProviderTest.class.getName()); 42 MappingFileProviderTest()43 public MappingFileProviderTest() { 44 SortedMap<Integer, Set<String>> mapping = new TreeMap<Integer, Set<String>>(); 45 mapping.put(1, newHashSet("en")); 46 mapping.put(86, newHashSet("zh", "en", "zh_Hant")); 47 mapping.put(41, newHashSet("de", "fr", "it", "rm")); 48 mapping.put(65, newHashSet("en", "zh_Hans", "ms", "ta")); 49 50 mappingProvider.readFileConfigs(mapping); 51 } 52 newHashSet(String... strings)53 private static HashSet<String> newHashSet(String... strings) { 54 HashSet<String> set = new HashSet<String>(); 55 set.addAll(Arrays.asList(strings)); 56 return set; 57 } 58 testReadWriteExternal()59 public void testReadWriteExternal() { 60 try { 61 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 62 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 63 mappingProvider.writeExternal(objectOutputStream); 64 objectOutputStream.flush(); 65 66 MappingFileProvider newMappingProvider = new MappingFileProvider(); 67 newMappingProvider.readExternal( 68 new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))); 69 assertEquals(mappingProvider.toString(), newMappingProvider.toString()); 70 } catch (IOException e) { 71 LOGGER.log(Level.SEVERE, e.getMessage()); 72 fail(); 73 } 74 } 75 testGetFileName()76 public void testGetFileName() { 77 assertEquals("1_en", mappingProvider.getFileName(1, "en", "", "")); 78 assertEquals("1_en", mappingProvider.getFileName(1, "en", "", "US")); 79 assertEquals("1_en", mappingProvider.getFileName(1, "en", "", "GB")); 80 assertEquals("41_de", mappingProvider.getFileName(41, "de", "", "CH")); 81 assertEquals("", mappingProvider.getFileName(44, "en", "", "GB")); 82 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", "")); 83 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", "")); 84 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", "CN")); 85 assertEquals("", mappingProvider.getFileName(86, "", "", "CN")); 86 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", "CN")); 87 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", "SG")); 88 assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", "SG")); 89 assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "", "TW")); 90 assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "", "HK")); 91 assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "Hant", "TW")); 92 } 93 } 94