Permalink
Browse files

make mock model persistences deep copy the models they return; update…

… tests not to check for instance equivalence when checking cache behavior; make ModelWithId's equals() method value-compare byte arrays
  • Loading branch information...
1 parent fa5f324 commit 1d4b44b7df3b02af4be868a9f649cc0532f2acc7 bryanduxbury committed Feb 8, 2012
@@ -15,13 +15,14 @@
package com.rapleaf.jack;
import java.io.Serializable;
+import java.nio.ByteBuffer;
import java.util.Set;
import org.apache.commons.lang.builder.HashCodeBuilder;
import com.rapleaf.jack.test_project.IDatabases;
-public abstract class ModelWithId implements Serializable {
+public abstract class ModelWithId<T> implements Serializable {
private final long id;
transient protected int cachedHashCode = 0;
@@ -76,6 +77,10 @@ public boolean equals(ModelWithId obj) {
Object value1 = getField(field.name());
Object value2 = obj.getField(field.name());
if (value1 != null) {
+ if (value1 instanceof byte[]) {
+ value1 = ByteBuffer.wrap((byte[]) value1);
+ value2 = ByteBuffer.wrap((byte[]) value2);
+ }
if(!value1.equals(value2)) {
return false;
}
@@ -88,9 +93,9 @@ public boolean equals(ModelWithId obj) {
return true;
}
- public abstract ModelWithId getCopy();
+ public abstract T getCopy();
- public abstract ModelWithId getCopy(IDatabases databases);
+ public abstract T getCopy(IDatabases databases);
public abstract Object getField(String fieldName);
@@ -67,6 +67,14 @@ public Comment create(final int commenter_id, final long commented_on_id, final
}
+ public Comment find(long id) throws IOException {
+ Comment temp = records.get(id);
+ if (temp == null) {
+ return null;
+ }
+ return temp.getCopy(databases);
+ }
+
public Set<Comment> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -53,6 +53,14 @@ public Image create(final Integer user_id) throws IOException {
}
+ public Image find(long id) throws IOException {
+ Image temp = records.get(id);
+ if (temp == null) {
+ return null;
+ }
+ return temp.getCopy(databases);
+ }
+
public Set<Image> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -55,6 +55,14 @@ public Post create(final String title, final Long posted_at_millis, final Intege
}
+ public Post find(long id) throws IOException {
+ Post temp = records.get(id);
+ if (temp == null) {
+ return null;
+ }
+ return temp.getCopy(databases);
+ }
+
public Set<Post> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -71,6 +71,14 @@ public User create(final String handle, final int num_posts) throws IOException
}
+ public User find(long id) throws IOException {
+ User temp = records.get(id);
+ if (temp == null) {
+ return null;
+ }
+ return temp.getCopy(databases);
+ }
+
public Set<User> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -236,11 +236,11 @@ public static Object getDefaultValue(_Fields field) {
}
@Override
- public ModelWithId getCopy() {
+ public Comment getCopy() {
return new Comment(this);
}
- public ModelWithId getCopy(IDatabases databases) {
+ public Comment getCopy(IDatabases databases) {
return new Comment(this, databases);
}
@@ -135,11 +135,11 @@ public static Object getDefaultValue(_Fields field) {
}
@Override
- public ModelWithId getCopy() {
+ public Image getCopy() {
return new Image(this);
}
- public ModelWithId getCopy(IDatabases databases) {
+ public Image getCopy(IDatabases databases) {
return new Image(this, databases);
}
@@ -204,11 +204,11 @@ public static Object getDefaultValue(_Fields field) {
}
@Override
- public ModelWithId getCopy() {
+ public Post getCopy() {
return new Post(this);
}
- public ModelWithId getCopy(IDatabases databases) {
+ public Post getCopy(IDatabases databases) {
return new Post(this, databases);
}
@@ -397,11 +397,11 @@ public static Object getDefaultValue(_Fields field) {
}
@Override
- public ModelWithId getCopy() {
+ public User getCopy() {
return new User(this);
}
- public ModelWithId getCopy(IDatabases databases) {
+ public User getCopy(IDatabases databases) {
return new User(this, databases);
}
Oops, something went wrong.
@@ -65,6 +65,14 @@ public class <%= model_defn.mock_impl_name %> extends AbstractMockDatabaseModel<
<%= render_mock_create_method(model_defn, create_signature_small, true) %>
<% end %>
+ public <%= model_defn.model_name %> find(long id) throws IOException {
+ <%= model_defn.model_name %> temp = records.get(id);
+ if (temp == null) {
+ return null;
+ }
+ return temp.getCopy(databases);
+ }
+
public Set<<%= model_defn.model_name %>> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -198,11 +198,11 @@ public class <%=model_defn.model_name%> extends ModelWithId {
}
@Override
- public ModelWithId getCopy() {
+ public <%= model_defn.model_name %> getCopy() {
return new <%= model_defn.model_name %>(this);
}
- public ModelWithId getCopy(IDatabases databases) {
+ public <%= model_defn.model_name %> getCopy(IDatabases databases) {
return new <%= model_defn.model_name %>(this, databases);
}
@@ -4,7 +4,6 @@
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
-import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -94,7 +93,8 @@ private void verifyCreatedUser(IUserPersistence users, long t0, long t1, long t2
assertEquals(1.2, bryand.getSomeFloat());
assertTrue(bryand.isSomeBoolean());
- assertTrue(bryand == users.find(bryand.getId()));
+ // no longer a valid assertion, since we are deep copying objects out of the cache
+// assertTrue(bryand == users.find(bryand.getId()));
}
public void testFind() throws Exception {
@@ -202,17 +202,17 @@ public void testFindSetFromCache() throws Exception {
keysToSearch.add(bryand.getId());
keysToSearch.add(notBryand.getId());
Set<User> foundValues = users.find(keysToSearch);
-
+
assertEquals(2, foundValues.size());
Iterator<User> iter = foundValues.iterator();
User bryand_again = users.find(bryand.getId());
User notBryand_again = users.find(notBryand.getId());
while(iter.hasNext()) {
User curUser = iter.next();
if(curUser.getId() == bryand.getId()) {
- assertTrue(bryand_again == curUser);
+ assertEquals(bryand_again, curUser);
} else if(curUser.getId() == notBryand.getId()) {
- assertTrue(notBryand_again == curUser);
+ assertEquals(notBryand_again, curUser);
} else {
fail("Unexpected user id: " + curUser.getId());
}
@@ -225,7 +225,7 @@ public void testFindCache() throws Exception {
User u1 = users.find(user.getId());
User u2 = users.find(user.getId());
- assertTrue(u1 == u2);
+ assertEquals(u1, u2);
}
public void testFindAllByForeignKey() throws Exception {
@@ -261,11 +261,11 @@ public void testFindAllFromCache() throws Exception {
// make sure findAll returned cached objects
int numFound = 0;
for (User user : allUsers) {
- if (user == u1_1) {
+ if (user.getId() == u1_1.getId()) {
numFound++;
- } else if (user == u2_1) {
+ } else if (user.getId() == u2_1.getId()) {
numFound++;
- } else if (user == u3_1) {
+ } else if (user.getId() == u3_1.getId()) {
numFound++;
}
}
@@ -287,7 +287,7 @@ public void testFindAllAndCache() throws Exception {
// make sure caching worked as expected
boolean found = false;
for (User user : allUsers) {
- if (user == u1_1) {
+ if (user.getId() == u1_1.getId()) {
found = true;
break;
}

0 comments on commit 1d4b44b

Please sign in to comment.