Commit 32e2966b authored by Ricki Hirner's avatar Ricki Hirner 🐑

Tests; remove option for RFC 6868

* add tests, especially for company- and group-only contacts
* RFC 6868 (caret encoding) is now always enabled
parent bf48e810
Pipeline #4692043 passed with stage
in 11 minutes and 17 seconds
......@@ -119,7 +119,7 @@ public class AndroidContactTest {
* So, ADR value components may contain DQUOTE (0x22) and don't have to be encoded as defined in RFC 6868 */
ByteArrayOutputStream os = new ByteArrayOutputStream();
contact.write(VCardVersion.V4_0, GroupMethod.GROUP_VCARDS, true, os);
contact.write(VCardVersion.V4_0, GroupMethod.GROUP_VCARDS, os);
Constants.log.info(os.toString());
assertTrue(os.toString().contains("ADR;LABEL=My ^'Label^'\\nLine 2:;;Street \"Address\";;;;"));
}
......
......@@ -106,7 +106,7 @@ public class Contact {
public boolean group;
/**
* List of UIDs of group members (only meaningful if {@link #group} is true).
* List of UIDs of group members without urn:uuid prefix (only meaningful if {@link #group} is true).
*/
public List<String> members = new LinkedList<>();
......@@ -354,7 +354,7 @@ public class Contact {
return c;
}
public void write(VCardVersion vCardVersion, GroupMethod groupMethod, boolean rfc6868, OutputStream os) throws IOException {
public void write(VCardVersion vCardVersion, GroupMethod groupMethod, OutputStream os) throws IOException {
VCard vCard = null;
try {
if (unknownProperties != null)
......@@ -392,11 +392,13 @@ public class Contact {
vCard.setCategories(categories.toArray(new String[categories.size()]));
// FN
String fn = null;
if (displayName != null)
fn = displayName;
if (StringUtils.isEmpty(fn) && organization != null && organization.getValues() != null && !organization.getValues().isEmpty())
fn = organization.getValues().get(0);
String fn = displayName;
if (StringUtils.isEmpty(fn) && organization != null)
for (String part : organization.getValues()) {
fn = part;
if (!StringUtils.isEmpty(fn))
break;
}
if (StringUtils.isEmpty(fn) && nickName != null)
fn = nickName.getValues().get(0);
if (StringUtils.isEmpty(fn) && !emails.isEmpty())
......@@ -529,7 +531,7 @@ public class Contact {
Ezvcard .write(vCard)
.version(vCardVersion)
.versionStrict(false) // allow VCard4 properties in VCard3s
.caretEncoding(rfc6868) // enable RFC 6868 support
.caretEncoding(true) // enable RFC 6868 support
.prodId(productID == null)
.go(os);
}
......
......@@ -17,6 +17,7 @@ import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.UUID;
import java.util.logging.Level;
import ezvcard.VCardVersion;
......@@ -28,6 +29,8 @@ import ezvcard.parameter.TelephoneType;
import ezvcard.property.Address;
import ezvcard.property.Email;
import ezvcard.property.Impp;
import ezvcard.property.Nickname;
import ezvcard.property.Organization;
import ezvcard.property.Related;
import ezvcard.property.Telephone;
import ezvcard.property.Url;
......@@ -46,11 +49,139 @@ public class ContactTest {
private Contact regenerate(Contact c, VCardVersion vCardVersion) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
c.write(vCardVersion, GroupMethod.CATEGORIES, true, os);
c.write(vCardVersion, GroupMethod.CATEGORIES, os);
Constants.log.log(Level.INFO, "Re-generated VCard", os.toString());
return Contact.fromStream(new ByteArrayInputStream(os.toByteArray()), null, null)[0];
}
private String toString(Contact c, GroupMethod groupMethod, VCardVersion vCardVersion) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
c.write(vCardVersion, groupMethod, os);
return os.toString();
}
@Test
public void testGenerateOrganizationOnly() throws IOException {
Contact c = new Contact();
c.uid = UUID.randomUUID().toString();
c.organization = new Organization();
c.organization.getValues().add("My Organization");
c.organization.getValues().add("My Department");
// vCard 3 needs FN and N
String vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
assertTrue(vCard.contains("\nORG:My Organization;My Department\r\n"));
assertTrue(vCard.contains("\nFN:My Organization\r\n"));
assertTrue(vCard.contains("\nN:\r\n"));
// vCard 4 only needs FN
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V4_0);
assertTrue(vCard.contains("\nORG:My Organization;My Department\r\n"));
assertTrue(vCard.contains("\nFN:My Organization\r\n"));
assertFalse(vCard.contains("\nN:"));
}
@Test
public void testGenerateOrgDepartmentOnly() throws IOException {
Contact c = new Contact();
c.uid = UUID.randomUUID().toString();
c.organization = new Organization();
c.organization.getValues().add("");
c.organization.getValues().add("My Department");
// vCard 3 needs FN and N
String vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
assertTrue(vCard.contains("\nORG:;My Department\r\n"));
assertTrue(vCard.contains("\nFN:My Department\r\n"));
assertTrue(vCard.contains("\nN:\r\n"));
// vCard 4 only needs FN
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V4_0);
assertTrue(vCard.contains("\nORG:;My Department\r\n"));
assertTrue(vCard.contains("\nFN:My Department\r\n"));
assertFalse(vCard.contains("\nN:"));
}
@Test
public void testGenerateGroup() throws IOException {
Contact c = new Contact();
c.uid = UUID.randomUUID().toString();
c.displayName = "My Group";
c.group = true;
c.members.add("member1");
c.members.add("member2");
// vCard 3 needs FN and N
// exception for Apple: "N:<group name>"
String vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
assertTrue(vCard.contains("\nX-ADDRESSBOOKSERVER-KIND:group\r\n"));
assertTrue(vCard.contains("\nFN:My Group\r\n"));
assertTrue(vCard.contains("\nN:My Group\r\n"));
assertTrue(vCard.contains("\nX-ADDRESSBOOKSERVER-MEMBER:urn:uuid:member1\r\n"));
assertTrue(vCard.contains("\nX-ADDRESSBOOKSERVER-MEMBER:urn:uuid:member2\r\n"));
// vCard 4 only needs FN
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V4_0);
assertTrue(vCard.contains("\nKIND:group\r\n"));
assertTrue(vCard.contains("\nFN:My Group\r\n"));
assertFalse(vCard.contains("\nN:"));
assertTrue(vCard.contains("\nMEMBER:urn:uuid:member1\r\n"));
assertTrue(vCard.contains("\nMEMBER:urn:uuid:member2\r\n"));
}
@Test
public void testGenerateWithoutName() throws IOException {
/* no data */
Contact c = new Contact();
// vCard 3 needs FN and N
String vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
assertTrue(vCard.contains("\nFN:\r\n"));
assertTrue(vCard.contains("\nN:\r\n"));
// vCard 4 only needs FN
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V4_0);
assertTrue(vCard.contains("\nFN:\r\n"));
assertFalse(vCard.contains("\nN:"));
/* only UID */
c.uid = UUID.randomUUID().toString();
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
// vCard 3 needs FN and N
assertTrue(vCard.contains("\nFN:" + c.uid + "\r\n"));
assertTrue(vCard.contains("\nN:\r\n"));
// vCard 4 only needs FN
vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V4_0);
assertTrue(vCard.contains("\nFN:" + c.uid + "\r\n"));
assertFalse(vCard.contains("\nN:"));
// phone number available
c.phoneNumbers.add(new LabeledProperty<>(new Telephone("12345")));
assertTrue(toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0).contains("\nFN:12345\r\n"));
// email address available
c.emails.add(new LabeledProperty<>(new Email("[email protected]")));
assertTrue(toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0).contains("\nFN:[email protected]\r\n"));
// nick name available
c.nickName = new Nickname();
c.nickName.getValues().add("Nikki");
assertTrue(toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0).contains("\nFN:Nikki\r\n"));
}
@Test
public void testGenerateLabeledProperty() throws IOException {
Contact c = new Contact();
c.uid = UUID.randomUUID().toString();
c.phoneNumbers.add(new LabeledProperty<>(new Telephone("12345"), "My Phone"));
String vCard = toString(c, GroupMethod.GROUP_VCARDS, VCardVersion.V3_0);
assertTrue(vCard.contains("\ndavdroid1.TEL:12345\r\n"));
assertTrue(vCard.contains("\ndavdroid1.X-ABLabel:My Phone\r\n"));
c = regenerate(c, VCardVersion.V4_0);
assertEquals("12345", c.phoneNumbers.get(0).property.getText());
assertEquals("My Phone", c.phoneNumbers.get(0).label);
}
@Test
public void testVCard3FieldsAsVCard3() throws IOException {
......
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