Search in sources :

Example 81 with ObjectContext

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());
    }
}
Also used : IvOther(org.apache.cayenne.testdo.inheritance_vertical.IvOther) ObjectContext(org.apache.cayenne.ObjectContext) IvImpl(org.apache.cayenne.testdo.inheritance_vertical.IvImpl) Test(org.junit.Test)

Example 82 with ObjectContext

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());
    }
}
Also used : IvOther(org.apache.cayenne.testdo.inheritance_vertical.IvOther) ObjectContext(org.apache.cayenne.ObjectContext) IvImpl(org.apache.cayenne.testdo.inheritance_vertical.IvImpl) Test(org.junit.Test)

Example 83 with ObjectContext

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())));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) BaseContext(org.apache.cayenne.BaseContext) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 84 with ObjectContext

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);
}
Also used : Master(org.apache.cayenne.testdo.relationships_child_master.Master) ObjectContext(org.apache.cayenne.ObjectContext) Child(org.apache.cayenne.testdo.relationships_child_master.Child) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 85 with ObjectContext

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);
}
Also used : Master(org.apache.cayenne.testdo.relationships_child_master.Master) ObjectContext(org.apache.cayenne.ObjectContext) Child(org.apache.cayenne.testdo.relationships_child_master.Child) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)127 Test (org.junit.Test)116 Artist (org.apache.cayenne.testdo.testmap.Artist)35 Painting (org.apache.cayenne.testdo.testmap.Painting)14 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 Table1 (org.apache.cayenne.crypto.db.Table1)10 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 ObjectId (org.apache.cayenne.ObjectId)8 GraphDiff (org.apache.cayenne.graph.GraphDiff)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 IvImpl (org.apache.cayenne.testdo.inheritance_vertical.IvImpl)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 QueryResponse (org.apache.cayenne.QueryResponse)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4