FileDocCategorySizeDatePackage
MockTransport.javaAPI DocAndroid 1.5 API11241Wed May 06 22:42:46 BST 2009com.android.email.mail.transport

MockTransport

public class MockTransport extends Object implements com.android.email.mail.Transport
This is a mock Transport that is used to test protocols that use MailTransport.

Fields Summary
private static boolean
DEBUG_LOG_STREAMS
private static String
LOG_TAG
private boolean
mSslAllowed
private boolean
mTlsAllowed
private boolean
mOpen
private boolean
mInputOpen
private int
mConnectionSecurity
private ArrayList
mQueuedInput
private ArrayList
mPairs
Constructors Summary
Methods Summary
public booleancanTrySslSecurity()

        return (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED
                || mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL);
    
public booleancanTryTlsSecurity()

        return (mConnectionSecurity == Transport.CONNECTION_SECURITY_TLS_OPTIONAL
                || mConnectionSecurity == Transport.CONNECTION_SECURITY_TLS_REQUIRED);
    
public voidclose()

        mOpen = false;
        mInputOpen = false;
        // unless it was expected as part of a test, reset the stream
        if (mPairs.size() > 0) {
            Transaction expect = mPairs.remove(0);
            if (expect.mAction == Transaction.ACTION_CLIENT_CLOSE) {
                return;
            }
        }
        mQueuedInput.clear();
        mPairs.clear();
    
public voidcloseInputStream()
This simulates a condition where the server has closed its side, causing reads to fail.

        mInputOpen = false;
    
public voidexpect(java.lang.String pattern, java.lang.String response)
Give the mock a pattern to wait for and a response to send back.

param
pattern Java RegEx to wait for
param
response String to reply with, or null to acccept string but not respond to it

    
                                              
          
        expect(pattern, (response == null) ? null : new String[] { response });
    
public voidexpect(java.lang.String pattern, java.lang.String[] responses)
Give the mock a pattern to wait for and a multi-line response to send back.

param
pattern Java RegEx to wait for
param
responses Strings to reply with

        Transaction pair = new Transaction(pattern, responses);
        mPairs.add(pair);
    
public voidexpectClose()
Tell the Mock Transport that we expect it to be closed. This will preserve the remaining entries in the expect() stream and allow us to "ride over" the close (which would normally reset everything).

        mPairs.add(new Transaction(Transaction.ACTION_CLIENT_CLOSE));
    
public java.lang.StringgetHost()

        SmtpSenderUnitTests.fail("getHost() not implemented");
        return null;
    
public java.io.InputStreamgetInputStream()

        SmtpSenderUnitTests.assertTrue(mOpen);
        return new MockInputStream();
    
public java.io.OutputStreamgetOutputStream()

        SmtpSenderUnitTests.assertTrue(mOpen);
        return new MockOutputStream();
    
public intgetPort()

        SmtpSenderUnitTests.fail("getPort() not implemented");
        return 0;
    
public intgetSecurity()

        return mConnectionSecurity;
    
public java.lang.String[]getUserInfoParts()

        SmtpSenderUnitTests.fail("getUserInfoParts() not implemented");
        return null;
    
public booleanisOpen()

        return mOpen;
    
public com.android.email.mail.TransportnewInstanceWithConfiguration()
This normally serves as a pseudo-clone, for use by Imap. For the purposes of unit testing, until we need something more complex, we'll just return the actual MockTransport. Then we don't have to worry about dealing with test metadata like the expects list or socket state.

         return this;
    
public voidopen()

        mOpen = true;
        mInputOpen = true;
    
public java.lang.StringreadLine()
This returns one string (if available) to the caller. Usually this simply pulls strings from the mQueuedInput list, but if the list is empty, we also peek the expect list. This supports banners, multi-line responses, and any other cases where we respond without a specific expect pattern. If no response text is available, we assert (failing our test) as an underflow. Logs the read text if DEBUG_LOG_STREAMS is true.

        SmtpSenderUnitTests.assertTrue(mOpen);
        if (!mInputOpen) {
            throw new IOException("Reading from MockTransport with closed input");
        }
        // if there's nothing to read, see if we can find a null-pattern response
        if (0 == mQueuedInput.size()) {
            Transaction pair = mPairs.get(0);
            if (pair != null && pair.mPattern == null) {
                mPairs.remove(0);
                sendResponse(pair.mResponses);
            }
        }
        SmtpSenderUnitTests.assertTrue("Underflow reading from MockTransport", 0 != mQueuedInput.size());
        String line = mQueuedInput.remove(0);
        if (DEBUG_LOG_STREAMS) {
            Log.d(LOG_TAG, "<<< " + line);
        }
        return line;
    
public voidreopenTls()

        SmtpSenderUnitTests.assertTrue(mOpen);
        SmtpSenderUnitTests.assertTrue(mTlsAllowed);
        SmtpSenderUnitTests.fail("reopenTls() not implemented");
    
private voidsendResponse(java.lang.String[] responses)

        for (String s : responses) {
            mQueuedInput.add(s);
        }
    
public voidsetSecurity(int connectionSecurity)

        mConnectionSecurity = connectionSecurity;
    
public voidsetSoTimeout(int timeoutMilliseconds)

    
public voidsetUri(java.net.URI uri, int defaultPort)

        SmtpSenderUnitTests.assertTrue("Don't call setUri on a mock transport", false);
    
public voidwriteLine(java.lang.String s, java.lang.String sensitiveReplacement)
Accepts a single string (command or text) that was written by the code under test. Because we are essentially mocking a server, we check to see if this string was expected. If the string was expected, we push the corresponding responses into the mQueuedInput list, for subsequent calls to readLine(). If the string does not match, we assert the mismatch. If no string was expected, we assert it as an overflow. Logs the written text if DEBUG_LOG_STREAMS is true.

        if (DEBUG_LOG_STREAMS) {
            Log.d(LOG_TAG, ">>> " + s);
        }
        SmtpSenderUnitTests.assertTrue(mOpen);
        SmtpSenderUnitTests.assertTrue("Overflow writing to MockTransport", 0 != mPairs.size());
        Transaction pair = mPairs.remove(0);
        SmtpSenderUnitTests.assertTrue("Unexpected string written to MockTransport", 
                pair.mPattern != null && s.matches(pair.mPattern));
        if (pair.mResponses != null) {
            sendResponse(pair.mResponses);
        }