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) {
}
}
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());
}
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());
}
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());
}
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++;
// }
}
Aggregations