maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mic...@apache.org
Subject cvs commit: maven-wagon/wagon-api/src/test/java/org/apache/maven/wagon LazyFileOutputStreamTest.java
Date Wed, 26 May 2004 16:47:14 GMT
michal      2004/05/26 09:47:14

  Modified:    wagon-api/src/test/java/org/apache/maven/wagon/events
                        TransferEventTest.java
               wagon-api/src/main/java/org/apache/maven/wagon
                        AbstractWagon.java
               wagon-api/src/main/java/org/apache/maven/wagon/events
                        TransferEvent.java
               wagon-api/src/main/java/org/apache/maven/wagon/observers
                        Debug.java
  Added:       wagon-api/src/test/resources/repository maven-test-b-1.0.jar
                        maven-test-a-1.0.jar.md5 maven-test-a-1.0.jar
               wagon-api/src/main/java/org/apache/maven/wagon/observers
                        ChecksumObserver.java
               wagon-api/src/test/java/org/apache/maven/wagon/observers
                        ChecksumObserverTest.java
               wagon-api/src/test/java/org/apache/maven/wagon
                        LazyFileOutputStreamTest.java
  Removed:     wagon-api/src/main/java/org/apache/maven/wagon/observers
                        Md5SumObserver.java
  Log:
  I am chaning env from eclipse + win to linux + idea. I am commiting all modified classes
