Browse Source

JAVA: Adding unittests for offline geocoding. Also modified build file to produce separate jars.

pull/567/head
Shaopeng Jia 15 years ago
committed by Mihaela Rosca
parent
commit
39c190d178
18 changed files with 335 additions and 41 deletions
  1. +23
    -0
      java/build.xml
  2. +4
    -1
      java/resources/com/google/i18n/phonenumbers/GenerateAreaCodeData.java
  3. +12
    -2
      java/src/com/google/i18n/phonenumbers/AreaCodeMap.java
  4. +1
    -1
      java/src/com/google/i18n/phonenumbers/PhoneNumberOfflineGeocoder.java
  5. +0
    -0
      java/src/com/google/i18n/phonenumbers/geocoding_data/82_zh
  6. +0
    -0
      java/src/com/google/i18n/phonenumbers/geocoding_data/86_zh
  7. BIN
      java/src/com/google/i18n/phonenumbers/geocoding_data/config
  8. +125
    -0
      java/test/com/google/i18n/phonenumbers/AreaCodeMapTest.java
  9. +93
    -0
      java/test/com/google/i18n/phonenumbers/MappingFileProviderTest.java
  10. +45
    -7
      java/test/com/google/i18n/phonenumbers/PhoneNumberOfflineGeocoderTest.java
  11. BIN
      java/test/com/google/i18n/phonenumbers/geocoding_testing_data/82_en
  12. BIN
      java/test/com/google/i18n/phonenumbers/geocoding_testing_data/82_ko
  13. BIN
      java/test/com/google/i18n/phonenumbers/geocoding_testing_data/config
  14. +0
    -0
      resources/geocoding/zh/82.txt
  15. +0
    -0
      resources/geocoding/zh/86.txt
  16. +16
    -5
      resources/test/geocoding/en/82.txt
  17. +16
    -6
      resources/test/geocoding/ko/82.txt
  18. +0
    -19
      resources/test/geocoding/zh_Hant/86.txt

+ 23
- 0
java/build.xml View File

@ -35,13 +35,33 @@
<fileset dir="${classes.dir}">
<include name="**/*.class"/>
<exclude name="**/*Test*"/>
<exclude name="**/AreaCodeMap*"/>
<exclude name="**/BuildMetadata*"/>
<exclude name="**/GenerateAreaCodeData*"/>
<exclude name="**/JSArrayBuilder*"/>
<exclude name="**/MappingFileProvider*"/>
<exclude name="**/PhoneNumberOfflineGeocoder*"/>
</fileset>
<fileset dir="${src.dir}">
<include name="**/PhoneNumberMetadataProto*"/>
</fileset>
</jar>
<jar destfile="${jar.dir}/offline-geocoder.jar">
<fileset dir="${classes.dir}">
<include name="**/*.class"/>
<exclude name="**/*Test*"/>
<exclude name="**/AsYouTypeFormatter*"/>
<exclude name="**/BuildMetadata*"/>
<exclude name="**/GenerateAreaCodeData*"/>
<exclude name="**/JSArrayBuilder*"/>
<exclude name="**/PhoneNumberMatch*"/>
</fileset>
<fileset dir="${src.dir}">
<include name="**/PhoneNumberMetadataProto*"/>
</fileset>
<fileset dir="${src.dir}">
<include name="**/geocoding_data/*"/>
</fileset>
</jar>
</target>
@ -61,6 +81,9 @@
<fileset dir="${test.dir}">
<include name="**/PhoneNumberMetadataProtoForTesting*"/>
</fileset>
<fileset dir="${test.dir}">
<include name="**/geocoding_testing_data/*"/>
</fileset>
</jar>
</target>


+ 4
- 1
java/resources/com/google/i18n/phonenumbers/GenerateAreaCodeData.java View File

