SimpleIcsWriterTests.java revision d38b4e9c1893a6c4ac70c08c5211629da3840cb2
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; 19d38b4e9c1893a6c4ac70c08c5211629da3840cb2Makoto Onukiimport com.android.email.Utility; 208d5c79fe3d792065b72edb6231d51e4301fd2bccMarc Blank 21c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankimport junit.framework.TestCase; 22c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 23c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank/** 24bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * Test for {@link SimpleIcsWriter}. 25c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * You can run this entire test case with: 26c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank * runtest -c com.android.exchange.utility.SimpleIcsWriterTests email 27c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank */ 28c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blankpublic class SimpleIcsWriterTests extends TestCase { 29bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static final String CRLF = "\r\n"; 30bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static final String UTF8_1_BYTE = "a"; 31bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static final String UTF8_2_BYTES = "\u00A2"; 32bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static final String UTF8_3_BYTES = "\u20AC"; 33bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static final String UTF8_4_BYTES = "\uD852\uDF62"; 34c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 35bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki /** 36bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * Test for {@link SimpleIcsWriter#writeTag}. It also covers {@link SimpleIcsWriter#getBytes()} 37bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * and {@link SimpleIcsWriter#escapeTextValue}. 38bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki */ 39bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki public void testWriteTag() { 40bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki final SimpleIcsWriter ics = new SimpleIcsWriter(); 41bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeTag("TAG1", null); 42bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeTag("TAG2", ""); 43bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeTag("TAG3", "xyz"); 44bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeTag("SUMMARY", "TEST-TEST,;\r\n\\TEST"); 45bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeTag("SUMMARY2", "TEST-TEST,;\r\n\\TEST"); 46d38b4e9c1893a6c4ac70c08c5211629da3840cb2Makoto Onuki final String actual = Utility.fromUtf8(ics.getBytes()); 47c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 48bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertEquals( 49bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki "TAG3:xyz" + CRLF + 50bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki "SUMMARY:TEST-TEST\\,\\;\\n\\\\TEST" + CRLF + // escaped 51bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki "SUMMARY2:TEST-TEST,;\r\n\\TEST" + CRLF // not escaped 52bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki , actual); 53f773512c0701a616188320ec96218284f777d3bfMarc Blank } 54f773512c0701a616188320ec96218284f777d3bfMarc Blank 55bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki /** 56bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * Verify that: We're folding lines correctly, and we're not splitting up a UTF-8 character. 57bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki */ 58bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki public void testWriteLine() { 59bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki for (String last : new String[] {UTF8_1_BYTE, UTF8_2_BYTES, UTF8_3_BYTES, UTF8_4_BYTES}) { 60bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki for (int i = 70; i < 160; i++) { 61bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki String input = stringOfLength(i) + last; 62bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki checkWriteLine(input); 63bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 64bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 65bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 66bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki 67bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki /** 68bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki * @return a String of {@code length} bytes in UTF-8. 69bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki */ 70bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private static String stringOfLength(int length) { 71bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki StringBuilder sb = new StringBuilder(); 72bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki for (int i = 0; i < length; i++) { 73bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki sb.append('0' +(i % 10)); 74bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 75bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki return sb.toString(); 76bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 77c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 78bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki private void checkWriteLine(String input) { 79bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki final SimpleIcsWriter ics = new SimpleIcsWriter(); 80bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki ics.writeLine(input); 81bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki final byte[] bytes = ics.getBytes(); 82c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 83bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki // Verify that no lines are longer than 75 bytes. 84bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki int numBytes = 0; 85bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki for (byte b : bytes) { 86bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki if (b == '\r') { 87bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki continue; // ignore 88bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 89bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki if (b == '\n') { 90bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertTrue("input=" + input, numBytes <= 75); 91bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki numBytes = 0; 92bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki continue; 93bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 94bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki numBytes++; 95bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki } 96bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertTrue("input=" + input, numBytes <= 75); 97c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank 98bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki // If we're splitting up a UTF-8 character, fromUtf8() won't restore it correctly. 99bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki // If it becomes the same as input, we're doing the right thing. 100d38b4e9c1893a6c4ac70c08c5211629da3840cb2Makoto Onuki final String actual = Utility.fromUtf8(bytes); 101bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki final String unfolded = actual.replace("\r\n\t", ""); 102bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertEquals("input=" + input, input + "\r\n", unfolded); 103c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank } 104302238ee714b518fc0423459064946357f988683Makoto Onuki 105302238ee714b518fc0423459064946357f988683Makoto Onuki public void testQuoteParamValue() { 106302238ee714b518fc0423459064946357f988683Makoto Onuki assertNull(SimpleIcsWriter.quoteParamValue(null)); 107302238ee714b518fc0423459064946357f988683Makoto Onuki assertEquals("\"\"", SimpleIcsWriter.quoteParamValue("")); 108302238ee714b518fc0423459064946357f988683Makoto Onuki assertEquals("\"a\"", SimpleIcsWriter.quoteParamValue("a")); 109bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertEquals("\"''\"", SimpleIcsWriter.quoteParamValue("\"'")); 110302238ee714b518fc0423459064946357f988683Makoto Onuki assertEquals("\"abc\"", SimpleIcsWriter.quoteParamValue("abc")); 111bc0c8c1523fc0bc42eb82dba6c2977492e441c03Makoto Onuki assertEquals("\"a'b'c\"", SimpleIcsWriter.quoteParamValue("a\"b\"c")); 112302238ee714b518fc0423459064946357f988683Makoto Onuki } 113c8dc8009bcbb9dbf781f0028f07b2bbca600aeebMarc Blank} 114