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