Search in sources :

Example 61 with ObjectId

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

the class ObjectIdQueryTest method testEquals.

/**
 * Proper 'equals' and 'hashCode' implementations are important when mapping
 * results obtained in a QueryChain back to the query.
 */
@Test
public void testEquals() throws Exception {
    ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("abc", "a", 1));
    ObjectIdQuery q2 = new ObjectIdQuery(new ObjectId("abc", "a", 1));
    ObjectIdQuery q3 = new ObjectIdQuery(new ObjectId("abc", "a", 3));
    ObjectIdQuery q4 = new ObjectIdQuery(new ObjectId("123", "a", 1));
    assertTrue(q1.equals(q2));
    assertEquals(q1.hashCode(), q2.hashCode());
    assertFalse(q1.equals(q3));
    assertFalse(q1.hashCode() == q3.hashCode());
    assertFalse(q1.equals(q4));
    assertFalse(q1.hashCode() == q4.hashCode());
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 62 with ObjectId

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

the class EvaluatorTest method testEvaluator_Persistent_MultiKey.

@Test
public void testEvaluator_Persistent_MultiKey() {
    Map<String, Object> lhsIdMap = new HashMap<>();
    lhsIdMap.put("a", 1);
    lhsIdMap.put("b", "B");
    ObjectId lhsId = new ObjectId("X", lhsIdMap);
    Persistent lhs = mock(Persistent.class);
    when(lhs.getObjectId()).thenReturn(lhsId);
    Evaluator e = Evaluator.evaluator(lhs);
    assertNotNull(e);
    Map<String, Object> rhsId1Map = new HashMap<>();
    rhsId1Map.put("a", 1);
    rhsId1Map.put("b", "B");
    ObjectId rhsId1 = new ObjectId("X", rhsId1Map);
    Persistent rhs1 = mock(Persistent.class);
    when(rhs1.getObjectId()).thenReturn(rhsId1);
    assertTrue(e.eq(lhs, rhs1));
    assertTrue(e.eq(lhs, rhsId1));
    assertTrue(e.eq(lhs, rhsId1Map));
    Map<String, Object> rhsId2Map = new HashMap<>();
    rhsId2Map.put("a", 1);
    rhsId2Map.put("b", "BX");
    ObjectId rhsId2 = new ObjectId("X", rhsId2Map);
    Persistent rhs2 = mock(Persistent.class);
    when(rhs2.getObjectId()).thenReturn(rhsId2);
    assertFalse(e.eq(lhs, rhs2));
    assertFalse(e.eq(lhs, rhsId2));
    assertFalse(e.eq(lhs, rhsId2Map));
    assertFalse(e.eq(lhs, "B"));
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 63 with ObjectId

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

the class EvaluatorTest method testEvaluator_Persistent.

@Test
public void testEvaluator_Persistent() {
    ObjectId lhsId = new ObjectId("X", "k", 3);
    Persistent lhs = mock(Persistent.class);
    when(lhs.getObjectId()).thenReturn(lhsId);
    Evaluator e = Evaluator.evaluator(lhs);
    assertNotNull(e);
    ObjectId rhsId1 = new ObjectId("X", "k", 3);
    Persistent rhs1 = mock(Persistent.class);
    when(rhs1.getObjectId()).thenReturn(rhsId1);
    assertTrue(e.eq(lhs, rhs1));
    assertTrue(e.eq(lhs, rhsId1));
    assertTrue(e.eq(lhs, 3));
    ObjectId rhsId2 = new ObjectId("X", "k", 4);
    Persistent rhs2 = mock(Persistent.class);
    when(rhs2.getObjectId()).thenReturn(rhsId2);
    assertFalse(e.eq(lhs, rhs2));
    assertFalse(e.eq(lhs, rhsId2));
    assertFalse(e.eq(lhs, 4));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 64 with ObjectId

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

the class DataContextExtrasIT method testResolveFaultFailure.

@Test
public void testResolveFaultFailure() {
    Persistent o1 = context.findOrCreateObject(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 234));
    try {
        context.prepareForAccess(o1, null, false);
        fail("Must blow on non-existing fault.");
    } catch (CayenneRuntimeException ignored) {
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 65 with ObjectId

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

the class DataContextIT method testCurrentSnapshotWithToOneFault.

/**
 * Testing snapshot with to-one fault. This was a bug CAY-96.
 */
@Test
public void testCurrentSnapshotWithToOneFault() throws Exception {
    createGalleriesAndExhibitsDataSet();
    // Exhibit with Gallery as Fault must still include Gallery
    // Artist and Exhibit (Exhibit has unresolved to-one to gallery as in
    // the
    // CAY-96 bug report)
    ObjectId eId = new ObjectId("Exhibit", Exhibit.EXHIBIT_ID_PK_COLUMN, 2);
    Exhibit e = (Exhibit) context.performQuery(new ObjectIdQuery(eId)).get(0);
    assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
    DataRow snapshot = context.currentSnapshot(e);
    // assert that after taking a snapshot, we have FK in, but the
    // relationship
    // is still a Fault
    assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
    assertEquals(new Integer(33002), snapshot.get("GALLERY_ID"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) Fault(org.apache.cayenne.Fault) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow) 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