Skip to content

Commit

Permalink
Some more evolution tests, also related to issue #71
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Jul 15, 2015
1 parent 48c5563 commit b24c415
Show file tree
Hide file tree
Showing 3 changed files with 473 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/org/zoodb/internal/DataDeSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public ZooPC readObject(ZooPC pc, int page, int offs) {

if (clsDef.getNextVersion() != null) {
throw DBLogger.newUser("Objecty has not been evolved to the latest schema version: " +
pc.jdoZooGetOid());
Util.oidToString(oid));
}

return readObjPrivate(pc, clsDef);
Expand Down Expand Up @@ -1047,6 +1047,8 @@ private final Object hollowForOid(long oid, ZooClassDef clsDef) {
}
} else {
//ensure latest version, otherwise there is no Class
//TODO instead we should store the schema ID, so that we automatically get the
// latest version...
while (clsDef.getNextVersion() != null) {
clsDef = clsDef.getNextVersion();
}
Expand Down
161 changes: 161 additions & 0 deletions tst/org/zoodb/test/jdo/Test_034_SchemaEvolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.zoodb.api.impl.ZooPC;
import org.zoodb.jdo.ZooJdoHelper;
import org.zoodb.jdo.ZooJdoProperties;
import org.zoodb.jdo.spi.PersistenceCapableImpl;
import org.zoodb.schema.ZooClass;
import org.zoodb.schema.ZooField;
Expand Down Expand Up @@ -120,6 +122,146 @@ public void testSimpleEvolution() {
TestTools.closePM();
}

@Test
public void testSimpleEvolutionError_Issue71() {
PersistenceManager pm = TestTools.openPM();
pm.currentTransaction().begin();
ZooJdoHelper.schema(pm).addClass(TestClassTiny.class);
TestClassTiny t1 = new TestClassTiny(1, 3);
TestClassTiny t2 = new TestClassTiny(4, 5);
pm.makePersistent(t1);
pm.makePersistent(t2);
Object oid1 = pm.getObjectId(t1);
pm.currentTransaction().commit();

TestTools.closePM();
pm = TestTools.openPM();

pm.currentTransaction().begin();

ZooClass s1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class.getName());
s1.rename(TestClassSmall.class.getName());
// private int myInt;
// private long myLong;
// private String myString;
// private int[] myInts;
// private Object refO;
// private TestClassTiny refP;

s1.getField("_int").remove();
s1.addField("myInt", Integer.TYPE);
s1.getField("_long").rename("myLong");
s1.addField("myString", String.class);
s1.addField("myInts", Integer[].class);
s1.addField("refO", Object.class);
ZooJdoHelper.schema(pm).addClass(TestClassTiny.class);
//Omit one field to trigger error
//s1.addField("refP", TestClassTiny.class);

pm.currentTransaction().commit();
TestTools.closePM();

pm = TestTools.openPM();
pm.currentTransaction().begin();

try {
pm.getObjectById(oid1);
fail();
} catch (JDOUserException e) {
assertFalse(e.getMessage(), e.getMessage().contains("class not found"));
assertTrue(e.getMessage(), e.getMessage().contains("field"));
assertTrue(e.getMessage(), e.getMessage().contains("mismatch"));
}

pm.currentTransaction().rollback();
TestTools.closePM();
}

@Test
public void testSimpleEvolutionViaReference() {
//test that evolves objects and then accesses them via hollowForOid() in the deserializer.

ZooJdoProperties p = TestTools.getProps();
p.setZooAutoCreateSchema(true);
PersistenceManager pm = TestTools.openPM();
pm.currentTransaction().begin();
ZooJdoHelper.schema(pm).addClass(TestClassTiny.class);
ZooJdoHelper.schema(pm).addClass(TestClassTinyRef.class);
TestClassTiny t1 = new TestClassTiny(1, 3);
TestClassTinyRef r = new TestClassTinyRef(t1);
pm.makePersistent(r);
Object oidR = pm.getObjectId(r);
pm.currentTransaction().commit();
TestTools.closePM();

//create incompatible schema
pm = TestTools.openPM();
pm.currentTransaction().begin();
ZooClass s1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class.getName());
s1.getField("_int").remove();
s1.addField("myInt", Integer.TYPE);
s1.addField("myString", String.class);
//evolve objects
Iterator<ZooHandle> it = s1.getHandleIterator(true);
while (it.hasNext()) {
ZooHandle h = it.next();
h.setValue("myInt", 123);
}
pm.currentTransaction().commit();
TestTools.closePM();

//change back to proper schema
pm = TestTools.openPM();
pm.currentTransaction().begin();
s1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class.getName());
s1.addField("_int", Integer.TYPE);
s1.getField("myInt").remove();
s1.getField("myString").remove();
pm.currentTransaction().commit();
TestTools.closePM();

//Load data
pm = TestTools.openPM();
pm.currentTransaction().begin();

TestClassTinyRef r2 = (TestClassTinyRef) pm.getObjectById(oidR);
assertNotNull(r2.getTiny());
try {
assertEquals(0, r2.getTiny().getInt());
assertEquals(3L, r2.getTiny().getLong());
} catch (JDOUserException e) {
assertTrue(e.getMessage(), e.getMessage().contains("not been evolved"));
}

pm.currentTransaction().rollback();
TestTools.closePM();

//Okay, we need to evolve first
pm = TestTools.openPM();
pm.currentTransaction().begin();
s1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class.getName());
Iterator<ZooHandle> it2 = s1.getHandleIterator(true);
while (it2.hasNext()) {
ZooHandle h = it2.next();
h.setValue("_int", 2);
}
pm.currentTransaction().commit();
TestTools.closePM();


//Load data again
pm = TestTools.openPM();
pm.currentTransaction().begin();

TestClassTinyRef r3 = (TestClassTinyRef) pm.getObjectById(oidR);
assertNotNull(r3.getTiny());
assertEquals(2, r3.getTiny().getInt());
assertEquals(3L, r3.getTiny().getLong());

pm.currentTransaction().rollback();
TestTools.closePM();
}

@Test
public void testDataMigration() {
PersistenceManager pm = TestTools.openPM();
Expand Down Expand Up @@ -1176,3 +1318,22 @@ public void testLocate() {
}

}

class TestClassTinyRef extends ZooPC {
private TestClassTiny tiny;

@SuppressWarnings("unused")
private TestClassTinyRef() {
//nothing
}

public TestClassTinyRef(TestClassTiny tiny) {
this.tiny = tiny;
}

public TestClassTiny getTiny() {
zooActivateRead();
return tiny;
}

}
Loading

0 comments on commit b24c415

Please sign in to comment.