Browse Source

separate unittests to test directory; modified build.xml

pull/567/head
Shaopeng Jia 16 years ago
committed by Mihaela Rosca
parent
commit
268c2ab07b
6 changed files with 137 additions and 2 deletions
  1. +4
    -2
      java/build.xml
  2. +0
    -0
      java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
  3. +133
    -0
      java/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java
  4. +0
    -0
      java/test/com/google/i18n/phonenumbers/PhoneNumberMetaDataForTesting.xml
  5. +0
    -0
      java/test/com/google/i18n/phonenumbers/PhoneNumberMetadataProtoForTesting
  6. +0
    -0
      java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java

+ 4
- 2
java/build.xml View File

@ -2,6 +2,7 @@
<project name="libphonenumber" default="compile">
<property name="src.dir" value="src"/>
<property name="test.dir" value="test"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
@ -23,6 +24,7 @@
<target name="compile" description="Compile Java source.">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
<javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>
<target name="jar" depends="compile">
@ -45,7 +47,7 @@
<include name="**/*.class"/>
<exclude name="**/*Test*"/>
</fileset>
<fileset dir="${src.dir}">
<fileset dir="${test.dir}">
<include name="**/PhoneNumberMetadataProtoForTesting"/>
</fileset>
</jar>
@ -57,7 +59,7 @@
<classpath refid="test.classpath"/>
<formatter type="xml"/>
<batchtest fork="no" todir="${report.dir}">
<fileset dir="${src.dir}" includes="**/*Test.java"/>
<fileset dir="${test.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>


java/src/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java → java/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java View File


+ 133
- 0
java/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java View File

@ -0,0 +1,133 @@
/*
* Copyright (C) 2009 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.PhoneNumberUtil.PhoneNumberType;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Lara Rennie
*
* Verifies all of the example numbers in the metadata are valid and of the correct type. If no
* example number exists for a particular type, the test still passes.
*/
public class ExampleNumbersTest extends TestCase {
private static final Logger LOGGER = Logger.getLogger(ExampleNumbersTest.class.getName());
private PhoneNumberUtil phoneNumberUtil;
private List<PhoneNumber> invalidCases = new ArrayList<PhoneNumber>();
private List<PhoneNumber> wrongTypeCases = new ArrayList<PhoneNumber>();
public ExampleNumbersTest() {
PhoneNumberUtil.resetInstance();
phoneNumberUtil = PhoneNumberUtil.getInstance();
}
@Override
protected void setUp() throws Exception {
super.setUp();
invalidCases.clear();
wrongTypeCases.clear();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* @param exampleNumberRequestedType type we are requesting an example number for
* @param possibleExpectedTypes acceptable types that this number should match, such as
* FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
*/
private void checkNumbersValidAndCorrectType(PhoneNumberType exampleNumberRequestedType,
Set<PhoneNumberType> possibleExpectedTypes) {
for (String regionCode : phoneNumberUtil.getSupportedCountries()) {
PhoneNumber exampleNumber =
phoneNumberUtil.getExampleNumberForType(regionCode, exampleNumberRequestedType);
if (exampleNumber != null) {
if (!phoneNumberUtil.isValidNumber(exampleNumber)) {
invalidCases.add(exampleNumber);
LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
} else {
// We know the number is valid, now we check the type.
PhoneNumberType exampleNumberType = phoneNumberUtil.getNumberType(exampleNumber);
if (!possibleExpectedTypes.contains(exampleNumberType)) {
wrongTypeCases.add(exampleNumber);
LOGGER.log(Level.SEVERE,
"Wrong type for " + exampleNumber.toString() + ": got " + exampleNumberType);
LOGGER.log(Level.WARNING, "Expected types: ");
for (PhoneNumberType type : possibleExpectedTypes) {
LOGGER.log(Level.WARNING, type.toString());
}
}
}
}
}
}
public void testFixedLine() throws Exception {
Set<PhoneNumberType> fixedLineTypes = EnumSet.of(PhoneNumberType.FIXED_LINE,
PhoneNumberType.FIXED_LINE_OR_MOBILE);
checkNumbersValidAndCorrectType(PhoneNumberType.FIXED_LINE, fixedLineTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
public void testMobile() throws Exception {
Set<PhoneNumberType> mobileTypes = EnumSet.of(PhoneNumberType.MOBILE,
PhoneNumberType.FIXED_LINE_OR_MOBILE);
checkNumbersValidAndCorrectType(PhoneNumberType.MOBILE, mobileTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
public void testTollFree() throws Exception {
Set<PhoneNumberType> tollFreeTypes = EnumSet.of(PhoneNumberType.TOLL_FREE);
checkNumbersValidAndCorrectType(PhoneNumberType.TOLL_FREE, tollFreeTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
public void testPremiumRate() throws Exception {
Set<PhoneNumberType> premiumRateTypes = EnumSet.of(PhoneNumberType.PREMIUM_RATE);
checkNumbersValidAndCorrectType(PhoneNumberType.PREMIUM_RATE, premiumRateTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
public void testVoip() throws Exception {
Set<PhoneNumberType> voipTypes = EnumSet.of(PhoneNumberType.VOIP);
checkNumbersValidAndCorrectType(PhoneNumberType.VOIP, voipTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
public void testSharedCost() throws Exception {
Set<PhoneNumberType> sharedCostTypes = EnumSet.of(PhoneNumberType.SHARED_COST);
checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes);
assertEquals(0, invalidCases.size());
assertEquals(0, wrongTypeCases.size());
}
}

java/src/com/google/i18n/phonenumbers/PhoneNumberMetaDataForTesting.xml → java/test/com/google/i18n/phonenumbers/PhoneNumberMetaDataForTesting.xml View File


java/src/com/google/i18n/phonenumbers/PhoneNumberMetadataProtoForTesting → java/test/com/google/i18n/phonenumbers/PhoneNumberMetadataProtoForTesting View File


java/src/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java → java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File


Loading…
Cancel
Save