public void testUpdate()
{
// Old value with removal
InstanceList list = Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, list.size());
Instance contact = list.getInstance(0);
assertEquals("Test", contact.getOldValue("lastName"));
assertFalse(contact.isDirty("lastName"));
assertEquals("Test", contact.getPreValue("lastName"));
assertFalse(contact.isUpdated("lastName"));
contact.setValue("lastName", "Best");
assertEquals("Test", contact.getOldValue("lastName"));
assertTrue(contact.isDirty("lastName"));
assertEquals("Test", contact.getPreValue("lastName"));
assertTrue(contact.isUpdated("lastName"));
assertEquals("CON", contact.getOldValueDirect(contact.getMetaclass().getAttribute("classCode").getOrdinal()));
contact.setValue("type", null);
assertEquals("Joe Best {jtest}", contact.getValue("fullName"));
assertEquals("Joe Test [Employee] {jtest}", contact.getOldValue("fullName"));
assertFalse(contact.isDirty("fullName"));
assertEquals("Joe Test [Employee] {jtest}", contact.getPreValue("fullName"));
assertFalse(contact.isUpdated("fullName"));
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertFalse(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
((InstanceList)contact.getValue("addresses")).remove(0);
assertEquals(1, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
contact.setValue("lastUpdated", new Timestamp(3));
assertSame(Invalid.VALUE,
contact.getValueDirect(contact.getMetaclass().getAttribute("lastUpdatedMillisecond").getOrdinal()));
contact.invoke("update");
assertEquals("Test", contact.getOldValue("lastName"));
assertTrue(contact.isDirty("lastName"));
assertEquals("Best", contact.getPreValue("lastName"));
assertFalse(contact.isUpdated("lastName"));
assertEquals("Joe Best {jtest}", contact.getValue("fullName"));
assertEquals("Joe Test [Employee] {jtest}", contact.getOldValue("fullName"));
assertFalse(contact.isDirty("fullName"));
assertEquals("Joe Best {jtest}", contact.getPreValue("fullName"));
assertFalse(contact.isUpdated("fullName"));
assertEquals(1, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(1, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
rollback();
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertFalse(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
((InstanceList)contact.getValue("addresses")).remove(0);
assertEquals(1, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
rollback();
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertFalse(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
((InstanceList)contact.getValue("phones")).remove(0);
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(phones)"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
rollback();
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
((InstanceList)contact.getValue("phones")).remove(0);
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(phones)"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
rollback();
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
reset();
// Old value with adding
list = Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
contact = list.getInstance(0);
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
Instance address = new Instance(getMetadata().getMetaclass("Address"), Instance.NEW, m_context);
address.setValue("contact", contact);
assertEquals(3, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(3, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
contact.invoke("update");
assertEquals(3, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(3, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
rollback();
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertFalse(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
address = new Instance(getMetadata().getMetaclass("Address"), Instance.NEW, m_context);
address.setValue("contact", contact);
assertEquals(3, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(3, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertTrue(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertTrue(contact.isUpdated("addresses"));
rollback();
assertEquals(2, ((InstanceList)contact.getValue("addresses")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("addresses")).size());
assertFalse(contact.isDirty("addresses"));
assertEquals(2, ((InstanceList)contact.getPreValue("addresses")).size());
assertFalse(contact.isUpdated("addresses"));
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
((InstanceList)contact.getValue("phones")).remove(0);
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(phones)"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(1, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
rollback();
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
Instance phone = new Instance(getMetadata().getMetaclass("Phone"), Instance.NEW, m_context);
phone.setValue("contact", contact);
assertEquals(3, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(phones)"),
parse("(and (= lastName \"Test\") (= firstName \"Joe\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(3, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
rollback();
assertEquals(2, ((InstanceList)contact.getValue("phones")).size());
assertEquals(2, ((InstanceList)contact.getOldValue("phones")).size());
reset();
// Other tests
list = Query.createRead(getMetadata().getMetaclass("Contact"),
parse("(firstName lastName (addresses type typeEnum city))"),
parse("(= lastName \"Test\")"),
null, -1, 0, false, Query.SEC_NONE, m_context).read();
assertEquals(2, list.size());
Instance cottage = Query.createRead(getMetadata().getMetaclass("AddressType"),
parse("(type)"), parse("(and (= type \"Cottage\") (= locale \"en\"))"),
null, -1, 0, false, Query.SEC_NONE, m_context).read().getInstance(0);
for (int i = 0; i < list.size(); ++i)
{
contact = list.getInstance(i);
contact.setValue("lastName", "QA");
InstanceList addrs = (InstanceList)contact.getValue("addresses");
for (int k = 0; k < addrs.size(); ++k)
{
Instance addr = addrs.getInstance(k);
if ("Home".equals(addr.getValue("type")))
{
addr.setValue("type", "Residence");
contact.setValue("primaryAddress", addr);