]> source.dussan.org Git - archiva.git/commitdiff
use 1.7 features for files
authorOlivier Lamy <olamy@apache.org>
Mon, 23 Mar 2015 02:32:46 +0000 (13:32 +1100)
committerOlivier Lamy <olamy@apache.org>
Mon, 23 Mar 2015 03:31:58 +0000 (14:31 +1100)
archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/Checksum.java
archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java

index e809258654e1eda892a7950d4baf83a5cc3a75a3..c83b9d4339e36fd3b95b6b1d5625f4789fcb293a 100644 (file)
@@ -19,6 +19,9 @@ package org.apache.archiva.checksum;
  * under the License.
  */
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.NullOutputStream;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.DigestInputStream;
@@ -26,13 +29,8 @@ import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.output.NullOutputStream;
-
 /**
- * Checksum - simple checksum hashing routines. 
- *
- *
+ * Checksum - simple checksum hashing routines.
  */
 public class Checksum
 {
@@ -67,8 +65,9 @@ public class Checksum
         catch ( NoSuchAlgorithmException e )
         {
             // Not really possible, but here none-the-less
-            throw new IllegalStateException( "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm()
-                + " : " + e.getMessage(), e );
+            throw new IllegalStateException(
+                "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm() + " : "
+                    + e.getMessage(), e );
         }
     }
 
@@ -96,9 +95,10 @@ public class Checksum
     public Checksum update( InputStream stream )
         throws IOException
     {
-        DigestInputStream dig = new DigestInputStream( stream, md );
-        IOUtils.copy( dig, new NullOutputStream() );
-
+        try (DigestInputStream dig = new DigestInputStream( stream, md ))
+        {
+            IOUtils.copy( dig, new NullOutputStream() );
+        }
         return this;
     }
 }
