Source:
<code>public static void testMoveToList() { <START> int listId1 = StoredList.STANDARD_LIST_ID; <END> int listId2 = StoredList.STANDARD_LIST_ID; int listId3 = StoredList.STANDARD_LIST_ID; final Geocache cache1 = new Geocache(); cache1.setGeocode("Cache 1"); final Geocache cache2 = new Geocache(); cache2.setGeocode("Cache 2"); assertThat(cache2).isNotNull(); try { listId1 = DataStore.createList("DataStore Test"); assertThat(listId1).isGreaterThan(StoredList.STANDARD_LIST_ID); listId2 = DataStore.createList("DataStoreTest"); assertThat(listId2).isGreaterThan(StoredList.STANDARD_LIST_ID); listId3 = DataStore.createList("Target"); assertThat(DataStore.getLists().size()).isGreaterThanOrEqualTo(3); cache1.setDetailed(true); cache1.getLists().add(listId1); cache1.getLists().add(listId2); cache2.setDetailed(true); cache2.getLists().add(listId1); DataStore.saveCache(cache1, LoadFlags.SAVE_ALL); DataStore.saveCache(cache2, LoadFlags.SAVE_ALL); assertThat(DataStore.getAllCachesCount()).isGreaterThanOrEqualTo(2); DataStore.moveToList(Collections.singletonList(cache1), listId3); assertThat(DataStore.getAllStoredCachesCount(CacheType.ALL, listId3)).isEqualTo(1); Geocache movedCache = DataStore.loadCache(cache1.getGeocode(), LoadFlags.LOAD_ALL_DB_ONLY); assertThat(movedCache).isNotNull(); if (movedCache != null) { assertThat(movedCache.getLists().size()).isEqualTo(1); } } finally { final Set<String> geocodes = new HashSet<>(); geocodes.add(cache1.getGeocode()); geocodes.add(cache2.getGeocode()); DataStore.removeCaches(geocodes, LoadFlags.REMOVE_ALL); DataStore.removeList(listId1); DataStore.removeList(listId2); DataStore.removeList(listId3); } }</code><technical_language>If exception in DataStore.createList, remove wrong list. If exception, initialize list1 friends right final</technical_language>

Target:
public static void testMoveToList() { Integer listId1 = null; Integer listId2 = null; Integer listId3 = null; final Geocache cache1 = new Geocache(); cache1.setGeocode("Cache 1"); final Geocache cache2 = new Geocache(); cache2.setGeocode("Cache 2"); assertThat(cache2).isNotNull(); try { listId1 = DataStore.createList("DataStore Test"); assertThat(listId1).isGreaterThan(StoredList.STANDARD_LIST_ID); listId2 = DataStore.createList("DataStoreTest"); assertThat(listId2).isGreaterThan(StoredList.STANDARD_LIST_ID); listId3 = DataStore.createList("Target"); assertThat(DataStore.getLists().size()).isGreaterThanOrEqualTo(3); cache1.setDetailed(true); cache1.getLists().add(listId1); cache1.getLists().add(listId2); cache2.setDetailed(true); cache2.getLists().add(listId1); DataStore.saveCache(cache1, LoadFlags.SAVE_ALL); DataStore.saveCache(cache2, LoadFlags.SAVE_ALL); assertThat(DataStore.getAllCachesCount()).isGreaterThanOrEqualTo(2); DataStore.moveToList(Collections.singletonList(cache1), listId3); assertThat(DataStore.getAllStoredCachesCount(CacheType.ALL, listId3)).isEqualTo(1); Geocache movedCache = DataStore.loadCache(cache1.getGeocode(), LoadFlags.LOAD_ALL_DB_ONLY); assertThat(movedCache).isNotNull(); if (movedCache != null) { assertThat(movedCache.getLists().size()).isEqualTo(1); } } finally { final Set<String> geocodes = new HashSet<>(); geocodes.add(cache1.getGeocode()); geocodes.add(cache2.getGeocode()); DataStore.removeCaches(geocodes, LoadFlags.REMOVE_ALL); if (listId1 != null) { DataStore.removeList(listId1); } if (listId2 != null) { DataStore.removeList(listId2); } if (listId3 != null) { DataStore.removeList(listId3); } } }