public void testWriteInterMineObject() throws Exception {
   InterMineObject o =
       (InterMineObject) DynamicUtil.createObject(Collections.singleton(InterMineObject.class));
   try {
     writer.store(o);
   } finally {
     writer.delete(o);
   }
 }
 public void testWriteBigDepartment() throws Exception {
   InterMineObject o = new BigDepartment();
   try {
     writer.store(o);
     o = writer.getObjectById(o.getId(), Department.class);
     if (!(o instanceof BigDepartment)) {
       fail("Expected a BigDepartment back");
     }
   } finally {
     writer.delete(o);
   }
 }
 public void testWriteCleaner() throws Exception {
   InterMineObject o = new Cleaner();
   try {
     writer.store(o);
     o = writer.getObjectById(o.getId(), Employee.class);
     if (!(o instanceof Cleaner)) {
       fail("Expected a Cleaner back");
     }
   } finally {
     writer.delete(o);
   }
 }
 public void testWriteCloneable() throws Exception {
   InterMineObject o =
       (InterMineObject)
           DynamicUtil.createObject(
               new HashSet(Arrays.asList(new Class[] {Employee.class, Cloneable.class})));
   try {
     writer.store(o);
     o = writer.getObjectById(o.getId(), Employee.class);
     if (!(o instanceof Cloneable)) {
       fail("Expected a Cloneable back");
     }
   } finally {
     writer.delete(o);
   }
 }
  /** Test that transactions do actually commit and that isInTransaction() works. */
  public void testCommitTransactions() throws Exception {
    Address address1 = new Address();
    address1.setAddress("Address 1");
    Address address2 = new Address();
    address2.setAddress("Address 2");

    Query q = new Query();
    QueryClass qcAddress = new QueryClass(Address.class);
    QueryField qf = new QueryField(qcAddress, "address");
    ConstraintSet cs1 = new ConstraintSet(ConstraintOp.OR);
    cs1.addConstraint(new SimpleConstraint(qf, ConstraintOp.MATCHES, new QueryValue("Address%")));
    q.addToSelect(qcAddress);
    q.addFrom(qcAddress);
    q.addToOrderBy(qf);
    q.setConstraint(cs1);

    try {
      writer.beginTransaction();
      assertTrue(writer.isInTransaction());

      writer.store(address1);
      writer.store(address2);

      // Should be nothing in OS until we commit
      Results res = realOs.execute(q);
      assertEquals(0, res.size());

      // However, they should be in the WRITER.
      // TODO: These lines now fail, because we do not allow querying on writers with uncommitted
      // data. The writer should relax this restriction.
      res = writer.execute(q);
      assertEquals(2, res.size());

      writer.commitTransaction();
      assertFalse(writer.isInTransaction());
      res = realOs.execute(q);
      assertEquals(2, res.size());
      assertEquals(address1, (Address) ((ResultsRow) res.get(0)).get(0));
      assertEquals(address2, (Address) ((ResultsRow) res.get(1)).get(0));

    } finally {
      writer.delete(address1);
      writer.delete(address2);
    }
  }
 public void testWriteDynamicObject2() throws Exception {
   InterMineObject o =
       (InterMineObject)
           DynamicUtil.createObject(
               new HashSet(Arrays.asList(new Class[] {ImportantPerson.class, Employee.class})));
   try {
     writer.store(o);
     o = writer.getObjectById(o.getId(), Employee.class);
     if (!(o instanceof ImportantPerson)) {
       fail("Expected an ImportantPerson back");
     }
     if (!(o instanceof Employee)) {
       fail("Expected an Employee back");
     }
   } finally {
     writer.delete(o);
   }
 }
  public void testAddToCollection() throws Exception {
    Company c1 = (Company) DynamicUtil.createObject(Collections.singleton(Company.class));
    Contractor c2 = new Contractor();
    c1.setName("Michael");
    c2.setName("Albert");

    try {
      writer.store(c1);
      writer.store(c2);

      Company c3 = (Company) writer.getObjectById(c1.getId(), Company.class);
      assertEquals(0, c3.getContractors().size());

      writer.addToCollection(c1.getId(), Company.class, "contractors", c2.getId());

      c3 = (Company) writer.getObjectById(c1.getId(), Company.class);
      assertEquals(1, c3.getContractors().size());
      assertTrue(c3.getContractors().iterator().next() instanceof Contractor);
      assertEquals(c2.getId(), ((Contractor) c3.getContractors().iterator().next()).getId());
    } finally {
      writer.delete(c1);
      writer.delete(c2);
    }
  }
  public void testTransactionsAndCaches() throws Exception {
    Address address1 = new Address();
    address1.setAddress("Address 1");
    Address address2 = new Address();
    address2.setAddress("Address 2");

    writer.flushObjectById();
    realOs.flushObjectById();

    try {
      writer.store(address1);
      Integer id = address1.getId();
      address2.setId(id);

      assertNull(realOs.pilferObjectById(id));
      assertNull(writer.pilferObjectById(id));

      assertNotNull("Looked for id " + id, realOs.getObjectById(id, Address.class));
      assertNull(writer.pilferObjectById(id));
      assertNotNull(realOs.pilferObjectById(id));
      realOs.flushObjectById();

      assertNotNull(writer.getObjectById(id, Address.class));
      assertNotNull(writer.pilferObjectById(id));
      assertNull(realOs.pilferObjectById(id));
      assertNotNull(realOs.getObjectById(id, Address.class));

      writer.store(address2);
      assertNotNull(writer.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress());
      assertNotNull(realOs.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress());

      writer.delete(address2);
      assertNull(writer.getObjectById(id, Address.class));
      assertNull(realOs.getObjectById(id, Address.class));

      writer.store(address1);
      writer.beginTransaction();
      writer.store(address2);
      assertNotNull(writer.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress());
      assertNotNull(realOs.getObjectById(id, Address.class));
      assertEquals("Address 1", ((Address) realOs.getObjectById(id, Address.class)).getAddress());

      writer.commitTransaction();
      assertNotNull(writer.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress());
      assertNotNull(realOs.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress());

      writer.beginTransaction();
      writer.delete(address1);
      assertNull(writer.getObjectById(id, Address.class));
      assertNotNull(realOs.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress());

      writer.abortTransaction();
      assertNotNull(writer.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress());
      assertNotNull(realOs.getObjectById(id, Address.class));
      assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress());
    } finally {
      writer.delete(address1);
    }
  }