SimpleIcsWriterTests.java revision f773512c0701a616188320ec96218284f777d3bf
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
188d5c79fe3d792065b72edb6231d51e4301fd2bccMarc Blankimport java.io.IOException;
198d5c79fe3d792065b72edb6231d51e4301fd2bccMarc Blank
20c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankimport junit.framework.TestCase;
21c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
22c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank/**
23c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * Tests of EAS Calendar Utilities
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 {
28c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    private final String string63Chars =
29c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*";
30c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    private final String string80Chars =
31c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ12345" + "67890";
32c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    private static final String tag11Chars = "DESCRIPTION";
33c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
34c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    // Where our line breaks should end up
35c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    private final String expectedFirstLineBreak =
36c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        string63Chars.charAt(string63Chars.length() - 1) + SimpleIcsWriter.LINE_BREAK;
37c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    private final String expectedSecondLineBreak =
38c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        string80Chars.charAt(SimpleIcsWriter.MAX_LINE_LENGTH - 1) + SimpleIcsWriter.LINE_BREAK;
39c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
40f773512c0701a616188320ec96218284f777d3bfMarc Blank    public void testCrlf() throws IOException {
41f773512c0701a616188320ec96218284f777d3bfMarc Blank        SimpleIcsWriter w = new SimpleIcsWriter();
42f773512c0701a616188320ec96218284f777d3bfMarc Blank        w.writeTag("TAG", "A\r\nB\nC\r\nD");
43f773512c0701a616188320ec96218284f777d3bfMarc Blank        String str = w.toString();
44f773512c0701a616188320ec96218284f777d3bfMarc Blank        // Make sure \r's are stripped and that \n is turned into two chars, \ and n
45f773512c0701a616188320ec96218284f777d3bfMarc Blank        assertEquals("TAG:A\\nB\\nC\\nD\r\n", str);
46f773512c0701a616188320ec96218284f777d3bfMarc Blank    }
47f773512c0701a616188320ec96218284f777d3bfMarc Blank
488d5c79fe3d792065b72edb6231d51e4301fd2bccMarc Blank    public void testWriter() throws IOException {
49c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        // Sanity test on constant strings
50c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        assertEquals(63, string63Chars.length());
51c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        assertEquals(80, string80Chars.length());
52c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        // Add 1 for the colon between the tag and the value
53c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        assertEquals(SimpleIcsWriter.MAX_LINE_LENGTH,
54c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank                tag11Chars.length() + 1 + string63Chars.length());
55c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
56c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        SimpleIcsWriter w = new SimpleIcsWriter();
57c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        w.writeTag(tag11Chars, string63Chars + string80Chars);
58c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
59c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        // We should always end a tag on a new line
60f773512c0701a616188320ec96218284f777d3bfMarc Blank        assertEquals(0, w.mColumnCount);
61c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank
62c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        // Get the final string
63c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        String str = w.toString();
64c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        assertEquals(SimpleIcsWriter.MAX_LINE_LENGTH-1, str.indexOf(expectedFirstLineBreak));
65c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank        assertEquals(SimpleIcsWriter.MAX_LINE_LENGTH + SimpleIcsWriter.LINE_BREAK_LENGTH +
66c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank                (SimpleIcsWriter.MAX_LINE_LENGTH - 1), str.indexOf(expectedSecondLineBreak));
67c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank    }
68c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank}
69