Search in sources :

Example 11 with DataObject

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()));
}
Also used : DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 12 with DataObject

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"));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) BaseDataObject(org.apache.cayenne.BaseDataObject) DataObject(org.apache.cayenne.DataObject) Expression(org.apache.cayenne.exp.Expression) BaseDataObject(org.apache.cayenne.BaseDataObject) DataObject(org.apache.cayenne.DataObject) BaseDataObject(org.apache.cayenne.BaseDataObject) Test(org.junit.Test)

Example 13 with DataObject

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 14 with DataObject

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 15 with DataObject

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2