}
   public void testNew()
   {
      Metaclass metaclass = getMetadata().getMetaclass("Principal");
      OID oid = new OID(new Object[]{Binary.parse("00000000000000000000000000000004")});
      Instance principal = m_context.findInstance(metaclass, oid);
      if (principal == null)
      {
         principal = new Instance(metaclass, m_context);
         principal.cache(oid);
      }
      Instance contact = new Instance(getMetadata().getMetaclass("Contact"), Instance.NEW, m_context);
      contact.setValue("firstName", "Ozzy");
      contact.setValue("lastName", "Osbourne");
      contact.setValue("testCategory", "");
      contact.setValue("readPrincipal", principal);
      contact.setValue("version", Primitive.ZERO_INTEGER);
      // Test async invocation on new instances
      getMetadata().getMetaclass("SysQueue").invoke("invoke", new Object[]{contact, Symbol.define("testAsync")});
      getMetadata().getMetaclass("SysQueue").invoke("invoke", new Object[]{contact, Symbol.define("testAsync"), new Object[]{"test"}});
      InstanceList list = (InstanceList)contact.getValue("addresses");
      Instance address = new Instance(getMetadata().getMetaclass("Address"), Instance.NEW, m_context);
      address.setValue("type", "Business");
      address.setValue("country", "USA");
      list.add(address);
      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);
      address = new Instance(getMetadata().getMetaclass("Address"), Instance.NEW, m_context);
      address.setValue("typeEnum", cottage);
      address.setValue("country", "Canada");
      list.add(address);
      contact.setValue("primaryAddress", address);
      contact = new Instance(getMetadata().getMetaclass("Contact"), Instance.NEW, m_context);
      contact.setValue("firstName", "Ronnie");
      contact.setValue("lastName", "Dio");
      contact.setValue("readPrincipal", principal);
      contact.setValue("version", Primitive.ZERO_INTEGER);
      list = (InstanceList)contact.getValue("phones");
      Instance phone = new Instance(getMetadata().getMetaclass("Phone"), Instance.NEW, m_context);
      phone.setValue("type", "Business");
      phone.setValue("number", "(111) 222-3344");
      list.add(phone);
      commit();
      assertEquals(Primitive.ZERO_INTEGER, m_context.getMachine().getGlobalEnvironment().getVariable(Symbol.define("update-count")));
      assertEquals(Primitive.createInteger(2), m_context.getMachine().getGlobalEnvironment().getVariable(Symbol.define("commit-count")));
      
      assertEquals(7, Query.createRead(getMetadata().getMetaclass("Contact"), null,
         parse("(= classCode \"CON\")"), null, -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(2, Query.createRead(getMetadata().getMetaclass("Contact"), null,
         parse("(in? lastName \"Osbourne\" \"Dio\")"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      list = Query.createRead(getMetadata().getMetaclass("Contact"), null,
         parse("(= testCategory \"\")"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(1, list.size());
      assertEquals("", list.getInstance(0).getValue("testCategory"));
      assertEquals(8, Query.createRead(getMetadata().getMetaclass("Contact"), null,
         parse("(null? testCategory)"), null, -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(1, Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(and (= (@ contact firstName) \"Ozzy\") (= country \"USA\"))"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(1, Query.createRead(getMetadata().getMetaclass("Address"), null,
         parse("(and (= (@ contact firstName) \"Ozzy\") (= country \"Canada\"))"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(1, Query.createRead(getMetadata().getMetaclass("Phone"), null,
         parse("(= number \"(111) 222-3344\")"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      Instance ref = new Instance(getMetadata().getMetaclass("PrincipalRef2"), Instance.NEW, m_context);
      ref.setValue("name", "a");
      ref.setValue("count", Primitive.createInteger(3));
      ref = new Instance(getMetadata().getMetaclass("PrincipalRef2"), Instance.NEW, m_context);
      ref.setValue("name", "b");
      commit();
      m_context.removeInstance(ref);
      ref = Query.createRead(getMetadata().getMetaclass("PrincipalRef2"), null,
         parse("(= name \"b\")"), null, -1, 0, false, Query.SEC_NONE, m_context).read().getInstance(0);
      ref.setValue("count", Primitive.createInteger(7));
      commit();
      assertEquals(1, Query.createRead(getMetadata().getMetaclass("PrincipalRef2"), null,
         parse("(and (= name \"a\") (= count 3))"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(1, Query.createRead(getMetadata().getMetaclass("PrincipalRef2"), null,
         parse("(and (= name \"b\") (= count 7))"), null,
         -1, 0, false, Query.SEC_NONE, m_context).read().size());
      // KeyGenerator.MaxPlus1 and KeyGenerator.Counter test
      new Instance(getMetadata().getMetaclass("Max1Test"), Instance.NEW, m_context);
      new Instance(getMetadata().getMetaclass("Max1Test"), Instance.NEW, m_context);
      new Instance(getMetadata().getMetaclass("CounterTest"), Instance.NEW, m_context);
      new Instance(getMetadata().getMetaclass("CounterTest"), Instance.NEW, m_context);
      commit();
      assertEquals(2, Query.createRead(getMetadata().getMetaclass("Max1Test"), null,
         null, null, -1, 0, false, Query.SEC_NONE, m_context).read().size());
      assertEquals(2, Query.createRead(getMetadata().getMetaclass("CounterTest"), null,
         null, null, -1, 0, false, Query.SEC_NONE, m_context).read().size());
      // Test non-primitive initializer
      new Instance(getMetadata().getMetaclass("ContactWithInits"), Instance.NEW, m_context);
      commit();
      reset();
      list = Query.createRead(getMetadata().getMetaclass("ContactWithInits"),
         parse("((initAddress city))"), null, null, -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(1, list.size());
      assertEquals("Canada", ((Instance)list.getInstance(0).getValue("initAddress")).getValue("country"));
      // Duplicate unique constraint test
      m_context.setSecure(false);
      Instance wfqueue = new Instance(getMetadata().getMetaclass("SysWorkflowQueue"), Instance.NEW, m_context);
      wfqueue.setOID(new OID(new Object[]{Binary.parse("00000000000000000000000000000001")}));
      wfqueue.setValue("name", "custom");
      wfqueue.setValue("type", "WFLIFOQueue");
      wfqueue.setValue("caption", "LIFO");
      wfqueue.setValue("customized", Boolean.FALSE);