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 17package com.android.email.mail.transport; 18 19import android.test.MoreAsserts; 20 21import junit.framework.TestCase; 22 23public class DiscourseLoggerTest extends TestCase { 24 25 /** Shortcut to create a byte array */ 26 private static byte[] b(String chars) { 27 byte[] ret = new byte[chars.length()]; 28 for (int i = 0; i < chars.length(); i++) { 29 ret[i] = (byte) chars.charAt(i); 30 } 31 return ret; 32 } 33 34 /** Shortcut to create a String array */ 35 private static String[] s(String... strings) { 36 return strings; 37 } 38 39 /** Shortcut to create an Object array */ 40 private static Object[] o(Object... objects) { 41 return objects; 42 } 43 44 public void testDiscourseLogger() { 45 checkDiscourseStore(4, o(), s()); 46 checkDiscourseStore(4, 47 o( 48 "command" 49 ), 50 s( 51 "command" 52 )); 53 checkDiscourseStore(4, 54 o( 55 "1a", 56 "2b", 57 "3", 58 "4dd" 59 ), 60 s( 61 "1a", 62 "2b", 63 "3", 64 "4dd" 65 )); 66 checkDiscourseStore(4, 67 o( 68 "1", 69 "2", 70 "3", 71 "4", 72 "5" 73 ), 74 s( 75 "2", 76 "3", 77 "4", 78 "5" 79 )); 80 checkDiscourseStore(4, 81 o( 82 b("A") 83 ), 84 s( 85 "A" 86 )); 87 checkDiscourseStore(4, 88 o( 89 b("A\nB\nC") 90 ), 91 s( 92 "A", 93 "B", 94 "C" 95 )); 96 checkDiscourseStore(4, 97 o( 98 b("A\nBCD\nC\nDEF\u0080\u0001G\r\n") 99 ), 100 s( 101 "A", 102 "BCD", 103 "C", 104 "DEF\\x80\\x01G" 105 )); 106 checkDiscourseStore(4, 107 o( 108 "1", 109 b("2"), 110 "3", 111 b("4\n5\n"), 112 "6 7 8", 113 "7 a bbb ccc", 114 b("* aaa8\n* bbb9\n7 ccc 10") 115 ), 116 s( 117 "7 a bbb ccc", 118 "* aaa8", 119 "* bbb9", 120 "7 ccc 10" 121 )); 122 } 123 124 private void checkDiscourseStore(int storeSize, Object[] discource, String[] expected) { 125 DiscourseLogger store = new DiscourseLogger(storeSize); 126 for (Object o : discource) { 127 if (o instanceof String) { 128 store.addSentCommand((String) o); 129 } else if (o instanceof byte[]) { 130 for (byte b : (byte[]) o) { 131 store.addReceivedByte(b); 132 } 133 } else { 134 fail("Invalid argument. Test broken."); 135 } 136 } 137 MoreAsserts.assertEquals(expected, store.getLines()); 138 139 // logLastDiscourse should empty the buffer. 140 store.logLastDiscourse(); 141 assertEquals(0, store.getLines().length); 142 } 143} 144