michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.sync.helpers; michael@0: michael@0: import static junit.framework.Assert.assertEquals; michael@0: import static junit.framework.Assert.assertTrue; michael@0: import static junit.framework.Assert.fail; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.HashMap; michael@0: import java.util.HashSet; michael@0: import java.util.Set; michael@0: import java.util.concurrent.ExecutorService; michael@0: michael@0: import junit.framework.AssertionFailedError; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionFetchRecordsDelegate; michael@0: import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate; michael@0: import org.mozilla.gecko.sync.repositories.domain.Record; michael@0: michael@0: public class DefaultFetchDelegate extends DefaultDelegate implements RepositorySessionFetchRecordsDelegate { michael@0: michael@0: private static final String LOG_TAG = "DefaultFetchDelegate"; michael@0: public ArrayList records = new ArrayList(); michael@0: public Set ignore = new HashSet(); michael@0: michael@0: @Override michael@0: public void onFetchFailed(Exception ex, Record record) { michael@0: performNotify("Fetch failed.", ex); michael@0: } michael@0: michael@0: protected void onDone(ArrayList records, HashMap expected, long end) { michael@0: Logger.debug(LOG_TAG, "onDone."); michael@0: Logger.debug(LOG_TAG, "End timestamp is " + end); michael@0: Logger.debug(LOG_TAG, "Expected is " + expected); michael@0: Logger.debug(LOG_TAG, "Records is " + records); michael@0: Set foundGuids = new HashSet(); michael@0: try { michael@0: int expectedCount = 0; michael@0: int expectedFound = 0; michael@0: Logger.debug(LOG_TAG, "Counting expected keys."); michael@0: for (String key : expected.keySet()) { michael@0: if (!ignore.contains(key)) { michael@0: expectedCount++; michael@0: } michael@0: } michael@0: Logger.debug(LOG_TAG, "Expected keys: " + expectedCount); michael@0: for (Record record : records) { michael@0: Logger.debug(LOG_TAG, "Record."); michael@0: Logger.debug(LOG_TAG, record.guid); michael@0: michael@0: // Ignore special GUIDs (e.g., for bookmarks). michael@0: if (!ignore.contains(record.guid)) { michael@0: if (foundGuids.contains(record.guid)) { michael@0: fail("Found duplicate guid " + record.guid); michael@0: } michael@0: Record expect = expected.get(record.guid); michael@0: if (expect == null) { michael@0: fail("Do not expect to get back a record with guid: " + record.guid); // Caught below michael@0: } michael@0: Logger.debug(LOG_TAG, "Checking equality."); michael@0: try { michael@0: assertTrue(expect.equalPayloads(record)); // Caught below michael@0: } catch (Exception e) { michael@0: Logger.error(LOG_TAG, "ONOZ!", e); michael@0: } michael@0: Logger.debug(LOG_TAG, "Checked equality."); michael@0: expectedFound += 1; michael@0: // Track record once we've found it. michael@0: foundGuids.add(record.guid); michael@0: } michael@0: } michael@0: assertEquals(expectedCount, expectedFound); // Caught below michael@0: Logger.debug(LOG_TAG, "Notifying success."); michael@0: performNotify(); michael@0: } catch (AssertionFailedError e) { michael@0: Logger.error(LOG_TAG, "Notifying assertion failure."); michael@0: performNotify(e); michael@0: } catch (Exception e) { michael@0: Logger.error(LOG_TAG, "No!"); michael@0: performNotify(); michael@0: } michael@0: } michael@0: michael@0: public int recordCount() { michael@0: return (this.records == null) ? 0 : this.records.size(); michael@0: } michael@0: michael@0: @Override michael@0: public void onFetchedRecord(Record record) { michael@0: Logger.debug(LOG_TAG, "onFetchedRecord(" + record.guid + ")"); michael@0: records.add(record); michael@0: } michael@0: michael@0: @Override michael@0: public void onFetchCompleted(final long fetchEnd) { michael@0: Logger.debug(LOG_TAG, "onFetchCompleted. Doing nothing."); michael@0: } michael@0: michael@0: @Override michael@0: public RepositorySessionFetchRecordsDelegate deferredFetchDelegate(final ExecutorService executor) { michael@0: return new DeferredRepositorySessionFetchRecordsDelegate(this, executor); michael@0: } michael@0: }