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