FileDocCategorySizeDatePackage
VCardVerifier.javaAPI DocAndroid 5.1 API13710Thu Mar 12 22:22:54 GMT 2015com.android.vcard.tests.testutils

VCardVerifier

public class VCardVerifier extends Object

The class lets users checks that given expected vCard data are same as given actual vCard data. Able to verify both vCard importer/exporter.

First a user has to initialize the object by calling either {@link #initForImportTest(int, int)} or {@link #initForExportTest(int)}. "Round trip test" (import -> export -> import, or export -> import -> export) is not supported.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DEBUG
static final String
VCARD_TEST_AUTHORITY
Special URI for testing.
private static final android.net.Uri
VCARD_TEST_AUTHORITY_URI
static final android.net.Uri
CONTACTS_TEST_CONTENT_URI
private final android.test.AndroidTestCase
mAndroidTestCase
private int
mVCardType
private boolean
mIsDoCoMo
private ExportTestResolver
mExportTestResolver
private InputStream
mInputStream
private PropertyNodesVerifier
mPropertyNodesVerifier
private LineVerifier
mLineVerifier
private ContentValuesVerifier
mContentValuesVerifier
private boolean
mInitialized
private boolean
mVerified
private String
mCharset
private String
mExceptionContents
Constructors Summary
public VCardVerifier(android.test.AndroidTestCase androidTestCase)


    // Called by VCardTestsBase
       
        mAndroidTestCase = androidTestCase;
        mExportTestResolver = null;
        mInputStream = null;
        mInitialized = false;
        mVerified = false;
    
Methods Summary
public ContentValuesVerifierElemaddContentValuesVerifierElem()

        if (!mInitialized) {
            AndroidTestCase.fail("Not initialized");
        }
        if (mContentValuesVerifier == null) {
            mContentValuesVerifier = new ContentValuesVerifier();
        }

        return mContentValuesVerifier.addElem(mAndroidTestCase);
    
public ContactEntryaddInputEntry()

        if (!mInitialized) {
            AndroidTestCase.fail("Not initialized");
        }
        if (mInputStream != null) {
            AndroidTestCase.fail("setInputStream is called");
        }
        return mExportTestResolver.addInputContactEntry();
    
public LineVerifierElemaddLineVerifierElem()

        if (!mInitialized) {
            AndroidTestCase.fail("Not initialized");
        }
        if (mLineVerifier == null) {
            mLineVerifier = new LineVerifier(mAndroidTestCase, mVCardType);
        }
        return mLineVerifier.addLineVerifierElem();
    
public PropertyNodesVerifierElemaddPropertyNodesVerifierElem()

        final PropertyNodesVerifierElem elem = addPropertyNodesVerifierElemWithoutVersion();
        final String versionString;
        if (VCardConfig.isVersion21(mVCardType)) {
            versionString = "2.1";
        } else if (VCardConfig.isVersion30(mVCardType)) {
            versionString = "3.0";
        } else if (VCardConfig.isVersion40(mVCardType)) {
            versionString = "4.0";
        } else {
            throw new RuntimeException("Unexpected vcard type during a unit test");
        }
        elem.addExpectedNodeWithOrder("VERSION", versionString);

        return elem;
    
public PropertyNodesVerifierElemaddPropertyNodesVerifierElemWithEmptyName()

        if (!mInitialized) {
            AndroidTestCase.fail("Not initialized");
        }
        final PropertyNodesVerifierElem elem = addPropertyNodesVerifierElem();
        if (VCardConfig.isVersion40(mVCardType)) {
            elem.addExpectedNodeWithOrder("FN", "");
        } else if (VCardConfig.isVersion30(mVCardType)) {
            elem.addExpectedNodeWithOrder("N", "");
            elem.addExpectedNodeWithOrder("FN", "");
        } else if (mIsDoCoMo) {
            elem.addExpectedNodeWithOrder("N", "");
        }
        return elem;
    
public PropertyNodesVerifierElemaddPropertyNodesVerifierElemWithoutVersion()

        if (!mInitialized) {
            AndroidTestCase.fail("Not initialized");
        }
        if (mPropertyNodesVerifier == null) {
            mPropertyNodesVerifier = new PropertyNodesVerifier(mAndroidTestCase);
        }
        return mPropertyNodesVerifier.addPropertyNodesVerifierElem();
    
public voidaddVCardExceptionVerifier(java.lang.String contents)

        mExceptionContents = contents;
    
private java.lang.reflect.MethodgetMockGetEntityIteratorMethod()

        return this.getClass().getMethod("mockGetEntityIteratorMethod",
                ContentResolver.class, Uri.class, String.class, String[].class, String.class);
    
public voidinitForExportTest(int vcardType)

        initForExportTest(vcardType, "UTF-8");
    
public voidinitForExportTest(int vcardType, java.lang.String charset)

        if (mInitialized) {
            AndroidTestCase.fail("Already initialized");
        }
        mExportTestResolver = new ExportTestResolver(mAndroidTestCase);
        mVCardType = vcardType;
        mIsDoCoMo = VCardConfig.isDoCoMo(vcardType);
        mInitialized = true;
        if (TextUtils.isEmpty(charset)) {
            mCharset = "UTF-8";
        } else {
            mCharset = charset;
        }
    
public voidinitForImportTest(int vcardType, int resId)

        if (mInitialized) {
            AndroidTestCase.fail("Already initialized");
        }
        mVCardType = vcardType;
        mIsDoCoMo = VCardConfig.isDoCoMo(vcardType);
        setInputResourceId(resId);
        mInitialized = true;
    
public static android.content.EntityIteratormockGetEntityIteratorMethod(android.content.ContentResolver resolver, android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)

        if (ExportTestResolver.class.equals(resolver.getClass())) {
            return ((ExportTestResolver)resolver).getProvider().queryEntities(
                    uri, selection, selectionArgs, sortOrder);
        }

        Log.e(LOG_TAG, "Unexpected provider given.");
        return null;
    
private voidsetInputResourceId(int resId)

        final InputStream inputStream =
                mAndroidTestCase.getContext().getResources().openRawResource(resId);
        if (inputStream == null) {
            AndroidTestCase.fail("Wrong resId: " + resId);
        }
        setInputStream(inputStream);
    
private voidsetInputStream(java.io.InputStream inputStream)

        if (mExportTestResolver != null) {
            AndroidTestCase.fail("addInputEntry() is called.");
        } else if (mInputStream != null) {
            AndroidTestCase.fail("InputStream is already set");
        }
        mInputStream = inputStream;
    
public voidverify()

        if (!mInitialized) {
            TestCase.fail("Not initialized.");
        }
        if (mVerified) {
            TestCase.fail("verify() was called twice.");
        }

        if (mInputStream != null) {
            if (mExportTestResolver != null){
                TestCase.fail("There are two input sources.");
            }
            verifyForImportTest();
        } else if (mExportTestResolver != null){
            verifyForExportTest();
        } else {
            TestCase.fail("No input is determined");
        }
        mVerified = true;
    
private voidverifyForExportTest()

        final CustomMockContext context = new CustomMockContext(mExportTestResolver);
        final ContentResolver resolver = context.getContentResolver();
        final VCardComposer composer = new VCardComposer(context, mVCardType, mCharset);
        // projection is ignored.
        final Cursor cursor = resolver.query(CONTACTS_TEST_CONTENT_URI, null, null, null, null);
        if (!composer.init(cursor)) {
            AndroidTestCase.fail("init() failed. Reason: " + composer.getErrorReason());
        }
        AndroidTestCase.assertFalse(composer.isAfterLast());
        try {
            while (!composer.isAfterLast()) {
                Method mockGetEntityIteratorMethod = null;
                try {
                    mockGetEntityIteratorMethod = getMockGetEntityIteratorMethod();
                } catch (Exception e) {
                    AndroidTestCase.fail("Exception thrown: " + e);
                }
                AndroidTestCase.assertNotNull(mockGetEntityIteratorMethod);
                final String vcard = composer.createOneEntry(mockGetEntityIteratorMethod);
                AndroidTestCase.assertNotNull(vcard);
                if (mLineVerifier != null) {
                    mLineVerifier.verify(vcard);
                }
                verifyOneVCardForExport(vcard);
            }
        } finally {
            composer.terminate();
        }
    
private voidverifyForImportTest()

        if (mLineVerifier != null) {
            AndroidTestCase.fail("Not supported now.");
        }

        try {
            verifyWithInputStream(mInputStream);
        } catch (IOException e) {
            AndroidTestCase.fail("IOException was thrown: " + e.getMessage());
        } finally {
            if (mInputStream != null) {
                try {
                    mInputStream.close();
                } catch (IOException e) {
                }
            }
        }
    
private voidverifyOneVCardForExport(java.lang.String vcard)

        if (DEBUG) Log.d(LOG_TAG, vcard);
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(vcard.getBytes(mCharset));
            verifyWithInputStream(is);
        } catch (IOException e) {
            AndroidTestCase.fail("Unexpected IOException: " + e.getMessage());
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    AndroidTestCase.fail("Unexpected IOException: " + e.getMessage());
                }
            }
        }
    
