jdcasey 2005/04/19 17:44:17 Modified: sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/digest ArtifactDigestVerifier.java ArtifactDigestor.java sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean TestSupport.java Added: sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean/digest ArtifactDigestVerifierTest.java ArtifactDigestorTest.java sandbox/repoclean/src/test/resources/digest/ArtifactDigestorTest digestFormatVerifyArtifact.jar Log: o Added creation of SHA1 digests alongside MD5's o Modified digest-file creation to be text files, not binaries. PR: MNG-311, MNG-287 Revision Changes Path 1.6 +26 -11 maven-components/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestVerifier.java Index: ArtifactDigestVerifier.java =================================================================== RCS file: /home/cvs/maven-components/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestVerifier.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ArtifactDigestVerifier.java 19 Apr 2005 20:05:15 -0000 1.5 +++ ArtifactDigestVerifier.java 20 Apr 2005 00:44:17 -0000 1.6 @@ -18,7 +18,8 @@ */ import org.apache.maven.artifact.Artifact; -import org.apache.maven.tools.repoclean.report.FileReporter; +import org.apache.maven.tools.repoclean.report.ReportWriteException; +import org.apache.maven.tools.repoclean.report.Reporter; import org.codehaus.plexus.util.FileUtils; import java.io.File; @@ -33,22 +34,36 @@ public static final String ROLE = ArtifactDigestVerifier.class.getName(); private ArtifactDigestor artifactDigestor; + + public void setArtifactDigestor(ArtifactDigestor artifactDigestor) + { + this.artifactDigestor = artifactDigestor; + } + + public void verifyDigest( Artifact artifact, File artifactTarget, Reporter reporter, boolean reportOnly ) + throws ArtifactDigestException, ReportWriteException, IOException + { + verifyDigestFile( artifact, artifactTarget, reporter, reportOnly, ".md5", ArtifactDigestor.MD5 ); + + verifyDigestFile( artifact, artifactTarget, reporter, reportOnly, ".sha1", ArtifactDigestor.SHA ); + } - public void verifyDigest( Artifact artifact, File artifactTarget, FileReporter reporter, boolean reportOnly ) throws Exception + private void verifyDigestFile( Artifact artifact, File artifactTarget, Reporter reporter, boolean reportOnly, + String digestExt, String digestAlgorithm ) + throws ArtifactDigestException, ReportWriteException, IOException { // create the digest source file from which to copy/verify. - File digestSourceFile = new File( artifact.getFile() + ".md5" ); + File digestSourceFile = new File( artifact.getFile() + digestExt ); // create the digest target file from which to copy/create. - File digestTargetFile = new File( artifactTarget + ".md5" ); - + File digestTargetFile = new File( artifactTarget + digestExt ); + boolean verified = false; // if the digest source file exists, then verify it. if ( digestSourceFile.exists() ) { - verified = artifactDigestor.verifyArtifactDigest( artifactTarget, digestTargetFile, - ArtifactDigestor.MD5 ); + verified = artifactDigestor.verifyArtifactDigest( artifactTarget, digestTargetFile, digestAlgorithm ); if ( verified ) { @@ -61,7 +76,7 @@ catch ( IOException e ) { reporter.error( "Cannot copy digest file for artifact[" + artifact.getId() - + "] from source to target.", e ); + + "] from source to target for digest algorithm: \'" + digestAlgorithm + "\'.", e ); throw e; } @@ -69,12 +84,12 @@ } else { - reporter.warn( ".md5 for artifact[" + artifact.getId() + "] in target repository is wrong." ); + reporter.warn( digestExt + " for artifact[" + artifact.getId() + "] in target repository is wrong." ); } } else { - reporter.warn( ".md5 for artifact[" + artifact.getId() + "] is missing in source repository." ); + reporter.warn( digestExt + " for artifact[" + artifact.getId() + "] is missing in source repository." ); } // if the .md5 was missing or did not verify correctly, create a new one @@ -83,7 +98,7 @@ { if ( !reportOnly ) { - artifactDigestor.createArtifactDigest( artifactTarget, digestTargetFile, ArtifactDigestor.MD5 ); + artifactDigestor.createArtifactDigest( artifactTarget, digestTargetFile, digestAlgorithm ); } } } 1.4 +24 -19 maven-components/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestor.java Index: ArtifactDigestor.java =================================================================== RCS file: /home/cvs/maven-components/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestor.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ArtifactDigestor.java 2 Apr 2005 02:17:19 -0000 1.3 +++ ArtifactDigestor.java 20 Apr 2005 00:44:17 -0000 1.4 @@ -6,10 +6,10 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; +import java.io.Writer; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -43,24 +43,24 @@ public boolean verifyArtifactDigest( File artifactFile, File digestFile, String algorithm ) throws ArtifactDigestException { - if(artifactFile.exists() && digestFile.exists()) + if ( artifactFile.exists() && digestFile.exists() ) { byte[] generatedDigest = generateArtifactDigest( artifactFile, algorithm ); - + InputStream in = null; try { in = new FileInputStream( artifactFile ); - + int digestLen = generatedDigest.length; int currentIdx = 0; - + boolean matched = true; - + int read = -1; while ( ( read = in.read() ) > -1 ) { - if(currentIdx >= digestLen || read != generatedDigest[currentIdx]) + if ( currentIdx >= digestLen || read != generatedDigest[currentIdx] ) { return false; } @@ -68,23 +68,25 @@ } catch ( IOException e ) { - throw new ArtifactDigestException("Cannot verify digest for artifact file: \'" + artifactFile + "\' against digest file: \'" + digestFile + "\' using algorithm: \'" + algorithm + "\'", e); + throw new ArtifactDigestException( "Cannot verify digest for artifact file: \'" + artifactFile + + "\' against digest file: \'" + digestFile + "\' using algorithm: \'" + algorithm + "\'", e ); } finally { IOUtil.close( in ); } - + } else { return false; } - + return true; } - - private byte[] generateArtifactDigest( File artifactFile, String algorithm ) throws ArtifactDigestException + + public byte[] generateArtifactDigest( File artifactFile, String algorithm ) + throws ArtifactDigestException { MessageDigest digest = null; try @@ -105,7 +107,7 @@ int read = -1; while ( ( read = in.read( buffer ) ) > -1 ) { - digest.update(buffer, 0, read); + digest.update( buffer, 0, read ); } } catch ( IOException e ) @@ -120,14 +122,17 @@ return digest.digest(); } - private void writeDigestFile( File digestFile, byte[] digestData ) throws IOException + private void writeDigestFile( File digestFile, byte[] digestData ) + throws IOException { - OutputStream out = null; + Writer out = null; try { - out = new FileOutputStream( digestFile ); - out.write( digestData ); - out.flush(); + out = new FileWriter( digestFile ); + for ( int i = 0; i < digestData.length; i++ ) + { + out.write( Integer.toHexString( digestData[i] ) ); + } } finally { 1.1 maven-components/sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestVerifierTest.java Index: ArtifactDigestVerifierTest.java =================================================================== package org.apache.maven.tools.repoclean.digest; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.tools.repoclean.TestSupport; import org.apache.maven.tools.repoclean.report.DummyReporter; import org.codehaus.plexus.PlexusTestCase; import java.io.File; /* * Copyright 2001-2005 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. */ public class ArtifactDigestVerifierTest extends PlexusTestCase { public void testShouldWriteBothMD5AndSHA1DigestFiles() throws Exception { ArtifactDigestVerifier verifier = (ArtifactDigestVerifier) lookup( ArtifactDigestVerifier.ROLE ); Artifact artifact = new DefaultArtifact("testGroup", "testArtifact", "1.0", "jar"); File artifactFile = TestSupport.getResource("digest/ArtifactDigestorTest/digestFormatVerifyArtifact.jar"); artifact.setFile(artifactFile); File tempFile = File.createTempFile("artifactDigestFileVerifyBase", "jar"); File md5 = new File(tempFile + ".md5"); File sha1 = new File(tempFile + ".sha1"); System.out.println("[INFO] We expect warnings for missing source digest files here:"); verifier.verifyDigest(artifact, tempFile, new DummyReporter(), false); System.out.println("[INFO] Target digest files should have been created."); assertTrue(md5.exists()); assertTrue(sha1.exists()); } } 1.1 maven-components/sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean/digest/ArtifactDigestorTest.java Index: ArtifactDigestorTest.java =================================================================== package org.apache.maven.tools.repoclean.digest; import org.apache.maven.tools.repoclean.TestSupport; import java.io.File; import java.io.FileReader; import java.util.Arrays; import junit.framework.TestCase; /* * Copyright 2001-2005 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. */ public class ArtifactDigestorTest extends TestCase { private static final String DIGEST_FORMAT_VERIFY_ARTIFACT = "digestFormatVerifyArtifact.jar"; public void testShouldWriteDigestFileInHexNotBinary() throws Exception { ArtifactDigestor digestor = new ArtifactDigestor(); File artifact = TestSupport.getMyResource(this, DIGEST_FORMAT_VERIFY_ARTIFACT); byte[] rawDigest = digestor.generateArtifactDigest( artifact, ArtifactDigestor.MD5 ); StringBuffer rawConverted = new StringBuffer(rawDigest.length * 2); for ( int i = 0; i < rawDigest.length; i++ ) { rawConverted.append(Integer.toHexString(rawDigest[i])); } File digestFile = File.createTempFile("repoclean-artifactDigest-formatTest", ".md5"); digestor.createArtifactDigest( artifact, digestFile, ArtifactDigestor.MD5 ); FileReader reader = new FileReader(digestFile); StringBuffer written = new StringBuffer(rawDigest.length * 2); char[] cbuf = new char[rawDigest.length * 2]; int read = -1; while((read = reader.read(cbuf)) > -1) { written.append(cbuf, 0, read); } reader.close(); assertEquals(rawConverted.length(), written.length()); cbuf = new char[written.length()]; char[] cbuf2 = new char[cbuf.length]; written.getChars(0, cbuf.length, cbuf, 0); rawConverted.getChars(0, cbuf2.length, cbuf2, 0); assertTrue(Arrays.equals(cbuf, cbuf2)); } } 1.2 +42 -18 maven-components/sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean/TestSupport.java Index: TestSupport.java =================================================================== RCS file: /home/cvs/maven-components/sandbox/repoclean/src/test/java/org/apache/maven/tools/repoclean/TestSupport.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestSupport.java 19 Apr 2005 20:05:15 -0000 1.1 +++ TestSupport.java 20 Apr 2005 00:44:17 -0000 1.2 @@ -21,39 +21,63 @@ public final class TestSupport { - + + private static final String REPO_SUBDIR = "repo/"; + private static final String REPO_MARKER = "repo-marker.txt"; - private static final int MY_PACKAGE_TRIM = TestSupport.class.getPackage().getName().length() + 1; - + + private static final int PACKAGE_TRIM = TestSupport.class.getPackage().getName().length() + 1; + private TestSupport() { } - - public static String getMyRepositoryPath(Object testInstance) + + public static String getMyRepositoryPath( Object testInstance ) { Class testClass = testInstance.getClass(); - - String myRepo = testClass.getName().substring(MY_PACKAGE_TRIM); - - return getRepositoryPath(myRepo); + + String myRepo = testClass.getName().substring( PACKAGE_TRIM ); + + return getRepositoryPath( myRepo ); + } + + public static File getMyResource( Object testInstance, String relativePath ) + { + Class testClass = testInstance.getClass(); + + String myPath = testClass.getName().substring( PACKAGE_TRIM ); + + String resource = myPath.replace( '.', '/' ); + + if ( !relativePath.startsWith( "/" ) ) + { + resource += "/"; + } + + resource += relativePath; + + return getResource( resource ); } public static String getRepositoryPath( String relativePath ) { - String base = relativePath.replace('.', '/'); - - if(!base.endsWith("/")) + String base = relativePath.replace( '.', '/' ); + + if ( !base.endsWith( "/" ) ) { base += "/"; } - + + return getResource( base + REPO_SUBDIR + REPO_MARKER ).getParentFile().getAbsolutePath(); + } + + public static File getResource( String relativePath ) + { ClassLoader cloader = Thread.currentThread().getContextClassLoader(); - URL repoMarkerResource = cloader.getResource(base + REPO_MARKER); - - File repoMarker = new File(repoMarkerResource.getPath()).getAbsoluteFile(); - - return repoMarker.getParentFile().getPath(); + URL resource = cloader.getResource( relativePath ); + + return new File( resource.getPath() ).getAbsoluteFile(); } } 1.1 maven-components/sandbox/repoclean/src/test/resources/digest/ArtifactDigestorTest/digestFormatVerifyArtifact.jar <<Binary file>>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]