Search in sources :

Example 51 with ObjectContext

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

the class SyncMessageTest method testConstructorInvalid.

@Test
public void testConstructorInvalid() {
    ObjectContext source = mock(ObjectContext.class);
    new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, new CompoundDiff());
    new SyncMessage(source, DataChannel.FLUSH_CASCADE_SYNC, new CompoundDiff());
    new SyncMessage(null, DataChannel.ROLLBACK_CASCADE_SYNC, new CompoundDiff());
    int bogusType = 45678;
    try {
        new SyncMessage(source, bogusType, new CompoundDiff());
        fail("invalid type was allowed to go unnoticed...");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 52 with ObjectContext

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

the class SyncMessageTest method testConstructor.

@Test
public void testConstructor() {
    ObjectContext source = mock(ObjectContext.class);
    GraphDiff diff = new CompoundDiff();
    SyncMessage message = new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, diff);
    assertSame(source, message.getSource());
    assertEquals(DataChannel.FLUSH_NOCASCADE_SYNC, message.getType());
    assertSame(diff, message.getSenderChanges());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 53 with ObjectContext

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

the class NestedObjectContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.

@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    a.setGlobalAttribute1("X");
    ClientMtTable2 px = clientContext.newObject(ClientMtTable2.class);
    px.setTable1(a);
    px.setGlobalAttribute("PX");
    ClientMtTable2 py = clientContext.newObject(ClientMtTable2.class);
    py.setGlobalAttribute("PY");
    clientContext.commitChanges();
    ObjectContext peer1 = runtime.newContext(clientContext);
    ClientMtTable2 py1 = peer1.localObject(py);
    ClientMtTable1 a1 = peer1.localObject(a);
    ObjectContext peer2 = runtime.newContext(clientContext);
    ClientMtTable2 py2 = peer2.localObject(py);
    ClientMtTable1 a2 = peer2.localObject(a);
    a1.addToTable2Array(py1);
    assertEquals(1, a2.getTable2Array().size());
    assertFalse(a2.getTable2Array().contains(py2));
    peer1.commitChangesToParent();
    assertEquals(2, a2.getTable2Array().size());
    assertTrue(a2.getTable2Array().contains(py2));
    assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 54 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 : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Module(org.apache.cayenne.di.Module) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) EntityResolver(org.apache.cayenne.map.EntityResolver) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) Key(org.apache.cayenne.di.Key) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) TransactionDescriptor(org.apache.cayenne.tx.TransactionDescriptor) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EventManager(org.apache.cayenne.event.EventManager) QueryResponse(org.apache.cayenne.QueryResponse) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) 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 55 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(ObjectSelect.query(Qualified1.class));
    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(ObjectSelect.query(Qualified1.class));
    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) SQLException(java.sql.SQLException) DataMap(org.apache.cayenne.map.DataMap) 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