1 package org.apache.maven.archiva.reporting.reporter;
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.digest.Digester;
20 import org.codehaus.plexus.digest.DigesterException;
21 import org.codehaus.plexus.util.FileUtils;
22 import org.apache.commons.io.IOUtils;
23 import org.apache.maven.archiva.reporting.AbstractRepositoryReportsTestCase;
25 import java.io.BufferedOutputStream;
26 import java.io.BufferedReader;
28 import java.io.FileOutputStream;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.util.jar.JarEntry;
34 import java.util.jar.JarOutputStream;
37 * This class creates the artifact and metadata files used for testing the ChecksumArtifactReportProcessor.
38 * It is extended by ChecksumArtifactReporterTest class.
40 public abstract class AbstractChecksumArtifactReporterTestCase
41 extends AbstractRepositoryReportsTestCase
43 private static final String[] validArtifactChecksumJars = {"validArtifact-1.0"};
45 private static final String[] invalidArtifactChecksumJars = {"invalidArtifact-1.0"};
47 private static final String metadataChecksumFilename = "maven-metadata";
49 private Digester sha1Digest;
51 private Digester md5Digest;
58 sha1Digest = (Digester) lookup( Digester.ROLE, "sha1" );
59 md5Digest = (Digester) lookup( Digester.ROLE, "md5" );
63 * Create checksum files.
65 * @param type The type of checksum file to be created.
67 protected void createChecksumFile( String type )
68 throws DigesterException, IOException
70 //loop through the valid artifact names..
71 if ( "VALID".equals( type ) )
73 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
75 writeChecksumFile( "checksumTest/", validArtifactChecksumJars[i], "jar", true );
78 else if ( "INVALID".equals( type ) )
80 for ( int i = 0; i < invalidArtifactChecksumJars.length; i++ )
82 writeChecksumFile( "checksumTest/", invalidArtifactChecksumJars[i], "jar", false );
88 * Create checksum files for metadata.
90 * @param type The type of checksum to be created. (Valid or invalid)
92 protected void createMetadataFile( String type )
93 throws DigesterException, IOException
95 //loop through the valid artifact names..
96 if ( "VALID".equals( type ) )
98 writeMetadataFile( "checksumTest/validArtifact/1.0/", metadataChecksumFilename, "xml", true );
99 writeMetadataFile( "checksumTest/validArtifact/", metadataChecksumFilename, "xml", true );
100 writeMetadataFile( "checksumTest/", metadataChecksumFilename, "xml", true );
102 else if ( "INVALID".equals( type ) )
104 writeMetadataFile( "checksumTest/invalidArtifact/1.0/", metadataChecksumFilename, "xml", false );
109 * Create artifact together with its checksums.
111 * @param relativePath The groupId
112 * @param filename The filename of the artifact to be created.
113 * @param type The file type (JAR)
114 * @param isValid Indicates whether the checksum to be created is valid or not.
116 private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid )
117 throws IOException, DigesterException
119 //Initialize variables for creating jar files
120 String repoUrl = repository.getBasedir();
122 String dirs = filename.replace( '-', '/' );
123 //create the group level directory of the artifact
124 File dirFiles = new File( repoUrl + relativePath + dirs );
126 if ( dirFiles.mkdirs() )
129 String path = repoUrl + relativePath + dirs + "/" + filename + "." + type;
130 FileOutputStream f = new FileOutputStream( path );
131 JarOutputStream out = new JarOutputStream( new BufferedOutputStream( f ) );
134 String filename1 = repoUrl + relativePath + dirs + "/sample.txt";
135 createSampleFile( filename1 );
137 BufferedReader in = new BufferedReader( new FileReader( filename1 ) );
138 out.putNextEntry( new JarEntry( filename1 ) );
139 IOUtils.copy( in, out );
143 //Create md5 and sha-1 checksum files..
145 File file = new File( path + ".md5" );
146 OutputStream os = new FileOutputStream( file );
147 OutputStreamWriter osw = new OutputStreamWriter( os );
148 String sum = md5Digest.calc( new File( path ) );
151 osw.write( sum + "1" );
159 file = new File( path + ".sha1" );
160 os = new FileOutputStream( file );
161 osw = new OutputStreamWriter( os );
162 String sha1sum = sha1Digest.calc( new File( path ) );
165 osw.write( sha1sum + "2" );
169 osw.write( sha1sum );
176 * Create metadata file together with its checksums.
178 * @param relativePath The groupId
179 * @param filename The filename of the artifact to be created.
180 * @param type The file type (JAR)
181 * @param isValid Indicates whether the checksum to be created is valid or not.
183 private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid )
184 throws IOException, DigesterException
186 //create checksum for the metadata file..
187 String repoUrl = repository.getBasedir();
188 String url = repository.getBasedir() + "/" + filename + "." + type;
190 String path = repoUrl + relativePath + filename + "." + type;
191 FileUtils.copyFile( new File( url ), new File( path ) );
193 //Create md5 and sha-1 checksum files..
194 File file = new File( path + ".md5" );
195 OutputStream os = new FileOutputStream( file );
196 OutputStreamWriter osw = new OutputStreamWriter( os );
197 String md5sum = md5Digest.calc( new File( path ) );
200 osw.write( md5sum + "1" );
208 file = new File( path + ".sha1" );
209 os = new FileOutputStream( file );
210 osw = new OutputStreamWriter( os );
211 String sha1sum = sha1Digest.calc( new File( path ) );
214 osw.write( sha1sum + "2" );
218 osw.write( sha1sum );
224 * Create the sample file that will be included in the jar.
228 private void createSampleFile( String filename )
231 File file = new File( filename );
232 OutputStream os = new FileOutputStream( file );
233 OutputStreamWriter osw = new OutputStreamWriter( os );
234 osw.write( "This is the content of the sample file that will be included in the jar file." );
239 * Delete the test directory created in the repository.
241 * @param dir The directory to be deleted.
243 protected void deleteTestDirectory( File dir )
247 FileUtils.deleteDirectory( dir );
249 catch ( IOException e )
255 private void deleteFile( String filename )
257 File f = new File( filename );
261 protected void deleteChecksumFiles( String type )
263 //delete valid checksum files of artifacts created
264 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
266 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
267 "/" + validArtifactChecksumJars[i] + "." + type + ".md5" );
269 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
270 "/" + validArtifactChecksumJars[i] + "." + type + ".sha1" );
273 //delete valid checksum files of metadata file
274 for ( int i = 0; i < validArtifactChecksumJars.length; i++ )
276 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
277 "/" + metadataChecksumFilename + ".xml.md5" );
279 deleteFile( repository.getBasedir() + "checksumTest/" + validArtifactChecksumJars[i].replace( '-', '/' ) +
280 "/" + metadataChecksumFilename + ".xml.sha1" );