public void testFormatByPattern() {
PhoneNumber usNumber = new PhoneNumber();
usNumber.setCountryCode(1).setNationalNumber(6502530000L);
NumberFormat newNumFormat = new NumberFormat();
newNumFormat.setPattern("(\\d{3})(\\d{3})(\\d{4})");
newNumFormat.setFormat("($1) $2-$3");
List<NumberFormat> newNumberFormats = new ArrayList<NumberFormat>();
newNumberFormats.add(newNumFormat);
assertEquals("(650) 253-0000", phoneUtil.formatByPattern(usNumber, PhoneNumberFormat.NATIONAL,
newNumberFormats));
assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(usNumber,
PhoneNumberFormat.INTERNATIONAL,
newNumberFormats));
// $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are
// followed.
newNumFormat.setNationalPrefixFormattingRule("$NP ($FG)");
newNumFormat.setFormat("$1 $2-$3");
PhoneNumber bsNumber = new PhoneNumber();
bsNumber.setCountryCode(1).setNationalNumber(4168819999L);
assertEquals("1 (416) 881-9999",
phoneUtil.formatByPattern(bsNumber, PhoneNumberFormat.NATIONAL, newNumberFormats));
assertEquals("+1 416 881-9999",
phoneUtil.formatByPattern(bsNumber, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats));
PhoneNumber itNumber = new PhoneNumber();
itNumber.setCountryCode(39).setNationalNumber(236618300L).setItalianLeadingZero(true);
newNumFormat.setPattern("(\\d{2})(\\d{5})(\\d{3})");
newNumFormat.setFormat("$1-$2 $3");
newNumberFormats.set(0, newNumFormat);
assertEquals("02-36618 300",
phoneUtil.formatByPattern(itNumber, PhoneNumberFormat.NATIONAL, newNumberFormats));
assertEquals("+39 02-36618 300",
phoneUtil.formatByPattern(itNumber, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats));
PhoneNumber gbNumber = new PhoneNumber();
gbNumber.setCountryCode(44).setNationalNumber(2012345678L);
newNumFormat.setNationalPrefixFormattingRule("$NP$FG");
newNumFormat.setPattern("(\\d{2})(\\d{4})(\\d{4})");
newNumFormat.setFormat("$1 $2 $3");
newNumberFormats.set(0, newNumFormat);
assertEquals("020 1234 5678",
phoneUtil.formatByPattern(gbNumber, PhoneNumberFormat.NATIONAL, newNumberFormats));
newNumFormat.setNationalPrefixFormattingRule("($NP$FG)");
assertEquals("(020) 1234 5678",
phoneUtil.formatByPattern(gbNumber, PhoneNumberFormat.NATIONAL, newNumberFormats));
newNumFormat.setNationalPrefixFormattingRule("");
assertEquals("20 1234 5678",
phoneUtil.formatByPattern(gbNumber, PhoneNumberFormat.NATIONAL, newNumberFormats));
newNumFormat.setNationalPrefixFormattingRule("");
assertEquals("+44 20 1234 5678",
phoneUtil.formatByPattern(gbNumber, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats));
}