2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.email.mail.store.imap;
19 import static com.android.email.mail.store.imap.ImapTestUtils.*;
21 import com.android.email.Email;
22 import com.android.email.Utility;
23 import com.android.email.mail.store.imap.ImapMemoryLiteral;
24 import com.android.email.mail.store.imap.ImapSimpleString;
25 import com.android.email.mail.store.imap.ImapString;
26 import com.android.email.mail.store.imap.ImapTempFileLiteral;
28 import org.apache.commons.io.IOUtils;
30 import android.test.AndroidTestCase;
31 import android.test.suitebuilder.annotation.SmallTest;
33 import java.io.IOException;
34 import java.util.Date;
35 import java.util.Locale;
39 * Test for {@link ImapString} and its subclasses.
42 public class ImapStringTest extends AndroidTestCase {
45 protected void setUp() throws Exception {
47 Email.setTempDirectory(getContext());
50 public void testEmpty() throws Exception {
51 assertTrue(ImapString.EMPTY.isEmpty());
52 assertEquals("", ImapString.EMPTY.getString());
53 assertEquals("", Utility.fromAscii(IOUtils.toByteArray(ImapString.EMPTY.getAsStream())));
54 assertFalse(ImapString.EMPTY.isNumber());
55 assertFalse(ImapString.EMPTY.isDate());
57 assertTrue(ImapString.EMPTY.is(""));
58 assertTrue(ImapString.EMPTY.startsWith(""));
59 assertFalse(ImapString.EMPTY.is("a"));
60 assertFalse(ImapString.EMPTY.startsWith("a"));
62 assertTrue(new ImapSimpleString(null).isEmpty());
65 public void testBasics() throws Exception {
66 final ImapSimpleString s = new ImapSimpleString("AbcD");
67 assertFalse(s.isEmpty());
68 assertEquals("AbcD", s.getString());
69 assertEquals("AbcD", Utility.fromAscii(IOUtils.toByteArray(s.getAsStream())));
71 assertFalse(s.isNumber());
72 assertFalse(s.isDate());
74 assertFalse(s.is(null));
75 assertFalse(s.is(""));
76 assertTrue(s.is("abcd"));
77 assertFalse(s.is("abc"));
79 assertFalse(s.startsWith(null));
80 assertTrue(s.startsWith(""));
81 assertTrue(s.startsWith("a"));
82 assertTrue(s.startsWith("abcd"));
83 assertFalse(s.startsWith("Z"));
84 assertFalse(s.startsWith("abcde"));
87 public void testGetNumberOrZero() {
88 assertEquals(1234, new ImapSimpleString("1234").getNumberOrZero());
89 assertEquals(-1, new ImapSimpleString("-1").getNumberOrZero());
90 assertEquals(0, new ImapSimpleString("").getNumberOrZero());
91 assertEquals(0, new ImapSimpleString("X").getNumberOrZero());
92 assertEquals(0, new ImapSimpleString("1234E").getNumberOrZero());
94 // Too large for 32 bit int
95 assertEquals(0, new ImapSimpleString("99999999999999999999").getNumberOrZero());
98 public void testGetDateOrNull() {
99 final ImapString date = new ImapSimpleString("01-Jan-2009 11:34:56 -0100");
101 assertTrue(date.isDate());
102 Date d = date.getDateOrNull();
104 assertEquals("1 Jan 2009 12:34:56 GMT", d.toGMTString());
106 final ImapString nonDate = new ImapSimpleString("1234");
107 assertFalse(nonDate.isDate());
108 assertNull(nonDate.getDateOrNull());
112 * Confirms that getDateOrNull() works fine regardless of the current locale.
114 public void testGetDateOrNullOnDifferentLocales() throws Exception {
115 Locale savedLocale = Locale.getDefault();
117 Locale.setDefault(Locale.US);
118 checkGetDateOrNullOnDifferentLocales();
119 Locale.setDefault(Locale.JAPAN);
120 checkGetDateOrNullOnDifferentLocales();
122 Locale.setDefault(savedLocale);
126 private static void checkGetDateOrNullOnDifferentLocales() throws Exception {
127 ImapSimpleString s = new ImapSimpleString("01-Jan-2009 11:34:56 -0100");
128 assertEquals("1 Jan 2009 12:34:56 GMT", s.getDateOrNull().toGMTString());
131 /** Test for ImapMemoryLiteral */
132 public void testImapMemoryLiteral() throws Exception {
133 final String CONTENT = "abc";
134 doLiteralTest(new ImapMemoryLiteral(createFixedLengthInputStream(CONTENT)), CONTENT);
137 /** Test for ImapTempFileLiteral */
138 public void testImapTempFileLiteral() throws Exception {
139 final String CONTENT = "def";
140 ImapTempFileLiteral l = new ImapTempFileLiteral(createFixedLengthInputStream(CONTENT));
141 doLiteralTest(l, CONTENT);
143 // destroy() should remove the temp file.
144 assertTrue(l.tempFileExistsForTest());
146 assertFalse(l.tempFileExistsForTest());
149 private static void doLiteralTest(ImapString s, String content) throws IOException {
150 assertEquals(content, s.getString());
151 assertEquals(content, Utility.fromAscii(IOUtils.toByteArray(s.getAsStream())));