avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/extension/src/test/org/apache/avalon/excalibur/packagemanager/impl/test ComparatorTestCase.java TestExtensionManager.java
Date Mon, 07 Oct 2002 10:00:46 GMT
donaldp     2002/10/07 03:00:46

  Modified:    extension/src/java/org/apache/avalon/excalibur/packagemanager
                        OptionalPackage.java PackageManager.java
               extension/src/java/org/apache/avalon/excalibur/packagemanager/impl
                        DefaultExtensionManager.java
                        DelegatingExtensionManager.java
  Added:       extension/src/java/org/apache/avalon/excalibur/packagemanager/impl
                        OptionalPackageComparator.java
               extension/src/test/org/apache/avalon/excalibur/packagemanager/impl/test
                        ComparatorTestCase.java TestExtensionManager.java
  Log:
  Sort OptionalPackages according to spec version and then impl version of extension that
they are satisfying. Add a set of unit tests to make sure that the behaviour is as expected.
  
  Revision  Changes    Path
  1.5       +4 -2      jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/OptionalPackage.java
  
  Index: OptionalPackage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/OptionalPackage.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- OptionalPackage.java	7 Oct 2002 09:26:48 -0000	1.4
  +++ OptionalPackage.java	7 Oct 2002 10:00:45 -0000	1.5
  @@ -143,7 +143,8 @@
               {
                   sb.append( " " );
               }
  -            sb.append( m_available[ i ].getExtensionName() );
  +            //sb.append( m_available[ i ].getExtensionName() );
  +            sb.append( m_available[ i ] );
           }
   
           sb.append( "], Required[" );
  @@ -153,7 +154,8 @@
               {
                   sb.append( " " );
               }
  -            sb.append( m_required[ i ].getExtensionName() );
  +            //sb.append( m_required[ i ].getExtensionName() );
  +            sb.append( m_required[ i ] );
           }
   
           sb.append( "] ]" );
  
  
  
  1.4       +3 -4      jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/PackageManager.java
  
  Index: PackageManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/PackageManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PackageManager.java	2 Oct 2002 01:46:58 -0000	1.3
  +++ PackageManager.java	7 Oct 2002 10:00:45 -0000	1.4
  @@ -66,14 +66,13 @@
       public OptionalPackage getOptionalPackage( final Extension extension )
       {
           final OptionalPackage[] packages = m_repository.getOptionalPackages( extension
);
  -
           if( null == packages || 0 == packages.length )
           {
               return null;
           }
   
  -        //TODO: Use heurisitic to find which is best package
  -
  +        //The best candidate is always returned first so we
  +        //can just return it.
           return packages[ 0 ];
       }
   
  
  
  
  1.5       +5 -7      jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/impl/DefaultExtensionManager.java
  
  Index: DefaultExtensionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/impl/DefaultExtensionManager.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DefaultExtensionManager.java	7 Oct 2002 06:20:14 -0000	1.4
  +++ DefaultExtensionManager.java	7 Oct 2002 10:00:45 -0000	1.5
  @@ -12,6 +12,7 @@
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.StringTokenizer;
  +import java.util.Collections;
   import java.util.jar.JarFile;
   import java.util.jar.Manifest;
   import org.apache.avalon.excalibur.extension.Extension;
  @@ -124,9 +125,9 @@
               }
           }
   
  -        //TODO: Sort packages so that most relevant is first
  -        //ie Sort on spec version first and then on Imp version
  -
  +        final OptionalPackageComparator comparator =
  +            new OptionalPackageComparator( extension.getExtensionName() );
  +        Collections.sort( results, comparator );
           return (OptionalPackage[])results.toArray( new OptionalPackage[ 0 ] );
       }
   
  @@ -305,9 +306,6 @@
               m_packages.put( extension.getExtensionName(), candidates );
           }
   
  -        //TODO: Add this in descending order so that don't have to sort in
  -        //getOptionalPackages ????
  -        //yes, sort by Spec Version then vendor, Imp Version
           candidates.add( optionalPackage );
       }
   
  
  
  
  1.5       +5 -1      jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/impl/DelegatingExtensionManager.java
  
  Index: DelegatingExtensionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/impl/DelegatingExtensionManager.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DelegatingExtensionManager.java	7 Oct 2002 06:32:06 -0000	1.4
  +++ DelegatingExtensionManager.java	7 Oct 2002 10:00:45 -0000	1.5
  @@ -8,6 +8,7 @@
   package org.apache.avalon.excalibur.packagemanager.impl;
   
   import java.util.ArrayList;
  +import java.util.Collections;
   import org.apache.avalon.excalibur.extension.Extension;
   import org.apache.avalon.excalibur.packagemanager.ExtensionManager;
   import org.apache.avalon.excalibur.packagemanager.OptionalPackage;
  @@ -123,6 +124,9 @@
               }
           }
   
  +        final OptionalPackageComparator comparator =
  +            new OptionalPackageComparator( extension.getExtensionName() );
  +        Collections.sort( resultPackages, comparator );
           final OptionalPackage[] resultData =
               new OptionalPackage[ resultPackages.size() ];
           return (OptionalPackage[])resultPackages.toArray( resultData );
  
  
  
  1.1                  jakarta-avalon-excalibur/extension/src/java/org/apache/avalon/excalibur/packagemanager/impl/OptionalPackageComparator.java
  
  Index: OptionalPackageComparator.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.avalon.excalibur.packagemanager.impl;
  
  import java.util.Comparator;
  import org.apache.avalon.excalibur.extension.Extension;
  import org.apache.avalon.excalibur.extension.DeweyDecimal;
  import org.apache.avalon.excalibur.packagemanager.OptionalPackage;
  
  /**
   * A simple class to compare two extensions and sort them
   * on spec version and then on impl version. Unspecified
   * versions rate lower than specified versions.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/07 10:00:45 $
   */
  class OptionalPackageComparator
      implements Comparator
  {
      /**
       * The name of extension the comparator is working with.
       */
      private final String m_name;
  
      public OptionalPackageComparator( final String name )
      {
          if( null == name )
          {
              throw new NullPointerException( "name" );
          }
  
          m_name = name;
      }
  
      public int compare( final Object o1,
                          final Object o2 )
      {
          final OptionalPackage pkg1 = (OptionalPackage)o1;
          final OptionalPackage pkg2 = (OptionalPackage)o2;
          final Extension e1 = getMatchingExtension( pkg1 );
          final Extension e2 = getMatchingExtension( pkg2 );
          int result = compareSpecVersion( e1, e2 );
          if( 0 != result )
          {
              return result;
          }
          else
          {
              return compareImplVersion( e1, e2 );
          }
      }
  
      private Extension getMatchingExtension( final OptionalPackage pkg )
      {
          final Extension[] extensions = pkg.getAvailableExtensions();
          for( int i = 0; i < extensions.length; i++ )
          {
              final Extension extension = extensions[ i ];
              if( extension.getExtensionName().equals( m_name ) )
              {
                  return extension;
              }
          }
  
          final String message = "Unable to locate extension " +
              m_name + " in package " + pkg;
          throw new IllegalStateException( message );
      }
  
      private int compareImplVersion( final Extension e1, final Extension e2 )
      {
          final String implVersion1 = e1.getImplementationVersion();
          final String implVersion2 = e2.getImplementationVersion();
          if( null == implVersion1 && null == implVersion2 )
          {
              return 0;
          }
          else if( null != implVersion1 && null == implVersion2 )
          {
              return -1;
          }
          else if( null == implVersion1 && null != implVersion2 )
          {
              return 1;
          }
          else
          {
              return -implVersion1.compareTo( implVersion2 );
          }
      }
  
      private int compareSpecVersion( final Extension e1,
                                      final Extension e2 )
      {
          final DeweyDecimal specVersion1 = e1.getSpecificationVersion();
          final DeweyDecimal specVersion2 = e2.getSpecificationVersion();
          if( null == specVersion1 && null == specVersion2 )
          {
              return 0;
          }
          else if( null != specVersion1 && null == specVersion2 )
          {
              return -1;
          }
          else if( null == specVersion1 && null != specVersion2 )
          {
              return 1;
          }
          else
          {
              if( specVersion1.isEqual( specVersion2 ) )
              {
                  return 0;
              }
              else if( specVersion1.isGreaterThan( specVersion2 ) )
              {
                  return -1;
              }
              else
              {
                  return 1;
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/extension/src/test/org/apache/avalon/excalibur/packagemanager/impl/test/ComparatorTestCase.java
  
  Index: ComparatorTestCase.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.avalon.excalibur.packagemanager.impl.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.excalibur.packagemanager.ExtensionManager;
  import org.apache.avalon.excalibur.packagemanager.OptionalPackage;
  import org.apache.avalon.excalibur.packagemanager.impl.DelegatingExtensionManager;
  import org.apache.avalon.excalibur.extension.Extension;
  import java.io.File;
  
  /**
   * A basic test case for comparator.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/07 10:00:45 $
   */
  public class ComparatorTestCase
      extends TestCase
  {
      private static final String NAME = "Extension1";
      private static final String VENDOR1 = "Vendor1";
      private static final String VENDOR2 = "Vendor2";
  
      public ComparatorTestCase( final String name )
      {
          super( name );
      }
  
      public void testAllNull()
      {
          runCompareTest( null, null, null, null, VENDOR1, VENDOR2 );
      }
  
      public void testSpecNonNullV1()
      {
          runCompareTest( "1.0", null, "1.1", null, VENDOR2, VENDOR1 );
      }
  
      public void testSpecNonNullV2()
      {
          runCompareTest( "1.1", null, "1.0", null, VENDOR1, VENDOR2 );
      }
  
      public void testSpecNonNullV3()
      {
          runCompareTest( "1.1", null, "1.1", null, VENDOR1, VENDOR2 );
      }
  
      public void testSpec1Null()
      {
          runCompareTest( null, null, "1.1", null, VENDOR2, VENDOR1 );
      }
  
      public void testSpec2Null()
      {
          runCompareTest( "1.1", null, null, null, VENDOR1, VENDOR2 );
      }
  
      public void testImplNull()
      {
          runCompareTest( "1.0", null, "1.0", null, VENDOR1, VENDOR2 );
      }
  
      public void testImplNonNullV1()
      {
          runCompareTest( "1.0", "1.1", "1.0", "1.0", VENDOR1, VENDOR2 );
      }
  
      public void testImplNonNullV2()
      {
          runCompareTest( "1.0", "1.0", "1.0", "1.1", VENDOR2, VENDOR1 );
      }
  
      public void testImplNonNullV3()
      {
          runCompareTest( "1.0", "1.1", "1.0", "1.1", VENDOR1, VENDOR2 );
      }
  
      public void testImpl1Null()
      {
          runCompareTest( "1.0", null, "1.0", "1.1", VENDOR2, VENDOR1 );
      }
  
      public void testImpl2Null()
      {
          runCompareTest( "1.0", "1.1", "1.0", null, VENDOR1, VENDOR2 );
      }
  
      private void runCompareTest( final String specVersion1,
                                   final String implVersion1,
                                   final String specVersion2,
                                   final String implVersion2,
                                   final String vendor1,
                                   final String vendor2 )
      {
          final ExtensionManager manager =
              createExtensionManager( specVersion1, implVersion1, specVersion2, implVersion2
);
          final OptionalPackage[] pkgs = getOptionalPackages( manager );
  
          assertEquals( "pkgs.length", 2, pkgs.length );
  
          final Extension extension1 = pkgs[ 0 ].getAvailableExtensions()[ 0 ];
          final Extension extension2 = pkgs[ 1 ].getAvailableExtensions()[ 0 ];
          assertEquals( "pkgs[0].vendor", vendor1, extension1.getImplementationVendor() );
          assertEquals( "pkgs[1].vendor", vendor2, extension2.getImplementationVendor() );
      }
  
      private OptionalPackage[] getOptionalPackages( final ExtensionManager manager )
      {
          return manager.getOptionalPackages( new Extension( NAME, null, null, null, null,
null, null ) );
      }
  
      private ExtensionManager createExtensionManager( final String specVersion1,
                                                       final String implVersion1,
                                                       final String specVersion2,
                                                       final String implVersion2 )
      {
          final OptionalPackage optionalPackage1 =
              createPackage( VENDOR1, specVersion1, implVersion1 );
          final OptionalPackage optionalPackage2 =
              createPackage( VENDOR2, specVersion2, implVersion2 );
          final OptionalPackage[] pkgs =
              new OptionalPackage[]{optionalPackage1, optionalPackage2};
          return createExtensionManager( pkgs );
      }
  
      private OptionalPackage createPackage( final String vendor,
                                             final String specVersion,
                                             final String implVersion )
      {
          final Extension extension = new Extension( NAME,
                                                     specVersion, null,
                                                     implVersion, vendor, null,
                                                     null );
          final File file = new File( "." );
          final Extension[] available = new Extension[]{extension};
          final Extension[] required = new Extension[ 0 ];
          return new OptionalPackage( file, available, required );
      }
  
      private ExtensionManager createExtensionManager( final OptionalPackage[] packages )
      {
          final TestExtensionManager manager =
              new TestExtensionManager( packages );
          return new DelegatingExtensionManager( new ExtensionManager[]{manager} );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/extension/src/test/org/apache/avalon/excalibur/packagemanager/impl/test/TestExtensionManager.java
  
  Index: TestExtensionManager.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.avalon.excalibur.packagemanager.impl.test;
  
  import org.apache.avalon.excalibur.packagemanager.ExtensionManager;
  import org.apache.avalon.excalibur.packagemanager.OptionalPackage;
  import org.apache.avalon.excalibur.extension.Extension;
  
  /**
   * a class to help test sorting of ExtensionManager.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/07 10:00:45 $
   */
  class TestExtensionManager
      implements ExtensionManager
  {
      private final OptionalPackage[] m_optionalPackages;
  
      public TestExtensionManager( final OptionalPackage[] optionalPackages )
      {
          m_optionalPackages = optionalPackages;
      }
  
      public OptionalPackage[] getOptionalPackages( final Extension extension )
      {
          return m_optionalPackages;
      }
  }
  
  
  

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


Mime
View raw message