mkalen      2005/02/23 13:51:34

  Modified:    src/schema Tag: OJB_1_0_RELEASE ojbtest-data.xml
               src/test/org/apache/ojb/broker/metadata Tag: OJB_1_0_RELEASE
                        MetadataMultithreadedTest.java
  Log:
  Add new test method to provoke bug when materialising dynamic proxy created 
with pre-thread metadata changes activated. See thread at dev-list 
http://mail-archives.apache.org/eyebrowse/[EMAIL PROTECTED]&msgNo=9143
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.15.2.3  +4 -0      db-ojb/src/schema/ojbtest-data.xml
  
  Index: ojbtest-data.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/schema/ojbtest-data.xml,v
  retrieving revision 1.15.2.2
  retrieving revision 1.15.2.3
  diff -u -r1.15.2.2 -r1.15.2.3
  --- ojbtest-data.xml  15 Dec 2004 12:39:23 -0000      1.15.2.2
  +++ ojbtest-data.xml  23 Feb 2005 21:51:32 -0000      1.15.2.3
  @@ -25,6 +25,7 @@
       <ProductGroup groupId="6" groupName="bread" description=""/>
       <ProductGroup groupId="7" groupName="Fruits" description="Fr�chte etc."/>
       <ProductGroup groupId="8" groupName="Dairy Products" description="Kat 
8"/>
  +    <ProductGroup groupId="4711" groupName="Random Articles" 
description="Used for MetadataMultithreadedTest"/>
   
       <Article articleId="1" articleName="Chai" supplierId="1" 
productGroupId="1" unit="10 boxes x 20 bags" price="18.53" stock="39" 
orderedUnits="0" minimumStock="10" inSelloutArticle="0"/>
       <Article articleId="2" articleName="Chang" supplierId="1" 
productGroupId="1" unit="24 - 12 oz bottles" price="19.89" stock="17" 
orderedUnits="40" minimumStock="25" inSelloutArticle="0"/>
  @@ -103,6 +104,9 @@
       <Article articleId="75" articleName="Rh�nbr�u Klosterbier" 
supplierId="12" productGroupId="1" unit="24 - 0.5 l bottles" price="7.75" 
stock="125" orderedUnits="0" minimumStock="25" inSelloutArticle="0"/>
       <Article articleId="76" articleName="Lakkalik��ri" supplierId="23" 
productGroupId="1" unit="500 ml" price="18.0" stock="57" orderedUnits="0" 
minimumStock="20" inSelloutArticle="0"/>
       <Article articleId="77" articleName="Original Frankfurter gr�ne So�e" 
supplierId="12" productGroupId="2" unit="12 boxes" price="13.0" stock="32" 
orderedUnits="0" minimumStock="15" inSelloutArticle="0"/>
  +    <Article articleId="4711" articleName="Proxied Article 1" 
supplierId="12" productGroupId="4711" unit="1 Java class" price="1.3" stock="5" 
orderedUnits="0" minimumStock="1" inSelloutArticle="0"/>
  +    <Article articleId="4712" articleName="Proxied Article 2" 
supplierId="12" productGroupId="4711" unit="1 Java class" price="1.5" stock="2" 
orderedUnits="0" minimumStock="0" inSelloutArticle="0"/>
  +    <Article articleId="4713" articleName="Proxied Article 3" 
supplierId="12" productGroupId="4711" unit="1 Java class" price="1.9" stock="7" 
orderedUnits="0" minimumStock="5" inSelloutArticle="0"/>
   
       <Books ArtikelNr="100" Artikelname="Hamlet" LieferantenNr="12" 
KategorieNr="5" Liefereinheit="12 boxes" Einzelpreis="34.0" Lagerbestand="32" 
Bestellteeinheiten="0" Mindestbestand="15" Auslaufartikel="0" Isbn="1234567890" 
Author="W. Shakespeare"/>
       <Books ArtikelNr="101" Artikelname="Faust" LieferantenNr="12" 
KategorieNr="5" Liefereinheit="12 boxes" Einzelpreis="56.0" Lagerbestand="32" 
Bestellteeinheiten="0" Mindestbestand="15" Auslaufartikel="0" Isbn="1234567890" 
Author="J.W. v. Goethe"/>
  
  
  
  No                   revision
  No                   revision
  1.7.2.2   +129 -22   
