CVS: plexus-container/src/test/org/apache/plexus DefaultPlexusContainerTest.java,1.8,1.9 configuration.xml,1.7,1.8

Jason van Zyl <[email protected]> Tue, 5 Aug 2003 14:26:26 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container/src/test/org/apache/plexus
In directory hogshead.codehaus.org:/tmp/cvs-serv25662/src/test/org/apache/plexus

Modified Files:
	DefaultPlexusContainerTest.java configuration.xml 
Log Message:
o first pass at integrating bert's patch. checking it in so that bert can
  look at some stuff for me.


Index: DefaultPlexusContainerTest.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container/src/test/org/apache/plexus/DefaultPlexusContainerTest.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- DefaultPlexusContainerTest.java	21 Jul 2003 02:16:31 -0000	1.8
+++ DefaultPlexusContainerTest.java	5 Aug 2003 19:26:24 -0000	1.9
@@ -4,9 +4,11 @@
 
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Iterator;
 
-import org.apache.plexus.service.repository.instance.InstanceManager;
 import org.apache.plexus.service.repository.factory.ComponentFactory;
+import org.apache.plexus.util.AbstractTestThread;
+import org.apache.plexus.util.TestThreadManager;
 
 /**
  *  @author <a href="mailto:[email protected]">Jason van Zyl</a>
@@ -54,29 +56,31 @@
         container.start();
 
         int defaultComponents = 0;
-        int testComponents = 6;
+        int testComponents = 7;
 
         // These are some default components that we used internally. These components don't
         // usually need to be replaced but they can be if the user desires.
 
-        // Per-lookup instance manager.
-        InstanceManager plim =
-            (InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "per-lookup" );
-        assertNotNull( plim );
-        defaultComponents++;
-
-        // Poolable instance manager.
-        InstanceManager pim =
-            (InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "poolable" );
-        assertNotNull( pim );
-        defaultComponents++;
+        //NOTE: these have now been included through a differnet method
+        /*
+// Per-lookup instance manager.
+InstanceManager plim =
+(InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "per-lookup" );
+assertNotNull( plim );
+defaultComponents++;
 
-        // Singleton instance manager.
-        InstanceManager sim =
-            (InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "singleton" );
-        assertNotNull( sim );
-        defaultComponents++;
+// Poolable instance manager.
+InstanceManager pim =
+(InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "poolable" );
+assertNotNull( pim );
+defaultComponents++;
 
+// Singleton instance manager.
+InstanceManager sim =
+(InstanceManager) container.getComponentRepository().lookup( InstanceManager.ROLE + "singleton" );
+assertNotNull( sim );
+defaultComponents++;
+*/
         // Java Component factory.
         // Singleton instance manager.
         ComponentFactory jcf =
@@ -94,6 +98,7 @@
         assertEquals( true, container.getComponentRepository().hasService( ServiceA.ROLE ) );
         assertEquals( true, container.getComponentRepository().hasService( ServiceB.ROLE ) );
         assertEquals( true, container.getComponentRepository().hasService( ServiceC.ROLE + "only-instance" ) );
+        assertEquals( true, container.getComponentRepository().hasService( ServiceG.ROLE ) );
 
         // ----------------------------------------------------------------------
         //  ServiceA
@@ -122,11 +127,13 @@
         container.getComponentRepository().release( serviceA );
 
         // Now we have released the component so there should be no instantiated services.
-        assertEquals( 0 + defaultComponents, container.getComponentRepository().instantiatedComponents() );
+        //no longer correct! The instance managers may keep an instance alive.
+//        assertEquals( 0 + defaultComponents, container.getComponentRepository().instantiatedComponents() );
 
         // Make sure the number of configured components is still 3.
         assertEquals( testComponents + defaultComponents, container.getComponentRepository().configuredComponents() );
 
+        // make sure we get the same instance back everytime
         DefaultServiceA a0 = (DefaultServiceA) container.getComponentRepository().lookup( ServiceA.ROLE );
         DefaultServiceA a1 = (DefaultServiceA) container.getComponentRepository().lookup( ServiceA.ROLE );
         DefaultServiceA a2 = (DefaultServiceA) container.getComponentRepository().lookup( ServiceA.ROLE );
@@ -199,6 +206,87 @@
         assertNotSame( serviceD2, serviceD3 );
         assertNotSame( serviceD1, serviceD3 );
 
