1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package cgeo.geocaching;
import cgeo.CGeoTestCase;
import cgeo.geocaching.connector.gc.GCConnector;
import cgeo.geocaching.connector.gc.Tile;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.list.StoredList;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DataStoreTest extends CGeoTestCase {
public static void testStoredLists() {
int listId1 = StoredList.STANDARD_LIST_ID;
int listId2 = StoredList.STANDARD_LIST_ID;
// create caches
final Geocache cache1 = new Geocache();
cache1.setGeocode("Cache 1");
final Geocache cache2 = new Geocache();
cache2.setGeocode("Cache 2");
assertNotNull(cache2);
try {
// create lists
listId1 = DataStore.createList("DataStore Test");
assertTrue(listId1 > StoredList.STANDARD_LIST_ID);
listId2 = DataStore.createList("DataStoreTest");
assertTrue(listId2 > StoredList.STANDARD_LIST_ID);
assertTrue(DataStore.getLists().size() >= 2);
cache1.setDetailed(true);
cache1.setListId(listId1);
cache2.setDetailed(true);
cache2.setListId(listId1);
// save caches to DB (cache1=listId1, cache2=listId1)
DataStore.saveCache(cache1, LoadFlags.SAVE_ALL);
DataStore.saveCache(cache2, LoadFlags.SAVE_ALL);
assertTrue(DataStore.getAllCachesCount() >= 2);
// rename list (cache1=listId1, cache2=listId1)
assertEquals(1, DataStore.renameList(listId1, "DataStore Test (renamed)"));
// get list
final StoredList list1 = DataStore.getList(listId1);
assertEquals("DataStore Test (renamed)", list1.title);
// move to list (cache1=listId2, cache2=listId2)
DataStore.moveToList(Collections.singletonList(cache1), listId2);
assertEquals(1, DataStore.getAllStoredCachesCount(CacheType.ALL, listId2));
// remove list (cache1=listId2, cache2=listId2)
assertTrue(DataStore.removeList(listId1));
// mark dropped (cache1=1, cache2=0)
DataStore.markDropped(Collections.singletonList(cache2));
// mark stored (cache1=1, cache2=listId2)
DataStore.moveToList(Collections.singletonList(cache2), listId2);
assertEquals(2, DataStore.getAllStoredCachesCount(CacheType.ALL, listId2));
// drop stored (cache1=0, cache2=0)
DataStore.removeList(listId2);
} finally {
// remove caches
final Set<String> geocodes = new HashSet<String>();
geocodes.add(cache1.getGeocode());
geocodes.add(cache2.getGeocode());
DataStore.removeCaches(geocodes, LoadFlags.REMOVE_ALL);
// remove list
DataStore.removeList(listId1);
DataStore.removeList(listId2);
}
}
// Check that queries don't throw an exception (see issue #1429).
public static void testLoadWaypoints() {
final Viewport viewport = new Viewport(new Geopoint(-1, -2), new Geopoint(3, 4));
DataStore.loadWaypoints(viewport, false, false, CacheType.ALL);
DataStore.loadWaypoints(viewport, false, true, CacheType.ALL);
DataStore.loadWaypoints(viewport, true, false, CacheType.ALL);
DataStore.loadWaypoints(viewport, true, true, CacheType.ALL);
DataStore.loadWaypoints(viewport, false, false, CacheType.TRADITIONAL);
DataStore.loadWaypoints(viewport, false, true, CacheType.TRADITIONAL);
DataStore.loadWaypoints(viewport, true, false, CacheType.TRADITIONAL);
DataStore.loadWaypoints(viewport, true, true, CacheType.TRADITIONAL);
}
// Check that saving a cache and trackable without logs works (see #2199)
public static void testSaveWithoutLogs() {
final String GEOCODE_CACHE = "TEST";
// create cache and trackable
final Geocache cache = new Geocache();
cache.setGeocode(GEOCODE_CACHE);
cache.setDetailed(true);
final Trackable trackable = new Trackable();
trackable.setLogs(null);
final List<Trackable> inventory = new ArrayList<Trackable>();
inventory.add(trackable);
cache.setInventory(inventory);
try {
DataStore.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
final Geocache loadedCache = DataStore.loadCache(GEOCODE_CACHE, LoadFlags.LOAD_ALL_DB_ONLY);
assertNotNull("Cache was not saved!", loadedCache);
assertEquals(1, loadedCache.getInventory().size());
} finally {
DataStore.removeCache(GEOCODE_CACHE, LoadFlags.REMOVE_ALL);
}
}
// Check that loading a cache by case insensitive geo code works correctly (see #3139)
public static void testGeocodeCaseInsensitive() {
final String GEOCODE_CACHE = "TEST";
final String upperCase = GEOCODE_CACHE;
final String lowerCase = StringUtils.lowerCase(upperCase);
assertFalse(upperCase.equals(lowerCase));
// create cache and trackable
final Geocache cache = new Geocache();
cache.setGeocode(upperCase);
cache.setDetailed(true);
try {
final Geocache oldCache = DataStore.loadCache(upperCase, LoadFlags.LOAD_ALL_DB_ONLY);
assertNull("Database contained old cache!", oldCache);
DataStore.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
final Geocache cacheWithOriginalCode = DataStore.loadCache(upperCase, LoadFlags.LOAD_ALL_DB_ONLY);
assertNotNull("Cache was not saved correctly!", cacheWithOriginalCode);
final Geocache cacheLowerCase = DataStore.loadCache(lowerCase, LoadFlags.LOAD_ALL_DB_ONLY);
assertNotNull("Could not find cache by case insensitive geocode", cacheLowerCase);
} finally {
DataStore.removeCache(upperCase, LoadFlags.REMOVE_ALL);
}
}
// Loading logs for an empty geocode should return an empty list, not null!
public static void testLoadLogsFromEmptyGeocode() {
final List<LogEntry> logs = DataStore.loadLogs("");
assertNotNull("Logs must not be null", logs);
assertEquals("Logs from empty geocode must be empty", 0, logs.size());
}
public static void testLoadCacheHistory() {
int sumCaches = 0;
int allCaches = 0;
for (CacheType cacheType : CacheType.values()) {
SearchResult historyOfType = DataStore.getHistoryOfCaches(false, cacheType);
assertNotNull(historyOfType);
if (cacheType != CacheType.ALL) {
sumCaches += historyOfType.getCount();
} else {
allCaches = historyOfType.getCount();
}
}
// check that sum of types equals 'all'
assertEquals(sumCaches, allCaches);
// check that two different routines behave the same
assertEquals(DataStore.getAllHistoryCachesCount(), sumCaches);
}
public static void testCachedMissing() {
// Tile to test
final Tile tile = new Tile(new Geopoint("N49 44.0 E8 37.0"), 14);
final Set<Tile> tiles = new HashSet<Tile>();
tiles.add(tile);
// set up geocaches to fill into cacheCache
final Geocache main = new Geocache();
main.setGeocode("GC12345");
main.setCoords(new Geopoint("N49 44.0 E8 37.0"));
final Geocache inTileLowZoom = new Geocache();
inTileLowZoom.setGeocode("GC12346");
inTileLowZoom.setCoords(new Geopoint("N49 44.001 E8 37.001"), Tile.ZOOMLEVEL_MIN_PERSONALIZED - 5);
final Geocache outTile = new Geocache();
outTile.setGeocode("GC12347");
outTile.setCoords(new Geopoint(tile.getViewport().getLatitudeMin() - 0.1, tile.getViewport().getLongitudeMin() - 0.1));
final Geocache otherConnector = new Geocache();
otherConnector.setGeocode("OC0001");
otherConnector.setCoords(new Geopoint("N49 44.0 E8 37.0"));
final Geocache inTileHighZoom = new Geocache();
inTileHighZoom.setGeocode("GC12348");
inTileHighZoom.setCoords(new Geopoint("N49 44.001 E8 37.001"), Tile.ZOOMLEVEL_MIN_PERSONALIZED + 1);
// put in cache
DataStore.saveCache(main, EnumSet.of(SaveFlag.SAVE_CACHE));
DataStore.saveCache(inTileLowZoom, EnumSet.of(SaveFlag.SAVE_CACHE));
DataStore.saveCache(inTileHighZoom, EnumSet.of(SaveFlag.SAVE_CACHE));
DataStore.saveCache(outTile, EnumSet.of(SaveFlag.SAVE_CACHE));
DataStore.saveCache(otherConnector, EnumSet.of(SaveFlag.SAVE_CACHE));
final SearchResult search = new SearchResult(main);
Set<String> filteredGeoCodes = DataStore.getCachedMissingFromSearch(search, tiles, GCConnector.getInstance(), Tile.ZOOMLEVEL_MIN_PERSONALIZED - 1);
assertTrue(filteredGeoCodes.contains(inTileLowZoom.getGeocode()));
assertFalse(filteredGeoCodes.contains(inTileHighZoom.getGeocode()));
assertFalse(filteredGeoCodes.contains(otherConnector.getGeocode()));
assertFalse(filteredGeoCodes.contains(outTile.getGeocode()));
assertFalse(filteredGeoCodes.contains(main.getGeocode()));
}
}
|