and I will clean up remaing things in next few hours
  
  Revision  Changes    Path
  1.1                  maven-wagon/wagon-api/src/test/resources/repository/maven-test-b-1.0.jar
  
  	<<Binary file>>
  
  
  1.1                  maven-wagon/wagon-api/src/test/resources/repository/maven-test-a-1.0.jar.md5
  
  Index: maven-test-a-1.0.jar.md5
  ===================================================================
  a5c3b5cd0605bb7331d1846cdc14b13b
  
  
  
  1.1                  maven-wagon/wagon-api/src/test/resources/repository/maven-test-a-1.0.jar
  
  	<<Binary file>>
  
  
  1.2       +2 -4      maven-wagon/wagon-api/src/test/java/org/apache/maven/wagon/events/TransferEventTest.java
  
  Index: TransferEventTest.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/test/java/org/apache/maven/wagon/events/TransferEventTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TransferEventTest.java	30 Mar 2004 14:43:05 -0000	1.1
  +++ TransferEventTest.java	26 May 2004 16:47:13 -0000	1.2
  @@ -126,9 +126,7 @@
           }
   
   
  -        event.setProgress( ( long ) 10 );
  -        assertEquals( ( long ) 10, event.getProgress() );
  -
  +        
       }
   
       public void testConstantValueConflict()
  
  
  
  1.4       +14 -9     maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/AbstractWagon.java
  
  Index: AbstractWagon.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/AbstractWagon.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractWagon.java	21 May 2004 16:48:50 -0000	1.3
  +++ AbstractWagon.java	26 May 2004 16:47:13 -0000	1.4
  @@ -144,23 +144,28 @@
       protected void transfer( String resource, InputStream input, OutputStream output, int
requestType )
           throws IOException
       {
  -        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
  -
  -        int n;
  +        byte[] buffer = new byte[ DEFAULT_BUFFER_SIZE ];
   
           TransferEvent transferEvent = new TransferEvent( this, resource, TransferEvent.TRANSFER_PROGRESS,
requestType );
   
  -        while ( -1 != ( n = input.read( buffer ) ) )
  +        while ( true )                                               
           {
  +            int n = input.read( buffer ) ;
  +            
  +            if ( n == -1 )
  +            {
  +               break;    
  +            }
  +            
               // @todo probably new event should be created!!
  -
  -            transferEvent.setProgress( n );
  -
  +            
               transferEvent.setData( buffer, n );
   
  +            fireTransferProgress( transferEvent );
  +            
               output.write( buffer, 0, n );
   
  -            fireTransferProgress( transferEvent );
  +            
           }
       }
   
  
  
  
  1.5       +2 -17     maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/events/TransferEvent.java
  
  Index: TransferEvent.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/events/TransferEvent.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TransferEvent.java	21 May 2004 16:48:50 -0000	1.4
  +++ TransferEvent.java	26 May 2004 16:47:13 -0000	1.5
  @@ -199,22 +199,7 @@
           this.resource = resource;
       }
   
  -    /**
  -     * @param progress 
  -     */
  -    public void setProgress( final long progress )
  -    {
  -
  -        this.progress = progress;
  -    }
  -
  -    /**
  -     * @return Returns the progress.
  -     */
  -    public long getProgress()
  -    {
  -        return progress;
  -    }
  +    
   
   
   
  
  
  
  1.2       +2 -2      maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers/Debug.java
  
  Index: Debug.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers/Debug.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Debug.java	21 May 2004 16:48:50 -0000	1.1
  +++ Debug.java	26 May 2004 16:47:14 -0000	1.2
  @@ -171,7 +171,7 @@
           out.print( "#" );
           //String data = new String( transferEvent.getData(),0, transferEvent.getDataLength());
           //out.println(data);
  -        transfer += transferEvent.getProgress();
  +        transfer += transferEvent.getDataLength();
       }
   
       /**
  
  
  
  1.1                  maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/observers/ChecksumObserver.java
  
  Index: ChecksumObserver.java
  ===================================================================
  package org.apache.maven.wagon.observers;
  
  import java.io.File;
  import java.io.InputStream;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.util.HashMap;
  import java.util.Map;
  
  import org.apache.maven.wagon.Wagon;
  import org.apache.maven.wagon.WagonUtils;
  import org.apache.maven.wagon.events.TransferEvent;
  import org.apache.maven.wagon.events.TransferListener;
  import org.codehaus.plexus.util.FileUtils;
  import org.codehaus.plexus.util.IOUtil;
  
  
  /**
   * 
   * TransferListeners which computes MD5 checksum on the fly when files are transfered.
   * 
   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
   * @version $Id: ChecksumObserver.java,v 1.1 2004/05/26 16:47:14 michal Exp $ 
   */
  public class ChecksumObserver implements TransferListener
  {
      
      private String algorithm;
          
      private MessageDigest digester;
      
      private String expectedChecksum;
      
      private String actualChecksum;
      
      private boolean running = false;
      
      
      private static Map algorithmExtensionMap = new HashMap();
      
      static
      {
         algorithmExtensionMap.put( "MD5", ".md5" );  
         
         algorithmExtensionMap.put( "MD2", ".md2" );
         
         algorithmExtensionMap.put( "SHA-1", ".sha1" );
         
      }
      
      
      public ChecksumObserver()
      {
         this( "MD5" );    
      }
      
      /**
       * 
       * @param algorithm One of the algorithms supported by JDK: MD5, MD2 or SHA-1
       */
      public ChecksumObserver( String algorithm )
      {
           this.algorithm = algorithm;    
      }
      
      /**
       * @see org.apache.maven.wagon.events.TransferListener#transferStarted(org.apache.maven.wagon.events.TransferEvent)
       */
      public void transferStarted( TransferEvent transferEvent )
      {
          
          
          if ( running )
          {
             return;    
          }
          
          expectedChecksum = null;
          
          actualChecksum = null;
          
          
          try
          {
              digester = MessageDigest.getInstance( algorithm );
          }
          catch ( NoSuchAlgorithmException e)
          {
           
          }
          
      }
  
      /**
       * @see org.apache.maven.wagon.events.TransferListener#transferProgress(org.apache.maven.wagon.events.TransferEvent)
       */
      public void transferProgress( TransferEvent transferEvent )
      {
          if ( digester != null )
          {
             byte[] data = transferEvent.getData();
                                  
             int len = transferEvent.getDataLength();
             
             digester.update( data, 0, len );
             
          }                
          
      }
  
      public void transferCompleted( TransferEvent transferEvent )
      {
          
          if ( digester == null )
          {        
              return;
          }
          
          
              
          Wagon wagon = transferEvent.getWagon();
          
          actualChecksum = encode ( digester.digest() );
          
          digester = null;
          
          InputStream inputStream = null;
          
          running = true;
          
          try
          {
              int type = transferEvent.getRequestType();
                                      
              String resource = transferEvent.getResource();
              
              String extension = ( String ) algorithmExtensionMap.get( algorithm );
              
              if ( type  == TransferEvent.REQUEST_GET )
              {
                  
                  //we will fetch md5 cheksum from server and
                  // read its content into memory
                  File artifactFile = transferEvent.getLocalFile();
                  
                  File md5File = new File( artifactFile.getPath() + extension );
                  
                  String  md5Resource = resource + extension;
                  
                  wagon.get( md5Resource, md5File );
                 
                  expectedChecksum = FileUtils.fileRead( md5File  ).trim();              

              }
              else
              {
                  //It's PUT put request we will also put md5 checksum
                  // which was computed on the fly
                  WagonUtils.fromString( resource + extension , wagon, actualChecksum );
                  
              }            
              
          }
          catch ( Exception e )
          {
              // ignore it. Observers should not throw any exceptions
          }    
          finally
          {
              if ( inputStream != null )
              {
                   IOUtil.close( inputStream );
              }            
              
              running = false;
          }
          
              
      }
  
      public void transferError( TransferEvent transferEvent )
      { 
          digester = null;  
      }
  
      public void debug( String message )
      {
         
          
      }
      
      
      
      /**
       * Returns the md5 checksum downloaded from the server
       *   
       * @return
       */
      public String getExpectedChecksum() 
      {       
         return expectedChecksum;
      }
     
      
      /**
       * Returns md5 checksum which was computed during transfer
       * @return
       */
      public String getActualChecksum() 
      {       
         return actualChecksum;
      }
      
      
      /**
       * Encodes a 128 bit (16 bytes) byte array into a 32 character String.
       * XXX I think it should at least throw an IllegalArgumentException rather than return
null
       *
       * @param binaryData Array containing the digest
       * @return Encoded hex string, or null if encoding failed
       */
      protected String encode( byte[] binaryData )
      {
          if ( binaryData.length != 16 )
          {
              return null;
          }
  
          String retValue = "";
  
          for ( int i = 0; i < 16; i++ )
          {
              String t = Integer.toHexString( binaryData[i] & 0xff );
  
              if ( t.length() == 1 )
              {
                  retValue += ( "0" + t );
              }
              else
              {
                  retValue += t;
              }
          }
  
          return retValue.trim();
      }
      
      
      public boolean cheksumIsValid()
      {
          boolean retValue = false;
          
          if ( actualChecksum != null && expectedChecksum !=null )
          {
               retValue = actualChecksum.equals( expectedChecksum );
          }
          
          return retValue;
      }
  
     
      
      
      
      
  }
  
  
  
  1.1                  maven-wagon/wagon-api/src/test/java/org/apache/maven/wagon/observers/ChecksumObserverTest.java
  
  Index: ChecksumObserverTest.java
  ===================================================================
  package org.apache.maven.wagon.observers;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import org.apache.maven.wagon.providers.file.FileWagon;
  import org.apache.maven.wagon.repository.Repository;
  
  import java.io.File;
  
  import junit.framework.TestCase;
  
  /**
   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka </a>
   * @version $Id: ChecksumObserverTest.java,v 1.1 2004/05/26 16:47:14 michal Exp $
   */
  public class ChecksumObserverTest extends TestCase
  {
  
      public void testMd5SumObserver()
      {
          try
          {
              FileWagon wagon = new FileWagon();
  
              ChecksumObserver observer = new ChecksumObserver( );
  
              String url = "file://" + System.getProperty( "basedir" )
                      + "/target/test-classes/repository";
  
              Repository repository = new Repository( );
              
              repository.setUrl( url );
  
              wagon.connect( repository );
              
              File temp = new File( System.getProperty( "basedir" ), "target/test-output"
);
              
              temp.mkdirs();
              
              File dest = new File ( temp, "maven-test-a-1.0.jar" );
                          
              wagon.addTransferListener( observer );
                                     
              wagon.get( "maven-test-a-1.0.jar", dest );
              
              File destMd5 = new File ( temp, "maven-test-a-1.0.jar.md5" );
              
              assertTrue( destMd5.exists()  );
              
              assertEquals(  observer.getExpectedChecksum(), observer.getActualChecksum()
);
              
              assertTrue( observer.cheksumIsValid()  );
              
              wagon.get( "maven-test-b-1.0.jar", dest );
              
              assertFalse( observer.cheksumIsValid()  );
              
          }       
          catch ( Exception e )
          {            
              e.printStackTrace();
              
              fail( e.getMessage() );
          }
          
  
      }
  
  }
  
  
  1.1                  maven-wagon/wagon-api/src/test/java/org/apache/maven/wagon/LazyFileOutputStreamTest.java
  
  Index: LazyFileOutputStreamTest.java
  ===================================================================
  package org.apache.maven.wagon;
  
  import org.apache.maven.wagon.LazyFileOutputStream;
  import org.codehaus.plexus.util.FileUtils;
  
  import java.io.File;
  
  import junit.framework.TestCase;
  
  /**
   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a> 
   * @version $Id: LazyFileOutputStreamTest.java,v 1.1 2004/05/26 16:47:14 michal Exp $ 
   */
  public class LazyFileOutputStreamTest extends TestCase
  {
  
       public void testFileCreation()
       {
           try
           {
               File file = File.createTempFile( "wagon", "tmp" );
               
               file.deleteOnExit();
               
               assertFalse( file.exists() );
               
               LazyFileOutputStream stream = new LazyFileOutputStream( file );
               
               assertFalse( file.exists() );
               
               String exptected = "michal";
               
               stream.write( exptected.getBytes() );
               
               stream.close();
               
               assertTrue( file.exists() );
               
               String actual = FileUtils.fileRead( file );
               
               assertEquals( exptected, actual );
               
               
           }
           catch( Exception e )
           {
               e.printStackTrace();
               
               fail( e.getMessage() );    
           }
       
       }
       
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org


Mime
View raw message