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.Collections; 27 import java.util.SortedMap; 28 import java.util.TreeMap; 29 30 /** 31 * Unittests for FlyweightMapStorage.java 32 * 33 * @author Philippe Liard 34 */ 35 public class FlyweightMapStorageTest extends TestCase { 36 private static final SortedMap<Integer, String> areaCodeMap; 37 static { 38 SortedMap<Integer, String> tmpMap = new TreeMap<Integer, String>(); 39 tmpMap.put(331402, "Paris"); 40 tmpMap.put(331434, "Paris"); 41 tmpMap.put(334910, "Marseille"); 42 tmpMap.put(334911, "Marseille"); 43 tmpMap.put(334912, ""); 44 tmpMap.put(334913, ""); 45 areaCodeMap = Collections.unmodifiableSortedMap(tmpMap); 46 } 47 48 private FlyweightMapStorage mapStorage; 49 50 @Override setUp()51 protected void setUp() throws Exception { 52 mapStorage = new FlyweightMapStorage(); 53 mapStorage.readFromSortedMap(areaCodeMap); 54 } 55 testReadFromSortedMap()56 public void testReadFromSortedMap() { 57 assertEquals(331402, mapStorage.getPrefix(0)); 58 assertEquals(331434, mapStorage.getPrefix(1)); 59 assertEquals(334910, mapStorage.getPrefix(2)); 60 assertEquals(334911, mapStorage.getPrefix(3)); 61 62 assertEquals("Paris", mapStorage.getDescription(0)); 63 assertSame(mapStorage.getDescription(0), mapStorage.getDescription(1)); 64 65 assertEquals("Marseille", mapStorage.getDescription(2)); 66 assertSame(mapStorage.getDescription(2), mapStorage.getDescription(3)); 67 } 68 testReadFromSortedMapSupportsEmptyDescription()69 public void testReadFromSortedMapSupportsEmptyDescription() { 70 assertEquals(334912, mapStorage.getPrefix(4)); 71 assertEquals(334913, mapStorage.getPrefix(5)); 72 73 assertEquals("", mapStorage.getDescription(4)); 74 assertSame(mapStorage.getDescription(4), mapStorage.getDescription(5)); 75 } 76 testWriteAndReadExternal()77 public void testWriteAndReadExternal() throws IOException { 78 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 79 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 80 mapStorage.writeExternal(objectOutputStream); 81 objectOutputStream.flush(); 82 83 FlyweightMapStorage newMapStorage = new FlyweightMapStorage(); 84 ObjectInputStream objectInputStream = 85 new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); 86 newMapStorage.readExternal(objectInputStream); 87 88 String expected = mapStorage.toString(); 89 assertEquals(expected, newMapStorage.toString()); 90 } 91 testReadExternalThrowsIOExceptionWithMalformedData()92 public void testReadExternalThrowsIOExceptionWithMalformedData() throws IOException { 93 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 94 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 95 objectOutputStream.writeUTF("hello"); 96 objectOutputStream.flush(); 97 ObjectInputStream objectInputStream = 98 new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); 99 FlyweightMapStorage newMapStorage = new FlyweightMapStorage(); 100 try { 101 newMapStorage.readExternal(objectInputStream); 102 fail(); 103 } catch (IOException e) { 104 // Exception expected. 105 } 106 } 107 } 108