You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/02/05 18:06:10 UTC

cvs commit: jakarta-jetspeed-2/portal/src/test/org/apache/jetspeed/services/registry TestRegistry.java

weaver      2004/02/05 09:06:10

  Modified:    plugin/src/java/org/apache/jetspeed/persistence/impl
                        OTMOJBPersistencePlugin.java
               portal/src/test/org/apache/jetspeed/services/registry
                        TestRegistry.java
  Log:
  Test case transactions fixed.
  
  OTM plugin starting to work, still some issues, DO NOT USE.
  
  Revision  Changes    Path
  1.2       +78 -29    jakarta-jetspeed-2/plugin/src/java/org/apache/jetspeed/persistence/impl/OTMOJBPersistencePlugin.java
  
  Index: OTMOJBPersistencePlugin.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/plugin/src/java/org/apache/jetspeed/persistence/impl/OTMOJBPersistencePlugin.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- OTMOJBPersistencePlugin.java	4 Feb 2004 21:03:15 -0000	1.1
  +++ OTMOJBPersistencePlugin.java	5 Feb 2004 17:06:10 -0000	1.2
  @@ -6,18 +6,20 @@
    */
   package org.apache.jetspeed.persistence.impl;
   
  -import org.apache.fulcrum.InitializationException;
  +import java.util.Collection;
   
  +import org.apache.fulcrum.InitializationException;
   import org.apache.jetspeed.persistence.PersistencePlugin;
   import org.apache.jetspeed.persistence.TransactionStateException;
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PBKey;
  -import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.otm.OTMConnection;
  -import org.apache.ojb.otm.core.Transaction;
  -import org.apache.ojb.otm.core.TransactionException;
   import org.apache.ojb.otm.kit.SimpleKit;
  +import org.apache.ojb.otm.lock.LockType;
   import org.apache.ojb.otm.lock.LockingException;
   import org.apache.ojb.otm.states.State;
   
  @@ -61,9 +63,8 @@
           }
           finally
           {
  -        	close();
  +            close();
           }
  -      
   
       }
   
  @@ -85,7 +86,7 @@
                   "Unable to lock object " + obj.getClass().toString() + " to invalidate." + e.toString(),
                   e);
           }
  -        return obj;		
  +        return obj;
       }
   
       /**
  @@ -93,14 +94,14 @@
        */
       public void prepareForDelete(Object obj) throws TransactionStateException
       {
  -		OTMConnection conn = getOTMConnection();
  +        OTMConnection conn = getOTMConnection();
           if (!kit.getTransaction(conn).isInProgress())
           {
               throw new TransactionStateException("Transaction is not progress, cannot delete.");
           }
           try
           {
  -			conn.deletePersistent(obj);
  +            conn.deletePersistent(obj);
           }
           catch (LockingException e)
           {
  @@ -140,19 +141,18 @@
        */
       public void rollbackTransaction() throws TransactionStateException
       {
  -       try
  +        try
  +        {
  +            if (!kit.getTransaction(getOTMConnection()).isInProgress())
  +            {
  +                throw new TransactionStateException("Transaction is not progress, cannot rollback.");
  +            }
  +            kit.getTransaction(getOTMConnection()).rollback();
  +        }
  +        finally
           {
  -             if (!kit.getTransaction(getOTMConnection()).isInProgress())
  -                {
  -                    throw new TransactionStateException("Transaction is not progress, cannot rollback.");
  -                }
  -                kit.getTransaction(getOTMConnection()).rollback();
  -        }
  -       finally
  -       {
  -       		close();
  -       }
  -     
  +            close();
  +        }
   
       }
   
  @@ -180,7 +180,7 @@
               conn = kit.acquireConnection(pbKey);
               TLconn.set(conn);
           }
  -        return conn;		
  +        return conn;
       }
   
       /**
  @@ -192,11 +192,11 @@
   
           super.finalize();
       }
  -	
  -	/**
  -	 * Makes sure the OTM Connection has been closed properly
  -	 *
  -	 */
  +
  +    /**
  +     * Makes sure the OTM Connection has been closed properly
  +     *
  +     */
       protected void close()
       {
           OTMConnection conn = (OTMConnection) TLconn.get();
  @@ -219,9 +219,58 @@
           }
           catch (LockingException e)
           {
  -           throw new TransactionStateException("Unable to lock "+obj.getClass().getName());
  +            throw new TransactionStateException("Unable to lock " + obj.getClass().getName());
  +        }
  +
  +    }
  +
  +    /**
  +     * @see org.apache.jetspeed.persistence.PersistencePlugin#clearCache()
  +     */
  +    public void clearCache()
  +    {
  +
  +        try
  +        {
  +            getOTMConnection().invalidateAll();
  +        }
  +        catch (LockingException e)
  +        {
  +            throw new IllegalStateException("Some objects were locked while trying to clear the cache: " + e.toString());
           }
  +    }
   
  +    /**
  +     * @see org.apache.jetspeed.persistence.PersistencePlugin#getCollectionByQuery(java.lang.Class, java.lang.Object)
  +     */
  +    public Collection getCollectionByQuery(Class clazz, Object query)
  +    {
  +        Query useQuery = null;
  +        if (query instanceof Criteria)
  +        {
  +            useQuery = QueryFactory.newQuery(clazz, (Criteria) query);
  +        }
  +        else
  +        {
  +            useQuery = (Query) query;
  +        }
  +        return getOTMConnection().getCollectionByQuery(useQuery);
  +    }
  +
  +    /**
  +     * @see org.apache.jetspeed.persistence.PersistencePlugin#getObjectByQuery(java.lang.Class, java.lang.Object)
  +     */
  +    public Object getObjectByQuery(Class clazz, Object query)
  +    {
  +        Collection c = getCollectionByQuery(clazz, query);
  +        if (c.size() > 0)
  +        {
  +            return c.iterator().next();
  +        }
  +        else
  +        {
  +            return null;
  +        }
       }
   
   }
  
  
  
  1.9       +67 -59    jakarta-jetspeed-2/portal/src/test/org/apache/jetspeed/services/registry/TestRegistry.java
  
  Index: TestRegistry.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/portal/src/test/org/apache/jetspeed/services/registry/TestRegistry.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestRegistry.java	4 Feb 2004 21:35:25 -0000	1.8
  +++ TestRegistry.java	5 Feb 2004 17:06:10 -0000	1.9
  @@ -201,6 +201,7 @@
               portlet0.setName(PORTLET_0_NAME);
               pac.addPortletDefinition(portlet0);
   
  +            plugin.makePersistent(pac);
               //JetspeedPortletRegistry.registerPortletApplication(pac);
               JetspeedPortletRegistry.commitTransaction();
               //            plugin.invalidateObject(pac);
  @@ -223,46 +224,61 @@
           }
       }
   
  -    public void testAddApplication()
  +    public void testAddApplication() throws Throwable 
       {
           // JetspeedPortletRegistry.clearCache();
           // test that portlet application exists
  -        MutablePortletApplication appExists = JetspeedPortletRegistry.getPortletApplication(APP_1_NAME);
  -        assertNotNull(appExists);
  +        // plugin.clearCache();
  +        try
  +        {
  +            JetspeedPortletRegistry.beginTransaction();			
  +            MutablePortletApplication appExists = JetspeedPortletRegistry.getPortletApplication(APP_1_NAME);
  +            assertNotNull(appExists);
   
  -        // test that the web app exists
  -        WebApplicationDefinition wad = appExists.getWebApplicationDefinition();
  -        assertNotNull(wad);
  +            // test that the web app exists
  +            WebApplicationDefinition wad = appExists.getWebApplicationDefinition();
  +            assertNotNull(wad);
   
  -        assertNotNull(wad.getDescription(Jetspeed.getDefaultLocale()));
  -        assertNotNull(wad.getDisplayName(Jetspeed.getDefaultLocale()));
  +            assertNotNull(wad.getDescription(Jetspeed.getDefaultLocale()));
  +            assertNotNull(wad.getDisplayName(Jetspeed.getDefaultLocale()));
   
  -        PortletDefinition checkPd = appExists.getPortletDefinitionByName(PORTLET_0_NAME);
  +            PortletDefinition checkPd = appExists.getPortletDefinitionByName(PORTLET_0_NAME);
   
  -        assertNotNull(appExists.getName() + " did not have a portlet named \"" + PORTLET_0_NAME + "\"", checkPd);
  +            assertNotNull(appExists.getName() + " did not have a portlet named \"" + PORTLET_0_NAME + "\"", checkPd);
   
  -        String checkName = checkPd.getName();
  +            String checkName = checkPd.getName();
   
  -        checkPd = null;
  +            checkPd = null;
   
  -        // JetspeedPortletRegistry.clearCache();
  +            // JetspeedPortletRegistry.clearCache();
   
  -        PortletDefinitionComposite pdc0 =
  -            (PortletDefinitionComposite) JetspeedPortletRegistry.getPortletDefinitionByIndetifier(PORTLET_0_UID);
  +            PortletDefinitionComposite pdc0 =
  +                (PortletDefinitionComposite) JetspeedPortletRegistry.getPortletDefinitionByIndetifier(PORTLET_0_UID);
   
  -        PortletDefinitionComposite pdc2 =
  -            (PortletDefinitionComposite) JetspeedPortletRegistry.getPortletDefinitionByUniqueName(
  -                APP_1_NAME + "::" + PORTLET_0_NAME);
  -
  -        assertNotNull("Could not locate PortletDefinition with unique name \"" + APP_1_NAME + "::" + PORTLET_0_UID + "\"", pdc2);
  -
  -        assertNotNull(pdc0);
  -        assertEquals(checkName, pdc0.getName());
  -        assertNotNull(pdc0.getName() + " does not have a PortletApplicationDefinition.", pdc0.getPortletApplicationDefinition());
  +            PortletDefinitionComposite pdc2 =
  +                (PortletDefinitionComposite) JetspeedPortletRegistry.getPortletDefinitionByUniqueName(
  +                    APP_1_NAME + "::" + PORTLET_0_NAME);
  +
  +            assertNotNull(
  +                "Could not locate PortletDefinition with unique name \"" + APP_1_NAME + "::" + PORTLET_0_UID + "\"",
  +                pdc2);
  +
  +            assertNotNull(pdc0);
  +            assertEquals(checkName, pdc0.getName());
  +            assertNotNull(
  +                pdc0.getName() + " does not have a PortletApplicationDefinition.",
  +                pdc0.getPortletApplicationDefinition());
  +            JetspeedPortletRegistry.commitTransaction();
  +        }
  +        catch (Throwable e)
  +        {
  +            JetspeedPortletRegistry.rollbackTransaction();
  +            throw e;
  +        }
   
       }
   
  -    public void testAddingPortlet() throws TransactionStateException
  +    public void testAddingPortlet() throws Throwable 
       {
           try
           {
  @@ -272,7 +288,7 @@
   
               assertNotNull(app);
               JetspeedPortletRegistry.beginTransaction();
  -            JetspeedPortletRegistry.writeLock(app);
  +           JetspeedPortletRegistry.writeLock(app);
   
               //add a portlet
               PortletDefinitionComposite portlet1 =
  @@ -294,18 +310,9 @@
           }
           catch (Throwable e)
           {
  -            e.printStackTrace();
  -            try
  -            {
  -                JetspeedPortletRegistry.rollbackTransaction();
  -            }
  -            catch (TransactionStateException e1)
  -            {
  -
  -                e1.printStackTrace();
  -            }
  -
  -            throw new AssertionFailedError();
  +            e.printStackTrace();            
  +           JetspeedPortletRegistry.rollbackTransaction(); 
  +            throw e;
           }
   
           //		test that portlet application exists
  @@ -340,7 +347,7 @@
   
       }
   
  -    public void testAddPortletInfo() throws Exception
  +    public void testAddPortletInfo() throws Throwable
       {
           try
           {
  @@ -392,9 +399,8 @@
               JetspeedPortletRegistry.commitTransaction();
   
           }
  -        catch (Exception e)
  -        {
  -            e.printStackTrace();
  +        catch (Throwable e)
  +        {            
               try
               {
                   JetspeedPortletRegistry.rollbackTransaction();
  @@ -586,30 +592,31 @@
           // // JetspeedPortletRegistry.clearCache();
   
           Iterator itr = JetspeedPortletRegistry.getPortletApplications().iterator();
  -
  -        while (itr.hasNext())
  +        try
           {
  -            MutablePortletApplication pac = (MutablePortletApplication) itr.next();
  +            JetspeedPortletRegistry.beginTransaction();
  +            while (itr.hasNext())
  +            {
  +                MutablePortletApplication pac = (MutablePortletApplication) itr.next();
  +
  +                JetspeedPortletRegistry.removeApplication(pac);
  +
  +            }
  +			JetspeedPortletRegistry.commitTransaction();
   
  +        }
  +        catch (Exception e)
  +        {
               try
               {
  -                JetspeedPortletRegistry.beginTransaction();
  -                JetspeedPortletRegistry.removeApplication(pac);
  -                JetspeedPortletRegistry.commitTransaction();
  +                JetspeedPortletRegistry.rollbackTransaction();
               }
  -            catch (Exception e)
  +            catch (TransactionStateException e1)
               {
  -                try
  -                {
  -                    JetspeedPortletRegistry.rollbackTransaction();
  -                }
  -                catch (TransactionStateException e1)
  -                {
  -                    e1.printStackTrace();
  -                }
  -                System.out.println("Unable to tear down test.");
  -                e.printStackTrace();
  +                e1.printStackTrace();
               }
  +            System.out.println("Unable to tear down test.");
  +            e.printStackTrace();
           }
   
           Iterator pitr = JetspeedPortletRegistry.getAllPortletDefinitions().iterator();
  @@ -618,6 +625,7 @@
               PortletDefinition pd = (PortletDefinition) pitr.next();
               System.err.println("Test pass [" + testPasses + "]: Left over PortletDefinition: " + pd.getId() + ":" + pd.getName());
           }
  +
       }
   
       /**
  
  
  

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