Skip to content

Commit

Permalink
Closes #595
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkel committed Sep 20, 2015
1 parent 7418dd4 commit 7884ec6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
== 1.5.10 (1555) tbd

* Fixed unquoted blank in local part of email address (#595)
* Updated Korean translation (#592)
* Updated notification images
* Rebased K9 to current head (https://github.com/k9mail/k-9/commit/952da74b0e2559feb23994a42d3f929baea22d81)

== 1.5.10 (1554) 08-20-2015

* New app icons (#580, Simon Shimon)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<groupId>com.fsck.k9.mail</groupId>
<artifactId>lib</artifactId>
<type>aar</type>
<version>0.0.1.96a00c1</version>
<version>0.0.1.996179a</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/zegoggles/smssync/mail/PersonLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PersonLookup(ContentResolver resolver) {
record = new PersonRecord(
id,
c.getString(c.getColumnIndex(PHONE_PROJECTION[1])),
getPrimaryEmail(id, number),
getPrimaryEmail(id),
number
);

Expand All @@ -81,7 +81,7 @@ record = new PersonRecord(0, null, null, address);

@TargetApi(Build.VERSION_CODES.ECLAIR)
@SuppressWarnings("deprecation")
private String getPrimaryEmail(final long personId, final String number) {
private String getPrimaryEmail(final long personId) {
if (personId <= 0) {
return null;
}
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/com/zegoggles/smssync/mail/PersonRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class PersonRecord {
private final long _id;
private final String name, email, number;

/**
* @param id the id of the record
* @param name email name
* @param email the actual email address
* @param number the telephone number
*/
public PersonRecord(long id, String name, String email, String number) {
this._id = id;
this.name = sanitize(name);
Expand All @@ -28,22 +34,22 @@ public boolean isUnknown() {
}

public Address getAddress(AddressStyle style) {
final String name;
switch (style) {
case NUMBER:
return new Address(getEmail(), getNumber());
name = getNumber(); break;
case NAME_AND_NUMBER:
return new Address(getEmail(),
name == null ? getNumber() :
String.format(Locale.ENGLISH, "%s (%s)", getName(), getNumber()));
name = getNameWithNumber(); break;
case NAME:
return new Address(getEmail(), getName());
name = getName(); break;
default:
return new Address(getEmail());
name = null;
}
return new Address(getEmail(), name, !isEmailUnknown());
}

public String getEmail() {
return isUnknown() || TextUtils.isEmpty(email) ? getUnknownEmail(number) : email;
return isEmailUnknown() ? getUnknownEmail(number) : email;
}

public String getId() {
Expand All @@ -66,6 +72,14 @@ public String toString() {
return String.format(Locale.ENGLISH, "[name=%s email=%s id=%d]", getName(), email, _id);
}

private boolean isEmailUnknown() {
return isUnknown() || TextUtils.isEmpty(email);
}

private String getNameWithNumber() {
return name != null ? String.format(Locale.ENGLISH, "%s (%s)", getName(), getNumber()) : getNumber();
}

private static String getUnknownEmail(String number) {
final String no = (number == null || "-1".equals(number)) ? UNKNOWN_NUMBER : number;
return encodeLocal(no.trim()) + "@" + UNKNOWN_EMAIL;
Expand Down
109 changes: 85 additions & 24 deletions src/test/java/com/zegoggles/smssync/mail/PersonRecordTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.zegoggles.smssync.mail;

import com.zegoggles.smssync.preferences.AddressStyle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static com.zegoggles.smssync.preferences.AddressStyle.NAME;
import static com.zegoggles.smssync.preferences.AddressStyle.NAME_AND_NUMBER;
import static com.zegoggles.smssync.preferences.AddressStyle.NUMBER;
import static org.fest.assertions.api.Assertions.assertThat;

@RunWith(RobolectricTestRunner.class)
public class PersonRecordTest {

@Test public void shouldSanitizeInputData() throws Exception {
@Test public void shouldSanitizeInputDataEmail() throws Exception {
PersonRecord r = new PersonRecord(1, "foo\n\r\n", "foo\n@gmail.com", "\r\r1234");

assertThat(r.getEmail()).isEqualTo("[email protected]");
}

@Test public void shouldSanitizeInputDataName() throws Exception {
PersonRecord r = new PersonRecord(1, "foo\n\r\n", "foo\n@gmail.com", "\r\r1234");
assertThat(r.getName()).isEqualTo("foo");
}

@Test public void shouldSanitizeInputDataNumber() throws Exception {
PersonRecord r = new PersonRecord(1, "foo\n\r\n", "foo\n@gmail.com", "\r\r1234");
assertThat(r.getNumber()).isEqualTo("1234");
}

Expand All @@ -38,53 +47,105 @@ public class PersonRecordTest {
assertThat(record.getEmail()).isEqualTo("[email protected]");
}

@Test public void shouldGetAddress() throws Exception {
// all fields present

@Test public void shouldGetAddressNameAndNumber() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", "+141543432");
assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo(
"\"John Appleseed (+141543432)\" <[email protected]>");
}

@Test public void shouldGetAddressName() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", "+141543432");
assertThat(record.getAddress(AddressStyle.NAME_AND_NUMBER).toString()).isEqualTo(
"\"John Appleseed (+141543432)\" <[email protected]>");

assertThat(record.getAddress(AddressStyle.NAME).toString()).isEqualTo(
assertThat(record.getAddress(NAME).toString()).isEqualTo(
"John Appleseed <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NUMBER).toString()).isEqualTo(
@Test public void shouldGetAddressNumber() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", "+141543432");
assertThat(record.getAddress(NUMBER).toString()).isEqualTo(
"+141543432 <[email protected]>");
}

@Test public void shouldGetAddressMissingEmail() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432");
// email missing

assertThat(record.getAddress(AddressStyle.NAME_AND_NUMBER).toString()).isEqualTo(
@Test public void shouldGetAddressMissingEmail_NameAndNumber() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432");
assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo(
"\"John Appleseed (+141543432)\" <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NAME).toString()).isEqualTo(
@Test public void shouldGetAddressMissingEmail_Name() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432");
assertThat(record.getAddress(NAME).toString()).isEqualTo(
"John Appleseed <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NUMBER).toString()).isEqualTo(
@Test public void shouldGetAddressMissingEmail_Number() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432");
assertThat(record.getAddress(NUMBER).toString()).isEqualTo(
"+141543432 <[email protected]>");
}

@Test public void shouldGetAddressMissingName() throws Exception {
PersonRecord record = new PersonRecord(1, null, "[email protected]", "+141543432");
// name is missing

assertThat(record.getAddress(AddressStyle.NAME_AND_NUMBER).toString()).isEqualTo(
@Test public void shouldGetAddressMissingName_Name() throws Exception {
PersonRecord record = new PersonRecord(1, null, "[email protected]", "+141543432");
assertThat(record.getAddress(NAME).toString()).isEqualTo(
"+141543432 <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NAME).toString()).isEqualTo(
@Test public void shouldGetAddressMissingName_NameAndNumber() throws Exception {
PersonRecord record = new PersonRecord(1, null, "[email protected]", "+141543432");
assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo(
"+141543432 <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NUMBER).toString()).isEqualTo(
@Test public void shouldGetAddressMissingName_Number() throws Exception {
PersonRecord record = new PersonRecord(1, null, "[email protected]", "+141543432");
assertThat(record.getAddress(NUMBER).toString()).isEqualTo(
"+141543432 <[email protected]>");
}

@Test public void shouldGetAddressMissingNumber() throws Exception {
// number is missing

@Test public void shouldGetAddressMissingNumber_Number() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", null);
assertThat(record.getAddress(AddressStyle.NAME_AND_NUMBER).toString()).isEqualTo(
"\"John Appleseed (Unknown)\" <[email protected]>");
assertThat(record.getAddress(NUMBER).toString()).isEqualTo(
"Unknown <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NAME).toString()).isEqualTo(
@Test public void shouldGetAddressMissingNumber_Name() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", null);
assertThat(record.getAddress(NAME).toString()).isEqualTo(
"John Appleseed <[email protected]>");
}

assertThat(record.getAddress(AddressStyle.NUMBER).toString()).isEqualTo(
"Unknown <[email protected]>");
@Test public void shouldGetAddressMissingNumber_NameAndNumber() throws Exception {
PersonRecord record = new PersonRecord(1, "John Appleseed", "[email protected]", null);

assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo(
"\"John Appleseed (Unknown)\" <[email protected]>");
}

// local part quote (#595)

@Test public void shouldQuoteLocalPart_NameAndNumber() throws Exception {
PersonRecord record = new PersonRecord(1, null, null, "name with space");
assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo(
"name with space <\"name with space\"@unknown.email>");
}

@Test public void shouldQuoteLocalPart_Name() throws Exception {
PersonRecord record = new PersonRecord(1, null, null, "name with space");
assertThat(record.getAddress(NAME).toString()).isEqualTo(
"name with space <\"name with space\"@unknown.email>");
}

@Test public void shouldQuoteLocalPart_Number() throws Exception {
PersonRecord record = new PersonRecord(1, null, null, "name with space");
assertThat(record.getAddress(NUMBER).toString()).isEqualTo(
"name with space <\"name with space\"@unknown.email>");
}
}

0 comments on commit 7884ec6

Please sign in to comment.