/**
* Tests reading an association.
*/
public void testReadManyToOneHeterogeneousAssoc()
{
Metaclass animalClass = m_metadata.getMetaclass("Animal");
Metaclass penClass = m_metadata.getMetaclass("AnimalPen");
Instance inst, assocInst;
InstanceList list;
int nStartCallCount, nEndCallCount;
list = Query.createRead(animalClass,
parse("(commonName limbCount pen)"), null, null, -1, 0, false, Query.SEC_NODE, m_context
).read();
assertEquals(4, list.size());
inst = list.getInstance(0);
assertEquals("Dog", inst.getValue("commonName"));
assocInst = (Instance)inst.getValue("pen");
assertNotNull(assocInst);
nStartCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals("Dog pen", assocInst.getValue("name"));
nEndCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals(nStartCallCount + 1, nEndCallCount); // Lazy loaded
assertEquals(2, ((InstanceList)assocInst.getValue("animals")).size());
assertSame(inst, ((InstanceList)assocInst.getValue("animals")).getInstance(0));
inst = list.getInstance(1);
assertEquals("Cat", inst.getValue("commonName"));
assocInst = (Instance)inst.getValue("pen");
assertNotNull(assocInst);
nStartCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals("Cat pen", assocInst.getValue("name"));
nEndCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals(nStartCallCount + 1, nEndCallCount); // Lazy loaded
assertEquals(1, ((InstanceList)assocInst.getValue("animals")).size());
assertSame(inst, ((InstanceList)assocInst.getValue("animals")).getInstance(0));
inst = list.getInstance(2);
assertEquals("Spider", inst.getValue("commonName"));
assocInst = (Instance)inst.getValue("pen");
assertNotNull(assocInst);
nStartCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals("Dog pen", assocInst.getValue("name"));
nEndCallCount = ((Integer)penClass.getValue("objectKeyReadCount")).intValue();
assertEquals(nStartCallCount, nEndCallCount); // Loaded by a previous lazy load
assertEquals(2, ((InstanceList)assocInst.getValue("animals")).size());
assertSame(inst, ((InstanceList)assocInst.getValue("animals")).getInstance(1));
}