You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ol...@apache.org on 2003/07/13 16:08:14 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/otm SwizzleTests.java

olegnitz    2003/07/13 07:08:14

  Modified:    src/test/org/apache/ojb/otm SwizzleTests.java
  Log:
  Added tests with modifications of relations and collections
  
  Revision  Changes    Path
  1.10      +356 -242  db-ojb/src/test/org/apache/ojb/otm/SwizzleTests.java
  
  Index: SwizzleTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/otm/SwizzleTests.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SwizzleTests.java	9 Jul 2003 00:14:39 -0000	1.9
  +++ SwizzleTests.java	13 Jul 2003 14:08:14 -0000	1.10
  @@ -22,246 +22,360 @@
    */
   public class SwizzleTests extends TestCase
   {
  -	private static Class CLASS = SwizzleTests.class;
  -	private TestKit _kit;
  -	private OTMConnection _conn;
  -	private static final int COUNT = 1;
  -	private static final long TIME = System.currentTimeMillis();
  -
  -	public void setUp() throws LockingException
  -	{
  -		_kit = TestKit.getTestInstance();
  -		_conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  -	}
  -
  -	public void tearDown() throws LockingException
  -	{
  -		_conn.close();
  -		_conn = null;
  -	}
  -
  -	public static void main(String[] args)
  -	{
  -		String[] arr = {CLASS.getName()};
  -		junit.textui.TestRunner.main(arr);
  -	}
  -
  -	public void testSwizzle() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
  -	{
  -		deleteAllData();
  -		createTestData();
  -		/**
  -		 * first get the contract object.
  -		 */
  -		PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
  -		Transaction tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		Criteria crit = new Criteria();
  -		crit.addEqualTo("pk", "C" + TIME);
  -		Query q = QueryFactory.newQuery(Contract.class, crit);
  -		Iterator it = _conn.getIteratorByQuery(q, LockType.WRITE_LOCK);
  -		Object retval = null;
  -		RelatedToContract r2c = new RelatedToContract();
  -		r2c.setPk("R2C" + TIME);
  -		r2c.setRelatedValue1("matt");
  -		r2c.setRelatedValue2(34);
  -		r2c.setRelatedValue3(new Timestamp(TIME));
  -		_conn.makePersistent(r2c);
  -		while (it.hasNext())
  -		{
  -			retval = it.next();
  -			((Contract) retval).setRelatedToContract(r2c);
  -		}
  -		tx.commit();
  -		r2c = null;
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		crit = new Criteria();
  -		crit.addEqualTo("pk", "E" + TIME);
  -		q = QueryFactory.newQuery(Effectiveness.class, crit);
  -		it = _conn.getIteratorByQuery(q);
  -		retval = null;
  -		while (it.hasNext())
  -		{
  -			retval = it.next();
  -		}
  -		tx.commit();
  -		assertTrue("contract object should have a RelatedToContract instance attached", ((Effectiveness) retval).getVersion().getContract().getRelatedToContract() != null);
  -	}
  -
  -	private void createTestData() throws TransactionException, LockingException
  -	{
  -		for (int i = 0; i < COUNT; i++)
  -		{
  -			Transaction tx = _kit.getTransaction(_conn);
  -			tx.begin();
  -			Contract contract = new Contract();
  -			contract.setPk("C" + TIME);
  -			contract.setContractValue1("contractvalue1");
  -			contract.setContractValue2(1);
  -			contract.setContractValue3("contractvalue3");
  -			contract.setContractValue4(new Timestamp(TIME));
  -			_conn.makePersistent(contract);
  -			tx.commit();
  -			tx = _kit.getTransaction(_conn);
  -			tx.begin();
  -			Version version = new Version();
  -			version.setPk("V" + TIME);
  -			version.setVersionValue1("versionvalue1");
  -			version.setVersionValue2(1);
  -			version.setVersionValue3(new Timestamp(TIME));
  -			version.setContract(contract);
  -			_conn.makePersistent(version);
  -			tx.commit();
  -			tx = _kit.getTransaction(_conn);
  -			tx.begin();
  -			Effectiveness eff = new Effectiveness();
  -			eff.setPk("E" + TIME);
  -			eff.setEffValue1("effvalue1");
  -			eff.setEffValue2(1);
  -			eff.setEffValue3(new Timestamp(TIME));
  -			eff.setVersion(version);
  -			_conn.makePersistent(eff);
  -			tx.commit();
  -		}
  -	}
  -
  -	public void deleteAllData() throws LockingException
  -	{
  -		Criteria crit = new Criteria();
  -		Query q;
  -		Iterator iter;
  -		/**
  -		 * delete effectiveness first
  -		 */
  -		Transaction tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		q = QueryFactory.newQuery(Effectiveness.class, crit);
  -		iter = _conn.getIteratorByQuery(q);
  -		while (iter.hasNext())
  -		{
  -			_conn.deletePersistent(iter.next());
  -		}
  -		tx.commit();
  -		/**
  -		 * then version
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		q = QueryFactory.newQuery(Version.class, crit);
  -		iter = _conn.getIteratorByQuery(q);
  -		while (iter.hasNext())
  -		{
  -			_conn.deletePersistent(iter.next());
  -		}
  -		tx.commit();
  -		/**
  -		 * the contract
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		q = QueryFactory.newQuery(Contract.class, crit);
  -		iter = _conn.getIteratorByQuery(q);
  -		while (iter.hasNext())
  -		{
  -			_conn.deletePersistent(iter.next());
  -		}
  -		tx.commit();
  -	}
  -
  -	public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
  -	{
  -		clearTestData();
  -		TestClassA a = generateTestData();
  -		Transaction tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		_conn.makePersistent(a.getB());
  -		_conn.makePersistent(a);
  -		tx.commit();
  -		/**
  -		 * clear to start test
  -		 */
  -		_conn.invalidateAll();
  -		/**
  -		 * get A to make it and the related B in cache
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		Identity oid = _conn.getIdentity(a);
  -		TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
  -		assertTrue(a1.getB() != null);
  -		assertTrue(a1.getB().getValue1().equals("hi there"));
  -		/**
  -		 * everything is good, update b
  -		 */
  -		tx.commit();
  -
  -		/**
  -		 * now get B and update it, do NOT get it by traversing A
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		Identity boid = _conn.getIdentity(a.getB());
  -		TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
  -		assertTrue(b1 != null);
  -		assertTrue(b1.getValue1().equals("hi there"));
  -		/**
  -		 * everything is good, update b
  -		 */
  -		_conn.lockForWrite(b1);
  -		b1.setValue1("goodbye there");
  -		tx.commit();
  -		/**
  -		 * make sure b was updated
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		boid = _conn.getIdentity(a.getB());
  -		b1 = (TestClassB) _conn.getObjectByIdentity(boid);
  -		assertTrue(b1 != null);
  -		assertTrue(b1.getValue1().equals("goodbye there"));
  -		tx.commit();
  -
  -		/**
  -		 * now get A again and make sure the related B is updated to reflect
  -		 * the new value.
  -		 */
  -		tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
  -		assertTrue(a2.getB() != null);
  -		assertTrue(a2.getB().getValue1().equals("goodbye there"));
  -		tx.commit();
  -		clearTestData();
  -	}
  -
  -	private void clearTestData() throws LockingException
  -	{
  -		TestClassA a = generateTestData();
  -		Transaction tx = _kit.getTransaction(_conn);
  -		tx.begin();
  -		Identity oid = _conn.getIdentity(a);
  -		TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
  -		if (a1 != null)
  -		{
  -			if (a1.getB() != null)
  -				_conn.deletePersistent(a1.getB());
  -			_conn.deletePersistent(a1);
  -		}
  -		tx.commit();
  -	}
  -
  -	private TestClassA generateTestData()
  -	{
  -		TestClassA tca = new TestClassA();
  -		tca.setOid("someoid");
  -		tca.setValue1("abc");
  -		tca.setValue2("123");
  -		tca.setValue3(5);
  -		TestClassB tcb = new TestClassB();
  -		tcb.setOid("boid");
  -		tcb.setValue1("hi there");
  -		tca.setB(tcb);
  -		return tca;
  -	}
  +    private static Class CLASS = SwizzleTests.class;
  +    private TestKit _kit;
  +    private OTMConnection _conn;
  +    private static final int COUNT = 1;
  +    private static final long TIME = System.currentTimeMillis();
  +
  +    public void setUp() throws LockingException
  +    {
  +        _kit = TestKit.getTestInstance();
  +        _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +    }
  +
  +    public void tearDown() throws LockingException
  +    {
  +        _conn.close();
  +        _conn = null;
  +    }
  +
  +    public static void main(String[] args)
  +    {
  +        String[] arr = {CLASS.getName()};
  +        junit.textui.TestRunner.main(arr);
  +    }
  +
  +    public void testSwizzle() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
  +    {
  +        deleteAllData();
  +        createTestData();
  +        /**
  +        * first get the contract object.
  +        */
  +        PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Criteria crit = new Criteria();
  +        crit.addEqualTo("pk", "C" + TIME);
  +        Query q = QueryFactory.newQuery(Contract.class, crit);
  +        Iterator it = _conn.getIteratorByQuery(q, LockType.WRITE_LOCK);
  +        Object retval = null;
  +        RelatedToContract r2c = new RelatedToContract();
  +        r2c.setPk("R2C" + TIME);
  +        r2c.setRelatedValue1("matt");
  +        r2c.setRelatedValue2(34);
  +        r2c.setRelatedValue3(new Timestamp(TIME));
  +        _conn.makePersistent(r2c);
  +        while (it.hasNext())
  +        {
  +            retval = it.next();
  +            ((Contract) retval).setRelatedToContract(r2c);
  +        }
  +        tx.commit();
  +        r2c = null;
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        crit = new Criteria();
  +        crit.addEqualTo("pk", "E" + TIME);
  +        q = QueryFactory.newQuery(Effectiveness.class, crit);
  +        it = _conn.getIteratorByQuery(q);
  +        retval = null;
  +        while (it.hasNext())
  +        {
  +            retval = it.next();
  +        }
  +        tx.commit();
  +        assertTrue("contract object should have a RelatedToContract instance attached", ((Effectiveness) retval).getVersion().getContract().getRelatedToContract() != null);
  +    }
  +
  +    private void createTestData() throws TransactionException, LockingException
  +    {
  +        for (int i = 0; i < COUNT; i++)
  +        {
  +            Transaction tx = _kit.getTransaction(_conn);
  +            tx.begin();
  +            Contract contract = new Contract();
  +            contract.setPk("C" + TIME);
  +            contract.setContractValue1("contractvalue1");
  +            contract.setContractValue2(1);
  +            contract.setContractValue3("contractvalue3");
  +            contract.setContractValue4(new Timestamp(TIME));
  +            _conn.makePersistent(contract);
  +            tx.commit();
  +            tx = _kit.getTransaction(_conn);
  +            tx.begin();
  +            Version version = new Version();
  +            version.setPk("V" + TIME);
  +            version.setVersionValue1("versionvalue1");
  +            version.setVersionValue2(1);
  +            version.setVersionValue3(new Timestamp(TIME));
  +            version.setContract(contract);
  +            _conn.makePersistent(version);
  +            tx.commit();
  +            tx = _kit.getTransaction(_conn);
  +            tx.begin();
  +            Effectiveness eff = new Effectiveness();
  +            eff.setPk("E" + TIME);
  +            eff.setEffValue1("effvalue1");
  +            eff.setEffValue2(1);
  +            eff.setEffValue3(new Timestamp(TIME));
  +            eff.setVersion(version);
  +            _conn.makePersistent(eff);
  +            tx.commit();
  +        }
  +    }
  +
  +    public void deleteAllData() throws LockingException
  +    {
  +        Criteria crit = new Criteria();
  +        Query q;
  +        Iterator iter;
  +        /**
  +        * delete effectiveness first
  +        */
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        q = QueryFactory.newQuery(Effectiveness.class, crit);
  +        iter = _conn.getIteratorByQuery(q);
  +        while (iter.hasNext())
  +        {
  +            _conn.deletePersistent(iter.next());
  +        }
  +        tx.commit();
  +        /**
  +        * then version
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        q = QueryFactory.newQuery(Version.class, crit);
  +        iter = _conn.getIteratorByQuery(q);
  +        while (iter.hasNext())
  +        {
  +            _conn.deletePersistent(iter.next());
  +        }
  +        tx.commit();
  +        /**
  +        * the contract
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        q = QueryFactory.newQuery(Contract.class, crit);
  +        iter = _conn.getIteratorByQuery(q);
  +        while (iter.hasNext())
  +        {
  +            _conn.deletePersistent(iter.next());
  +        }
  +        tx.commit();
  +    }
  +
  +    public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
  +    {
  +        clearTestData();
  +        TestClassA a = generateTestData();
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        _conn.makePersistent(a.getB());
  +        _conn.makePersistent(a);
  +        tx.commit();
  +        /**
  +        * clear to start test
  +        */
  +        _conn.invalidateAll();
  +        /**
  +        * get A to make it and the related B in cache
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Identity oid = _conn.getIdentity(a);
  +        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
  +        assertTrue(a1.getB() != null);
  +        assertTrue(a1.getB().getValue1().equals("hi there"));
  +        /**
  +        * everything is good, update b
  +        */
  +        tx.commit();
  +
  +        /**
  +        * now get B and update it, do NOT get it by traversing A
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Identity boid = _conn.getIdentity(a.getB());
  +        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
  +        assertTrue(b1 != null);
  +        assertTrue(b1.getValue1().equals("hi there"));
  +        /**
  +        * everything is good, update b
  +        */
  +        _conn.lockForWrite(b1);
  +        b1.setValue1("goodbye there");
  +        tx.commit();
  +        /**
  +        * make sure b was updated
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        boid = _conn.getIdentity(a.getB());
  +        b1 = (TestClassB) _conn.getObjectByIdentity(boid);
  +        assertTrue(b1 != null);
  +        assertTrue(b1.getValue1().equals("goodbye there"));
  +        tx.commit();
  +
  +        /**
  +        * now get A again and make sure the related B is updated to reflect
  +        * the new value.
  +        */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
  +        assertTrue(a2.getB() != null);
  +        assertTrue(a2.getB().getValue1().equals("goodbye there"));
  +        tx.commit();
  +        clearTestData();
  +    }
  +
  +    public void testSwizzleNto1() throws Exception
  +    {
  +        clearTestData();
  +        TestClassA a = generateTestData();
  +        TestClassB b2 = generateAnotherB();
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        _conn.makePersistent(a);
  +        tx.commit();
  +        /**
  +         * change B
  +         */
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Identity oid = _conn.getIdentity(a);
  +        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
  +        a1.setB(b2);
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        a = (TestClassA) _conn.getObjectByIdentity(oid);
  +        assertTrue(a.getB() != null);
  +        assertTrue(a.getB().getValue1().equals("value2"));
  +        a.setB(null);
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        a = (TestClassA) _conn.getObjectByIdentity(oid);
  +        assertTrue(a.getB() == null);
  +        tx.commit();
  +    }
  +
  +    public void testSwizzle1toN() throws Exception
  +    {
  +        clearTestData();
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Article article = Article.createInstance();
  +        article.setArticleId(77777);
  +        article.setStock(333);
  +        ProductGroup pg = new ProductGroup();
  +        pg.setId(77777);
  +        pg.setName("1");
  +        pg.add(article);
  +        article.setProductGroup(pg);
  +        _conn.makePersistent(pg);
  +        _conn.makePersistent(article);
  +        Identity pgOid = _conn.getIdentity(pg);
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
  +        pg.getAllArticlesInGroup().clear();
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
  +        assertEquals("should be equal", 0, pg.getAllArticlesInGroup().size());
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
  +        pg.getAllArticlesInGroup().add(article);
  +        tx.commit();
  +
  +        tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
  +        assertEquals("should be equal", 1, pg.getAllArticlesInGroup().size());
  +        tx.commit();
  +        clearTestData();
  +    }
  +
  +    private void clearTestData() throws LockingException
  +    {
  +        TestClassA a = generateTestData();
  +        TestClassB b2 = generateAnotherB();
  +        Transaction tx = _kit.getTransaction(_conn);
  +        tx.begin();
  +        Identity oid = _conn.getIdentity(a);
  +        Identity oidb = _conn.getIdentity(a.getB());
  +        Identity oidb2 = _conn.getIdentity(b2);
  +        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
  +        if (a1 != null)
  +        {
  +            _conn.deletePersistent(a1);
  +        }
  +        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
  +        if (b1 != null)
  +        {
  +            _conn.deletePersistent(b1);
  +        }
  +        b2 = (TestClassB) _conn.getObjectByIdentity(oidb2);
  +        if (b2 != null)
  +        {
  +            _conn.deletePersistent(b2);
  +        }
  +
  +        Article article = Article.createInstance();
  +        article.setArticleId(77777);
  +        ProductGroup pg = new ProductGroup();
  +        pg.setId(77777);
  +        Identity oidArt = _conn.getIdentity(article);
  +        Identity oidPG = _conn.getIdentity(pg);
  +        article = (Article) _conn.getObjectByIdentity(oidArt);
  +        if (article != null)
  +        {
  +            _conn.deletePersistent(article);
  +        }
  +        pg = (ProductGroup) _conn.getObjectByIdentity(oidPG);
  +        if (pg != null)
  +        {
  +            _conn.deletePersistent(pg);
  +        }
  +        tx.commit();
  +    }
  +
  +    private TestClassA generateTestData()
  +    {
  +        TestClassA tca = new TestClassA();
  +        tca.setOid("someoid");
  +        tca.setValue1("abc");
  +        tca.setValue2("123");
  +        tca.setValue3(5);
  +        TestClassB tcb = new TestClassB();
  +        tcb.setOid("boid");
  +        tcb.setValue1("hi there");
  +        tca.setB(tcb);
  +        return tca;
  +    }
  +
  +    private TestClassB generateAnotherB()
  +    {
  +        TestClassB tcb = new TestClassB();
  +        tcb.setOid("boid2");
  +        tcb.setValue1("value2");
  +        return tcb;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org