db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb repository_junit.xml
Date Sat, 27 Nov 2004 00:11:03 GMT
arminw      2004/11/26 16:11:02

  Modified:    src/test/org/apache/ojb/junit OJBTestCase.java
               src/test/org/apache/ojb repository_junit.xml
  Log:
  add new helper method to change auto-xxx setting for test case with auto-restore of the
old settings at the end of the test.
  add missing 'autoincrement'
  
  Revision  Changes    Path
  1.9       +153 -1    db-ojb/src/test/org/apache/ojb/junit/OJBTestCase.java
  
  Index: OJBTestCase.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/junit/OJBTestCase.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- OJBTestCase.java	14 Nov 2004 09:42:55 -0000	1.8
  +++ OJBTestCase.java	27 Nov 2004 00:11:02 -0000	1.9
  @@ -15,9 +15,18 @@
    * limitations under the License.
    */
   
  +import java.util.Map;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +
   import junit.framework.TestCase;
   import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.OJBRuntimeException;
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  +import org.apache.commons.lang.SerializationUtils;
   
   /**
    * Extension of the JUnit test class.
  @@ -29,6 +38,7 @@
   {
       private static final String SKIP_STR = "OJB.skip.issues";
       private static final String SKIP_DEFAULT_VALUE = "false";
  +    private MetadataHelper referenceHelper;
   
       /** The OJB main class instance */
       protected OJB ojb;
  @@ -54,6 +64,21 @@
   
       protected void tearDown() throws Exception
       {
  +        if(referenceHelper != null)
  +        {
  +            PersistenceBroker temp = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            try
  +            {
  +                referenceHelper.restoreMetadataSettings(temp);
  +            }
  +            finally
  +            {
  +                if(temp != null)
  +                {
  +                    temp.close();
  +                }
  +            }
  +        }
           ojb.releaseAllConnections();
           super.tearDown();
           System.gc();
  @@ -114,5 +139,132 @@
               System.out.println("# [Skip test in " + className + "] " + message + " #");
           }
           return skip;
  +    }
  +
  +    /**
  +     * Allows to do a global change of object/collection reference settings. When the test
  +     * is tear down the old settings will be restored. Be careful when override setUp/tearDown
method, don't
  +     * forget the "super call", else this method couldn't work properly.
  +     *
  +     * @param clazz
  +     * @param referenceField
  +     * @param autoRetrieve
  +     * @param autoUpdate
  +     * @param autoDelete
  +     * @param useProxy
  +     */
  +    public void ojbChangeReferenceSetting(Class clazz, String referenceField, boolean autoRetrieve,
int autoUpdate, int autoDelete, boolean useProxy)
  +    {
  +        if(referenceHelper == null)
  +        {
  +            referenceHelper = new MetadataHelper();
  +        }
  +        PersistenceBroker temp = PersistenceBrokerFactory.defaultPersistenceBroker();
  +        try
  +        {
  +            referenceHelper.changeReferenceSetting(temp, clazz, referenceField, autoRetrieve,
autoUpdate, autoDelete, useProxy);
  +        }
  +        finally
  +        {
  +            if(temp != null)
  +            {
  +                temp.close();
  +            }
  +        }
  +    }
  +
  +
  +
  +    //================================================================
  +    // inner class
  +    //================================================================
  +    /**
  +     * Class that help us to do changes on metadata and restore old state on
  +     * tear down of the test.
  +     * NOTE: This strategy is not recommended in production application because
  +     * the made changes will be global and all threads will recognize them immediately.
  +     *
  +     */
  +    public class MetadataHelper
  +    {
  +        private Map oldSettings;
  +
  +        public MetadataHelper()
  +        {
  +            oldSettings = new HashMap();
  +        }
  +
  +        protected void restoreMetadataSettings(PersistenceBroker broker)
  +        {
  +            if(oldSettings.size() == 0) return;
  +            Iterator it = oldSettings.entrySet().iterator();
  +            Map.Entry entry;
  +            while(it.hasNext())
  +            {
  +                entry =  (Map.Entry) it.next();
  +                String clazz =  (String) entry.getKey();
  +                Map fieldMap = (Map) entry.getValue();
  +                Iterator iter = fieldMap.entrySet().iterator();
  +                Map.Entry entry2;
  +                ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(clazz);
  +                while(iter.hasNext())
  +                {
  +                    entry2 = (Map.Entry) iter.next();
  +                    String oldRefName = (String) entry2.getKey();
  +                    ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) entry2.getValue();
  +                    // lookup single object or collection descriptor
  +                    ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(oldRefName);
  +                    if(ref == null) ref = cld.getObjectReferenceDescriptorByName(oldRefName);
  +
  +//                    System.out.println("Restoring metadata for " + clazz
  +//                            + " from " + ref.toXML()
  +//                            + " === to ===> " + oldRef.toXML());
  +                    ref.setCascadeRetrieve(oldRef.getCascadeRetrieve());
  +                    ref.setCascadingStore(oldRef.getCascadingStore());
  +                    ref.setCascadingDelete(oldRef.getCascadingDelete());
  +                    ref.setLazy(oldRef.isLazy());
  +//                    System.out.println("Restore metadata for " + clazz
  +//                            + " to " + ref.toXML());
  +                }
  +            }
  +            oldSettings.clear();
  +        }
  +
  +        public void changeReferenceSetting(PersistenceBroker broker, Class clazz,
  +                                           String referenceField, boolean autoRetrieve,
  +                                           int autoUpdate, int autoDelete, boolean useProxy)
  +        {
  +            ClassDescriptor cld = broker.getClassDescriptor(clazz);
  +            ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
  +            if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
  +            if(ref == null)
  +            {
  +                throw new OJBRuntimeException("Given field " + referenceField + " does
not match a reference in " + clazz);
  +            }
  +
  +            HashMap fieldMap = (HashMap) oldSettings.get(cld.getClassNameOfObject());
  +            if(fieldMap == null)
  +            {
  +                fieldMap = new HashMap();
  +                oldSettings.put(cld.getClassNameOfObject(), fieldMap);
  +            }
  +
  +            ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) fieldMap.get(ref.getPersistentField().getName());
  +            // if we don't find old settings buffer it
  +            if(oldRef == null)
  +            {
  +                // buffer deep copy of old settings
  +                oldRef = (ObjectReferenceDescriptor) SerializationUtils.clone(ref);
  +                fieldMap.put(ref.getPersistentField().getName(), oldRef);
  +            }
  +
  +            ref.setLazy(useProxy);
  +            ref.setCascadeRetrieve(autoRetrieve);
  +            ref.setCascadingStore(autoUpdate);
  +            ref.setCascadingDelete(autoDelete);
  +
  +//            System.out.println("old settings: " + oldRef.toXML());
  +//            System.out.println("new settings: " + ref.toXML());
  +        }
       }
   }
  
  
  
  1.127     +2 -2      db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.126
  retrieving revision 1.127
  diff -u -r1.126 -r1.127
  --- repository_junit.xml	25 Nov 2004 20:30:24 -0000	1.126
  +++ repository_junit.xml	27 Nov 2004 00:11:02 -0000	1.127
  @@ -3398,7 +3398,7 @@
   	</class-descriptor>
   
   	<class-descriptor class="org.apache.ojb.broker.News" table="NEWS">
  -	   <field-descriptor name="id" column="ID" jdbc-type="INTEGER" primarykey="true"/>
  +	   <field-descriptor name="id" column="ID" jdbc-type="INTEGER" primarykey="true" autoincrement="true"/>
   	   <field-descriptor name="headline" column="HEADLINE" jdbc-type="VARCHAR"/>
       	   <collection-descriptor 	name="qualifiers"
              				element-class-ref="org.apache.ojb.broker.Qualifier"
  
  
  

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