Search in sources :

Example 31 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class NestedDataContextWriteIT method testCommitChangesToParentFlattened.

@Test
public void testCommitChangesToParentFlattened() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("Master");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("Detail1");
    childO2.addToArtistArray(childO1);
    ObjEntity ent = childContext.getEntityResolver().getObjEntity("ArtGroup");
    Collection<ObjRelationship> rels = ent.getDeclaredRelationships();
    for (ObjRelationship rel : rels) {
        System.out.println(rel.getName());
    }
    assertEquals(1, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
            Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
            assertNotNull(parentO1);
            assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
            ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
            assertNotNull(parentO2);
            assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
            assertEquals(1, parentO1.getGroupArray().size());
            assertEquals(1, parentO2.getArtistArray().size());
            assertTrue(parentO2.getArtistArray().contains(parentO1));
            assertTrue(parentO1.getGroupArray().contains(parentO2));
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 32 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class NestedDataContextWriteIT method testNullifyToOne.

/**
 * A test case for CAY-698 bug.
 */
@Test
public void testNullifyToOne() throws Exception {
    createNullifyToOneDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    ObjectContext childContextPeer = runtime.newContext(context);
    final Painting childP1 = Cayenne.objectForPK(childContext, Painting.class, 33001);
    // trigger object creation in the peer nested DC
    Cayenne.objectForPK(childContextPeer, Painting.class, 33001);
    childP1.setToArtist(null);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
            Painting parentP1 = (Painting) context.getGraphManager().getNode(childP1.getObjectId());
            assertNotNull(parentP1);
            assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
            assertNull(parentP1.getToArtist());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 33 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ServerRuntimeTest method testGetObjectContext_CustomModule.

@Test
public void testGetObjectContext_CustomModule() {
    final ObjectContext context = new DataContext();
    final ObjectContextFactory factory = new ObjectContextFactory() {

        public ObjectContext createContext(DataChannel parent) {
            return context;
        }

        public ObjectContext createContext() {
            return context;
        }
    };
    Module module = binder -> binder.bind(ObjectContextFactory.class).toInstance(factory);
    ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
    assertSame(context, runtime.newContext());
    assertSame(context, runtime.newContext());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) EventManager(org.apache.cayenne.event.EventManager) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Key(org.apache.cayenne.di.Key) Mockito.mock(org.mockito.Mockito.mock) DataContext(org.apache.cayenne.access.DataContext) DataChannel(org.apache.cayenne.DataChannel) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) Test(org.junit.Test)

Example 34 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ConcurrentPkGeneratorIT method testConcurrentInserts.

/*
     * Attempts to discover any problems regarding thread locking in the PkGenerator
     */
@Test
public void testConcurrentInserts() {
    if (!unitDbAdapter.supportsPKGeneratorConcurrency()) {
        return;
    }
    final DataMap dataMap = runtime.getDataDomain().getDataMap("qualified");
    // clear out the table
    ObjectContext context = runtime.newContext();
    List<Qualified1> qualified1s = context.select(SelectQuery.query(Qualified1.class, null));
    context.deleteObjects(qualified1s);
    context.commitChanges();
    // perform concurrent inserts
    int numThreads = 2;
    int insertsPerThread = 100;
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    Runnable task = () -> {
        try {
            ObjectContext context1 = runtime.newContext();
            for (ObjEntity entity : dataMap.getObjEntities()) {
                context1.newObject(entity.getJavaClass());
            }
            context1.commitChanges();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
    for (int j = 0; j < insertsPerThread; j++) {
        for (int i = 0; i < numThreads; i++) {
            executor.submit(task);
        }
    }
    // check for completion or deadlock
    executor.shutdown();
    try {
        // normally this completes in less than 2 seconds. If it takes 30 then it failed.
        boolean didFinish = executor.awaitTermination(30, TimeUnit.SECONDS);
        if (!didFinish) {
            fail("Concurrent inserts either deadlocked or contended over the lock too long.");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    // check for gaps in the generated sequence numbers
    qualified1s = context.select(SelectQuery.query(Qualified1.class, null));
    assertEquals(insertsPerThread * numThreads, qualified1s.size());
// PKs will be used in order most of the time, but the implementation doesn't guarantee it.
// qualified1s.sort(Comparator.comparing(Cayenne::intPKForObject));
// 
// int lastPk = Cayenne.intPKForObject(qualified1s.get(0)) - 1;
// for (Qualified1 qualified1 : qualified1s) {
// if (lastPk+1 != Cayenne.intPKForObject(qualified1)) {
// fail("Found gap in sequence number: " + lastPk + " - " + Cayenne.intPKForObject(qualified1));
// }
// lastPk++;
// }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Qualified1(org.apache.cayenne.testdo.qualified.Qualified1) ExecutorService(java.util.concurrent.ExecutorService) ObjectContext(org.apache.cayenne.ObjectContext) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 35 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class DataContextSerializationIT method testSerializeNestedChannel.

@Test
public void testSerializeNestedChannel() throws Exception {
    ObjectContext child = runtime.newContext(context);
    ObjectContext deserializedContext = Util.cloneViaSerialization(child);
    assertNotNull(deserializedContext.getChannel());
    assertNotNull(deserializedContext.getEntityResolver());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)108 Test (org.junit.Test)99 Artist (org.apache.cayenne.testdo.testmap.Artist)32 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)14 SelectQuery (org.apache.cayenne.query.SelectQuery)13 Painting (org.apache.cayenne.testdo.testmap.Painting)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 GraphDiff (org.apache.cayenne.graph.GraphDiff)9 ObjectId (org.apache.cayenne.ObjectId)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 QueryResponse (org.apache.cayenne.QueryResponse)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 Query (org.apache.cayenne.query.Query)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4