use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class VerticalInheritanceMultipleAttributesIT method testCreateObjectsWithData.
@Test
public void testCreateObjectsWithData() throws SQLException {
ivOtherTable.insert(1, "other1");
ivOtherTable.insert(2, "other2");
IvOther other1 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other1")).selectOne(context);
IvOther other2 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other2")).selectOne(context);
IvImpl impl1 = context.newObject(IvImpl.class);
impl1.setName("name");
impl1.setAttr1("attr1");
impl1.setAttr2("attr2");
impl1.setOther1(other1);
impl1.setOther2(other2);
IvImpl impl2 = context.newObject(IvImpl.class);
impl2.setName("name");
impl2.setAttr1("attr1");
impl2.setAttr2("attr2");
impl2.setOther1(other1);
impl2.setOther2(other2);
context.commitChanges();
// Check result via clean context
ObjectContext cleanContext = runtime.newContext();
List<IvImpl> implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
assertEquals(2, implResult.size());
for (IvImpl record : implResult) {
assertEquals("name", record.getName());
assertEquals("attr1", record.getAttr1());
assertEquals("attr2", record.getAttr2());
assertEquals(other1.getObjectId(), record.getOther1().getObjectId());
assertEquals(other2.getObjectId(), record.getOther2().getObjectId());
}
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class VerticalInheritanceMultipleAttributesIT method testCreateEmptyObjectsWithUpdate.
@Test
public void testCreateEmptyObjectsWithUpdate() throws SQLException {
ivOtherTable.insert(1, "other1");
ivOtherTable.insert(2, "other2");
IvOther other1 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other1")).selectOne(context);
IvOther other2 = ObjectSelect.query(IvOther.class).where(IvOther.NAME.eq("other2")).selectOne(context);
IvImpl impl1 = context.newObject(IvImpl.class);
impl1.setName("name");
IvImpl impl2 = context.newObject(IvImpl.class);
impl2.setName("name");
context.commitChanges();
ObjectContext cleanContext = runtime.newContext();
List<IvImpl> implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
assertEquals(2, implResult.size());
for (IvImpl record : implResult) {
assertEquals("name", record.getName());
assertNull(record.getAttr1());
assertNull(record.getAttr2());
assertNull(record.getOther1());
assertNull(record.getOther2());
}
impl1.setAttr1("attr1");
impl1.setAttr2("attr2");
impl1.setOther1(other1);
impl1.setOther2(other2);
impl2.setAttr1("attr1");
impl2.setAttr2("attr2");
impl2.setOther1(other1);
impl2.setOther2(other2);
context.commitChanges();
cleanContext = runtime.newContext();
implResult = ObjectSelect.query(IvImpl.class).select(cleanContext);
assertEquals(2, implResult.size());
for (IvImpl record : implResult) {
assertEquals("name", record.getName());
assertEquals("attr1", record.getAttr1());
assertEquals("attr2", record.getAttr2());
assertEquals(other1.getObjectId(), record.getOther1().getObjectId());
assertEquals(other2.getObjectId(), record.getOther2().getObjectId());
}
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextLocalCacheIT method testLocalCacheStaysLocal.
@Test
public void testLocalCacheStaysLocal() {
ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).cacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
ObjectContext child1 = runtime.newContext(context);
assertNull(((BaseContext) child1).getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(context.getQueryCache().get(query.getMetaData(context.getEntityResolver())));
List<?> results = child1.performQuery(query);
assertSame(results, ((BaseContext) child1).getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(context.getQueryCache().get(query.getMetaData(context.getEntityResolver())));
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextParentPeerEventsIT method testPeerObjectUpdatedToOneRelationship.
@Test
public void testPeerObjectUpdatedToOneRelationship() throws Exception {
Master a = parentContext1.newObject(Master.class);
Master altA = parentContext1.newObject(Master.class);
Child p = parentContext1.newObject(Child.class);
p.setMaster(a);
a.setName("X");
altA.setName("Y");
parentContext1.commitChanges();
Child p1 = parentContext2.localObject(p);
Master altA1 = parentContext2.localObject(altA);
final ObjectContext childContext1 = runtime.newContext(parentContext1);
final Child p2 = childContext1.localObject(p);
final Master altA2 = childContext1.localObject(altA);
Master a2 = childContext1.localObject(a);
p1.setMaster(altA1);
assertSame(a2, p2.getMaster());
assertNotSame(altA2, p2.getMaster());
parentContext2.commitChanges();
new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertSame(altA2, p2.getMaster());
assertFalse("Peer data context became dirty on event processing", childContext1.hasChanges());
}
}.runTest(2000);
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextParentPeerEventsIT method testPeerObjectUpdatedToManyRelationship.
@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
Master a = parentContext1.newObject(Master.class);
a.setName("X");
Child px = parentContext1.newObject(Child.class);
px.setMaster(a);
Child py = parentContext1.newObject(Child.class);
parentContext1.commitChanges();
Child py1 = parentContext2.localObject(py);
Master a1 = parentContext2.localObject(a);
final ObjectContext peer2 = runtime.newContext(parentContext1);
final Child py2 = peer2.localObject(py);
final Master a2 = peer2.localObject(a);
a1.addToChildren(py1);
assertEquals(1, a2.getChildren().size());
assertFalse(a2.getChildren().contains(py2));
parentContext2.commitChangesToParent();
new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals(2, a2.getChildren().size());
assertTrue(a2.getChildren().contains(py2));
assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
}
}.runTest(2000);
}
Aggregations