use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextExtrasIT method testIdObjectFromDataRow.
@Test
public void testIdObjectFromDataRow() {
DataRow row = new DataRow(10);
row.put("ARTIST_ID", 100000);
DataObject obj = context.objectFromDataRow(Artist.class, row);
assertNotNull(obj);
assertTrue(context.getGraphManager().registeredNodes().contains(obj));
assertEquals(PersistenceState.HOLLOW, obj.getPersistenceState());
assertNull(context.getObjectStore().getCachedSnapshot(obj.getObjectId()));
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextPrefetchExtrasIT method testPrefetch10.
/**
* Tests to-one prefetching over relationships with compound keys.
*/
@Test
public void testPrefetch10() throws Exception {
createCompoundDataSet();
Expression e = ExpressionFactory.matchExp("name", "CFK2");
SelectQuery q = new SelectQuery(CompoundFkTestEntity.class, e);
q.addPrefetch("toCompoundPk");
List<?> objects = context.performQuery(q);
assertEquals(1, objects.size());
BaseDataObject fk1 = (BaseDataObject) objects.get(0);
Object toOnePrefetch = fk1.readNestedProperty("toCompoundPk");
assertNotNull(toOnePrefetch);
assertTrue("Expected DataObject, got: " + toOnePrefetch.getClass().getName(), toOnePrefetch instanceof DataObject);
DataObject pk1 = (DataObject) toOnePrefetch;
assertEquals(PersistenceState.COMMITTED, pk1.getPersistenceState());
assertEquals("CPK2", pk1.readPropertyDirectly("name"));
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testShouldProcessDeleteOnExternalChange.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store.
*
* @throws Exception
*/
@Test
public void testShouldProcessDeleteOnExternalChange() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldProcessDelete(DataObject object) {
methodInvoked[0] = true;
return true;
}
};
context1.setDelegate(delegate);
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertFalse(altArtist == artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
// Update and save artist in peer context
context.deleteObjects(artist);
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testBlockShouldProcessDeleteOnExternalChange.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store, and is able to block further object processing.
*
* @throws Exception
*/
@Test
public void testBlockShouldProcessDeleteOnExternalChange() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldProcessDelete(DataObject object) {
methodInvoked[0] = true;
return false;
}
};
context1.setDelegate(delegate);
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertFalse(altArtist == artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
// Update and save artist in peer context
context.deleteObjects(artist);
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed, and actually blocked object expulsion
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
assertNotNull(altArtist.getObjectContext());
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testShouldMergeChanges.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store.
*/
@Test
public void testShouldMergeChanges() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldMergeChanges(DataObject object, DataRow snapshotInStore) {
methodInvoked[0] = true;
return true;
}
};
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertNotSame(altArtist, artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
context1.setDelegate(delegate);
// Update and save artist in peer context
artist.setArtistName("version2");
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
}
Aggregations