@ -183,12 +183,15 @@ public class GenerateAreaCodeData {
File[] languageDirectories = inputPath.listFiles();
for (File languageDirectory : languageDirectories) {
if (!languageDirectory.isDirectory()) {
if (!languageDirectory.isDirectory() || languageDirectory.isHidden()) {
continue;
}
File[] countryCodeFiles = languageDirectory.listFiles();
for (File countryCodeFile : countryCodeFiles) {
if (countryCodeFile.isHidden()) {
continue;
}
String countryCodeFileName = countryCodeFile.getName();
int indexOfDot = countryCodeFileName.indexOf('.');
if (indexOfDot == -1) {


+ 12
- 2
java/src/com/google/i18n/phonenumbers/AreaCodeMap.java View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
/**
@ -36,7 +37,7 @@ public class AreaCodeMap implements Externalizable {
private TreeSet<Integer> possibleLengths = new TreeSet<Integer>();
private int[] phoneNumberPrefixes;
private String[] descriptions;
private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
private PhoneNumberUtil phoneUtil;
/**
* Creates an empty {@link AreaCodeMap}. The default constructor is necessary for implementing
@ -44,6 +45,12 @@ public class AreaCodeMap implements Externalizable {
* {@link #readAreaCodeMap(java.util.SortedMap)} or {@link #readExternal(java.io.ObjectInput)}.
*/
public AreaCodeMap() {
phoneUtil = PhoneNumberUtil.getInstance();
}
// @VisibleForTesting
AreaCodeMap(PhoneNumberUtil phoneUtil) {
this.phoneUtil = phoneUtil;
}
/**
@ -115,7 +122,9 @@ public class AreaCodeMap implements Externalizable {
long phonePrefix =
Long.parseLong(number.getCountryCode() + phoneUtil.getNationalSignificantNumber(number));
int currentIndex = numOfEntries - 1;
for (Integer possibleLength : possibleLengths.descendingSet()) {
SortedSet<Integer> currentSetOfLengths = possibleLengths;
while (currentSetOfLengths.size() > 0) {
Integer possibleLength = currentSetOfLengths.last();
String phonePrefixStr = String.valueOf(phonePrefix);
if (phonePrefixStr.length() > possibleLength) {
phonePrefix = Long.parseLong(phonePrefixStr.substring(0, possibleLength));
@ -127,6 +136,7 @@ public class AreaCodeMap implements Externalizable {
if (phonePrefix == phoneNumberPrefixes[currentIndex]) {
return descriptions[currentIndex];
}
currentSetOfLengths = possibleLengths.headSet(possibleLength);
}
return "";
}


+ 1
- 1
java/src/com/google/i18n/phonenumbers/PhoneNumberOfflineGeocoder.java View File

@ -90,7 +90,7 @@ public class PhoneNumberOfflineGeocoder {
ObjectInputStream in;
try {
in = new ObjectInputStream(source);
AreaCodeMap map = new AreaCodeMap();
AreaCodeMap map = new AreaCodeMap(phoneUtil);
map.readExternal(in);
availablePhonePrefixMaps.put(fileName, map);
} catch (IOException e) {


java/src/com/google/i18n/phonenumbers/geocoding_data/82_zh_Hans → java/src/com/google/i18n/phonenumbers/geocoding_data/82_zh View File


java/src/com/google/i18n/phonenumbers/geocoding_data/86_zh_Hans → java/src/com/google/i18n/phonenumbers/geocoding_data/86_zh View File


BIN
java/src/com/google/i18n/phonenumbers/geocoding_data/config View File


+ 125
- 0
java/test/com/google/i18n/phonenumbers/AreaCodeMapTest.java View File

@ -0,0 +1,125 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.i18n.phonenumbers;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Unittests for AreaCodeMap.java
*
* @author Shaopeng Jia
*/
public class AreaCodeMapTest extends TestCase {
private final AreaCodeMap areaCodeMap;
private PhoneNumber number = new PhoneNumber();
private static final Logger LOGGER = Logger.getLogger(AreaCodeMapTest.class.getName());
static final String TEST_META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
public AreaCodeMapTest() {
PhoneNumberUtil.resetInstance();
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(
TEST_META_DATA_FILE_PREFIX,
CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
areaCodeMap = new AreaCodeMap(phoneUtil);
SortedMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
sortedMap.put(1212, "New York");
sortedMap.put(1480, "Arizona");
sortedMap.put(1650, "California");
sortedMap.put(1907, "Alaska");
sortedMap.put(1201664, "Westwood, NJ");
sortedMap.put(1480893, "Phoenix, AZ");
sortedMap.put(1501372, "Little Rock, AR");
sortedMap.put(1626308, "Alhambra, CA");
sortedMap.put(1650345, "San Mateo, CA");
sortedMap.put(1867993, "Dawson, YT");
sortedMap.put(1972480, "Richardson, TX");
areaCodeMap.readAreaCodeMap(sortedMap);
}
public void testLookupInvalidNumber_US() {
// central office code cannot start with 1.
number.setCountryCode(1).setNationalNumber(2121234567L);
assertEquals("New York", areaCodeMap.lookup(number));
}
public void testLookupNumber_NJ() {
number.setCountryCode(1).setNationalNumber(2016641234L);
assertEquals("Westwood, NJ", areaCodeMap.lookup(number));
}
public void testLookupNumber_NY() {
number.setCountryCode(1).setNationalNumber(2126641234L);
assertEquals("New York", areaCodeMap.lookup(number));
}
public void testLookupNumber_CA_1() {
number.setCountryCode(1).setNationalNumber(6503451234L);
assertEquals("San Mateo, CA", areaCodeMap.lookup(number));
}
public void testLookupNumber_CA_2() {
number.setCountryCode(1).setNationalNumber(6502531234L);
assertEquals("California", areaCodeMap.lookup(number));
}
public void testLookupNumberFound_TX() {
number.setCountryCode(1).setNationalNumber(9724801234L);
assertEquals("Richardson, TX", areaCodeMap.lookup(number));
}
public void testLookupNumberNotFound_TX() {
number.setCountryCode(1).setNationalNumber(9724811234L);
assertEquals("", areaCodeMap.lookup(number));
}
public void testLookupNumber_CH() {
number.setCountryCode(41).setNationalNumber(446681300L);
assertEquals("", areaCodeMap.lookup(number));
}
public void testReadWriteExternal() {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
areaCodeMap.writeExternal(objectOutputStream);
objectOutputStream.flush();
AreaCodeMap newAreaCodeMap = new AreaCodeMap();
newAreaCodeMap.readExternal(
new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
assertEquals(areaCodeMap.toString(), newAreaCodeMap.toString());
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
fail();
}
}
}

+ 93
- 0
java/test/com/google/i18n/phonenumbers/MappingFileProviderTest.java View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.i18n.phonenumbers;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Unittests for MappingFileProvider.java
*
* @author Shaopeng Jia
*/
public class MappingFileProviderTest extends TestCase {
private final MappingFileProvider mappingProvider = new MappingFileProvider();
private static final Logger LOGGER = Logger.getLogger(MappingFileProviderTest.class.getName());
public MappingFileProviderTest() {
SortedMap<Integer, Set<String>> mapping = new TreeMap<Integer, Set<String>>();
mapping.put(1, newHashSet("en"));
mapping.put(86, newHashSet("zh", "en", "zh_Hant"));
mapping.put(41, newHashSet("de", "fr", "it", "rm"));
mapping.put(65, newHashSet("en", "zh_Hans", "ms", "ta"));
mappingProvider.readFileConfigs(mapping);
}
private static HashSet<String> newHashSet(String... strings) {
HashSet<String> set = new HashSet<String>();
set.addAll(Arrays.asList(strings));
return set;
}
public void testReadWriteExternal() {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
mappingProvider.writeExternal(objectOutputStream);
objectOutputStream.flush();
MappingFileProvider newMappingProvider = new MappingFileProvider();
newMappingProvider.readExternal(
new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
assertEquals(mappingProvider.toString(), newMappingProvider.toString());
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
fail();
}
}
public void testGetFileName() {
assertEquals("1_en", mappingProvider.getFileName(1, "en", "", ""));
assertEquals("1_en", mappingProvider.getFileName(1, "en", "", "US"));
assertEquals("1_en", mappingProvider.getFileName(1, "en", "", "GB"));
assertEquals("41_de", mappingProvider.getFileName(41, "de", "", "CH"));
assertEquals("", mappingProvider.getFileName(44, "en", "", "GB"));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", ""));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", ""));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", "CN"));
assertEquals("", mappingProvider.getFileName(86, "", "", "CN"));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", "CN"));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "Hans", "SG"));
assertEquals("86_zh", mappingProvider.getFileName(86, "zh", "", "SG"));
assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "", "TW"));
assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "", "HK"));
assertEquals("86_zh_Hant", mappingProvider.getFileName(86, "zh", "Hant", "TW"));
}
}