db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java
  
  Index: MetadataMultithreadedTest.java
  ===================================================================
  RCS file: 
/home/cvs/db-ojb/src/test/org/apache/ojb/broker/metadata/MetadataMultithreadedTest.java,v
  retrieving revision 1.7.2.1
  retrieving revision 1.7.2.2
  diff -u -r1.7.2.1 -r1.7.2.2
  --- MetadataMultithreadedTest.java    15 Feb 2005 19:17:49 -0000      1.7.2.1
  +++ MetadataMultithreadedTest.java    23 Feb 2005 21:51:34 -0000      1.7.2.2
  @@ -2,18 +2,19 @@
   
   import java.util.ArrayList;
   import java.util.List;
  +import java.util.Collection;
  +import java.util.Iterator;
   
   import org.apache.commons.lang.ClassUtils;
  -import org.apache.ojb.broker.OJBRuntimeException;
  -import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  -import org.apache.ojb.broker.Person;
  -import org.apache.ojb.broker.Project;
  -import org.apache.ojb.broker.Role;
  +import org.apache.ojb.broker.*;
  +import org.apache.ojb.broker.accesslayer.OJBIterator;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.broker.sequence.Repository;
   import org.apache.ojb.broker.util.ClassHelper;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.junit.JUnitExtensions;
   
   /**
  @@ -35,6 +36,8 @@
       private String oldTestObjectString;
       DescriptorRepository defaultRepository;
   
  +    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  +
       public MetadataMultithreadedTest(String s)
       {
           super(s);
  @@ -57,6 +60,10 @@
       protected void setUp() throws Exception
       {
           super.setUp();
  +        MetadataManager mm = MetadataManager.getInstance();
  +        // enable the per thread changes of metadata
  +        mm.setEnablePerThreadChanges(true);
  +        defaultRepository = mm.copyOfGlobalRepository();
       }
   
       protected void tearDown() throws Exception
  @@ -70,17 +77,76 @@
           return oldTestObjectString;
       }
   
  -    public void testRuntimeMetadataChanges() throws Exception
  +    public void testProxiedLoading() throws Exception
       {
           PersistenceBroker broker = null;
           try
           {
               MetadataManager mm = MetadataManager.getInstance();
  -            // enable the per thread changes of metadata
  -            mm.setEnablePerThreadChanges(true);
  -            defaultRepository = mm.copyOfGlobalRepository();
  +            // Store the current repository mappings under a profile key
  +            DescriptorRepository repository = mm.getRepository();
  +            String profileKey = "TestMappings";
  +            mm.addProfile(profileKey, repository);
  +
  +            // "Destroy" this thread's mappings
               mm.setDescriptor(defaultRepository);
   
  +            ProductGroupWithCollectionProxy pgTemplate = new 
ProductGroupWithCollectionProxy();
  +            pgTemplate.setGroupId(new Integer(6));
  +            Query query = QueryFactory.newQueryByExample(pgTemplate);
  +
  +            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            Collection groups;
  +            Iterator groupIter;
  +            ProductGroupWithCollectionProxy pg;
  +
  +            assertNotNull(groupIter = (OJBIterator) 
broker.getIteratorByQuery(query));
  +            assertTrue(groupIter.hasNext());
  +
  +            // We have not named any OJB profiles, so using dynamic proxies 
at this stage is not
  +            // supported
  +            Throwable expectedThrowable = null;
  +            try {
  +                System.err.println("------ The following exception is part 
of the tests...");
  +                groupIter.next();
  +            } catch (Throwable t) {
  +                expectedThrowable = t;
  +                System.err.println("------");
  +            }
  +            assertNotNull("Should get metadata exception from proxy", 
expectedThrowable);
  +            ((OJBIterator) groupIter).releaseDbResources();
  +
  +            // Load the repository profile and re-try loading.
  +            broker.clearCache();
  +            mm.loadProfile(profileKey);
  +            assertNotNull(groups = broker.getCollectionByQuery(query));
  +            assertEquals(1, groups.size());
  +            assertNotNull(groupIter = groups.iterator());
  +            assertTrue(groupIter.hasNext());
  +            assertNotNull(pg = (ProductGroupWithCollectionProxy) 
groupIter.next());
  +            assertFalse(groupIter.hasNext());
  +            assertEquals(pgTemplate.getGroupId(), pg.getGroupId());
  +            Collection articles;
  +            assertNotNull(articles = pg.getAllArticlesInGroup());
  +            assertEquals(6, articles.size());
  +
  +            TestCaseRunnable tct [] = new TestCaseRunnable[]{new 
LazyLoading(articles)};
  +            runTestCaseRunnables(tct);
  +        }
  +        finally
  +        {
  +            if (broker != null) broker.close();
  +        }
  +
  +    }
  +
  +    public void testRuntimeMetadataChanges() throws Exception
  +    {
  +        PersistenceBroker broker = null;
  +        try
  +        {
  +            MetadataManager.getInstance().setDescriptor(defaultRepository);
  +
               ClassDescriptor cld;
               long memoryUseBeforeTest;
               long memoryUseAfterTest;
  @@ -119,15 +185,21 @@
                       // run test classes
                       runTestCaseRunnables(tct);
                       ++count;
  -                    System.out.println("Free/total Memory after loop " + 
count + ":          "
  -                            + convertToMB(Runtime.getRuntime().freeMemory())
  -                            + "/" + convertToMB(getTotalMemory()) + "MB");
  +                    if (logger.isDebugEnabled())
  +                    {
  +                        logger.debug("Free/total Memory after loop " + count 
+ ":          "
  +                                     + 
convertToMB(Runtime.getRuntime().freeMemory())
  +                                     + "/" + convertToMB(getTotalMemory()) + 
"MB");
  +                    }
                   }
                   period = System.currentTimeMillis() - period;
  -                
System.out.println(ClassUtils.getShortClassName(MetadataMultithreadedTest.class)
 + " take: "
  -                        + period + " ms for " + loops + " loops, creating 
each with " + threads + " threads");
  -                System.out.println("Free/total Memory before test:          "
  -                        + convertToMB(memory) + "/" + 
convertToMB(totalMemory) + "MB");
  +                if (logger.isDebugEnabled())
  +                {
  +                    
logger.debug(ClassUtils.getShortClassName(MetadataMultithreadedTest.class) + " 
take: "
  +                                 + period + " ms for " + loops + " loops, 
creating each with " + threads + " threads");
  +                    logger.debug("Free/total Memory before test:          "
  +                                 + convertToMB(memory) + "/" + 
convertToMB(totalMemory) + "MB");
  +                }
                   Runtime.getRuntime().gc();
                   Thread.sleep(200);
                   Runtime.getRuntime().gc();
  @@ -135,10 +207,13 @@
   
                   memoryUseBeforeTest = convertToMB(memory);
                   memoryUseAfterTest = 
convertToMB(Runtime.getRuntime().freeMemory());
  -                System.out.println("Free/total Memory after test and gc:   "
  -                        + memoryUseAfterTest
  -                        + "/" + convertToMB(getTotalMemory()) + "MB");
  -                System.out.println("Do cleanup now ...");
  +                if (logger.isDebugEnabled())
  +                {
  +                    logger.debug("Free/total Memory after test and gc:   "
  +                                 + memoryUseAfterTest
  +                                 + "/" + convertToMB(getTotalMemory()) + 
"MB");
  +                    logger.debug("Do cleanup now ...");
  +                }
               }
               finally
               {
  @@ -376,4 +451,36 @@
               }
           }
       }
  +
  +    /**
  +     * Inner test class for lazy materialization of CollectionProxy in 
different thread.
  +     */
  +    protected class LazyLoading extends 
JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
  +    {
  +        private Collection articles;
  +
  +        public LazyLoading(Collection articles)
  +        {
  +            assertNotNull(this.articles = articles);
  +        }
  +
  +        public void runTestCase() throws Throwable
  +        {
  +            // Explicitly clear descriptor repository in this thread 
(similar to loading
  +            // profile with unrelated class-mappings).
  +            DescriptorRepository dr = new DescriptorRepository();
  +            MetadataManager.getInstance().setDescriptor(dr);
  +            Article article;
  +            int numArticles = 0;
  +            for (Iterator iterator = articles.iterator(); 
iterator.hasNext();)
  +            {
  +                assertNotNull(article = (Article) iterator.next());
  +                assertNotNull(article.getArticleId());
  +                assertFalse(new Integer(0).equals(article.getArticleId()));
  +                numArticles++;
  +            }
  +            assertEquals(6, numArticles);
  +        }
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to