1 /* 2 * Copyright (C) 2009 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; 18 19 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; 20 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; 21 22 import junit.framework.TestCase; 23 24 /** 25 * Tests for the Phonenumber.PhoneNumber object itself. 26 * 27 * @author Lara Rennie 28 */ 29 public class PhonenumberTest extends TestCase { 30 testEqualSimpleNumber()31 public void testEqualSimpleNumber() throws Exception { 32 PhoneNumber numberA = new PhoneNumber(); 33 numberA.setCountryCode(1).setNationalNumber(6502530000L); 34 35 PhoneNumber numberB = new PhoneNumber(); 36 numberB.setCountryCode(1).setNationalNumber(6502530000L); 37 38 assertEquals(numberA, numberB); 39 assertEquals(numberA.hashCode(), numberB.hashCode()); 40 } 41 testEqualWithItalianLeadingZeroSetToDefault()42 public void testEqualWithItalianLeadingZeroSetToDefault() throws Exception { 43 PhoneNumber numberA = new PhoneNumber(); 44 numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(false); 45 46 PhoneNumber numberB = new PhoneNumber(); 47 numberB.setCountryCode(1).setNationalNumber(6502530000L); 48 49 // These should still be equal, since the default value for this field is false. 50 assertEquals(numberA, numberB); 51 assertEquals(numberA.hashCode(), numberB.hashCode()); 52 } 53 testEqualWithCountryCodeSourceSet()54 public void testEqualWithCountryCodeSourceSet() throws Exception { 55 PhoneNumber numberA = new PhoneNumber(); 56 numberA.setRawInput("+1 650 253 00 00"). 57 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 58 PhoneNumber numberB = new PhoneNumber(); 59 numberB.setRawInput("+1 650 253 00 00"). 60 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 61 assertEquals(numberA, numberB); 62 assertEquals(numberA.hashCode(), numberB.hashCode()); 63 } 64 testNonEqualWithItalianLeadingZeroSetToTrue()65 public void testNonEqualWithItalianLeadingZeroSetToTrue() throws Exception { 66 PhoneNumber numberA = new PhoneNumber(); 67 numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(true); 68 69 PhoneNumber numberB = new PhoneNumber(); 70 numberB.setCountryCode(1).setNationalNumber(6502530000L); 71 72 assertFalse(numberA.equals(numberB)); 73 assertFalse(numberA.hashCode() == numberB.hashCode()); 74 } 75 testNonEqualWithDifferingRawInput()76 public void testNonEqualWithDifferingRawInput() throws Exception { 77 PhoneNumber numberA = new PhoneNumber(); 78 numberA.setCountryCode(1).setNationalNumber(6502530000L).setRawInput("+1 650 253 00 00"). 79 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 80 81 PhoneNumber numberB = new PhoneNumber(); 82 // Although these numbers would pass an isNumberMatch test, they are not considered "equal" as 83 // objects, since their raw input is different. 84 numberB.setCountryCode(1).setNationalNumber(6502530000L).setRawInput("+1-650-253-00-00"). 85 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 86 87 assertFalse(numberA.equals(numberB)); 88 assertFalse(numberA.hashCode() == numberB.hashCode()); 89 } 90 testNonEqualWithPreferredDomesticCarrierCodeSetToDefault()91 public void testNonEqualWithPreferredDomesticCarrierCodeSetToDefault() throws Exception { 92 PhoneNumber numberA = new PhoneNumber(); 93 numberA.setCountryCode(1).setNationalNumber(6502530000L).setPreferredDomesticCarrierCode(""); 94 95 PhoneNumber numberB = new PhoneNumber(); 96 numberB.setCountryCode(1).setNationalNumber(6502530000L); 97 98 assertFalse(numberA.equals(numberB)); 99 assertFalse(numberA.hashCode() == numberB.hashCode()); 100 } 101 testEqualWithPreferredDomesticCarrierCodeSetToDefault()102 public void testEqualWithPreferredDomesticCarrierCodeSetToDefault() throws Exception { 103 PhoneNumber numberA = new PhoneNumber(); 104 numberA.setCountryCode(1).setNationalNumber(6502530000L).setPreferredDomesticCarrierCode(""); 105 106 PhoneNumber numberB = new PhoneNumber(); 107 numberB.setCountryCode(1).setNationalNumber(6502530000L).setPreferredDomesticCarrierCode(""); 108 109 assertEquals(numberA, numberB); 110 assertEquals(numberA.hashCode(), numberB.hashCode()); 111 } 112 } 113