ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/aut/src/test/org/apache/aut/jprocess/test DemuxPolicyTestCase.java DemuxPropertiesTestCase.java DemuxTestCase.java
Date Sun, 28 Apr 2002 04:51:39 GMT
donaldp     02/04/27 21:51:39

  Added:       aut/src/test/org/apache/aut/jprocess/test
                        DemuxPolicyTestCase.java
                        DemuxPropertiesTestCase.java
  Removed:     aut/src/test/org/apache/aut/jprocess/test DemuxTestCase.java
  Log:
  Add unit tests for Demux versions of
  Proeprties and Policy
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/aut/src/test/org/apache/aut/jprocess/test/DemuxPolicyTestCase.java
  
  Index: DemuxPolicyTestCase.java
  ===================================================================
  /*
   * Copyright  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.aut.jprocess.test;
  
  import java.security.CodeSource;
  import java.security.PermissionCollection;
  import java.security.Permissions;
  import java.security.Policy;
  import java.util.HashMap;
  import java.util.Iterator;
  import junit.framework.TestCase;
  import org.apache.aut.jprocess.DemuxPolicy;
  
  /**
   * Basic unit tests for the multiplexing Policy.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   */
  public final class DemuxPolicyTestCase
      extends TestCase
  {
      private static final String T1 = "Thread1";
      private static final String T2 = "Thread2";
      private static final String T3 = "Thread3";
      private static final String T4 = "Thread4";
  
      private final HashMap m_threadMap = new HashMap();
  
      public DemuxPolicyTestCase( final String name )
      {
          super( name );
      }
  
      private void doStart()
          throws Exception
      {
          final Iterator iterator = m_threadMap.keySet().iterator();
          while( iterator.hasNext() )
          {
              final String name = (String)iterator.next();
              final Thread thread = (Thread)m_threadMap.get( name );
              thread.start();
          }
      }
  
      private void doJoin()
          throws Exception
      {
          final Iterator iterator = m_threadMap.keySet().iterator();
          while( iterator.hasNext() )
          {
              final String name = (String)iterator.next();
              final Thread thread = (Thread)m_threadMap.get( name );
              thread.join();
          }
      }
  
      private void startThread( final String name,
                                final DemuxPolicy demux )
          throws Exception
      {
          final PolicyThread thread =
              new PolicyThread( name, new TestPolicy( name ), demux );
          m_threadMap.put( name, thread );
      }
  
      public void testProperties()
          throws Exception
      {
          final DemuxPolicy policy = new DemuxPolicy( null );
          startThread( T1, policy );
          startThread( T2, policy );
          startThread( T3, policy );
          startThread( T4, policy );
  
          doStart();
          doJoin();
  
          final PermissionCollection permissions1 = getPermissions( T1 );
          final PermissionCollection permissions2 = getPermissions( T2 );
          final PermissionCollection permissions3 = getPermissions( T3 );
          final PermissionCollection permissions4 = getPermissions( T4 );
          assertTrue( "PolicyName1", permissions1.implies( new RuntimePermission( T1 ) ) );
          assertTrue( "PolicyName2", permissions2.implies( new RuntimePermission( T2 ) ) );
          assertTrue( "PolicyName3", permissions3.implies( new RuntimePermission( T3 ) ) );
          assertTrue( "PolicyName4", permissions4.implies( new RuntimePermission( T4 ) ) );
      }
  
      private PermissionCollection getPermissions( final String threadName )
      {
          final PolicyThread thread =
              (PolicyThread)m_threadMap.get( threadName );
          assertNotNull( "getPolicyThread()", thread );
  
          return thread.getPermissionCollection();
      }
  
      private static class PolicyThread
          extends Thread
      {
          private final TestPolicy m_policy;
          private final DemuxPolicy m_demux;
          private PermissionCollection m_result;
  
          PolicyThread( final String name,
                        final TestPolicy policy,
                        final DemuxPolicy demux )
          {
              super( name );
              m_policy = policy;
              m_demux = demux;
          }
  
          PermissionCollection getPermissionCollection()
          {
              return m_result;
          }
  
          public void run()
          {
              m_demux.bindPolicy( m_policy );
              m_result = m_demux.getPermissions( null );
          }
      }
  
      private static class TestPolicy
          extends Policy
      {
          private final String m_name;
  
          TestPolicy( final String name )
          {
              m_name = name;
          }
  
          String getName()
          {
              return m_name;
          }
  
          public PermissionCollection getPermissions( CodeSource codesource )
          {
              final Permissions permissions = new Permissions();
              permissions.add( new RuntimePermission( m_name ) );
              return permissions;
          }
  
          public void refresh()
          {
          }
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/aut/src/test/org/apache/aut/jprocess/test/DemuxPropertiesTestCase.java
  
  Index: DemuxPropertiesTestCase.java
  ===================================================================
  /*
   * Copyright  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.aut.jprocess.test;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Properties;
  import junit.framework.TestCase;
  import org.apache.aut.jprocess.DemuxProperties;
  
  /**
   * Basic unit tests for the multiplexing properties.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   */
  public final class DemuxPropertiesTestCase
      extends TestCase
  {
      private static final String T1 = "Thread1";
      private static final String T2 = "Thread2";
      private static final String T3 = "Thread3";
      private static final String T4 = "Thread4";
  
      private static final String KEY1 = "Key1";
      private static final String KEY2 = "Key2";
      private static final String KEY3 = "Key3";
      private static final String KEY4 = "Key4";
  
      private static final String DATA1 = "Data1";
      private static final String DATA2 = "Data2";
      private static final String DATA3 = "Data3";
      private static final String DATA4 = "Data4";
  
      private final HashMap m_threadMap = new HashMap();
  
      public DemuxPropertiesTestCase( final String name )
      {
          super( name );
      }
  
      private void doStart()
          throws Exception
      {
          final Iterator iterator = m_threadMap.keySet().iterator();
          while( iterator.hasNext() )
          {
              final String name = (String)iterator.next();
              final Thread thread = (Thread)m_threadMap.get( name );
              thread.start();
          }
      }
  
      private void doJoin()
          throws Exception
      {
          final Iterator iterator = m_threadMap.keySet().iterator();
          while( iterator.hasNext() )
          {
              final String name = (String)iterator.next();
              final Thread thread = (Thread)m_threadMap.get( name );
              thread.join();
          }
      }
  
      private void startThread( final String name,
                                final DemuxProperties demux )
          throws Exception
      {
          final PropertyThread thread =
              new PropertyThread( name, new Properties(), demux );
          m_threadMap.put( name, thread );
      }
  
      public void testProperties()
          throws Exception
      {
          final DemuxProperties properties = new DemuxProperties( new Properties() );
          startThread( T1, properties );
          startThread( T2, properties );
          startThread( T3, properties );
          startThread( T4, properties );
  
          doStart();
          doJoin();
  
          final Properties properties1 = getProperties( T1 );
          assertEquals( "Data1/1", T1 + DATA1, properties1.getProperty( KEY1 ) );
          assertEquals( "Data1/2", T1 + DATA2, properties1.getProperty( KEY2 ) );
          assertEquals( "Data1/3", T1 + DATA3, properties1.getProperty( KEY3 ) );
          assertEquals( "Data1/4", T1 + DATA4, properties1.getProperty( KEY4 ) );
  
          final Properties properties2 = getProperties( T2 );
          assertEquals( "Data2/1", T2 + DATA1, properties2.getProperty( KEY1 ) );
          assertEquals( "Data2/2", T2 + DATA2, properties2.getProperty( KEY2 ) );
          assertEquals( "Data2/3", T2 + DATA3, properties2.getProperty( KEY3 ) );
          assertEquals( "Data2/4", T2 + DATA4, properties2.getProperty( KEY4 ) );
  
          final Properties properties3 = getProperties( T3 );
          assertEquals( "Data3/1", T3 + DATA1, properties3.getProperty( KEY1 ) );
          assertEquals( "Data3/2", T3 + DATA2, properties3.getProperty( KEY2 ) );
          assertEquals( "Data3/3", T3 + DATA3, properties3.getProperty( KEY3 ) );
          assertEquals( "Data3/4", T3 + DATA4, properties3.getProperty( KEY4 ) );
  
          final Properties properties4 = getProperties( T4 );
          assertEquals( "Data4/1", T4 + DATA1, properties4.getProperty( KEY1 ) );
          assertEquals( "Data4/2", T4 + DATA2, properties4.getProperty( KEY2 ) );
          assertEquals( "Data4/3", T4 + DATA3, properties4.getProperty( KEY3 ) );
          assertEquals( "Data4/4", T4 + DATA4, properties4.getProperty( KEY4 ) );
      }
  
      private Properties getProperties( final String threadName )
      {
          final PropertyThread thread =
              (PropertyThread)m_threadMap.get( threadName );
          assertNotNull( "getProperties()", thread );
  
          return thread.getProperties();
      }
  
      private static class PropertyThread
          extends Thread
      {
          private final Properties m_properties;
          private final DemuxProperties m_demux;
  
          PropertyThread( final String name,
                          final Properties properties,
                          final DemuxProperties demux )
          {
              super( name );
              m_properties = properties;
              m_demux = demux;
          }
  
          Properties getProperties()
          {
              return m_properties;
          }
  
          public void run()
          {
              m_demux.bindProperties( m_properties );
              m_demux.setProperty( KEY1, getName() + DATA1 );
              m_demux.setProperty( KEY2, getName() + DATA2 );
              m_demux.setProperty( KEY3, getName() + DATA3 );
              m_demux.setProperty( KEY4, getName() + DATA4 );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message