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.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.DefaultArtifact;
21 import org.apache.maven.artifact.handler.ArtifactHandler;
22 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
23 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.apache.maven.repository.digest.DigesterException;
31 import java.io.IOException;
32 import java.util.Iterator;
35 * This class tests the ChecksumArtifactReporter.
36 * It extends the AbstractChecksumArtifactReporterTestCase class.
38 public class ChecksumArtifactReporterTest
39 extends AbstractChecksumArtifactReporterTestCase
41 private ArtifactReportProcessor artifactReportProcessor;
43 private ArtifactReporter reporter = new MockArtifactReporter();
45 private MetadataReportProcessor metadataReportProcessor;
51 artifactReportProcessor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "checksum" );
52 metadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE, "checksum-metadata" );
56 * Test the ChecksumArtifactReporter when the checksum files are valid.
58 public void testChecksumArtifactReporterSuccess()
59 throws ReportProcessorException, IOException, DigesterException
61 createChecksumFile( "VALID" );
62 createChecksumFile( "INVALID" );
64 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
65 VersionRange version = VersionRange.createFromVersion( "1.0" );
67 new DefaultArtifact( "checksumTest", "validArtifact", version, "compile", "jar", "", handler );
69 artifactReportProcessor.processArtifact( null, artifact, reporter, repository );
70 assertEquals( 2, reporter.getSuccesses() );
74 * Test the ChecksumArtifactReporter when the checksum files are invalid.
76 public void testChecksumArtifactReporterFailed()
77 throws ReportProcessorException
79 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
80 VersionRange version = VersionRange.createFromVersion( "1.0" );
82 new DefaultArtifact( "checksumTest", "invalidArtifact", version, "compile", "jar", "", handler );
84 artifactReportProcessor.processArtifact( null, artifact, reporter, repository );
85 assertEquals( 2, reporter.getFailures() );
89 * Test the valid checksum of a metadata file.
90 * The reporter should report 2 success validation.
92 public void testChecksumMetadataReporterSuccess()
93 throws ReportProcessorException, DigesterException, IOException
95 createMetadataFile( "VALID" );
96 createMetadataFile( "INVALID" );
98 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
99 VersionRange version = VersionRange.createFromVersion( "1.0" );
101 new DefaultArtifact( "checksumTest", "validArtifact", version, "compile", "jar", "", handler );
103 //Version level metadata
104 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
105 metadataReportProcessor.processMetadata( metadata, repository, reporter );
107 //Artifact level metadata
108 metadata = new ArtifactRepositoryMetadata( artifact );
109 metadataReportProcessor.processMetadata( metadata, repository, reporter );
111 //Group level metadata
112 metadata = new GroupRepositoryMetadata( "checksumTest" );
113 metadataReportProcessor.processMetadata( metadata, repository, reporter );
115 Iterator iter = reporter.getRepositoryMetadataSuccessIterator();
116 assertTrue( "check if there is a success", iter.hasNext() );
120 * Test the corrupted checksum of a metadata file.
121 * The reporter must report 2 failures.
123 public void testChecksumMetadataReporterFailure()
124 throws ReportProcessorException
126 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
127 VersionRange version = VersionRange.createFromVersion( "1.0" );
129 new DefaultArtifact( "checksumTest", "invalidArtifact", version, "compile", "jar", "", handler );
131 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
132 metadataReportProcessor.processMetadata( metadata, repository, reporter );
134 Iterator iter = reporter.getRepositoryMetadataFailureIterator();
135 assertTrue( "check if there is a failure", iter.hasNext() );
139 * Test the checksum of an artifact located in a remote location.
141 /* public void testChecksumArtifactRemote()
143 ArtifactHandler handler = new DefaultArtifactHandler( remoteArtifactType );
144 VersionRange version = VersionRange.createFromVersion( remoteArtifactVersion );
145 Artifact artifact = new DefaultArtifact( remoteArtifactGroup, remoteArtifactId, version, remoteArtifactScope,
146 remoteArtifactType, "", handler );
147 ArtifactRepository repository = new DefaultArtifactRepository( remoteRepoId, remoteRepoUrl,
148 new DefaultRepositoryLayout() );
150 artifactReportProcessor.processArtifact( null, artifact, reporter, repository );
151 if ( reporter.getFailures() == 2 )
152 assertTrue( reporter.getFailures() == 2 );
154 if ( reporter.getSuccesses() == 2 )
155 assertTrue( reporter.getSuccesses() == 2 );
161 * Test the checksum of a metadata file located in a remote location.
163 /* public void testChecksumMetadataRemote()
168 ArtifactHandler handler = new DefaultArtifactHandler( remoteArtifactType );
169 VersionRange version = VersionRange.createFromVersion( remoteArtifactVersion );
170 Artifact artifact = new DefaultArtifact( remoteArtifactGroup, remoteArtifactId, version,
171 remoteArtifactScope, remoteArtifactType, "", handler );
172 ArtifactRepository repository = new DefaultArtifactRepository( remoteRepoId, remoteRepoUrl,
173 new DefaultRepositoryLayout() );
175 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
177 metadataReportProcessor.processMetadata( metadata, repository, reporter );
178 Iterator iter = reporter.getRepositoryMetadataFailureIterator();
179 if ( iter.hasNext() )
180 assertTrue( "check if there is a failure", iter.hasNext() );
182 iter = reporter.getRepositoryMetadataSuccessIterator();
183 if ( iter.hasNext() )
184 assertTrue( "check if there is a success", iter.hasNext() );
187 catch ( Exception e )
195 * Test the conditional when the checksum files of the artifact & metadata do not exist.
197 public void testChecksumFilesDoNotExist()
198 throws ReportProcessorException, DigesterException, IOException
200 createChecksumFile( "VALID" );
201 createMetadataFile( "VALID" );
202 deleteChecksumFiles( "jar" );
204 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
205 VersionRange version = VersionRange.createFromVersion( "1.0" );
207 new DefaultArtifact( "checksumTest", "validArtifact", version, "compile", "jar", "", handler );
209 artifactReportProcessor.processArtifact( null, artifact, reporter, repository );
210 assertEquals( 2, reporter.getFailures() );
212 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
213 metadataReportProcessor.processMetadata( metadata, repository, reporter );
215 Iterator iter = reporter.getRepositoryMetadataFailureIterator();
216 assertTrue( "check if there is a failure", iter.hasNext() );
218 deleteTestDirectory( new File( repository.getBasedir() + "checksumTest" ) );