Search in sources :

Example 51 with ObjectId

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

the class DataRowStoreIT method testMaxSize.

/**
 * Tests LRU cache behavior.
 */
@Test
public void testMaxSize() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(Constants.SNAPSHOT_CACHE_SIZE_PROPERTY, String.valueOf(2));
    cache = new DataRowStore("cacheXYZ", new DefaultRuntimeProperties(props), null);
    assertEquals(2, cache.maximumSize());
    assertEquals(0, cache.size());
    ObjectId key1 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 1);
    Map<ObjectId, DataRow> diff1 = new HashMap<>();
    diff1.put(key1, new DataRow(1));
    ObjectId key2 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    Map<ObjectId, DataRow> diff2 = new HashMap<>();
    diff2.put(key2, new DataRow(1));
    ObjectId key3 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    Map<ObjectId, DataRow> diff3 = new HashMap<>();
    diff3.put(key3, new DataRow(1));
    cache.processSnapshotChanges(this, diff1, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(1, cache.size());
    cache.processSnapshotChanges(this, diff2, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    // this addition must overflow the cache, and throw out the first item
    cache.processSnapshotChanges(this, diff3, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    assertNotNull(cache.getCachedSnapshot(key2));
    assertNotNull(cache.getCachedSnapshot(key3));
    assertNull(cache.getCachedSnapshot(key1));
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 52 with ObjectId

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

the class DbArcIdTest method testEquals.

@Test
public void testEquals() {
    DbArcId id1 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    assertTrue(id1.equals(id1));
    DbArcId id1_eq = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    assertTrue(id1.equals(id1_eq));
    assertTrue(id1_eq.equals(id1));
    DbArcId id2 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r2"));
    assertFalse(id1.equals(id2));
    DbArcId id3 = new DbArcId(new ObjectId("y", "k", "v"), new DbRelationship("r1"));
    assertFalse(id1.equals(id3));
    assertFalse(id1.equals(new Object()));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) Test(org.junit.Test)

Example 53 with ObjectId

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

the class RelationshipQueryTest method testSerializability.

@Test
public void testSerializability() throws Exception {
    ObjectId oid = new ObjectId("test", "a", "b");
    RelationshipQuery query = new RelationshipQuery(oid, "relX");
    RelationshipQuery q1 = (RelationshipQuery) Util.cloneViaSerialization(query);
    assertNotNull(q1);
    assertEquals(oid, q1.getObjectId());
    assertEquals("relX", q1.getRelationshipName());
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 54 with ObjectId

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

the class SelectQueryMetadataCacheKeyTest method cacheKeyWithPersistentObject.

/**
 * Persistent objects should be converted to their ObjectIds.
 */
@Test
public void cacheKeyWithPersistentObject() {
    Persistent persistent1 = mock(Persistent.class);
    ObjectId objectId1 = mock(ObjectId.class);
    when(objectId1.toString()).thenReturn("objId1");
    when(persistent1.getObjectId()).thenReturn(objectId1);
    Persistent persistent2 = mock(Persistent.class);
    ObjectId objectId2 = mock(ObjectId.class);
    when(objectId2.toString()).thenReturn("objId2");
    when(persistent2.getObjectId()).thenReturn(objectId2);
    ExpressionFactory.greaterOrEqualExp("testPath", persistent1).traverse(newHandler());
    String s1 = cacheKey.toString();
    ExpressionFactory.greaterOrEqualExp("testPath", persistent1).traverse(newHandler());
    String s2 = cacheKey.toString();
    ExpressionFactory.greaterOrEqualExp("testPath", persistent2).traverse(newHandler());
    String s3 = cacheKey.toString();
    assertTrue(s1.contains("objId1"));
    assertTrue(s3.contains("objId2"));
    assertEquals(s1, s2);
    assertNotEquals(s2, s3);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 55 with ObjectId

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

the class LifecycleCallbackEventHandlerTest method testDefaultListeners.

@Test
public void testDefaultListeners() {
    LifecycleCallbackEventHandler map = new LifecycleCallbackEventHandler(new EntityResolver());
    L1 l1 = new L1();
    map.addDefaultListener(l1, "callback");
    C1 c1 = new C1();
    c1.setObjectId(new ObjectId("bogus"));
    assertEquals(0, l1.entities.size());
    map.performCallbacks(c1);
    assertEquals(1, l1.entities.size());
    assertTrue(l1.entities.contains(c1));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) EntityResolver(org.apache.cayenne.map.EntityResolver) Test(org.junit.Test)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9