index 366e16e0ab7ce3f24ddacf88664fdf78f680f079..2cd0169f041b5e02137960865913592aa24038b6 100644 (file)
@@ -35,7 +35,7 @@ import java.util.regex.Pattern;
 
 /**
  * ChecksummedFile
- *
+ * <p/>
  * <p>Terminology:</p>
  * <dl>
  * <dt>Checksum File</dt>
@@ -46,13 +46,13 @@ import java.util.regex.Pattern;
  * <dt>Reference File</dt>
  * <dd>The file that is being referenced in the checksum file.</dd>
  * </dl>
- *
- *
  */
 public class ChecksummedFile
 {
     private final Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
 
+    private static final Pattern METADATA_PATTERN = Pattern.compile( "maven-metadata-\\S*.xml" );
+
     private final File referenceFile;
 
     /**
@@ -76,7 +76,7 @@ public class ChecksummedFile
         throws IOException
     {
 
-        try (InputStream fis = Files.newInputStream( referenceFile.toPath() ) )
+        try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
         {
             Checksum checksum = new Checksum( checksumAlgorithm );
             checksum.update( fis );
@@ -115,7 +115,7 @@ public class ChecksummedFile
      * <p>
      * Given a checksum file, check to see if the file it represents is valid according to the checksum.
      * </p>
-     *
+     * <p/>
      * <p>
      * NOTE: Only supports single file checksums of type MD5 or SHA1.
      * </p>
@@ -199,7 +199,8 @@ public class ChecksummedFile
             }
 
             return valid;
-        } catch ( IOException e )
+        }
+        catch ( IOException e )
         {
             log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
             return false;
@@ -228,7 +229,6 @@ public class ChecksummedFile
             return true;
         }
 
-
         try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
         {
             // Parse file once, for all checksums.
@@ -281,8 +281,8 @@ public class ChecksummedFile
     private boolean isValidChecksumPattern( String filename, String path )
     {
         // check if it is a remote metadata file
-        Pattern pattern = Pattern.compile( "maven-metadata-\\S*.xml" );
-        Matcher m = pattern.matcher( path );
+
+        Matcher m = METADATA_PATTERN.matcher( path );
         if ( m.matches() )
         {
             return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
@@ -297,6 +297,7 @@ public class ChecksummedFile
      * Validate the expected path, and expected checksum algorithm, then return
      * the trimmed checksum hex string.
      * </p>
+     *
      * @param rawChecksumString
      * @param expectedHash
      * @param expectedPath
index 6bf6066e439ecf7b2f020f9ca0c5c46928b05ab5..8c12f3fc3fec66c3120fe354d4a5a7652c432ce1 100644 (file)
       <artifactId>xercesImpl</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.assertj</groupId>
+      <artifactId>assertj-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <pluginManagement>
index 70fb11933c9c159b0385ebfaf1302f1407e951a9..3bc13fa415507a8a87c93b16b017ffe460c12d89 100644 (file)
@@ -141,8 +141,8 @@ public class ArtifactMissingChecksumsConsumer
     public void processFile( String path )
         throws ConsumerException
     {
-        createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
-        createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
+        createFixChecksum( path, ChecksumAlgorithm.SHA1 );
+        createFixChecksum( path, ChecksumAlgorithm.MD5 );
     }
 
     @Override
@@ -152,19 +152,19 @@ public class ArtifactMissingChecksumsConsumer
         processFile( path );
     }
 
-    private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
+    private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
     {
         File artifactFile = new File( this.repositoryDir, path );
-        File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
+        File checksumFile = new File( this.repositoryDir, path  + checksumAlgorithm.getExt() );
 
         if ( checksumFile.exists() )
         {
             checksum = new ChecksummedFile( artifactFile );
             try
             {
-                if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
+                if ( !checksum.isValidChecksum( checksumAlgorithm ) )
                 {
-                    checksum.fixChecksums( checksumAlgorithm );
+                    checksum.fixChecksums( new ChecksumAlgorithm[]{ checksumAlgorithm } );
                     log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath() );
                     triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
                 }
@@ -181,7 +181,7 @@ public class ArtifactMissingChecksumsConsumer
             checksum = new ChecksummedFile( artifactFile );
             try
             {
-                checksum.createChecksum( checksumAlgorithm[0] );
+                checksum.createChecksum( checksumAlgorithm );
                 log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath() ); 
                 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
             }
index 41e64910ada4b7f188c395866316054bfd7dc06a..8645ab01ddbf8ecc592385c7fc115a5824bd6ee1 100644 (file)
@@ -1,16 +1,20 @@
 package org.apache.archiva.consumers.core;
 
-import java.io.File;
-import java.util.Calendar;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.checksum.ChecksumAlgorithm;
 import org.apache.archiva.checksum.ChecksummedFile;
 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
 import org.apache.commons.io.FileUtils;
-import static org.junit.Assert.*;
+import org.assertj.core.api.Assertions;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Calendar;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -58,21 +62,26 @@ public class ArtifactMissingChecksumsConsumerTest
     {
         String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
 
-        File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
-        File md5File = new File( repoConfig.getLocation(), path + ".md5" );
+        Path sha1Path = Paths.get( repoConfig.getLocation(),
+                                   path + ".sha1" );// new File( repoConfig.getLocation(), path + ".sha1" );
+        Path md5FilePath =
+            Paths.get( repoConfig.getLocation(), path + ".md5" );// new File( repoConfig.getLocation(), path + ".md5" );
+
+        Files.deleteIfExists( sha1Path );
+        Files.deleteIfExists( md5FilePath );
 
-        sha1File.delete();
-        md5File.delete();
+        //sha1File.delete();
+        //md5File.delete();
 
-        assertFalse( sha1File.exists() );
-        assertFalse( md5File.exists() );
+        Assertions.assertThat( sha1Path.toFile() ).doesNotExist();// assertFalse( sha1File.exists() );
+        Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();// assertFalse( md5File.exists() );
 
         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
 
         consumer.processFile( path );
 
-        assertTrue( sha1File.exists() );
-        assertTrue( md5File.exists() );
+        Assertions.assertThat( sha1Path.toFile() ).exists();// assertTrue( sha1File.exists() );
+        Assertions.assertThat( md5FilePath.toFile() ).exists();//assertTrue( md5File.exists() );
     }
 
     @Test
@@ -86,21 +95,28 @@ public class ArtifactMissingChecksumsConsumerTest
 
         String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
 
-        File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
-        File md5File = new File( repoConfig.getLocation(), path + ".md5" );
+        // new File( repoConfig.getLocation(), path + ".sha1" );
+        Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
+
+        //new File( repoConfig.getLocation(), path + ".md5" );
+        Path md5Path = Paths.get( repoConfig.getLocation(), path + ".md5" );
 
         ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
 
-        assertTrue( sha1File.exists() );
-        assertTrue( md5File.exists() );
-        assertFalse( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
+        Assertions.assertThat( sha1Path.toFile() ).exists();
+        Assertions.assertThat( md5Path.toFile() ).exists();
+        Assertions.assertThat(
+            checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
+            .isFalse();
 
         consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
 
         consumer.processFile( path );
 
-        assertTrue( sha1File.exists() );
-        assertTrue( md5File.exists() );
-        assertTrue( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );        
+        Assertions.assertThat( sha1Path.toFile() ).exists();
+        Assertions.assertThat( md5Path.toFile() ).exists();
+        Assertions.assertThat(
+            checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
+            .isTrue();
     }
 }