+
+        // ----------------------------------------------------------------------
+        //  ServiceG - singleton-keep-alive
+        //
+        //  Implements all the standard Avalon lifecycle phases.
+        // ----------------------------------------------------------------------
+
+        // Retrieve an instance of service G.
+        DefaultServiceG serviceG =
+            (DefaultServiceG) container.getComponentRepository().lookup( ServiceG.ROLE );
+
+        // Make sure the service is alive.
+        assertNotNull( serviceG );
+
+        // Make sure the component went through all the lifecycle phases
+        assertEquals( true, serviceG.enableLogging );
+        assertEquals( true, serviceG.contextualize );
+        assertEquals( true, serviceG.service );
+        assertEquals( true, serviceG.configure );
+        assertEquals( true, serviceG.initialize );
+        assertEquals( true, serviceG.start );
+
+        // Now how do we make sure it has been released and decomissioned
+        // properly.
+        container.getComponentRepository().release( serviceG );
+
+        // make sure we get the same instance back everytime
+        DefaultServiceG g0 =
+            (DefaultServiceG) container.getComponentRepository().lookup( ServiceG.ROLE );
+        DefaultServiceG g1 =
+            (DefaultServiceG) container.getComponentRepository().lookup( ServiceG.ROLE );
+        DefaultServiceG g2 =
+            (DefaultServiceG) container.getComponentRepository().lookup( ServiceG.ROLE );
+
+        assertTrue( g0.equals( g1 ) );
+        assertTrue( g1.equals( g2 ) );
+        assertTrue( g2.equals( g0 ) );
+
+        //Now try it again in seperate threads.Make sure the instance is the same for all threads
+        TestThreadManager reg = new TestThreadManager( this );
+        for ( int i = 0; i < 5; i++ )
+        {
+            SingletonComponentTestThread st =
+                new SingletonComponentTestThread( reg, container, ServiceG.ROLE, g0 );
+            reg.registerThread( st );
+        }
+        reg.runTestThreads();
+
+        while ( reg.isStillRunningThreads() )
+        {
+            //wait until all threads have finished execution
+            synchronized ( this )
+            {
+                try
+                {
+                    wait();
+                }
+                catch ( InterruptedException e )
+                {
+                }
+            }
+        }
+
+        assertEquals( "Expected 5 test threads to of run", reg.getRunThreads().size(), 5 );
+        //now test if any components were returned which was not the same instance
+        if ( reg.hasFailedThreads() )
+        {
+            //collect all failed tests
+            StringBuffer out = new StringBuffer();
+            Iterator iter = reg.getFailedTests().iterator();
+            String nl = System.getProperty( "line.separator" );
+            while ( iter.hasNext() )
+            {
+                out.append( nl );
+                out.append( ( (SingletonComponentTestThread) iter.next() ).getErrorMsg() );
+            }
+            fail(
+                "Singleton component 'ServiceG' being instantiated multiple times. Failed test threads: "
+                + out );
+        }
+
         // ----------------------------------------------------------------------
         // Per-lookup component
         // ----------------------------------------------------------------------
@@ -249,4 +337,63 @@
         // interpolated so no "${" sequence should be present.
         assertFalse( serviceF.getPlexusHome().indexOf( "${" ) > 0 );
     }
+
+    class SingletonComponentTestThread extends AbstractTestThread
+    {
+        private Object expectedComponent;
+        private Object returnedComponent;
+        private PlexusContainer container;
+        private String role;
+
+        /**
+         *
+         */
+        public SingletonComponentTestThread( PlexusContainer container, String role, Object expectedComponent )
+        {
+            super();
+            this.expectedComponent = expectedComponent;
+            this.container = container;
+            this.role = role;
+        }
+
+        /**
+         * @param registry
+         */
+        public SingletonComponentTestThread( TestThreadManager registry, PlexusContainer container, String role, Object expectedComponent )
+        {
+            super( registry );
+            this.expectedComponent = expectedComponent;
+            this.container = container;
+            this.role = role;
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.plexus.util.AbstractRegisteredThread#doRun()
+         */
+        public void doRun() throws Throwable
+        {
+            try
+            {
+                returnedComponent = container.getComponentRepository().lookup( role );
+                if ( returnedComponent == null )
+                {
+                    setErrorMsg( "Null component returned" );
+                }
+                else if ( returnedComponent == expectedComponent )
+                {
+                    setPassed( true );
+                }
+                else
+                {
+                    setErrorMsg( "Returned component was a different instance. Expected=" + expectedComponent + ", got=" + returnedComponent );
+                }
+            }
+            finally
+            {
+                container.getComponentRepository().release( returnedComponent );
+            }
+        }
+
+    }
+
 }

Index: configuration.xml
===================================================================
RCS file: /cvsroot/plexus/plexus-container/src/test/org/apache/plexus/configuration.xml,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- configuration.xml	31 May 2003 22:02:43 -0000	1.7
+++ configuration.xml	5 Aug 2003 19:26:24 -0000	1.8
@@ -1,7 +1,12 @@
 <plexus>
 
   <configurations-directory>${basedir}/src/test-input/configurations-directory</configurations-directory>
-
+  <logging>
+    <implementation>org.apache.plexus.logging.ConsoleLoggerManager</implementation>
+    <logger>
+      <threshold>ERROR</threshold>
+    </logger>
+  </logging>
   <service-repository>
     <implementation>org.apache.plexus.service.repository.DefaultComponentRepository</implementation>
   </service-repository>
@@ -77,6 +82,19 @@
       <role>org.apache.plexus.ServiceE</role>
       <implementation>org.apache.plexus.DefaultServiceE</implementation>
       <instantiation-strategy>per-lookup</instantiation-strategy>
+      <configuration/>
+    </component>
+
+
+    <!--
+    |
+    | G Service
+    |
+    -->
+    <component>
+      <role>org.apache.plexus.ServiceG</role>
+      <implementation>org.apache.plexus.DefaultServiceG</implementation>
+      <instantiation-strategy>singleton-keep-alive</instantiation-strategy>
       <configuration/>
     </component>