View Javadoc
1   package org.codehaus.plexus.component.registry;
2   
3   import junit.framework.TestCase;
4   
5   import org.codehaus.plexus.DefaultPlexusContainer;
6   import org.codehaus.plexus.PlexusContainer;
7   import org.codehaus.plexus.component.repository.ComponentDescriptor;
8   import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
9   
10  public class DefaultComponentRegistryTest
11      extends TestCase
12  {
13  
14      public void testConcurrentDisposeAndLookup()
15          throws Exception
16      {
17          final PlexusContainer plexus = new DefaultPlexusContainer();
18  
19          ComponentDescriptor<TestSynchronizedComponent> descriptor = new ComponentDescriptor<TestSynchronizedComponent>(
20              TestSynchronizedComponent.class, plexus.getContainerRealm() );
21          descriptor.setRole( TestSynchronizedComponent.class.getCanonicalName() );
22          descriptor.setImplementation( TestSynchronizedComponent.class.getCanonicalName() );
23          plexus.addComponentDescriptor( descriptor );
24  
25          TestSynchronizedComponent component = plexus.lookup( TestSynchronizedComponent.class );
26          
27          class LookupThread extends Thread
28          {
29              private TestSynchronizedComponent component;
30  
31              @Override
32              public synchronized void run()
33              {
34                  try
35                  {
36                      this.component =  plexus.lookup( TestSynchronizedComponent.class );
37                  }
38                  catch ( ComponentLookupException e )
39                  {
40                      // expected
41                  }
42              }
43  
44              public synchronized TestSynchronizedComponent getComponent()
45              {
46                  return component;
47              }
48          }
49  
50          LookupThread lookupThread = new LookupThread();
51  
52          component.setLookupThread( lookupThread );
53  
54          plexus.dispose();
55  
56          assertNull( lookupThread.getComponent() );
57      }
58  
59  }