am 21c04fe3: Merge "Further harden getListOrNull" into froyo

Merge commit '21c04fe31c978fc68c473a5bef5c93b20363fe8e' into froyo-plus-aosp

* commit '21c04fe31c978fc68c473a5bef5c93b20363fe8e':
  Further harden getListOrNull
This commit is contained in:
Marc Blank 2010-04-21 08:15:04 -07:00 committed by Android Git Automerger
commit a0c71419ce
2 changed files with 48 additions and 7 deletions

View File

@ -274,7 +274,7 @@ public class ImapResponseParser {
// ch == '"' || ch == '\' ||
ch == '"' || (ch >= 0x00 && ch <= 0x1f) || ch == 0x7f) {
if (sb.length() == 0) {
throw new IOException(String.format("parseAtom(): (%04x %c)", (int)ch, ch));
throw new IOException(String.format("parseAtom(): (%04x %c)", ch, ch));
}
return sb.toString();
} else {
@ -350,12 +350,13 @@ public class ImapResponseParser {
/** Safe version of getList() */
public ImapList getListOrNull(int index) {
Object list = get(index);
if (list instanceof ImapList) {
return (ImapList) list;
} else {
return null;
if (index < size()) {
Object list = get(index);
if (list instanceof ImapList) {
return (ImapList) list;
}
}
return null;
}
public String getString(int index) {
@ -460,7 +461,7 @@ public class ImapResponseParser {
FixedLengthInputStream is = (FixedLengthInputStream) o;
byte[] buffer = new byte[is.available()];
is.read(buffer);
set(last, (Object) new String(buffer));
set(last, new String(buffer));
}
}
}
@ -502,6 +503,7 @@ public class ImapResponseParser {
return null;
}
@Override
public String toString() {
return "#" + mTag + "# " + super.toString();
}

View File

@ -17,6 +17,7 @@
package com.android.email.mail.store;
import com.android.email.FixedLengthInputStream;
import com.android.email.mail.MessagingException;
import com.android.email.mail.store.ImapResponseParser.ImapList;
import com.android.email.mail.store.ImapResponseParser.ImapResponse;
import com.android.email.mail.transport.DiscourseLogger;
@ -26,6 +27,7 @@ import android.test.suitebuilder.annotation.SmallTest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
/**
* This is a series of unit tests for the ImapStore class. These tests must be locally
@ -131,4 +133,41 @@ public class ImapResponseParserUnitTests extends AndroidTestCase {
ImapResponse line3 = parser.readResponse();
assertNull(line3.getAlertText());
}
/**
* Test basic ImapList functionality
* TODO: Add tests for keyed lists
*/
public void testImapList() throws MessagingException {
ByteArrayInputStream is = new ByteArrayInputStream("foo".getBytes());
ImapResponseParser parser = new ImapResponseParser(is, new DiscourseLogger(4));
ImapList list1 = parser.new ImapList();
list1.add("foo");
list1.add("bar");
list1.add(20);
list1.add(is);
list1.add(new Date());
ImapList list2 = parser.new ImapList();
list2.add(list1);
// Test getString(), getStringOrNull(), getList(), getListOrNull, getNumber()
// getLiteral(), and getDate()
assertEquals("foo", list1.getString(0));
assertEquals("foo", list1.getStringOrNull(0));
assertEquals("bar", list1.getString(1));
assertEquals("bar", list1.getStringOrNull(1));
assertNull(list1.getStringOrNull(2));
assertNull(list1.getStringOrNull(3));
assertNull(list1.getStringOrNull(4));
assertNull(list1.getListOrNull(2));
assertNull(list1.getListOrNull(3));
assertNull(list1.getListOrNull(4));
assertEquals(20, list1.getNumber(2));
assertNull(list1.getStringOrNull(20));
assertNotNull(list1.getLiteral(3));
assertNotNull(list1.getDate(4));
// Test getList() and getListOrNull() with list value
assertEquals(list1, list2.getList(0));
assertEquals(list1, list2.getListOrNull(0));
assertNull(list2.getListOrNull(20));
}
}