2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.email.mail.store.imap;
19 import com.android.email.FixedLengthInputStream;
20 import com.android.email.Utility;
21 import com.android.email.mail.store.imap.ImapElement;
22 import com.android.email.mail.store.imap.ImapList;
23 import com.android.email.mail.store.imap.ImapResponse;
24 import com.android.email.mail.store.imap.ImapSimpleString;
25 import com.android.email.mail.store.imap.ImapString;
26 import com.android.email.mail.transport.DiscourseLogger;
28 import java.io.ByteArrayInputStream;
30 import junit.framework.Assert;
33 * Utility methods for IMAP tests.
35 public final class ImapTestUtils {
36 private ImapTestUtils() {}
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);
44 /** @see #assertElement(String, ImapElement, ImapElement) */
45 public static final void assertElement(ImapElement expected, ImapElement actual) {
46 assertElement("(no message)", expected, actual);
50 * Compare two {@link ImapElement}s and throws {@link AssertionFailedError} if different.
52 * Note this method used {@link ImapElement#equalsForTest} rather than equals().
54 public static final void assertElement(String message, ImapElement expected,
56 if (expected == null && actual == null) {
59 if (expected != null && expected.equalsForTest(actual)) {
62 Assert.fail(String.format("%s expected=%s\nactual=%s", message, expected, actual));
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) {
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) {
85 * Convenience method to build an {@link ImapResponse} from a single response.
87 public static final ImapResponse parseResponse(String line) {
88 ImapResponseParser p = new ImapResponseParser(
89 new ByteArrayInputStream(Utility.toAscii(line + "\r\n")), new DiscourseLogger(4));
91 return p.readResponse();
92 } catch (Exception e) {
93 throw new RuntimeException(e);
98 * Convenience method to build an {@link FixedLengthInputStream} from a String, using
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());