OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / Email / tests / src / com / android / email / mail / store / imap / ImapStringTest.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.email.mail.store.imap;
18
19 import static com.android.email.mail.store.imap.ImapTestUtils.*;
20
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;
27
28 import org.apache.commons.io.IOUtils;
29
30 import android.test.AndroidTestCase;
31 import android.test.suitebuilder.annotation.SmallTest;
32
33 import java.io.IOException;
34 import java.util.Date;
35 import java.util.Locale;
36
37
38 /**
39  * Test for {@link ImapString} and its subclasses.
40  */
41 @SmallTest
42 public class ImapStringTest extends AndroidTestCase {
43
44     @Override
45     protected void setUp() throws Exception {
46         super.setUp();
47         Email.setTempDirectory(getContext());
48     }
49
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());
56
57         assertTrue(ImapString.EMPTY.is(""));
58         assertTrue(ImapString.EMPTY.startsWith(""));
59         assertFalse(ImapString.EMPTY.is("a"));
60         assertFalse(ImapString.EMPTY.startsWith("a"));
61
62         assertTrue(new ImapSimpleString(null).isEmpty());
63     }
64
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())));
70
71         assertFalse(s.isNumber());
72         assertFalse(s.isDate());
73
74         assertFalse(s.is(null));
75         assertFalse(s.is(""));
76         assertTrue(s.is("abcd"));
77         assertFalse(s.is("abc"));
78
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"));
85     }
86
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());
93
94         // Too large for 32 bit int
95         assertEquals(0, new ImapSimpleString("99999999999999999999").getNumberOrZero());
96     }
97
98     public void testGetDateOrNull() {
99         final ImapString date = new ImapSimpleString("01-Jan-2009 11:34:56 -0100");
100
101         assertTrue(date.isDate());
102         Date d = date.getDateOrNull();
103         assertNotNull(d);
104         assertEquals("1 Jan 2009 12:34:56 GMT", d.toGMTString());
105
106         final ImapString nonDate = new ImapSimpleString("1234");
107         assertFalse(nonDate.isDate());
108         assertNull(nonDate.getDateOrNull());
109     }
110
111     /**
112      * Confirms that getDateOrNull() works fine regardless of the current locale.
113      */
114     public void testGetDateOrNullOnDifferentLocales() throws Exception {
115         Locale savedLocale = Locale.getDefault();
116         try {
117             Locale.setDefault(Locale.US);
118             checkGetDateOrNullOnDifferentLocales();
119             Locale.setDefault(Locale.JAPAN);
120             checkGetDateOrNullOnDifferentLocales();
121         } finally {
122             Locale.setDefault(savedLocale);
123         }
124     }
125
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());
129     }
130
131     /** Test for ImapMemoryLiteral */
132     public void testImapMemoryLiteral() throws Exception {
133         final String CONTENT = "abc";
134         doLiteralTest(new ImapMemoryLiteral(createFixedLengthInputStream(CONTENT)), CONTENT);
135     }
136
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);
142
143         // destroy() should remove the temp file.
144         assertTrue(l.tempFileExistsForTest());
145         l.destroy();
146         assertFalse(l.tempFileExistsForTest());
147     }
148
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())));
152     }
153 }