1 /* 2 * Copyright (C) 2011 Google Inc. 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.SortedMap; 27 import java.util.TreeMap; 28 29 /** 30 * Unittests for FlyweightMapStorage.java 31 * 32 * @author Philippe Liard 33 */ 34 public class FlyweightMapStorageTest extends TestCase { 35 private final SortedMap<Integer, String> areaCodeMap = new TreeMap<Integer, String>(); 36 37 public FlyweightMapStorageTest() { 38 areaCodeMap.put(331402, "Paris"); 39 areaCodeMap.put(331434, "Paris"); 40 areaCodeMap.put(334910, "Marseille"); 41 areaCodeMap.put(334911, "Marseille"); 42 } 43 44 public void testReadFromSortedMap() { 45 FlyweightMapStorage mapStorage = new FlyweightMapStorage(); 46 mapStorage.readFromSortedMap(areaCodeMap); 47 48 assertEquals(331402, mapStorage.getPrefix(0)); 49 assertEquals(331434, mapStorage.getPrefix(1)); 50 assertEquals(334910, mapStorage.getPrefix(2)); 51 assertEquals(334911, mapStorage.getPrefix(3)); 52 53 String desc = mapStorage.getDescription(0); 54 assertEquals("Paris", desc); 55 assertTrue(desc == mapStorage.getDescription(1)); // Same identity. 56 57 desc = mapStorage.getDescription(2); 58 assertEquals("Marseille", desc); 59 assertTrue(desc == mapStorage.getDescription(3)); // Same identity. 60 } 61 62 public void testWriteAndReadExternal() throws IOException { 63 FlyweightMapStorage mapStorage = new FlyweightMapStorage(); 64 mapStorage.readFromSortedMap(areaCodeMap); 65 66 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 67 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 68 mapStorage.writeExternal(objectOutputStream); 69 objectOutputStream.flush(); 70 71 FlyweightMapStorage newMapStorage = new FlyweightMapStorage(); 72 ObjectInputStream objectInputStream = 73 new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); 74 newMapStorage.readExternal(objectInputStream); 75 76 String expected = mapStorage.toString(); 77 assertFalse(expected.length() == 0); 78 assertEquals(expected, newMapStorage.toString()); 79 } 80 } 81