Search in sources :

Example 21 with DataObject

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

the class DataContextObjectTrackingIT method testInvalidateObjects_Vararg.

@Test
public void testInvalidateObjects_Vararg() {
    DataRow row = new DataRow(10);
    row.put("ARTIST_ID", new Integer(1));
    row.put("ARTIST_NAME", "ArtistXYZ");
    row.put("DATE_OF_BIRTH", new Date());
    DataObject obj = context.objectFromDataRow(Artist.class, row);
    ObjectId oid = obj.getObjectId();
    assertEquals(PersistenceState.COMMITTED, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(obj, context.getGraphManager().getNode(oid));
    context.invalidateObjects(obj);
    assertEquals(PersistenceState.HOLLOW, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(oid, obj.getObjectId());
    assertNull(context.getObjectStore().getCachedSnapshot(oid));
    assertNotNull(context.getGraphManager().getNode(oid));
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 22 with DataObject

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

the class DataContextObjectTrackingIT method testUnregisterObject.

@Test
public void testUnregisterObject() {
    DataRow row = new DataRow(10);
    row.put("ARTIST_ID", new Integer(1));
    row.put("ARTIST_NAME", "ArtistXYZ");
    row.put("DATE_OF_BIRTH", new Date());
    DataObject obj = context.objectFromDataRow(Artist.class, row);
    ObjectId oid = obj.getObjectId();
    assertEquals(PersistenceState.COMMITTED, obj.getPersistenceState());
    assertSame(context, obj.getObjectContext());
    assertSame(obj, context.getGraphManager().getNode(oid));
    context.unregisterObjects(Collections.singletonList(obj));
    assertEquals(PersistenceState.TRANSIENT, obj.getPersistenceState());
    assertNull(obj.getObjectContext());
    assertEquals(oid, obj.getObjectId());
    assertNull(context.getGraphManager().getNode(oid));
    assertNull(context.getObjectStore().getCachedSnapshot(oid));
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 23 with DataObject

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

the class DataContextDelegateSharedCacheIT method testBlockedShouldMergeChanges.

/**
 * Test case to prove that delegate method can block changes made by ObjectStore.
 *
 * @throws Exception
 */
@Test
public void testBlockedShouldMergeChanges() throws Exception {
    String oldName = artist.getArtistName();
    DataContextDelegate delegate = new MockDataContextDelegate() {

        @Override
        public boolean shouldMergeChanges(DataObject object, DataRow snapshotInStore) {
            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(oldName, altArtist.getArtistName());
    assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
    // Update and save artist in peer context
    artist.setArtistName("version2");
    context.commitChanges();
    // assert that delegate was able to block the merge
    assertEquals(oldName, altArtist.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 24 with DataObject

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

the class SimpleIdIncrementalFaultListPrefetchIT method testPrefetch4.

/**
 * Test that a to-one relationship is initialized.
 */
@Test
public void testPrefetch4() throws Exception {
    createArtistsAndPaintingsDataSet();
    SelectQuery q = new SelectQuery(Painting.class);
    q.setPageSize(3);
    q.addPrefetch("toArtist");
    IncrementalFaultList<?> result = (IncrementalFaultList) context.performQuery(q);
    // get an objects from the second page
    final DataObject p1 = (DataObject) result.get(q.getPageSize());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Object toOnePrefetch = p1.readNestedProperty("toArtist");
            assertNotNull(toOnePrefetch);
            assertTrue("Expected DataObject, got: " + toOnePrefetch.getClass().getName(), toOnePrefetch instanceof DataObject);
            DataObject a1 = (DataObject) toOnePrefetch;
            assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 25 with DataObject

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

the class Expression_ParamsTest method testInParameter_AsValues.

@Test
public void testInParameter_AsValues() throws Exception {
    Expression inExp = ExpressionFactory.exp("k1 in ($ap, $bp)");
    String e1String = "k1 in (\"a\", \"b\")";
    Expression e1 = ExpressionFactory.exp(e1String);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("ap", "a");
    params.put("bp", "b");
    Expression transformed = inExp.params(params);
    TstTraversalHandler.compareExps(e1, transformed);
    assertEquals(e1String, transformed.toString());
    // just in case manually check params
    DataObject o1 = new CayenneDataObject();
    o1.writePropertyDirectly("k1", "a");
    assertTrue(transformed.match(o1));
    DataObject o2 = new CayenneDataObject();
    o2.writePropertyDirectly("k1", "x");
    assertFalse(transformed.match(o2));
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) 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