1 package org.apache.maven.archiva.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.apache.maven.archiva.digest.Digester;
20 import org.apache.maven.archiva.digest.DigesterException;
21 import org.codehaus.plexus.util.FileUtils;
22 import org.codehaus.plexus.util.IOUtil;
24 import java.io.BufferedOutputStream;
25 import java.io.BufferedReader;
27 import java.io.FileOutputStream;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.io.OutputStreamWriter;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarOutputStream;
36 * This class creates the artifact and metadata files used for testing the ChecksumArtifactReportProcessor.
37 * It is extended by ChecksumArtifactReporterTest class.
39 public abstract class AbstractChecksumArtifactReporterTestCase
40 extends AbstractRepositoryReportsTestCase
42 private static final String[] validArtifactChecksumJars = {"validArtifact-1.0"};
44 private static final String[] invalidArtifactChecksumJars = {"invalidArtifact-1.0"};
46 private static final String metadataChecksumFilename = "maven-metadata";
48 private Digester sha1Digest;
50 private Digester md5Digest;
57 sha1Digest = (Digester) lookup( Digester.ROLE, "sha1" );
58 md5Digest = (Digester) lookup( Digester.ROLE, "md5" );
62 * Create checksum files.
64 * @param type The type of checksum file to be created.
66 protected void createChecksumFile( String type )
67 throws DigesterException, IOException
69 //loop through the valid artifact names..
70 if ( "VALID".equals( type ) )
72 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
74 writeChecksumFile( "checksumTest/", validArtifactChecksumJars[i], "jar", true );
77 else if ( "INVALID".equals( type ) )
79 for ( int i = 0; i < invalidArtifactChecksumJars.length; i++ )
81 writeChecksumFile( "checksumTest/", invalidArtifactChecksumJars[i], "jar", false );
87 * Create checksum files for metadata.
89 * @param type The type of checksum to be created. (Valid or invalid)
91 protected void createMetadataFile( String type )
92 throws DigesterException, IOException
94 //loop through the valid artifact names..
95 if ( "VALID".equals( type ) )
97 writeMetadataFile( "checksumTest/validArtifact/1.0/", metadataChecksumFilename, "xml", true );
98 writeMetadataFile( "checksumTest/validArtifact/", metadataChecksumFilename, "xml", true );
99 writeMetadataFile( "checksumTest/", metadataChecksumFilename, "xml", true );
101 else if ( "INVALID".equals( type ) )
103 writeMetadataFile( "checksumTest/invalidArtifact/1.0/", metadataChecksumFilename, "xml", false );
108 * Create artifact together with its checksums.
110 * @param relativePath The groupId
111 * @param filename The filename of the artifact to be created.
112 * @param type The file type (JAR)
113 * @param isValid Indicates whether the checksum to be created is valid or not.
115 private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid )
116 throws IOException, DigesterException
118 //Initialize variables for creating jar files
119 String repoUrl = repository.getBasedir();
121 String dirs = filename.replace( '-', '/' );
122 //create the group level directory of the artifact
123 File dirFiles = new File( repoUrl + relativePath + dirs );
125 if ( dirFiles.mkdirs() )
128 String path = repoUrl + relativePath + dirs + "/" + filename + "." + type;
129 FileOutputStream f = new FileOutputStream( path );
130 JarOutputStream out = new JarOutputStream( new BufferedOutputStream( f ) );
133 String filename1 = repoUrl + relativePath + dirs + "/sample.txt";
134 createSampleFile( filename1 );
136 BufferedReader in = new BufferedReader( new FileReader( filename1 ) );
137 out.putNextEntry( new JarEntry( filename1 ) );
138 IOUtil.copy( in, out );
142 //Create md5 and sha-1 checksum files..
144 File file = new File( path + ".md5" );
145 OutputStream os = new FileOutputStream( file );
146 OutputStreamWriter osw = new OutputStreamWriter( os );
147 String sum = md5Digest.calc( new File( path ) );
150 osw.write( sum + "1" );
158 file = new File( path + ".sha1" );
159 os = new FileOutputStream( file );
160 osw = new OutputStreamWriter( os );
161 String sha1sum = sha1Digest.calc( new File( path ) );
164 osw.write( sha1sum + "2" );
168 osw.write( sha1sum );
175 * Create metadata file together with its checksums.
177 * @param relativePath The groupId
178 * @param filename The filename of the artifact to be created.
179 * @param type The file type (JAR)
180 * @param isValid Indicates whether the checksum to be created is valid or not.
182 private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid )
183 throws IOException, DigesterException
185 //create checksum for the metadata file..
186 String repoUrl = repository.getBasedir();
187 String url = repository.getBasedir() + "/" + filename + "." + type;
189 String path = repoUrl + relativePath + filename + "." + type;
190 FileUtils.copyFile( new File( url ), new File( path ) );
192 //Create md5 and sha-1 checksum files..
193 File file = new File( path + ".md5" );
194 OutputStream os = new FileOutputStream( file );
195 OutputStreamWriter osw = new OutputStreamWriter( os );
196 String md5sum = md5Digest.calc( new File( path ) );
199 osw.write( md5sum + "1" );
207 file = new File( path + ".sha1" );
208 os = new FileOutputStream( file );
209 osw = new OutputStreamWriter( os );
210 String sha1sum = sha1Digest.calc( new File( path ) );
213 osw.write( sha1sum + "2" );
217 osw.write( sha1sum );
223 * Create the sample file that will be included in the jar.
227 private void createSampleFile( String filename )
230 File file = new File( filename );
231 OutputStream os = new FileOutputStream( file );
232 OutputStreamWriter osw = new OutputStreamWriter( os );
233 osw.write( "This is the content of the sample file that will be included in the jar file." );
238 * Delete the test directory created in the repository.
240 * @param dir The directory to be deleted.
242 protected void deleteTestDirectory( File dir )
246 FileUtils.deleteDirectory( dir );
248 catch ( IOException e )
254 private void deleteFile( String filename )
256 File f = new File( filename );
260 protected void deleteChecksumFiles( String type )
262 //delete valid checksum files of artifacts created
263 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
265 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
266 "/" + validArtifactChecksumJars[i] + "." + type + ".md5" );
268 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
269 "/" + validArtifactChecksumJars[i] + "." + type + ".sha1" );
272 //delete valid checksum files of metadata file
273 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
275 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
276 "/" + metadataChecksumFilename + ".xml.md5" );
278 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
279 "/" + metadataChecksumFilename + ".xml.sha1" );