Commit 628ef73f authored by Ricki Hirner's avatar Ricki Hirner

Handle IllegalStateExceptions which occur on invalid PREF values

parent 28140042
Pipeline #5611654 passed with stage
in 10 minutes
......@@ -14,6 +14,10 @@ import android.content.ContentProviderClient;
import android.provider.ContactsContract;
import android.support.annotation.RequiresPermission;
import junit.framework.Assert;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -21,6 +25,7 @@ import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import ezvcard.VCardVersion;
......@@ -56,7 +61,7 @@ public class AndroidContactTest {
@Test
public void testAddAndReadContact() throws FileNotFoundException, ContactsStorageException {
public void testAddAndReadContact() throws ContactsStorageException, FileNotFoundException {
Contact vcard = new Contact();
vcard.displayName = "Mya Contact";
vcard.prefix = "Magª";
......@@ -83,7 +88,28 @@ public class AndroidContactTest {
}
@Test
public void testLargeTransactionManyRows() throws FileNotFoundException, ContactsStorageException {
public void testInvalidPREF() throws ContactsStorageException, IOException {
Charset charset = Charsets.UTF_8;
String vCard = "BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"FN:Test\r\n" +
"TEL;CELL=;PREF=:+12345\r\n" +
"EMAIL;PREF=invalid:test@example.com\r\n" +
"END:VCARD\r\n";
Contact[] contacts = Contact.fromStream(IOUtils.toInputStream(vCard, charset), charset, null);
AndroidContact dbContact = new AndroidContact(addressBook, contacts[0], null, null);
dbContact.create();
@Cleanup("delete") AndroidContact dbContact2 = new AndroidContact(addressBook, dbContact.id, null, null);
Contact contact2 = dbContact2.getContact();
assertEquals("Test", contact2.displayName);
assertEquals("+12345", contact2.phoneNumbers.get(0).property.getText());
assertEquals("test@example.com", contact2.emails.get(0).property.getValue());
}
@Test
public void testLargeTransactionManyRows() throws ContactsStorageException, FileNotFoundException {
Contact vcard = new Contact();
vcard.displayName = "Large Transaction (many rows)";
for (int i = 0; i < 4000; i++)
......@@ -98,7 +124,7 @@ public class AndroidContactTest {
}
@Test(expected = ContactsStorageException.class)
public void testLargeTransactionSingleRow() throws FileNotFoundException, ContactsStorageException {
public void testLargeTransactionSingleRow() throws ContactsStorageException {
Contact vcard = new Contact();
vcard.displayName = "Large Transaction (one row which is too large)";
......
......@@ -782,7 +782,13 @@ public class AndroidContact {
List<TelephoneType> types = number.getTypes();
// preferred number?
boolean is_primary = number.getPref() != null;
Integer pref = null;
try {
pref = number.getPref();
} catch(IllegalStateException e) {
Constants.log.log(Level.FINER, "Can't understand phone number PREF", e);
}
boolean is_primary = pref != null;
if (types.contains(TelephoneType.PREF)) {
is_primary = true;
types.remove(TelephoneType.PREF);
......@@ -869,7 +875,13 @@ public class AndroidContact {
List<EmailType> types = email.getTypes();
// preferred email address?
boolean is_primary = email.getPref() != null;
Integer pref = null;
try {
pref = email.getPref();
} catch(IllegalStateException e) {
Constants.log.log(Level.FINER, "Can't understand email PREF", e);
}
boolean is_primary = pref != null;
if (types.contains(EmailType.PREF)) {
is_primary = true;
types.remove(EmailType.PREF);
......
......@@ -16,20 +16,6 @@ import ezvcard.VCard;
public class EzVCardTest {
/*
FAILS, see https://github.com/mangstadt/ez-vcard/issues/76
@Test
public void testInvalidPref() {
VCard vCard = Ezvcard.parse("BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"TEL;CELL=;PREF=:+12345\r\n" +
"END:VCARD").first();
assertEquals("+12345", vCard.getTelephoneNumbers().get(0).getText());
assertNull(vCard.getTelephoneNumbers().get(0).getPref());
}*/
@Test
public void testREV_UTC() {
VCard vCard = Ezvcard.parse("BEGIN:VCARD\r\n" +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment