SimpleIcsWriterTests.java revision bc0c8c1523fc0bc42eb82dba6c2977492e441c03
1c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank/* Copyright (C) 2010 The Android Open Source Project
2c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank *
3c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * Licensed under the Apache License, Version 2.0 (the "License");
4c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * you may not use this file except in compliance with the License.
5c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * You may obtain a copy of the License at
6c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank *
7c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank *      http://www.apache.org/licenses/LICENSE-2.0
8c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank *
9c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * Unless required by applicable law or agreed to in writing, software
10c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * distributed under the License is distributed on an "AS IS" BASIS,
11c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * See the License for the specific language governing permissions and
13c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * limitations under the License.
14c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank */
15c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
16c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankpackage com.android.exchange.utility;
17c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
18bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onukiimport com.android.email.TestUtils;
198d5c79fe3d792065b72edb6231d51e4301fd2bccMarc Blank
20c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankimport junit.framework.TestCase;
21c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
22c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank/**
23bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * Test for {@link SimpleIcsWriter}.
24c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * You can run this entire test case with:
25c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank *   runtest -c com.android.exchange.utility.SimpleIcsWriterTests email
26c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank */
27c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankpublic class SimpleIcsWriterTests extends TestCase {
28bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static final String CRLF = "\r\n";
29bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static final String UTF8_1_BYTE = "a";
30bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static final String UTF8_2_BYTES = "\u00A2";
31bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static final String UTF8_3_BYTES = "\u20AC";
32bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static final String UTF8_4_BYTES = "\uD852\uDF62";
33c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
34bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    /**
35bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     * Test for {@link SimpleIcsWriter#writeTag}.  It also covers {@link SimpleIcsWriter#getBytes()}
36bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     * and {@link SimpleIcsWriter#escapeTextValue}.
37bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     */
38bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    public void testWriteTag() {
39bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final SimpleIcsWriter ics = new SimpleIcsWriter();
40bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeTag("TAG1", null);
41bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeTag("TAG2", "");
42bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeTag("TAG3", "xyz");
43bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeTag("SUMMARY", "TEST-TEST,;\r\n\\TEST");
44bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeTag("SUMMARY2", "TEST-TEST,;\r\n\\TEST");
45bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final String actual = TestUtils.fromUtf8(ics.getBytes());
46c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
47bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        assertEquals(
48bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                "TAG1:0" + CRLF +
49bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                "TAG2:0" + CRLF +
50bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                "TAG3:xyz" + CRLF +
51bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                "SUMMARY:TEST-TEST\\,\\;\\n\\\\TEST" + CRLF + // escaped
52bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                "SUMMARY2:TEST-TEST,;\r\n\\TEST" + CRLF // not escaped
53bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                , actual);
54f773512c0701a616188320ec96218284f777d3bfMarc Blank    }
55f773512c0701a616188320ec96218284f777d3bfMarc Blank
56bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    /**
57bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     * Verify that: We're folding lines correctly, and we're not splitting up a UTF-8 character.
58bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     */
59bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    public void testWriteLine() {
60bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        for (String last : new String[] {UTF8_1_BYTE, UTF8_2_BYTES, UTF8_3_BYTES, UTF8_4_BYTES}) {
61bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            for (int i = 70; i < 160; i++) {
62bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                String input = stringOfLength(i) + last;
63bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                checkWriteLine(input);
64bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            }
65bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        }
66bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    }
67bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki
68bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    /**
69bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     * @return a String of {@code length} bytes in UTF-8.
70bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki     */
71bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private static String stringOfLength(int length) {
72bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        StringBuilder sb = new StringBuilder();
73bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        for (int i = 0; i < length; i++) {
74bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            sb.append('0' +(i % 10));
75bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        }
76bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        return sb.toString();
77bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    }
78c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
79bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki    private void checkWriteLine(String input) {
80bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final SimpleIcsWriter ics = new SimpleIcsWriter();
81bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        ics.writeLine(input);
82bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final byte[] bytes = ics.getBytes();
83c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
84bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        // Verify that no lines are longer than 75 bytes.
85bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        int numBytes = 0;
86bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        for (byte b : bytes) {
87bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            if (b == '\r') {
88bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                continue; // ignore
89bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            }
90bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            if (b == '\n') {
91bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                assertTrue("input=" + input, numBytes <= 75);
92bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                numBytes = 0;
93bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki                continue;
94bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            }
95bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki            numBytes++;
96bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        }
97bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        assertTrue("input=" + input, numBytes <= 75);
98c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
99bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        // If we're splitting up a UTF-8 character, fromUtf8() won't restore it correctly.
100bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        // If it becomes the same as input, we're doing the right thing.
101bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final String actual = TestUtils.fromUtf8(bytes);
102bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        final String unfolded = actual.replace("\r\n\t", "");
103bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        assertEquals("input=" + input, input + "\r\n", unfolded);
104c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    }
105302238ee714b518fc0423459064946357f988683Makoto Onuki
106302238ee714b518fc0423459064946357f988683Makoto Onuki    public void testQuoteParamValue() {
107302238ee714b518fc0423459064946357f988683Makoto Onuki        assertNull(SimpleIcsWriter.quoteParamValue(null));
108302238ee714b518fc0423459064946357f988683Makoto Onuki        assertEquals("\"\"", SimpleIcsWriter.quoteParamValue(""));
109302238ee714b518fc0423459064946357f988683Makoto Onuki        assertEquals("\"a\"", SimpleIcsWriter.quoteParamValue("a"));
110bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        assertEquals("\"''\"", SimpleIcsWriter.quoteParamValue("\"'"));
111302238ee714b518fc0423459064946357f988683Makoto Onuki        assertEquals("\"abc\"", SimpleIcsWriter.quoteParamValue("abc"));
112bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki        assertEquals("\"a'b'c\"", SimpleIcsWriter.quoteParamValue("a\"b\"c"));
113302238ee714b518fc0423459064946357f988683Makoto Onuki    }
114c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank}
115