private voidverifyWithInputStream(java.io.InputStream is)
Sets up sub-verifiers correctly and tries to parse vCard as {@link InputStream}. Errors around InputStream must be handled outside this method. Used both from {@link #verifyForImportTest()} and from {@link #verifyForExportTest()}.

        try {
            // Note: we must not specify charset toward vCard parsers. This code checks whether
            // those parsers are able to encode given binary without any extra information for
            // charset.
            final VCardParser parser = VCardUtils.getAppropriateParser(mVCardType);
            if (mContentValuesVerifier != null) {
                final VCardEntryConstructor constructor = new VCardEntryConstructor(mVCardType);
                constructor.addEntryHandler(mContentValuesVerifier);
                parser.addInterpreter(constructor);
            }
            if (mPropertyNodesVerifier != null) {
                parser.addInterpreter(mPropertyNodesVerifier);
            }
            parser.parse(is);
            if (mExceptionContents != null) {
                // exception contents exists, we expect an exception to occur.
                AndroidTestCase.fail();
            }
        } catch (VCardException e) {
            if (mExceptionContents != null) {
                AndroidTestCase.assertTrue(e.getMessage().contains(mExceptionContents));
            } else {
                Log.e(LOG_TAG, "VCardException", e);
                AndroidTestCase.fail("Unexpected VCardException: " + e.getMessage());
            }
        }