From f5022a27218c62310e82b62e53de0c5dee78abf2 Mon Sep 17 00:00:00 2001
From: Olivier Lamy
Date: Mon, 23 Mar 2015 13:32:46 +1100
Subject: [PATCH] use 1.7 features for files
---
.../org/apache/archiva/checksum/Checksum.java | 22 ++++----
.../archiva/checksum/ChecksummedFile.java | 19 +++----
.../archiva-core-consumers/pom.xml | 5 ++
.../ArtifactMissingChecksumsConsumer.java | 14 ++---
.../ArtifactMissingChecksumsConsumerTest.java | 54 ++++++++++++-------
5 files changed, 68 insertions(+), 46 deletions(-)
diff --git a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/Checksum.java b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/Checksum.java
index e80925865..c83b9d433 100644
--- a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/Checksum.java
+++ b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/Checksum.java
@@ -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;
}
}
diff --git a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
index 366e16e0a..2cd0169f0 100644
--- a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
+++ b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
@@ -35,7 +35,7 @@ import java.util.regex.Pattern;
/**
* ChecksummedFile
- *
+ *
* Terminology:
*
* - Checksum File
@@ -46,13 +46,13 @@ import java.util.regex.Pattern;
* - Reference File
* - The file that is being referenced in the checksum file.
*
- *
- *
*/
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
*
* Given a checksum file, check to see if the file it represents is valid according to the checksum.
*
- *
+ *
*
* NOTE: Only supports single file checksums of type MD5 or SHA1.
*
@@ -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.
*
+ *
* @param rawChecksumString
* @param expectedHash
* @param expectedPath
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml
index 6bf6066e4..8c12f3fc3 100644
--- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml
+++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/pom.xml
@@ -145,6 +145,11 @@
xercesImpl
test
+
+ org.assertj
+ assertj-core
+ test
+
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java
index 70fb11933..3bc13fa41 100644
--- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java
+++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java
@@ -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() );
}
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java
index 41e64910a..8645ab01d 100644
--- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java
+++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java
@@ -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();
}
}
--
2.39.5