mobile/android/tests/background/junit3/src/sync/helpers/DefaultFetchDelegate.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.sync.helpers;
michael@0 5
michael@0 6 import static junit.framework.Assert.assertEquals;
michael@0 7 import static junit.framework.Assert.assertTrue;
michael@0 8 import static junit.framework.Assert.fail;
michael@0 9
michael@0 10 import java.util.ArrayList;
michael@0 11 import java.util.HashMap;
michael@0 12 import java.util.HashSet;
michael@0 13 import java.util.Set;
michael@0 14 import java.util.concurrent.ExecutorService;
michael@0 15
michael@0 16 import junit.framework.AssertionFailedError;
michael@0 17
michael@0 18 import org.mozilla.gecko.background.common.log.Logger;
michael@0 19 import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionFetchRecordsDelegate;
michael@0 20 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate;
michael@0 21 import org.mozilla.gecko.sync.repositories.domain.Record;
michael@0 22
michael@0 23 public class DefaultFetchDelegate extends DefaultDelegate implements RepositorySessionFetchRecordsDelegate {
michael@0 24
michael@0 25 private static final String LOG_TAG = "DefaultFetchDelegate";
michael@0 26 public ArrayList<Record> records = new ArrayList<Record>();
michael@0 27 public Set<String> ignore = new HashSet<String>();
michael@0 28
michael@0 29 @Override
michael@0 30 public void onFetchFailed(Exception ex, Record record) {
michael@0 31 performNotify("Fetch failed.", ex);
michael@0 32 }
michael@0 33
michael@0 34 protected void onDone(ArrayList<Record> records, HashMap<String, Record> expected, long end) {
michael@0 35 Logger.debug(LOG_TAG, "onDone.");
michael@0 36 Logger.debug(LOG_TAG, "End timestamp is " + end);
michael@0 37 Logger.debug(LOG_TAG, "Expected is " + expected);
michael@0 38 Logger.debug(LOG_TAG, "Records is " + records);
michael@0 39 Set<String> foundGuids = new HashSet<String>();
michael@0 40 try {
michael@0 41 int expectedCount = 0;
michael@0 42 int expectedFound = 0;
michael@0 43 Logger.debug(LOG_TAG, "Counting expected keys.");
michael@0 44 for (String key : expected.keySet()) {
michael@0 45 if (!ignore.contains(key)) {
michael@0 46 expectedCount++;
michael@0 47 }
michael@0 48 }
michael@0 49 Logger.debug(LOG_TAG, "Expected keys: " + expectedCount);
michael@0 50 for (Record record : records) {
michael@0 51 Logger.debug(LOG_TAG, "Record.");
michael@0 52 Logger.debug(LOG_TAG, record.guid);
michael@0 53
michael@0 54 // Ignore special GUIDs (e.g., for bookmarks).
michael@0 55 if (!ignore.contains(record.guid)) {
michael@0 56 if (foundGuids.contains(record.guid)) {
michael@0 57 fail("Found duplicate guid " + record.guid);
michael@0 58 }
michael@0 59 Record expect = expected.get(record.guid);
michael@0 60 if (expect == null) {
michael@0 61 fail("Do not expect to get back a record with guid: " + record.guid); // Caught below
michael@0 62 }
michael@0 63 Logger.debug(LOG_TAG, "Checking equality.");
michael@0 64 try {
michael@0 65 assertTrue(expect.equalPayloads(record)); // Caught below
michael@0 66 } catch (Exception e) {
michael@0 67 Logger.error(LOG_TAG, "ONOZ!", e);
michael@0 68 }
michael@0 69 Logger.debug(LOG_TAG, "Checked equality.");
michael@0 70 expectedFound += 1;
michael@0 71 // Track record once we've found it.
michael@0 72 foundGuids.add(record.guid);
michael@0 73 }
michael@0 74 }
michael@0 75 assertEquals(expectedCount, expectedFound); // Caught below
michael@0 76 Logger.debug(LOG_TAG, "Notifying success.");
michael@0 77 performNotify();
michael@0 78 } catch (AssertionFailedError e) {
michael@0 79 Logger.error(LOG_TAG, "Notifying assertion failure.");
michael@0 80 performNotify(e);
michael@0 81 } catch (Exception e) {
michael@0 82 Logger.error(LOG_TAG, "No!");
michael@0 83 performNotify();
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 public int recordCount() {
michael@0 88 return (this.records == null) ? 0 : this.records.size();
michael@0 89 }
michael@0 90
michael@0 91 @Override
michael@0 92 public void onFetchedRecord(Record record) {
michael@0 93 Logger.debug(LOG_TAG, "onFetchedRecord(" + record.guid + ")");
michael@0 94 records.add(record);
michael@0 95 }
michael@0 96
michael@0 97 @Override
michael@0 98 public void onFetchCompleted(final long fetchEnd) {
michael@0 99 Logger.debug(LOG_TAG, "onFetchCompleted. Doing nothing.");
michael@0 100 }
michael@0 101
michael@0 102 @Override
michael@0 103 public RepositorySessionFetchRecordsDelegate deferredFetchDelegate(final ExecutorService executor) {
michael@0 104 return new DeferredRepositorySessionFetchRecordsDelegate(this, executor);
michael@0 105 }
michael@0 106 }

mercurial