+ 45
- 7
java/test/com/google/i18n/phonenumbers/PhoneNumberOfflineGeocoderTest.java View File

@ -30,10 +30,22 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
private PhoneNumberOfflineGeocoder geocoder;
static final String TEST_META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
private static final String TEST_MAPPING_DATA_DIRECTORY =
"/com/google/i18n/phonenumbers/geocoding_testing_data/";
// Set up some test numbers to re-use.
private static final PhoneNumber KO_NUMBER1 =
new PhoneNumber().setCountryCode(82).setNationalNumber(22123456L);
private static final PhoneNumber KO_NUMBER2 =
new PhoneNumber().setCountryCode(82).setNationalNumber(322123456L);
private static final PhoneNumber KO_NUMBER3 =
new PhoneNumber().setCountryCode(82).setNationalNumber(6421234567L);
private static final PhoneNumber US_NUMBER1 =
new PhoneNumber().setCountryCode(1).setNationalNumber(6502530000L);
private static final PhoneNumber US_NUMBER2 =
new PhoneNumber().setCountryCode(1).setNationalNumber(6509600000L);
private static final PhoneNumber US_NUMBER3 =
new PhoneNumber().setCountryCode(1).setNationalNumber(2128120000L);
private static final PhoneNumber BS_NUMBER1 =
new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L);
private static final PhoneNumber AU_NUMBER =
@ -46,19 +58,45 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(
TEST_META_DATA_FILE_PREFIX,
CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
geocoder = new PhoneNumberOfflineGeocoder(phoneUtil);
geocoder = new PhoneNumberOfflineGeocoder(TEST_MAPPING_DATA_DIRECTORY, phoneUtil);
}
public void testGetCompactDescriptionForNumber() {
assertEquals("United States",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.ENGLISH));
public void testGetDescriptionForNumberWithNoDataFile() {
// No data file containing mappings for US numbers is available in Chinese for the unittests. As
// a result, the country name of United States in simplified Chinese is returned.
assertEquals("\u7F8E\u56FD",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.SIMPLIFIED_CHINESE));
assertEquals("Stati Uniti",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.ITALIAN));
assertEquals("Bahamas",
geocoder.getDescriptionForNumber(BS_NUMBER1, Locale.ENGLISH));
geocoder.getDescriptionForNumber(BS_NUMBER1, new Locale("en", "US")));
assertEquals("Australia",
geocoder.getDescriptionForNumber(AU_NUMBER, Locale.ENGLISH));
geocoder.getDescriptionForNumber(AU_NUMBER, new Locale("en", "US")));
assertEquals("", geocoder.getDescriptionForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE,
Locale.ENGLISH));
new Locale("en", "US")));
}
public void testGetDescriptionForNumber_en_US() {
assertEquals("CA",
geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US")));
assertEquals("Mountain View, CA",
geocoder.getDescriptionForNumber(US_NUMBER2, new Locale("en", "US")));
assertEquals("New York, NY",
geocoder.getDescriptionForNumber(US_NUMBER3, new Locale("en", "US")));
}
public void testGetDescriptionForKoreanNumber() {
assertEquals("Seoul",
geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.ENGLISH));
assertEquals("Incheon",
geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.ENGLISH));
assertEquals("Jeju",
geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.ENGLISH));
assertEquals("\uC11C\uC6B8",
geocoder.getDescriptionForNumber(KO_NUMBER1, Locale.KOREAN));
assertEquals("\uC778\uCC9C",
geocoder.getDescriptionForNumber(KO_NUMBER2, Locale.KOREAN));
assertEquals("\uC81C\uC8FC",
geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN));
}
}

BIN
java/test/com/google/i18n/phonenumbers/geocoding_testing_data/82_en View File


BIN
java/test/com/google/i18n/phonenumbers/geocoding_testing_data/82_ko View File


BIN
java/test/com/google/i18n/phonenumbers/geocoding_testing_data/config View File


resources/geocoding/zh_Hans/82.txt → resources/geocoding/zh/82.txt View File


resources/geocoding/zh_Hans/86.txt → resources/geocoding/zh/86.txt View File


resources/test/geocoding/zh_Hans/86.txt → resources/test/geocoding/en/82.txt View File


resources/test/geocoding/en/86.txt → resources/test/geocoding/ko/82.txt View File


+ 0
- 19
resources/test/geocoding/zh_Hant/86.txt View File

@ -1,19 +0,0 @@
# Copyright (C) 2011 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
8620|廣州市
8623|重慶市
86311|河北省石家莊市
86553|安徽省蕪湖市
86888|雲南省麗江

Loading…
Cancel
Save