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 CopyTest.java
Date Fri, 11 Jul 2003 17:21:42 GMT
mattbaird    2003/07/11 10:21:42

  Modified:    src/java/org/apache/ojb/otm/copy
                        MetadataObjectCopyStrategy.java
                        ReflectiveObjectCopyStrategy.java
               src/test/org/apache/ojb/otm CopyTest.java
  Log:
  unfix for Oleg's better fix. Extended copy test to check for infinite recursion.
  
  Revision  Changes    Path
  1.4       +1 -0      db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java
  
  Index: MetadataObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MetadataObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.3
  +++ MetadataObjectCopyStrategy.java	11 Jul 2003 17:21:42 -0000	1.4
  @@ -118,6 +118,7 @@
   		try
   		{
   			retval = toCopy.getClass().newInstance();
  +			objMap.put(toCopy,retval);
   		}
   		catch (InstantiationException e)
   		{
  
  
  
  1.8       +41 -86    db-ojb/src/java/org/apache/ojb/otm/copy/ReflectiveObjectCopyStrategy.java
  
  Index: ReflectiveObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/ReflectiveObjectCopyStrategy.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ReflectiveObjectCopyStrategy.java	9 Jul 2003 23:46:47 -0000	1.7
  +++ ReflectiveObjectCopyStrategy.java	11 Jul 2003 17:21:42 -0000	1.8
  @@ -55,8 +55,6 @@
    */
   
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.accesslayer.IndirectionHandler;
   
   import java.lang.reflect.Array;
   import java.lang.reflect.Constructor;
  @@ -68,7 +66,6 @@
   import java.util.Set;
   //#ifdef JDK14
   import java.util.IdentityHashMap;
  -import java.sql.Timestamp;
   //#else
   /*
   import org.apache.ojb.otm.util.IdentityHashMap;
  @@ -98,55 +95,18 @@
   		FINAL_IMMUTABLE_CLASSES.add(Double.class);
   		FINAL_IMMUTABLE_CLASSES.add(Character.class);
   		FINAL_IMMUTABLE_CLASSES.add(Boolean.class);
  -		FINAL_IMMUTABLE_CLASSES.add(Timestamp.class);
  -		FINAL_IMMUTABLE_CLASSES.add(IndirectionHandler.class);
   	}
   
   	/**
   	 * makes a deep clone of the object, using reflection.
  -	 *
   	 * @param toCopy the object you want to copy
  -	 * @param broker the broker, if needed.
  +	 * @param broker the PersistenceBroker, if needed.
   	 * @return
   	 */
   	public final Object copy(final Object toCopy, final PersistenceBroker broker)
   	{
  -		return clone(toCopy, new IdentityHashMap(), new HashMap(), broker);
  -	}
  -
  -	private static final Object clone(Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
  -	{
  -		if (objMap.containsKey(toCopy)) return objMap.get(toCopy);
  -		/**
  -		 * eliminate some up front
  -		 */
  -		if (!FINAL_IMMUTABLE_CLASSES.contains(toCopy.getClass()))
  -		{
  -			/**
  -			 * only reload if this is a mapped class.
  -			 */
  -			if (broker.getDescriptorRepository().hasDescriptorFor(toCopy.getClass()))
  -			{
  -				Identity oid = new Identity(toCopy, broker);
  -
  -				Object temp = broker.serviceObjectCache().lookup(oid);
  -				if (temp == null)
  -				{
  -					temp = broker.getObjectByIdentity(oid);
  -					if (temp != null)
  -					{
  -						toCopy = temp;
  -					}
  -				}
  -				else
  -				{
  -					toCopy = temp;
  -				}
  -			}
  -		}
  -		return internalClone(toCopy, objMap, metadataMap, broker);
  +		return clone(toCopy, new IdentityHashMap(), new HashMap());
   	}
  -
   	/*
   	 * class used to cache class metadata info
   	 */
  @@ -159,17 +119,18 @@
   		boolean m_hasNoArgConstructor = true;
   	}
   
  -	private static Object internalClone(final Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
  +	private static Object clone(final Object toCopy, final Map objMap, final Map metadataMap)
   	{
   		/**
   		 * first, check to make sure we aren't recursing to some object that we've already copied.
   		 * if the toCopy is in the objMap, just return it.
   		 */
  +		if (objMap.containsKey(toCopy)) return objMap.get(toCopy);
   		final Class objClass = toCopy.getClass();
   		final Object retval;
   		if (objClass.isArray())
   		{
  -			retval = handleArray(toCopy, objMap, objClass, metadataMap, broker);
  +			retval = handleArray(toCopy, objMap, objClass, metadataMap);
   		}
   		else if (FINAL_IMMUTABLE_CLASSES.contains(objClass))
   		{
  @@ -178,14 +139,12 @@
   		}
   		else
   		{
  -			retval = handleObjectWithNoArgsConstructor(metadataMap, objClass, objMap, toCopy, broker);
  +			retval = handleObjectWithNoArgsConstructor(metadataMap, objClass, objMap, toCopy);
   		}
   		return retval;
   	}
   
  -	private static Object handleObjectWithNoArgsConstructor(final Map metadataMap, final Class
objClass,
  -	                                                        final Map objMap, final Object
toCopy,
  -	                                                        PersistenceBroker broker)
  +	private static Object handleObjectWithNoArgsConstructor(final Map metadataMap, final Class
objClass, final Map objMap, final Object toCopy)
   	{
   		Object retval = null;
   		ClassMetadata metadata = (ClassMetadata) metadataMap.get(objClass);
  @@ -194,9 +153,9 @@
   			metadata = new ClassMetadata();
   			metadataMap.put(objClass, metadata);
   		}
  +		Constructor noArg = metadata.m_noArgConstructor;
   		if (metadata.m_hasNoArgConstructor)
   		{
  -			Constructor noArg = metadata.m_noArgConstructor;
   			if (noArg == null)
   			{
   				try
  @@ -207,46 +166,45 @@
   				catch (Exception e)
   				{
   					metadata.m_hasNoArgConstructor = false;
  -					// throw new ObjectCopyException("class [" + objClass.getName() + "] has no noArg
constructor: " + e.toString(), e);
  +	//				throw new ObjectCopyException("class [" + objClass.getName() + "] has no noArg constructor:
" + e.toString(), e);
   				}
   			}
  -			if (metadata.m_hasNoArgConstructor)
  +		}
  +		if (metadata.m_hasNoArgConstructor)
  +		{
  +			if (!metadata.m_noArgConstructorAccessible && (Modifier.PUBLIC & noArg.getModifiers())
== 0)
   			{
  -				if (!metadata.m_noArgConstructorAccessible && (Modifier.PUBLIC & noArg.getModifiers())
== 0)
  -				{
  -					try
  -					{
  -						noArg.setAccessible(true);
  -					}
  -					catch (SecurityException e)
  -					{
  -						throw new ObjectCopyException("cannot access noArg constructor [" + noArg + "] of
class [" + objClass.getName() + "]: " + e.toString(), e);
  -					}
  -					metadata.m_noArgConstructorAccessible = true;
  -				}
   				try
   				{
  -					/**
  -					 * create the return value via the default no argument constructor
  -					 */
  -					retval = noArg.newInstance(EMPTY_OBJECT_ARRAY);
  -					objMap.put(toCopy, retval);
  -				}
  -				catch (Exception e)
  -				{
  -					throw new ObjectCopyException("cannot instantiate class [" + objClass.getName() +
"] using noArg constructor: " + e.toString(), e);
  +					noArg.setAccessible(true);
   				}
  -				for (Class c = objClass; c != Object.class; c = c.getSuperclass())
  +				catch (SecurityException e)
   				{
  -					copyClass(metadataMap, c, toCopy, retval, objMap, broker);
  +					throw new ObjectCopyException("cannot access noArg constructor [" + noArg + "] of
class [" + objClass.getName() + "]: " + e.toString(), e);
   				}
  +				metadata.m_noArgConstructorAccessible = true;
  +			}
  +			try
  +			{
  +				/**
  +				 * create the return value via the default no argument constructor
  +				 */
  +				retval = noArg.newInstance(EMPTY_OBJECT_ARRAY);
  +				objMap.put(toCopy, retval);
  +			}
  +			catch (Exception e)
  +			{
  +				throw new ObjectCopyException("cannot instantiate class [" + objClass.getName() + "]
using noArg constructor: " + e.toString(), e);
  +			}
  +			for (Class c = objClass; c != Object.class; c = c.getSuperclass())
  +			{
  +				copyClass(metadataMap, c, toCopy, retval, objMap);
   			}
   		}
   		return retval;
   	}
   
  -	private static void copyClass(final Map metadataMap, final Class c, final Object obj,
final Object retval,
  -	                              final Map objMap, PersistenceBroker broker)
  +	private static void copyClass(final Map metadataMap, final Class c, final Object obj,
final Object retval, final Map objMap)
   	{
   		ClassMetadata metadata;
   		metadata = (ClassMetadata) metadataMap.get(c);
  @@ -261,11 +219,11 @@
   			declaredFields = c.getDeclaredFields();
   			metadata.m_declaredFields = declaredFields;
   		}
  -		setFields(obj, retval, declaredFields, metadata.m_fieldsAccessible, objMap, metadataMap,
broker);
  +		setFields(obj, retval, declaredFields, metadata.m_fieldsAccessible, objMap, metadataMap);
   		metadata.m_fieldsAccessible = true;
   	}
   
  -	private static Object handleArray(final Object obj, final Map objMap, final Class objClass,
final Map metadataMap, PersistenceBroker broker)
  +	private static Object handleArray(final Object obj, final Map objMap, final Class objClass,
final Map metadataMap)
   	{
   		final Object retval;
   		final int arrayLength = Array.getLength(obj);
  @@ -300,7 +258,7 @@
   					final Object slot = Array.get(obj, i);
   					if (slot != null)
   					{
  -						final Object slotClone = clone(slot, objMap, metadataMap, broker);
  +						final Object slotClone = clone(slot, objMap, metadataMap);
   						Array.set(retval, i, slotClone);
   					}
   				}
  @@ -320,8 +278,7 @@
   	 */
   	private static void setFields(final Object from, final Object to,
   	                              final Field[] fields, final boolean accessible,
  -	                              final Map objMap, final Map metadataMap,
  -	                              PersistenceBroker broker)
  +	                              final Map objMap, final Map metadataMap)
   	{
   		for (int f = 0, fieldsLength = fields.length; f < fieldsLength; ++f)
   		{
  @@ -343,7 +300,7 @@
   			}
   			try
   			{
  -				cloneAndSetFieldValue(field, from, to, objMap, metadataMap, broker);
  +				cloneAndSetFieldValue(field, from, to, objMap, metadataMap);
   			}
   			catch (Exception e)
   			{
  @@ -352,9 +309,7 @@
   		}
   	}
   
  -	private static void cloneAndSetFieldValue(final Field field, final Object src, final Object
dest,
  -	                                          final Map objMap, final Map metadataMap, PersistenceBroker
broker)
  -	        throws IllegalAccessException
  +	private static void cloneAndSetFieldValue(final Field field, final Object src, final Object
dest, final Map objMap, final Map metadataMap) throws IllegalAccessException
   	{
   		Object value = field.get(src);
   		if (value == null)
  @@ -374,7 +329,7 @@
   				 * recursively call clone on value as it could be an object reference, an array,
   				 * or some mutable type
   				 */
  -				value = clone(value, objMap, metadataMap, broker);
  +				value = clone(value, objMap, metadataMap);
   			}
   			field.set(dest, value);
   		}
  
  
  
  1.5       +129 -50   db-ojb/src/test/org/apache/ojb/otm/CopyTest.java
  
  Index: CopyTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/otm/CopyTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CopyTest.java	9 Jul 2003 23:46:47 -0000	1.4
  +++ CopyTest.java	11 Jul 2003 17:21:42 -0000	1.5
  @@ -1,4 +1,5 @@
   package org.apache.ojb.otm;
  +
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  @@ -52,14 +53,17 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  +
   import junit.framework.TestCase;
  +import org.apache.ojb.broker.*;
  +import org.apache.ojb.odmg.TestClassA;
  +import org.apache.ojb.odmg.TestClassB;
   import org.apache.ojb.otm.copy.MetadataObjectCopyStrategy;
   import org.apache.ojb.otm.copy.ObjectCopyStrategy;
  -import org.apache.ojb.otm.copy.SerializeObjectCopyStrategy;
   import org.apache.ojb.otm.copy.ReflectiveObjectCopyStrategy;
  -import org.apache.ojb.odmg.TestClassA;
  -import org.apache.ojb.odmg.TestClassB;
  -import org.apache.ojb.broker.*;
  +import org.apache.ojb.otm.copy.SerializeObjectCopyStrategy;
  +import org.apache.ojb.otm.core.Transaction;
  +import org.apache.ojb.otm.lock.LockingException;
   
   /**
    * Created by IntelliJ IDEA.
  @@ -75,20 +79,29 @@
   	private ObjectCopyStrategy m_mdcs = new MetadataObjectCopyStrategy();
   	private ObjectCopyStrategy m_scs = new SerializeObjectCopyStrategy();
   	private ObjectCopyStrategy m_rcs = new ReflectiveObjectCopyStrategy();
  +	private TestKit _kit;
  +	private OTMConnection _conn;
  +	private Zoo m_zoo;
  +	private TestClassA m_tca;
  +	private BidirectionalAssociationObjectA m_baoa;
   
   	public CopyTest(String name)
   	{
   		super(name);
   	}
   
  -	public void setUp()
  +	public void setUp() throws LockingException
   	{
   		m_pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +		_kit = TestKit.getTestInstance();
  +		_conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
   	}
   
  -	public void tearDown()
  +	public void tearDown() throws LockingException
   	{
   		m_pb.close();
  +		_conn.close();
  +		_conn = null;
   	}
   
   	public static void main(String[] args)
  @@ -97,21 +110,21 @@
   		junit.textui.TestRunner.main(arr);
   	}
   
  -	public void testMetadataCopy()
  +	public void testMetadataCopy() throws LockingException
   	{
   		TestClassA tca = generateTestData();
   		TestClassB tcb = tca.getB();
   		internalTest(m_mdcs, tca, tcb);
   	}
   
  -	public void testSerializedCopy()
  +	public void testSerializedCopy() throws LockingException
   	{
   		TestClassA tca = generateTestData();
   		TestClassB tcb = tca.getB();
   		internalTest(m_scs, tca, tcb);
   	}
   
  -	public void testReflectiveCopy()
  +	public void testReflectiveCopy() throws LockingException
   	{
   		TestClassA tca = generateTestData();
   		TestClassB tcb = tca.getB();
  @@ -131,19 +144,19 @@
   		assertTrue(copy.getB().getValue1().equals("hi there"));
   	}
   
  -	public void testMetadataCopy2()
  +	public void testMetadataCopy2() throws LockingException
   	{
   		Zoo zoo = generateZoo();
   		internalTest2(m_mdcs, zoo);
   	}
   
  -	public void testSerializeCopy2()
  +	public void testSerializeCopy2() throws LockingException
   	{
   		Zoo zoo = generateZoo();
   		internalTest2(m_scs, zoo);
   	}
   
  -	public void testReflectiveCopy2()
  +	public void testReflectiveCopy2() throws LockingException
   	{
   		Zoo zoo = generateZoo();
   		internalTest2(m_rcs, zoo);
  @@ -152,23 +165,24 @@
   	/**
   	 * tests for recursion handling
   	 */
  -		public void testMetadataCopy3()
  +	public void testMetadataCopy3() throws LockingException
   	{
   		BidirectionalAssociationObjectA a = generateBidirectional();
   		internalTest3(m_mdcs, a);
   	}
   
  -	public void testSerializeCopy3()
  +	public void testSerializeCopy3() throws LockingException
   	{
   		BidirectionalAssociationObjectA a = generateBidirectional();
   		internalTest3(m_scs, a);
   	}
   
  -	public void testReflectiveCopy3()
  +	public void testReflectiveCopy3() throws LockingException
   	{
   		BidirectionalAssociationObjectA a = generateBidirectional();
   		internalTest3(m_rcs, a);
   	}
  +
   	private void internalTest3(ObjectCopyStrategy strategy, BidirectionalAssociationObjectA
a)
   	{
   		BidirectionalAssociationObjectA copy = (BidirectionalAssociationObjectA) strategy.copy(a,
m_pb);
  @@ -183,33 +197,78 @@
   		assertTrue(zoo != copy);
   		assertTrue(zoo.getAnimals().size() == copy.getAnimals().size());
   	}
  -	private BidirectionalAssociationObjectA generateBidirectional()
  +
  +	private BidirectionalAssociationObjectA generateBidirectional() throws LockingException
  +	{
  +		if (m_baoa != null)
  +		{
  +			return m_baoa;
  +		}
  +		else
  +		{
  +			Transaction tx = _kit.getTransaction(_conn);
  +			tx.begin();
  +			BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
  +			a.setPk("abc123");
  +			Identity oid = _conn.getIdentity(a);
  +			a = (BidirectionalAssociationObjectA) _conn.getObjectByIdentity(oid);
  +			if (a == null)
  +			{
  +				a = new BidirectionalAssociationObjectA();
  +				a.setPk("abc123");
  +				_conn.makePersistent(a);
  +				BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
  +				b.setPk("xyz987");
  +				_conn.makePersistent(b);
  +				a.setRelatedB(b);
  +				b.setRelatedA(a);
  +			}
  +			tx.commit();
  +			m_baoa = a;
  +			return m_baoa;
  +		}
  +	}
  +
  +
  +	private Zoo generateZoo() throws LockingException
   	{
  -		BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
  -		BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
  -		a.setPk("abc123");
  -		b.setPk("xyz987");
  -		a.setRelatedB(b);
  -		b.setRelatedA(a);
  -		return a;
  -	}
  -	private Zoo generateZoo()
  -	{
  -		Zoo zoo = new Zoo();
  -		Mammal mammal = new Mammal();
  -		mammal.setName("molly");
  -		mammal.setNumLegs(4);
  -		mammal.setAge(55);
  -		zoo.addAnimal(mammal);
  -		Reptile reptile = new Reptile();
  -		reptile.setColor("green");
  -		reptile.setName("hubert");
  -		reptile.setAge(51);
  -		zoo.addAnimal(reptile);
  -		return zoo;
  +		if (m_zoo != null)
  +		{
  +			return m_zoo;
  +		}
  +		else
  +		{
  +			Transaction tx = _kit.getTransaction(_conn);
  +			tx.begin();
  +			Zoo zoo = new Zoo();
  +			zoo.setZooId(1234);
  +			Identity oid = _conn.getIdentity(zoo);
  +			zoo = (Zoo) _conn.getObjectByIdentity(oid);
  +			if (zoo == null)
  +			{
  +				zoo = new Zoo();
  +				zoo.setZooId(1234);
  +				Mammal mammal = new Mammal();
  +				mammal.setName("molly");
  +				mammal.setNumLegs(4);
  +				mammal.setAge(55);
  +				_conn.makePersistent(mammal);
  +				zoo.addAnimal(mammal);
  +				Reptile reptile = new Reptile();
  +				reptile.setColor("green");
  +				reptile.setName("hubert");
  +				reptile.setAge(51);
  +				_conn.makePersistent(reptile);
  +				zoo.addAnimal(reptile);
  +				_conn.makePersistent(zoo);
  +			}
  +			tx.commit();
  +			m_zoo = zoo;
  +			return m_zoo;
  +		}
   	}
   
  -	public void testPerformance()
  +	public void testPerformance() throws LockingException
   	{
   		long start = System.currentTimeMillis();
   		for (int i = 0; i < ITERATIONS; i++)
  @@ -240,17 +299,37 @@
   		System.out.println("testReflectiveCopy took: " + (stop - start));
   	}
   
  -	private TestClassA generateTestData()
  +	private TestClassA generateTestData() throws LockingException
   	{
  -		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;
  +		if (m_tca != null)
  +		{
  +			return m_tca;
  +		}
  +		else
  +		{
  +			Transaction tx = _kit.getTransaction(_conn);
  +			tx.begin();
  +			TestClassA tca = new TestClassA();
  +			tca.setOid("someoid");
  +			Identity oid = _conn.getIdentity(tca);
  +			tca = (TestClassA) _conn.getObjectByIdentity(oid);
  +			if (tca == null)
  +			{
  +				tca = new TestClassA();
  +				tca.setOid("someoid");
  +				tca.setValue1("abc");
  +				tca.setValue2("123");
  +				tca.setValue3(5);
  +				_conn.makePersistent(tca);
  +				TestClassB tcb = new TestClassB();
  +				tcb.setOid("boid");
  +				tcb.setValue1("hi there");
  +				_conn.makePersistent(tcb);
  +				tca.setB(tcb);
  +			}
  +			tx.commit();
  +			m_tca = tca;
  +			return m_tca;
  +		}
   	}
   }
  
  
  

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


Mime
View raw message