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