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.store.imap; 18 19 import com.android.email.FixedLengthInputStream; 20 import com.android.email.mail.store.imap.ImapElement; 21 import com.android.email.mail.store.imap.ImapList; 22 import com.android.email.mail.store.imap.ImapResponse; 23 import com.android.email.mail.store.imap.ImapSimpleString; 24 import com.android.email.mail.store.imap.ImapString; 25 import com.android.email.mail.transport.DiscourseLogger; 26 import com.android.emailcommon.utility.Utility; 27 28 import java.io.ByteArrayInputStream; 29 30 import junit.framework.Assert; 31 32 /** 33 * Utility methods for IMAP tests. 34 */ 35 public final class ImapTestUtils { 36 private ImapTestUtils() {} 37 38 // Generic constants used by various tests. 39 public static final ImapString STRING_1 = new ImapSimpleString("aBc"); 40 public static final ImapString STRING_2 = new ImapSimpleString("X y z"); 41 public static final ImapList LIST_1 = buildList(STRING_1); 42 public static final ImapList LIST_2 = buildList(STRING_1, STRING_2, LIST_1); 43 44 /** @see #assertElement(String, ImapElement, ImapElement) */ 45 public static final void assertElement(ImapElement expected, ImapElement actual) { 46 assertElement("(no message)", expected, actual); 47 } 48 49 /** 50 * Compare two {@link ImapElement}s and throws {@link AssertionFailedError} if different. 51 * 52 * Note this method used {@link ImapElement#equalsForTest} rather than equals(). 53 */ 54 public static final void assertElement(String message, ImapElement expected, 55 ImapElement actual) { 56 if (expected == null && actual == null) { 57 return; 58 } 59 if (expected != null && expected.equalsForTest(actual)) { 60 return; // OK 61 } 62 Assert.fail(String.format("%s expected=%s\nactual=%s", message, expected, actual)); 63 } 64 65 /** Convenience method to build an {@link ImapList} */ 66 public static final ImapList buildList(ImapElement... elements) { 67 ImapList list = new ImapList(); 68 for (ImapElement e : elements) { 69 list.add(e); 70 } 71 return list; 72 } 73 74 /** Convenience method to build an {@link ImapResponse} */ 75 public static final ImapResponse buildResponse(String tag, boolean isContinuationRequest, 76 ImapElement... elements) { 77 ImapResponse res = new ImapResponse(tag, isContinuationRequest); 78 for (ImapElement e : elements) { 79 res.add(e); 80 } 81 return res; 82 } 83 84 /** 85 * Convenience method to build an {@link ImapResponse} from a single response. 86 */ 87 public static final ImapResponse parseResponse(String line) { 88 ImapResponseParser p = new ImapResponseParser( 89 new ByteArrayInputStream(Utility.toAscii(line + "\r\n")), new DiscourseLogger(4)); 90 try { 91 return p.readResponse(); 92 } catch (Exception e) { 93 throw new RuntimeException(e); 94 } 95 } 96 97 /** 98 * Convenience method to build an {@link FixedLengthInputStream} from a String, using 99 * US-ASCII. 100 */ 101 public static FixedLengthInputStream createFixedLengthInputStream(String content) { 102 // Add unnecessary part. FixedLengthInputStream should cut it. 103 ByteArrayInputStream in = new ByteArrayInputStream(Utility.toAscii(content + "#trailing")); 104 return new FixedLengthInputStream(in, content.length()); 105 } 106 } 107