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 Wed, 09 Jul 2003 23:46:47 GMT
mattbaird    2003/07/09 16:46:47

  Modified:    src/test/org/apache/ojb/odmg TestClassA.java
               src/java/org/apache/ojb/otm/copy
                        ReflectiveObjectCopyStrategy.java
               src/test/org/apache/ojb/otm CopyTest.java
  Log:
  new test cases to show & solve problems with no args objects
  
  Revision  Changes    Path
  1.5       +12 -2     db-ojb/src/test/org/apache/ojb/odmg/TestClassA.java
  
  Index: TestClassA.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/TestClassA.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestClassA.java	8 Nov 2002 14:01:27 -0000	1.4
  +++ TestClassA.java	9 Jul 2003 23:46:47 -0000	1.5
  @@ -1,15 +1,16 @@
   package org.apache.ojb.odmg;
   
   import java.io.Serializable;
  +import java.util.Locale;
   
   public class TestClassA implements Serializable
   {
   	private String oid;
   	private String value1;
   	private String value2;
  -
   	private int 	value3;
  -	
  +	private Locale locale = Locale.GERMANY;
  +
   	private String 	boid;
   	private TestClassB b;
   	/**
  @@ -120,4 +121,13 @@
   		this.boid = boid;
   	}
   
  +	public Locale getLocale()
  +	{
  +		return locale;
  +	}
  +
  +	public void setLocale(Locale locale)
  +	{
  +		this.locale = locale;
  +	}
   }
  
  
  
  1.7       +47 -37    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ReflectiveObjectCopyStrategy.java	9 Jul 2003 00:14:39 -0000	1.6
  +++ ReflectiveObjectCopyStrategy.java	9 Jul 2003 23:46:47 -0000	1.7
  @@ -111,7 +111,7 @@
   	 */
   	public final Object copy(final Object toCopy, final PersistenceBroker broker)
   	{
  -		return internalClone(toCopy, new IdentityHashMap(), new HashMap(), broker);
  +		return clone(toCopy, new IdentityHashMap(), new HashMap(), broker);
   	}
   
   	private static final Object clone(Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
  @@ -128,6 +128,7 @@
   			if (broker.getDescriptorRepository().hasDescriptorFor(toCopy.getClass()))
   			{
   				Identity oid = new Identity(toCopy, broker);
  +
   				Object temp = broker.serviceObjectCache().lookup(oid);
   				if (temp == null)
   				{
  @@ -145,6 +146,7 @@
   		}
   		return internalClone(toCopy, objMap, metadataMap, broker);
   	}
  +
   	/*
   	 * class used to cache class metadata info
   	 */
  @@ -154,6 +156,7 @@
   		Field[] m_declaredFields;
   		boolean m_noArgConstructorAccessible;
   		boolean m_fieldsAccessible;
  +		boolean m_hasNoArgConstructor = true;
   	}
   
   	private static Object internalClone(final Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
  @@ -184,53 +187,60 @@
   	                                                        final Map objMap, final Object
toCopy,
   	                                                        PersistenceBroker broker)
   	{
  -		final Object retval;
  +		Object retval = null;
   		ClassMetadata metadata = (ClassMetadata) metadataMap.get(objClass);
   		if (metadata == null)
   		{
   			metadata = new ClassMetadata();
   			metadataMap.put(objClass, metadata);
   		}
  -		Constructor noArg = metadata.m_noArgConstructor;
  -		if (noArg == null)
  -		{
  -			try
  -			{
  -				noArg = objClass.getDeclaredConstructor(EMPTY_CLASS_ARRAY);
  -				metadata.m_noArgConstructor = noArg;
  -			}
  -			catch (Exception e)
  -			{
  -				throw new ObjectCopyException("class [" + objClass.getName() + "] has no noArg constructor:
" + e.toString(), e);
  -			}
  -		}
  -		if (!metadata.m_noArgConstructorAccessible && (Modifier.PUBLIC & noArg.getModifiers())
== 0)
  +		if (metadata.m_hasNoArgConstructor)
   		{
  -			try
  +			Constructor noArg = metadata.m_noArgConstructor;
  +			if (noArg == null)
   			{
  -				noArg.setAccessible(true);
  +				try
  +				{
  +					noArg = objClass.getDeclaredConstructor(EMPTY_CLASS_ARRAY);
  +					metadata.m_noArgConstructor = noArg;
  +				}
  +				catch (Exception e)
  +				{
  +					metadata.m_hasNoArgConstructor = false;
  +					// throw new ObjectCopyException("class [" + objClass.getName() + "] has no noArg
constructor: " + e.toString(), e);
  +				}
   			}
  -			catch (SecurityException e)
  +			if (metadata.m_hasNoArgConstructor)
   			{
  -				throw new ObjectCopyException("cannot access noArg constructor [" + noArg + "] of class
[" + objClass.getName() + "]: " + e.toString(), e);
  +				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);
  +				}
  +				for (Class c = objClass; c != Object.class; c = c.getSuperclass())
  +				{
  +					copyClass(metadataMap, c, toCopy, retval, objMap, broker);
  +				}
   			}
  -			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, broker);
   		}
   		return retval;
   	}
  
  
  
  1.4       +38 -1     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CopyTest.java	8 Jul 2003 23:56:29 -0000	1.3
  +++ CopyTest.java	9 Jul 2003 23:46:47 -0000	1.4
  @@ -149,13 +149,50 @@
   		internalTest2(m_rcs, zoo);
   	}
   
  +	/**
  +	 * tests for recursion handling
  +	 */
  +		public void testMetadataCopy3()
  +	{
  +		BidirectionalAssociationObjectA a = generateBidirectional();
  +		internalTest3(m_mdcs, a);
  +	}
  +
  +	public void testSerializeCopy3()
  +	{
  +		BidirectionalAssociationObjectA a = generateBidirectional();
  +		internalTest3(m_scs, a);
  +	}
  +
  +	public void testReflectiveCopy3()
  +	{
  +		BidirectionalAssociationObjectA a = generateBidirectional();
  +		internalTest3(m_rcs, a);
  +	}
  +	private void internalTest3(ObjectCopyStrategy strategy, BidirectionalAssociationObjectA
a)
  +	{
  +		BidirectionalAssociationObjectA copy = (BidirectionalAssociationObjectA) strategy.copy(a,
m_pb);
  +		assertTrue(a != copy);
  +		assertTrue(copy.getPk().equals("abc123"));
  +		assertTrue(copy.getRelatedB().getPk().equals("xyz987"));
  +	}
  +
   	private void internalTest2(ObjectCopyStrategy strategy, Zoo zoo)
   	{
   		Zoo copy = (Zoo) strategy.copy(zoo, m_pb);
   		assertTrue(zoo != copy);
   		assertTrue(zoo.getAnimals().size() == copy.getAnimals().size());
   	}
  -
  +	private BidirectionalAssociationObjectA generateBidirectional()
  +	{
  +		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();
  
  
  

---------------------------------------------------------------------
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