db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattba...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/otm SwizzleTests.java
Date Sat, 21 Jun 2003 23:34:55 GMT
mattbaird    2003/06/21 16:34:55

  Added:       src/test/org/apache/ojb/otm SwizzleTests.java
  Log:
  Test case that shows failure to swizzle objects properly in the Contract-Version-Effectiveness
pattern.
  
  Revision  Changes    Path
  1.1                  db-ojb/src/test/org/apache/ojb/otm/SwizzleTests.java
  
  Index: SwizzleTests.java
  ===================================================================
  /*
  * Copyright 2003 NitroNutz, Inc.
  * Dublin, California, 94568
  * All rights reserved.
  *
  * This software is the confidential and proprietary information of
  * NitroNutz, Inc. ("Confidential Information").  You shall not
  * disclose such Confidential Information and shall use it only in
  * accordance with the terms of the license agreement you entered into
  * with NitroNutz, Inc.
  */
  package org.apache.ojb.otm;
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.*;
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.QueryFactory;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.otm.core.Transaction;
  import org.apache.ojb.otm.core.TransactionException;
  import org.apache.ojb.otm.lock.LockingException;
  import org.apache.ojb.otm.lock.LockType;
  
  import java.sql.Timestamp;
  import java.util.Iterator;
  import java.util.Date;
  
  /**
   * User: Matthew Baird
   * Date: Jun 21, 2003
   * Time: 3:59:08 PM
   */
  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());
  		createTestData();
  	}
  
  	public void tearDown() throws LockingException
  	{
  		deleteAllData();
  		_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.setRelatedValue1("matt");
  		r2c.setRelatedValue2(34);
  		r2c.setRelatedValue3(new Date());
  		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();
  	}
  }
  
  
  

Mime
View raw message