use of org.jpox.samples.models.leftright.LeftSub in project tests by datanucleus.
the class RelationshipTest method testQueryWithInheritanceAndOrdering.
/**
* Test case for ordering with inheritance involved.
* (This test is included in RelationshipTest because it uses
* the same classes as another "real" relationship test.)
*/
public void testQueryWithInheritanceAndOrdering() throws Exception {
try {
// prepare data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
LeftBase baseA = new LeftBase(1);
LeftSub groupB = new LeftSub(3, new LeftBase[] {});
LeftBase baseC = new LeftBase(2);
LeftSub groupD = new LeftSub(4, new LeftBase[] {});
baseA.setName("aaa");
groupB.setName("bbb");
baseC.setName("ccc");
groupD.setName("ddd");
pm.makePersistent(baseA);
pm.makePersistent(baseC);
pm.makePersistent(groupB);
pm.makePersistent(groupD);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery(pm.getExtent(LeftBase.class, true));
query.setOrdering("name ascending");
Collection result = (Collection) query.execute();
Iterator iter = result.iterator();
{
LeftBase base = (LeftBase) iter.next();
assertEquals("aaa", base.getName());
}
{
LeftBase base = (LeftBase) iter.next();
assertEquals("bbb", base.getName());
}
{
LeftBase base = (LeftBase) iter.next();
assertEquals("ccc", base.getName());
}
{
LeftBase base = (LeftBase) iter.next();
assertEquals("ddd", base.getName());
}
assertFalse("result set unexpectedly too large.", iter.hasNext());
query.closeAll();
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
for (Iterator iter = ((Collection) pm.newQuery(LeftSub.class).execute()).iterator(); iter.hasNext(); ) {
LeftSub grp = (LeftSub) iter.next();
grp.getMembers().clear();
}
tx.commit();
pm.close();
clean(RightSub.class);
clean(RightBase.class);
clean(LeftSub.class);
clean(LeftBase.class);
}
}
use of org.jpox.samples.models.leftright.LeftSub in project tests by datanucleus.
the class PersistenceModelsTest method test1toNInheritance.
/**
* Test case for 1-N inheritance relationships.
*/
public void test1toNInheritance() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
LeftBase base1 = new LeftBase(1);
LeftBase base2 = new LeftBase(2);
LeftBase base3 = new LeftBase(3);
LeftSub group1 = new LeftSub(4, new LeftBase[] { base1, base2 });
LeftSub group2 = new LeftSub(5, new LeftBase[] { group1, base3 });
RightSub gr1 = new RightSub(1, group2);
pm.makePersistent(gr1);
tx.commit();
id = pm.getObjectId(gr1);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
RightSub gr2 = (RightSub) pm.getObjectById(id, true);
assertEquals("Expect id == 5", 5, gr2.getBase().getId());
assertEquals("Expect Group class instance.", LeftSub.class, gr2.getBase().getClass());
assertEquals("Expect 2 members", 2, ((LeftSub) gr2.getBase()).getMembers().size());
List members = ((LeftSub) gr2.getBase()).getMembers();
assertEquals("Expect id == 4", 4, ((LeftBase) members.get(0)).getId());
assertEquals("Expect id == 3", 3, ((LeftBase) members.get(1)).getId());
assertEquals("Expect Group class instance in member.", LeftSub.class, members.get(0).getClass());
assertEquals("Expect Base class instance in member.", LeftBase.class, members.get(1).getClass());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(LeftSub.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
LeftSub lsub = (LeftSub) iter.next();
lsub.getMembers().clear();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
clean(RightSub.class);
clean(RightBase.class);
clean(LeftSub.class);
clean(LeftBase.class);
}
}
use of org.jpox.samples.models.leftright.LeftSub in project tests by datanucleus.
the class RelationshipTest method test1toNInheritance.
/**
* Test case for 1-1 inheritance relationships.
* TODO Is this JoinTable, or FK - change name of test to suit
*/
public void test1toNInheritance() throws Exception {
try {
// prepare data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
LeftBase base1 = new LeftBase(1);
LeftBase base2 = new LeftBase(2);
LeftBase base3 = new LeftBase(3);
LeftSub group1 = new LeftSub(4, new LeftBase[] { base1, base2 });
LeftSub group2 = new LeftSub(5, new LeftBase[] { group1, base3 });
RightSub gr1 = new RightSub(1, group2);
pm.makePersistent(gr1);
tx.commit();
id = pm.getObjectId(gr1);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
RightSub gr2 = (RightSub) pm.getObjectById(id, true);
assertEquals("Expect id == 5", 5, gr2.getBase().getId());
assertEquals("Expect Group class instance.", LeftSub.class, gr2.getBase().getClass());
assertEquals("Expect 2 members", 2, ((LeftSub) gr2.getBase()).getMembers().size());
List members = ((LeftSub) gr2.getBase()).getMembers();
assertEquals("Expect id == 4", 4, ((LeftBase) members.get(0)).getId());
assertEquals("Expect id == 3", 3, ((LeftBase) members.get(1)).getId());
assertEquals("Expect Group class instance in member.", LeftSub.class, members.get(0).getClass());
assertEquals("Expect Base class instance in member.", LeftBase.class, members.get(1).getClass());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
for (Iterator iter = ((Collection) pm.newQuery(LeftSub.class).execute()).iterator(); iter.hasNext(); ) {
LeftSub grp = (LeftSub) iter.next();
grp.getMembers().clear();
}
tx.commit();
pm.close();
clean(RightSub.class);
clean(RightBase.class);
clean(LeftSub.class);
clean(LeftBase.class);
}
}
Aggregations