Skip to content

Commit c08f208

Browse files
authored
BAEL-9494: Converting Phone Numbers into International Format (E.164) Using Java (#18948)
1 parent f15e959 commit c08f208

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

libraries-2/src/test/java/com/baeldung/libphonenumber/LibPhoneNumberUnitTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.baeldung.libphonenumber;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
5-
6-
import org.junit.Test;
7-
83
import com.google.i18n.phonenumbers.NumberParseException;
94
import com.google.i18n.phonenumbers.PhoneNumberUtil;
105
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
116
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
127
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.*;
1311

1412
public class LibPhoneNumberUnitTest {
1513

@@ -54,7 +52,7 @@ public void givenPhoneNumber_whenPossibleForType_thenValid() {
5452
public void givenPhoneNumber_whenPossible_thenValid() {
5553
PhoneNumber number = new PhoneNumber();
5654
number.setCountryCode(1)
57-
.setNationalNumber(123000L);
55+
.setNationalNumber(123000L);
5856
assertFalse(phoneNumberUtil.isPossibleNumber(number));
5957
assertFalse(phoneNumberUtil.isPossibleNumber("+1 343 253 00000", "US"));
6058
assertFalse(phoneNumberUtil.isPossibleNumber("(343) 253-00000", "US"));
@@ -69,11 +67,31 @@ public void givenPhoneNumber_whenNumberGeographical_thenValid() throws NumberPar
6967
assertTrue(phoneNumberUtil.isNumberGeographical(phone));
7068

7169
phone = new PhoneNumber().setCountryCode(1)
72-
.setNationalNumber(2530000L);
70+
.setNationalNumber(2530000L);
7371
assertFalse(phoneNumberUtil.isNumberGeographical(phone));
7472

7573
phone = new PhoneNumber().setCountryCode(800)
76-
.setNationalNumber(12345678L);
74+
.setNationalNumber(12345678L);
7775
assertFalse(phoneNumberUtil.isNumberGeographical(phone));
7876
}
77+
78+
@Test
79+
public void givenUSPhoneNumber_whenFormattedToE164_thenReturnsCorrectInternationalFormat() throws NumberParseException {
80+
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
81+
82+
PhoneNumber number = phoneNumberUtil.parse("(415) 555-2671", "US");
83+
String e164Format = phoneNumberUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.E164);
84+
85+
assertEquals("+14155552671", e164Format);
86+
}
87+
88+
@Test
89+
public void givenIndianPhoneNumber_whenFormattedToE164_thenReturnsCorrectInternationalFormat() throws NumberParseException {
90+
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
91+
92+
PhoneNumber number = phoneNumberUtil.parse("09876543210", "IN");
93+
String e164Format = phoneNumberUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.E164);
94+
95+
assertEquals("+919876543210", e164Format);
96+
}
7997
}

0 commit comments

Comments
 (0)