Refactored ChangesHappened to use generics better, started writing tests for sync message classes

pull/14/head
Sam Bosley 12 years ago
parent 646470c47b
commit cf5c725879

@ -17,22 +17,22 @@ import com.todoroo.astrid.data.OutstandingEntry;
import com.todoroo.astrid.data.RemoteModel;
@SuppressWarnings("nls")
public class ChangesHappened<TYPE extends RemoteModel> implements ClientToServerMessage {
public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEntry<TYPE>> implements ClientToServerMessage {
private final Class<? extends RemoteModel> modelClass;
private final Class<? extends OutstandingEntry<TYPE>> outstandingClass;
private final Class<OE> outstandingClass;
private final long id;
private final long uuid;
private final List<OutstandingEntry<TYPE>> changes;
private final List<OE> changes;
private long pushedAt;
private final OutstandingEntryDao<OutstandingEntry<TYPE>> outstandingDao;
private final OutstandingEntryDao<OE> outstandingDao;
public ChangesHappened(TYPE entity, RemoteModelDao<TYPE> modelDao,
OutstandingEntryDao<OutstandingEntry<TYPE>> outstandingDao) {
OutstandingEntryDao<OE> outstandingDao) {
this.modelClass = entity.getClass();
this.outstandingClass = getOutstandingClass(modelClass);
this.id = entity.getId();
this.changes = new ArrayList<OutstandingEntry<TYPE>>();
this.changes = new ArrayList<OE>();
this.outstandingDao = outstandingDao;
if (!entity.containsValue(RemoteModel.REMOTE_ID_PROPERTY)
@ -53,13 +53,17 @@ public class ChangesHappened<TYPE extends RemoteModel> implements ClientToServer
// Process changes list and send to server
}
public List<OE> getChanges() {
return changes;
}
private void populateChanges() {
TodorooCursor<OutstandingEntry<TYPE>> cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass))
TodorooCursor<OE> cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass))
.where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY)));
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
try {
OutstandingEntry<TYPE> instance = outstandingClass.newInstance();
OE instance = outstandingClass.newInstance();
instance.readPropertiesFromCursor(cursor);
changes.add(instance);
} catch (IllegalAccessException e) {
@ -73,9 +77,9 @@ public class ChangesHappened<TYPE extends RemoteModel> implements ClientToServer
}
}
private Class<? extends OutstandingEntry<TYPE>> getOutstandingClass(Class<? extends RemoteModel> model) {
private Class<OE> getOutstandingClass(Class<? extends RemoteModel> model) {
try {
return (Class<? extends OutstandingEntry<TYPE>>) getStaticFieldByReflection(model, "OUTSTANDING_MODEL");
return (Class<OE>) getStaticFieldByReflection(model, "OUTSTANDING_MODEL");
} catch (ClassCastException e) {
throw new RuntimeException("OUTSTANDING_MODEL field for class " + model.getName() + " is not of the correct type");
}

@ -0,0 +1,39 @@
package com.todoroo.astrid.sync;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskOutstandingDao;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
public class NewSyncTestCase extends DatabaseTestCase {
@Autowired
protected TaskDao taskDao;
@Autowired
protected TagDataDao tagDataDao;
@Autowired
protected TaskOutstandingDao taskOutstandingDao;
@Autowired
protected
TagOutstandingDao tagOutstandingDao;
protected Task createTask() {
Task task = new Task();
task.setValue(Task.TITLE, "new task");
taskDao.createNew(task);
return task;
}
protected TagData createTagData() {
TagData tag = new TagData();
tag.setValue(TagData.NAME, "new tag");
tagDataDao.createNew(tag);
return tag;
}
}

@ -0,0 +1,19 @@
package com.todoroo.astrid.sync;
import com.todoroo.astrid.actfm.sync.messages.ChangesHappened;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskOutstanding;
public class SyncMessageTest extends NewSyncTestCase {
public void testTaskChangesHappenedConstructor() {
Task t = createTask();
try {
ChangesHappened<Task, TaskOutstanding> changes = new ChangesHappened<Task, TaskOutstanding>(t, taskDao, taskOutstandingDao);
assertTrue(changes.getChanges().size() > 0);
} catch (Exception e) {
fail("ChangesHappened constructor threw exception " + e);
}
}
}

@ -17,13 +17,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskOutstanding;
import com.todoroo.astrid.test.DatabaseTestCase;
public class SyncModelTest extends DatabaseTestCase {
@Autowired TaskDao taskDao;
@Autowired TagDataDao tagDataDao;
@Autowired TaskOutstandingDao taskOutstandingDao;
@Autowired TagOutstandingDao tagOutstandingDao;
public class SyncModelTest extends NewSyncTestCase {
public void testCreateTaskMakesUuid() {
Task task = createTask();
@ -96,21 +90,4 @@ public class SyncModelTest extends DatabaseTestCase {
cursor.close();
}
}
private Task createTask() {
Task task = new Task();
task.setValue(Task.TITLE, "new task");
taskDao.createNew(task);
return task;
}
private TagData createTagData() {
TagData tag = new TagData();
tag.setValue(TagData.NAME, "new tag");
tagDataDao.createNew(tag);
return tag;
}
}

Loading…
Cancel
Save