/**
* Tests reads.
*/
public void testRead()
{
Metaclass animalClass = m_metadata.getMetaclass("Animal");
Instance inst = null;
InstanceArrayList list = null;
// Read all
list = (InstanceArrayList)animalClass.invoke("read", new Object[]{
null, null, parse("((commonName . #t))"), null, null, null
});
assertEquals(4, list.size());
inst = list.getInstance(0);
assertEquals("Beaver", inst.getValue("commonName"));
inst = list.getInstance(1);
assertEquals("Cat", inst.getValue("commonName"));
inst = list.getInstance(2);
assertEquals("Dog", inst.getValue("commonName"));
inst = list.getInstance(3);
assertEquals("Spider", inst.getValue("commonName"));
m_context.initialize(m_context.getPrincipal(), m_context.getMachine().getGlobalEnvironment());
// Read just 4-legged animals
list = (InstanceArrayList)animalClass.invoke("read", new Object[]{
parse("(commonName)"), Pair.list(Symbol.EQ, Pair.attribute("limbCount"), Primitive.createInteger(4)), null, null, null, null
});
assertEquals(3, list.size());
inst = list.getInstance(0);
assertEquals("Dog", inst.getValue("commonName"));
inst = list.getInstance(1);
assertEquals("Cat", inst.getValue("commonName"));
assertEquals("Felis catus", inst.getValue("scientificName"));
inst = list.getInstance(2);
assertEquals("Beaver", inst.getValue("commonName"));
// Read all with a cursor
Cursor cur = Query.createRead(animalClass, null, null, null, -1, 0, false, Query.SEC_ALL, m_context).openCursor();
inst = cur.next();
assertEquals("Dog", inst.getValue("commonName"));
inst = cur.next();
assertEquals("Cat", inst.getValue("commonName"));
inst = cur.next();
assertEquals("Spider", inst.getValue("commonName"));
inst = cur.next();
assertEquals("Beaver", inst.getValue("commonName"));
assertNull(cur.next());
// Read all with a cursor, canceling early
cur = Query.createRead(animalClass, null, null, null, -1, 0, false, Query.SEC_ALL, m_context).openCursor();
inst = cur.next();
assertEquals("Dog", inst.getValue("commonName"));
cur.close();
assertNull(cur.next());
assertNull(cur.next());
// Read class that has 2-part OID
Metaclass penClass = m_metadata.getMetaclass("AnimalPen");
list = (InstanceArrayList)penClass.invoke("read", new Object[] {
null, Pair.list(Symbol.EQ, Pair.attribute("name"), "Dog pen"), null, null, null, null
});
assertEquals(1, list.size());
inst = list.getInstance(0);
assertEquals(0, ((Integer)inst.getValue("locking")).intValue());
assertEquals(2, inst.getOID().getCount());
assertEquals("North", inst.getOID().getValue(0));
assertEquals(8, ((Integer)inst.getOID().getValue(1)).intValue());
assertEquals("North", inst.getValue("k1"));
assertEquals(8, ((Integer)inst.getValue("k2")).intValue());
// Read parent, getting sub-class attribute.
list = (InstanceArrayList)animalClass.invoke("read", new Object[]{
parse("(commonName (@@ DomesticAnimal petName))"), parse("(= (@ commonName) \"Dog\")"), null, null, null, null
});
assertEquals(1, list.size());
inst = list.getInstance(0);
assertEquals("DomesticAnimal", inst.getClassName());
assertEquals("Dog", inst.getValue("commonName"));
assertEquals("Kerberos", inst.getValue("petName"));
// Read sub-class directly.
Metaclass domAnimalClass = m_metadata.getMetaclass("DomesticAnimal");
list = (InstanceArrayList)domAnimalClass.invoke("read", new Object[]{
parse("(petName commonName)"), parse("(= (@ petName) \"Cleopatra\")"), null, null, null, null
});
assertEquals(1, list.size());
inst = list.getInstance(0);