donaldp     2002/11/10 04:39:27

  Added:       fortress/src/test/org/apache/excalibur/fortress/test
                        HandlersTestCase.java
  Log:
  Add in unit test that will eventually test all the different handler types.
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/fortress/src/test/org/apache/excalibur/fortress/test/HandlersTestCase.java
  
  Index: HandlersTestCase.java
  ===================================================================
  /*
  * Copyright (C) The Apache Software Foundation. All rights reserved.
  *
  * This software is published under the terms of the Apache Software License
  * version 1.1, a copy of which has been included with this distribution in
  * the LICENSE.txt file.
  */
  
  package org.apache.excalibur.fortress.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.framework.container.ContainerUtil;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.excalibur.fortress.ContainerManager;
  import org.apache.excalibur.fortress.DefaultContainerManager;
  import org.apache.excalibur.fortress.container.DefaultContainer;
  import org.apache.excalibur.fortress.test.data.BaseRole;
  import org.apache.excalibur.fortress.test.data.Role1;
  import org.apache.excalibur.fortress.test.data.Role2;
  import org.apache.excalibur.fortress.test.data.Role3;
  import org.apache.excalibur.fortress.util.ContextBuilder;
  
  /**
   * A testcase for the different handlers.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/10 12:39:27 $
   */
  public class HandlersTestCase extends TestCase
  {
      private Exception m_exception;
  
      public HandlersTestCase( final String name )
      {
          super( name );
      }
  
      public void testThreadsafe()
          throws Exception
      {
          final ServiceManager serviceManager = getServiceManager();
          final String key = Role1.ROLE;
          final BaseRole object1 = (BaseRole)serviceManager.lookup( key );
          final BaseRole object2 = (BaseRole)serviceManager.lookup( key );
  
          assertEquals( "Threadsafe object IDs (1 vs 2)", object1.getID(), 
object2.getID() );
  
          final Thread thread = new Thread()
          {
              public void run()
              {
                  try
                  {
                      final BaseRole object3 = (BaseRole)serviceManager.lookup( key );
                      final BaseRole object4 = (BaseRole)serviceManager.lookup( key );
  
                      assertEquals( "Threadsafe object IDs (1 vs 3)", object1.getID(), 
object3.getID() );
                      assertEquals( "Threadsafe object IDs (2 vs 4)", object2.getID(), 
object4.getID() );
                      assertEquals( "Threadsafe object IDs (3 vs 4)", object3.getID(), 
object4.getID() );
                  }
                  catch( final Exception e )
                  {
                      m_exception = e;
                  }
              }
          };
          thread.start();
          thread.join();
  
          if( null != m_exception )
          {
              final Exception exception = m_exception;
              m_exception = null;
              throw exception;
          }
      }
  
      public void testPerThread()
          throws Exception
      {
          final String key = Role3.ROLE;
          final String type = "PerThread";
  
          final ServiceManager serviceManager = getServiceManager();
          final BaseRole object1 = (BaseRole)serviceManager.lookup( key );
          final BaseRole object2 = (BaseRole)serviceManager.lookup( key );
  
          assertEquals( type + " object IDs (1 vs 2)", object1.getID(), 
object2.getID() );
  
          final Thread thread = new Thread()
          {
              public void run()
              {
                  try
                  {
                      final BaseRole object3 = (BaseRole)serviceManager.lookup( key );
                      final BaseRole object4 = (BaseRole)serviceManager.lookup( key );
  
                      assertTrue( type + " object IDs (1 vs 3)", object1.getID() != 
object3.getID() );
                      assertTrue( type + " object IDs (2 vs 4)", object2.getID() != 
object4.getID() );
                      assertEquals( type + " object IDs (3 vs 4)", object3.getID(), 
object4.getID() );
                  }
                  catch( final Exception e )
                  {
                      m_exception = e;
                  }
              }
          };
          thread.start();
          thread.join();
  
          if( null != m_exception )
          {
              final Exception exception = m_exception;
              m_exception = null;
              throw exception;
          }
      }
  
      private ServiceManager getServiceManager() throws Exception
      {
          final ContextBuilder contextBuilder = new ContextBuilder();
          contextBuilder.setContextDirectory( "./" );
          contextBuilder.setWorkDirectory( "./" );
          final String BASE = "resource://org/apache/excalibur/fortress/test/data/";
          contextBuilder.setContainerConfiguration( BASE + "test1.xconf" );
          contextBuilder.setLoggerManagerConfiguration( BASE + "test1.xlog" );
          contextBuilder.setRoleManagerConfiguration( BASE + "test1.roles" );
  
          final ContainerManager cm = new DefaultContainerManager( 
contextBuilder.getContext() );
          ContainerUtil.initialize( cm );
  
          final DefaultContainer container = (DefaultContainer)cm.getContainer();
          final ServiceManager serviceManager = container.getServiceManager();
          return serviceManager;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>

Reply via email to