* 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;
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
{
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 );
}
}
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;
}
}
/**
* ChecksummedFile
- *
+ * <p/>
* <p>Terminology:</p>
* <dl>
* <dt>Checksum File</dt>
* <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;
/**
throws IOException
{
- try (InputStream fis = Files.newInputStream( referenceFile.toPath() ) )
+ try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
{
Checksum checksum = new Checksum( checksumAlgorithm );
checksum.update( fis );
* <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>
}
return valid;
- } catch ( IOException e )
+ }
+ catch ( IOException e )
{
log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
return false;
return true;
}
-
try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
{
// Parse file once, for all checksums.
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" );
* Validate the expected path, and expected checksum algorithm, then return
* the trimmed checksum hex string.
* </p>
+ *
* @param rawChecksumString
* @param expectedHash
* @param expectedPath
<artifactId>xercesImpl</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<pluginManagement>
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
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() );
}
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() );
}
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
{
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
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();
}
}