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
17package com.android.email.mail.store.imap;
18
19import static com.android.email.mail.store.imap.ImapTestUtils.createFixedLengthInputStream;
20
21import com.android.emailcommon.TempDirectory;
22import com.android.emailcommon.utility.Utility;
23
24import org.apache.commons.io.IOUtils;
25
26import android.test.AndroidTestCase;
27import android.test.suitebuilder.annotation.SmallTest;
28
29import java.io.IOException;
30import java.util.Date;
31import java.util.Locale;
32
33
34/**
35 * Test for {@link ImapString} and its subclasses.
36 */
37@SmallTest
38public class ImapStringTest extends AndroidTestCase {
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43        TempDirectory.setTempDirectory(getContext());
44    }
45
46    public void testEmpty() throws Exception {
47        assertTrue(ImapString.EMPTY.isEmpty());
48        assertEquals("", ImapString.EMPTY.getString());
49        assertEquals("", Utility.fromAscii(IOUtils.toByteArray(ImapString.EMPTY.getAsStream())));
50        assertFalse(ImapString.EMPTY.isNumber());
51        assertFalse(ImapString.EMPTY.isDate());
52
53        assertTrue(ImapString.EMPTY.is(""));
54        assertTrue(ImapString.EMPTY.startsWith(""));
55        assertFalse(ImapString.EMPTY.is("a"));
56        assertFalse(ImapString.EMPTY.startsWith("a"));
57
58        assertTrue(new ImapSimpleString(null).isEmpty());
59    }
60
61    public void testBasics() throws Exception {
62        final ImapSimpleString s = new ImapSimpleString("AbcD");
63        assertFalse(s.isEmpty());
64        assertEquals("AbcD", s.getString());
65        assertEquals("AbcD", Utility.fromAscii(IOUtils.toByteArray(s.getAsStream())));
66
67        assertFalse(s.isNumber());
68        assertFalse(s.isDate());
69
70        assertFalse(s.is(null));
71        assertFalse(s.is(""));
72        assertTrue(s.is("abcd"));
73        assertFalse(s.is("abc"));
74
75        assertFalse(s.startsWith(null));
76        assertTrue(s.startsWith(""));
77        assertTrue(s.startsWith("a"));
78        assertTrue(s.startsWith("abcd"));
79        assertFalse(s.startsWith("Z"));
80        assertFalse(s.startsWith("abcde"));
81    }
82
83    public void testGetNumberOrZero() {
84        assertEquals(1234, new ImapSimpleString("1234").getNumberOrZero());
85        assertEquals(-1, new ImapSimpleString("-1").getNumberOrZero());
86        assertEquals(0, new ImapSimpleString("").getNumberOrZero());
87        assertEquals(0, new ImapSimpleString("X").getNumberOrZero());
88        assertEquals(0, new ImapSimpleString("1234E").getNumberOrZero());
89
90        // Too large for 32 bit int
91        assertEquals(0, new ImapSimpleString("99999999999999999999").getNumberOrZero());
92    }
93
94    public void testGetDateOrNull() {
95        final ImapString date = new ImapSimpleString("01-Jan-2009 11:34:56 -0100");
96
97        assertTrue(date.isDate());
98        Date d = date.getDateOrNull();
99        assertNotNull(d);
100        assertEquals("1 Jan 2009 12:34:56 GMT", d.toGMTString());
101
102        final ImapString nonDate = new ImapSimpleString("1234");
103        assertFalse(nonDate.isDate());
104        assertNull(nonDate.getDateOrNull());
105    }
106
107    /**
108     * Confirms that getDateOrNull() works fine regardless of the current locale.
109     */
110    public void testGetDateOrNullOnDifferentLocales() throws Exception {
111        Locale savedLocale = Locale.getDefault();
112        try {
113            Locale.setDefault(Locale.US);
114            checkGetDateOrNullOnDifferentLocales();
115            Locale.setDefault(Locale.JAPAN);
116            checkGetDateOrNullOnDifferentLocales();
117        } finally {
118            Locale.setDefault(savedLocale);
119        }
120    }
121
122    private static void checkGetDateOrNullOnDifferentLocales() throws Exception {
123        ImapSimpleString s =  new ImapSimpleString("01-Jan-2009 11:34:56 -0100");
124        assertEquals("1 Jan 2009 12:34:56 GMT", s.getDateOrNull().toGMTString());
125    }
126
127    /** Test for ImapMemoryLiteral */
128    public void testImapMemoryLiteral() throws Exception {
129        final String CONTENT = "abc";
130        doLiteralTest(new ImapMemoryLiteral(createFixedLengthInputStream(CONTENT)), CONTENT);
131    }
132
133    /** Test for ImapTempFileLiteral */
134    public void testImapTempFileLiteral() throws Exception {
135        final String CONTENT = "def";
136        ImapTempFileLiteral l = new ImapTempFileLiteral(createFixedLengthInputStream(CONTENT));
137        doLiteralTest(l, CONTENT);
138
139        // destroy() should remove the temp file.
140        assertTrue(l.tempFileExistsForTest());
141        l.destroy();
142        assertFalse(l.tempFileExistsForTest());
143    }
144
145    private static void doLiteralTest(ImapString s, String content) throws IOException {
146        assertEquals(content, s.getString());
147        assertEquals(content, Utility.fromAscii(IOUtils.toByteArray(s.getAsStream())));
148    }
149}
150