Home | History | Annotate | Download | only in transport
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.email.mail.transport;
     18 
     19 import android.test.MoreAsserts;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import junit.framework.TestCase;
     23 
     24 @SmallTest
     25 public class DiscourseLoggerTest extends TestCase {
     26 
     27     /** Shortcut to create a byte array */
     28     private static byte[] b(String chars) {
     29         byte[] ret = new byte[chars.length()];
     30         for (int i = 0; i < chars.length(); i++) {
     31             ret[i] = (byte) chars.charAt(i);
     32         }
     33         return ret;
     34     }
     35 
     36     /** Shortcut to create a String array */
     37     private static String[] s(String... strings) {
     38         return strings;
     39     }
     40 
     41     /** Shortcut to create an Object array */
     42     private static Object[] o(Object... objects) {
     43         return objects;
     44     }
     45 
     46     public void testDiscourseLogger() {
     47         checkDiscourseStore(4, o(), s());
     48         checkDiscourseStore(4,
     49                 o(
     50                         "command"
     51                 ),
     52                 s(
     53                         "command"
     54                 ));
     55         checkDiscourseStore(4,
     56                 o(
     57                         "1a",
     58                         "2b",
     59                         "3",
     60                         "4dd"
     61                 ),
     62                 s(
     63                         "1a",
     64                         "2b",
     65                         "3",
     66                         "4dd"
     67                 ));
     68         checkDiscourseStore(4,
     69                 o(
     70                         "1",
     71                         "2",
     72                         "3",
     73                         "4",
     74                         "5"
     75                 ),
     76                 s(
     77                         "2",
     78                         "3",
     79                         "4",
     80                         "5"
     81                 ));
     82         checkDiscourseStore(4,
     83                 o(
     84                         b("A")
     85                 ),
     86                 s(
     87                         "A"
     88                 ));
     89         checkDiscourseStore(4,
     90                 o(
     91                         b("A\nB\nC")
     92                 ),
     93                 s(
     94                         "A",
     95                         "B",
     96                         "C"
     97                 ));
     98         checkDiscourseStore(4,
     99                 o(
    100                         b("A\nBCD\nC\nDEF\u0080\u0001G\r\n")
    101                 ),
    102                 s(
    103                         "A",
    104                         "BCD",
    105                         "C",
    106                         "DEF\\x80\\x01G"
    107                 ));
    108         checkDiscourseStore(4,
    109                 o(
    110                         "1",
    111                         b("2"),
    112                         "3",
    113                         b("4\n5\n"),
    114                         "6 7 8",
    115                         "7 a bbb ccc",
    116                         b("* aaa8\n* bbb9\n7 ccc  10")
    117                 ),
    118                 s(
    119                         "7 a bbb ccc",
    120                         "* aaa8",
    121                         "* bbb9",
    122                         "7 ccc  10"
    123                 ));
    124     }
    125 
    126     private void checkDiscourseStore(int storeSize, Object[] discource, String[] expected) {
    127         DiscourseLogger store = new DiscourseLogger(storeSize);
    128         for (Object o : discource) {
    129             if (o instanceof String) {
    130                 store.addSentCommand((String) o);
    131             } else if (o instanceof byte[]) {
    132                 for (byte b : (byte[]) o) {
    133                     store.addReceivedByte(b);
    134                 }
    135             } else {
    136                 fail("Invalid argument.  Test broken.");
    137             }
    138         }
    139         MoreAsserts.assertEquals(expected, store.getLines());
    140 
    141         // logLastDiscourse should empty the buffer.
    142         store.logLastDiscourse();
    143         assertEquals(0, store.getLines().length);
    144     }
    145 }
    146