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