1 package org.apache.maven.repository.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.codehaus.plexus.util.FileUtils;
21 import java.io.BufferedOutputStream;
22 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.io.OutputStreamWriter;
32 import java.security.MessageDigest;
33 import java.security.NoSuchAlgorithmException;
34 import java.util.jar.JarEntry;
35 import java.util.jar.JarOutputStream;
38 * This class creates the artifact and metadata files used for testing the ChecksumArtifactReporter.
39 * It is extended by ChecksumArtifactReporterTest class.
41 public abstract class AbstractChecksumArtifactReporterTest
42 extends AbstractRepositoryReportsTestCase
44 protected static final String[] validArtifactChecksumJars = {"validArtifact-1.0"};
46 protected static final String[] invalidArtifactChecksumJars = {"invalidArtifact-1.0"};
48 protected static final String metadataChecksumFilename = "maven-metadata-repository";
50 public AbstractChecksumArtifactReporterTest()
60 public void tearDown()
67 * Create checksum files.
69 * @param type The type of checksum file to be created.
72 protected boolean createChecksumFile( String type )
74 boolean written = true;
76 //loop through the valid artifact names..
77 if ( "VALID".equals( type ) )
79 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
81 written = writeChecksumFile( "checksumTest/", validArtifactChecksumJars[i], "jar", true );
82 if ( written == false )
84 i = validArtifactChecksumJars.length;
88 else if ( "INVALID".equals( type ) )
90 for ( int i = 0; i < invalidArtifactChecksumJars.length; i++ )
92 written = writeChecksumFile( "checksumTest/", invalidArtifactChecksumJars[i], "jar", false );
93 if ( written == false )
95 i = invalidArtifactChecksumJars.length;
104 * Create checksum files for metadata.
106 * @param type The type of checksum to be created. (Valid or invalid)
109 protected boolean createMetadataFile( String type )
111 boolean written = true;
113 //loop through the valid artifact names..
114 if ( "VALID".equals( type ) )
116 writeMetadataFile( "checksumTest/validArtifact/1.0/", metadataChecksumFilename, "xml", true );
117 writeMetadataFile( "checksumTest/validArtifact/", metadataChecksumFilename, "xml", true );
118 writeMetadataFile( "checksumTest/", metadataChecksumFilename, "xml", true );
121 else if ( "INVALID".equals( type ) )
123 writeMetadataFile( "checksumTest/invalidArtifact/1.0/", metadataChecksumFilename, "xml", false );
130 * Create artifact together with its checksums.
132 * @param relativePath The groupId
133 * @param filename The filename of the artifact to be created.
134 * @param type The file type (JAR)
135 * @param isValid Indicates whether the checksum to be created is valid or not.
138 private boolean writeChecksumFile( String relativePath, String filename, String type, boolean isValid )
140 //System.out.println( " " );
141 //System.out.println( "========================= ARTIFACT CHECKSUM ==================================" );
143 //Initialize variables for creating jar files
144 FileOutputStream f = null;
145 JarOutputStream out = null;
146 String repoUrl = super.repository.getBasedir();
149 String dirs = filename.replace( '-', '/' );
150 //create the group level directory of the artifact
151 File dirFiles = new File( repoUrl + relativePath + dirs );
153 if ( dirFiles.mkdirs() )
157 f = new FileOutputStream( repoUrl + relativePath + dirs + "/" + filename + "." + type );
158 out = new JarOutputStream( new BufferedOutputStream( f ) );
161 String filename1 = repoUrl + relativePath + dirs + "/sample.txt";
162 boolean bool = createSampleFile( filename1 );
164 BufferedReader in = new BufferedReader( new FileReader( filename1 ) );
165 out.putNextEntry( new JarEntry( filename1 ) );
167 while ( ( c = in.read() ) != -1 )
174 //Create md5 and sha-1 checksum files..
175 byte[] md5chk = createChecksum( repoUrl + relativePath + dirs + "/" + filename + "." + type, "MD5" );
176 byte[] sha1chk = createChecksum( repoUrl + relativePath + dirs + "/" + filename + "." + type, "SHA-1" );
177 //System.out.println( "----- CREATED MD5 checksum ::: " + byteArrayToHexStr( md5chk ) );
178 //System.out.println( "----- CREATED SHA-1 checksum ::: " + byteArrayToHexStr( sha1chk ) );
182 if ( md5chk != null )
184 file = new File( repoUrl + relativePath + dirs + "/" + filename + "." + type + ".md5" );
185 OutputStream os = new FileOutputStream( file );
186 OutputStreamWriter osw = new OutputStreamWriter( os );
189 osw.write( byteArrayToHexStr( md5chk ) + "1" );
193 osw.write( byteArrayToHexStr( md5chk ) );
198 if ( sha1chk != null )
200 file = new File( repoUrl + relativePath + dirs + "/" + filename + "." + type + ".sha1" );
201 OutputStream os = new FileOutputStream( file );
202 OutputStreamWriter osw = new OutputStreamWriter( os );
205 osw.write( byteArrayToHexStr( sha1chk ) + "2" );
209 osw.write( byteArrayToHexStr( sha1chk ) );
215 catch ( Exception e )
223 * Create metadata file together with its checksums.
225 * @param relativePath The groupId
226 * @param filename The filename of the artifact to be created.
227 * @param type The file type (JAR)
228 * @param isValid Indicates whether the checksum to be created is valid or not.
231 private boolean writeMetadataFile( String relativePath, String filename, String type, boolean isValid )
233 // System.out.println( " " );
234 // System.out.println( "========================= METADATA CHECKSUM ==================================" );
237 //create checksum for the metadata file..
238 String repoUrl = repository.getBasedir();
239 String url = repository.getBasedir() + "/" + filename + "." + type;
241 FileUtils.copyFile( new File( url ), new File( repoUrl + relativePath + filename + "." + type ) );
243 //Create md5 and sha-1 checksum files..
244 byte[] md5chk = createChecksum( repoUrl + relativePath + filename + "." + type, "MD5" );
245 byte[] sha1chk = createChecksum( repoUrl + relativePath + filename + "." + type, "SHA-1" );
246 //System.out.println( "----- CREATED MD5 checksum ::: " + byteArrayToHexStr( md5chk ) );
247 //System.out.println( "----- CREATED SHA-1 checksum ::: " + byteArrayToHexStr( sha1chk ) );
251 if ( md5chk != null )
253 file = new File( repoUrl + relativePath + filename + "." + type + ".md5" );
254 OutputStream os = new FileOutputStream( file );
255 OutputStreamWriter osw = new OutputStreamWriter( os );
258 osw.write( byteArrayToHexStr( md5chk ) + "1" );
262 osw.write( byteArrayToHexStr( md5chk ) );
267 if ( sha1chk != null )
269 file = new File( repoUrl + relativePath + filename + "." + type + ".sha1" );
270 OutputStream os = new FileOutputStream( file );
271 OutputStreamWriter osw = new OutputStreamWriter( os );
274 osw.write( byteArrayToHexStr( sha1chk ) + "2" );
278 osw.write( byteArrayToHexStr( sha1chk ) );
283 catch ( Exception e )
293 * Create the sample file that will be included in the jar.
298 private boolean createSampleFile( String filename )
302 File file = new File( filename );
303 OutputStream os = new FileOutputStream( file );
304 OutputStreamWriter osw = new OutputStreamWriter( os );
305 osw.write( "This is the content of the sample file that will be included in the jar file." );
308 catch ( Exception e )
316 * Create a checksum from the specified metadata file.
320 * @throws FileNotFoundException
321 * @throws NoSuchAlgorithmException
322 * @throws IOException
324 private byte[] createChecksum( String filename, String algo )
325 throws FileNotFoundException, NoSuchAlgorithmException, IOException
328 InputStream fis = new FileInputStream( filename );
329 byte[] buffer = new byte[1024];
331 MessageDigest complete = MessageDigest.getInstance( algo );
335 numRead = fis.read( buffer );
338 complete.update( buffer, 0, numRead );
341 while ( numRead != -1 );
344 return complete.digest();
348 * Convert an incoming array of bytes into a string that represents each of
349 * the bytes as two hex characters.
354 private String byteArrayToHexStr( byte[] data )
360 for ( int cnt = 0; cnt < data.length; cnt++ )
362 tempInt = data[cnt] & 0xFF;
363 tempStr = Integer.toHexString( tempInt );
365 if ( tempStr.length() == 1 )
367 tempStr = "0" + tempStr;
369 output = output + tempStr;
372 return output.toUpperCase();
376 * Delete the test directory created in the repository.
378 * @param dirname The directory to be deleted.
381 protected boolean deleteTestDirectory( File dir )
387 FileUtils.deleteDirectory( dir );
390 catch ( IOException ioe )
392 ioe.printStackTrace();
398 private boolean deleteFile( String filename )
400 File f = new File( filename );
407 protected boolean deleteChecksumFiles( String type )
412 //delete valid checksum files of artifacts created
413 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
415 b = deleteFile( repository.getBasedir() + "checksumTest/" +
416 validArtifactChecksumJars[i].replace( '-', '/' ) + "/" + validArtifactChecksumJars[i] + "." + type +
423 b = deleteFile( repository.getBasedir() + "checksumTest/" +
424 validArtifactChecksumJars[i].replace( '-', '/' ) + "/" + validArtifactChecksumJars[i] + "." + type +
432 //delete valid checksum files of metadata file
433 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
435 b = deleteFile( repository.getBasedir() + "checksumTest/" +
436 validArtifactChecksumJars[i].replace( '-', '/' ) + "/" + metadataChecksumFilename + ".xml.md5" );
442 b = deleteFile( repository.getBasedir() + "checksumTest/" +
443 validArtifactChecksumJars[i].replace( '-', '/' ) + "/" + metadataChecksumFilename + ".xml.sha1" );