1f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia/*
2ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia * Copyright (C) 2011 The Libphonenumber Authors
3f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia *
4f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * Licensed under the Apache License, Version 2.0 (the "License");
5f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * you may not use this file except in compliance with the License.
6f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * You may obtain a copy of the License at
7f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia *
8f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * http://www.apache.org/licenses/LICENSE-2.0
9f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia *
10f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * Unless required by applicable law or agreed to in writing, software
11f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * distributed under the License is distributed on an "AS IS" BASIS,
12f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * See the License for the specific language governing permissions and
14f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * limitations under the License.
15f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia */
16f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
17f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jiapackage com.android.i18n.phonenumbers.prefixmapper;
18f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
19f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport junit.framework.TestCase;
20f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
21f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.io.ByteArrayInputStream;
22f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.io.ByteArrayOutputStream;
23f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.io.IOException;
24f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.io.ObjectInputStream;
25f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.io.ObjectOutputStream;
26ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jiaimport java.util.Collections;
27f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.util.SortedMap;
28f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiaimport java.util.TreeMap;
29f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
30f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia/**
31f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * Unittests for FlyweightMapStorage.java
32f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia *
33f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia * @author Philippe Liard
34f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia */
35f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jiapublic class FlyweightMapStorageTest extends TestCase {
36f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia  private static final SortedMap<Integer, String> phonePrefixMap;
37ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  static {
38ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    SortedMap<Integer, String> tmpMap = new TreeMap<Integer, String>();
39ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(331402, "Paris");
40ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(331434, "Paris");
41ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(334910, "Marseille");
42ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(334911, "Marseille");
43ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(334912, "");
44ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    tmpMap.put(334913, "");
45f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia    phonePrefixMap = Collections.unmodifiableSortedMap(tmpMap);
46f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia  }
47f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
48ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  private FlyweightMapStorage mapStorage;
49ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia
50ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  @Override
51ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  protected void setUp() throws Exception {
52ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    mapStorage = new FlyweightMapStorage();
53f9768eb3c8f303725fb4f899598481cbc4fb76a3Shaopeng Jia    mapStorage.readFromSortedMap(phonePrefixMap);
54ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  }
55f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
56ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public void testReadFromSortedMap() {
57f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    assertEquals(331402, mapStorage.getPrefix(0));
58f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    assertEquals(331434, mapStorage.getPrefix(1));
59f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    assertEquals(334910, mapStorage.getPrefix(2));
60f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    assertEquals(334911, mapStorage.getPrefix(3));
61f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
62ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertEquals("Paris", mapStorage.getDescription(0));
63ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertSame(mapStorage.getDescription(0), mapStorage.getDescription(1));
64f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
65ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertEquals("Marseille", mapStorage.getDescription(2));
66ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertSame(mapStorage.getDescription(2), mapStorage.getDescription(3));
67f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia  }
68f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
69ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public void testReadFromSortedMapSupportsEmptyDescription() {
70ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertEquals(334912, mapStorage.getPrefix(4));
71ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertEquals(334913, mapStorage.getPrefix(5));
72ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia
73ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertEquals("", mapStorage.getDescription(4));
74ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    assertSame(mapStorage.getDescription(4), mapStorage.getDescription(5));
75ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  }
76f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
77ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public void testWriteAndReadExternal() throws IOException {
78f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
79f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
80f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    mapStorage.writeExternal(objectOutputStream);
81f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    objectOutputStream.flush();
82f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
83f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    FlyweightMapStorage newMapStorage = new FlyweightMapStorage();
84f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    ObjectInputStream objectInputStream =
85f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia        new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
86f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    newMapStorage.readExternal(objectInputStream);
87f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia
88f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    String expected = mapStorage.toString();
89f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia    assertEquals(expected, newMapStorage.toString());
90f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia  }
91ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia
92ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  public void testReadExternalThrowsIOExceptionWithMalformedData() throws IOException {
93ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
94ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
95ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    objectOutputStream.writeUTF("hello");
96ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    objectOutputStream.flush();
97ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    ObjectInputStream objectInputStream =
98ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia        new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
99ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    FlyweightMapStorage newMapStorage = new FlyweightMapStorage();
100ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    try {
101ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia      newMapStorage.readExternal(objectInputStream);
102ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia      fail();
103ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    } catch (IOException e) {
104ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia      // Exception expected.
105ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia    }
106ca1e43d6e1fac07c7fc29c66c7da1fa9d7cf50f2Shaopeng Jia  }
107f7e0224b862054893f28d2736b3f6804d9935886Shaopeng Jia}
108