17e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki/*
27e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Copyright (C) 2010 The Android Open Source Project
37e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki *
47e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Licensed under the Apache License, Version 2.0 (the "License");
57e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * you may not use this file except in compliance with the License.
67e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * You may obtain a copy of the License at
77e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki *
87e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki *      http://www.apache.org/licenses/LICENSE-2.0
97e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki *
107e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Unless required by applicable law or agreed to in writing, software
117e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * distributed under the License is distributed on an "AS IS" BASIS,
127e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * See the License for the specific language governing permissions and
147e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * limitations under the License.
157e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki */
167e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
177e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukipackage com.android.email.mail.store.imap;
187e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
197e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.FixedLengthInputStream;
207e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.mail.store.imap.ImapElement;
217e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.mail.store.imap.ImapList;
227e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.mail.store.imap.ImapResponse;
237e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.mail.store.imap.ImapSimpleString;
247e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.mail.store.imap.ImapString;
2534f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onukiimport com.android.email.mail.transport.DiscourseLogger;
2631d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blankimport com.android.emailcommon.utility.Utility;
277e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
287e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport java.io.ByteArrayInputStream;
297e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
307e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport junit.framework.Assert;
317e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
327e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki/**
337e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Utility methods for IMAP tests.
347e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki */
357e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukipublic final class ImapTestUtils {
367e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    private ImapTestUtils() {}
377e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
387e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    // Generic constants used by various tests.
397e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapString STRING_1 = new ImapSimpleString("aBc");
407e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapString STRING_2 = new ImapSimpleString("X y z");
417e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapList LIST_1 = buildList(STRING_1);
427e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapList LIST_2 = buildList(STRING_1, STRING_2, LIST_1);
437e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
447e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    /** @see #assertElement(String, ImapElement, ImapElement) */
457e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final void assertElement(ImapElement expected, ImapElement actual) {
467e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        assertElement("(no message)", expected, actual);
477e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    }
487e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
497e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    /**
507e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     * Compare two {@link ImapElement}s and throws {@link AssertionFailedError} if different.
517e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     *
527e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     * Note this method used {@link ImapElement#equalsForTest} rather than equals().
537e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     */
547e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final void assertElement(String message, ImapElement expected,
557e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            ImapElement actual) {
567e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        if (expected == null && actual == null) {
577e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            return;
587e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        }
597e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        if (expected != null && expected.equalsForTest(actual)) {
607e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            return; // OK
617e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        }
627e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        Assert.fail(String.format("%s expected=%s\nactual=%s", message, expected, actual));
637e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    }
647e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
657e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    /** Convenience method to build an {@link ImapList} */
667e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapList buildList(ImapElement... elements) {
677e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        ImapList list = new ImapList();
687e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        for (ImapElement e : elements) {
697e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            list.add(e);
707e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        }
717e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        return list;
727e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    }
737e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
747e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    /** Convenience method to build an {@link ImapResponse} */
757e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static final ImapResponse buildResponse(String tag, boolean isContinuationRequest,
767e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            ImapElement... elements) {
777e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        ImapResponse res = new ImapResponse(tag, isContinuationRequest);
787e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        for (ImapElement e : elements) {
797e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki            res.add(e);
807e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        }
817e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        return res;
827e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    }
837e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki
847e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    /**
8534f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki     * Convenience method to build an {@link ImapResponse} from a single response.
8634f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki     */
8734f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki    public static final ImapResponse parseResponse(String line) {
8834f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki        ImapResponseParser p = new ImapResponseParser(
8934f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki                new ByteArrayInputStream(Utility.toAscii(line + "\r\n")), new DiscourseLogger(4));
9034f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki        try {
9134f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki            return p.readResponse();
9234f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki        } catch (Exception e) {
9334f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki            throw new RuntimeException(e);
9434f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki        }
9534f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki    }
9634f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki
9734f29c8a7478cf8c85578d176ac27d973ecca7e4Makoto Onuki    /**
987e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     * Convenience method to build an {@link FixedLengthInputStream} from a String, using
997e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     * US-ASCII.
1007e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki     */
1017e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    public static FixedLengthInputStream createFixedLengthInputStream(String content) {
1027e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        // Add unnecessary part.  FixedLengthInputStream should cut it.
1037e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        ByteArrayInputStream in = new ByteArrayInputStream(Utility.toAscii(content + "#trailing"));
1047e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki        return new FixedLengthInputStream(in, content.length());
1057e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki    }
1067e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki}
107