]> source.dussan.org Git - archiva.git/blob
9f7cb1da0a7445d7b9beba013f97bed371e77343
[archiva.git] /
1 package org.apache.maven.archiva.reporting.processor;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import org.apache.maven.archiva.reporting.model.ArtifactResults;
20 import org.apache.maven.archiva.reporting.model.Result;
21 import org.apache.maven.archiva.reporting.group.ReportGroup;
22 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
23 import org.apache.maven.archiva.reporting.AbstractRepositoryReportsTestCase;
24 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.commons.io.FileUtils;
27
28 import java.io.File;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.Iterator;
32
33 /**
34  * This class tests the OldArtifactReportProcessor.
35  */
36 public class OldSnapshotArtifactReportProcessorTest
37     extends AbstractRepositoryReportsTestCase
38 {
39     private ArtifactReportProcessor artifactReportProcessor;
40
41     private ReportingDatabase reportDatabase;
42
43     private File tempRepository;
44
45     public void setUp()
46         throws Exception
47     {
48         super.setUp();
49         artifactReportProcessor =
50             (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "old-snapshot-artifact" );
51
52         ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "old-artifact" );
53         reportDatabase = new ReportingDatabase( reportGroup );
54         tempRepository = getTestFile( "target/test-repository" );
55         FileUtils.deleteDirectory( tempRepository );
56     }
57
58     public void testOldSnapshotArtifact()
59     {
60         Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-20050611.202024-1", "pom" );
61
62         artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
63         assertEquals( 0, reportDatabase.getNumFailures() );
64         assertEquals( 0, reportDatabase.getNumWarnings() );
65         assertEquals( "Check notices", 1, reportDatabase.getNumNotices() );
66         Iterator artifactIterator = reportDatabase.getArtifactIterator();
67         assertArtifactResults( artifactIterator, artifact );
68     }
69
70     private static void assertArtifactResults( Iterator artifactIterator, Artifact artifact )
71     {
72         ArtifactResults results = (ArtifactResults) artifactIterator.next();
73         assertEquals( artifact.getArtifactId(), results.getArtifactId() );
74         assertEquals( artifact.getGroupId(), results.getGroupId() );
75         assertEquals( artifact.getVersion(), results.getVersion() );
76         assertFalse( artifact.getVersion().indexOf( "SNAPSHOT" ) >= 0 );
77         assertEquals( 1, results.getNotices().size() );
78         Iterator i = results.getNotices().iterator();
79         Result result = (Result) i.next();
80         assertEquals( "old-snapshot-artifact", result.getProcessor() );
81     }
82
83     public void testSNAPSHOTArtifact()
84     {
85         Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "pom" );
86
87         artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
88         assertEquals( 0, reportDatabase.getNumFailures() );
89         assertEquals( 0, reportDatabase.getNumWarnings() );
90         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
91     }
92
93     public void testNonSnapshotArtifact()
94     {
95         Artifact artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1" );
96
97         artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
98         assertEquals( 0, reportDatabase.getNumFailures() );
99         assertEquals( 0, reportDatabase.getNumWarnings() );
100         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
101     }
102
103     public void testNewSnapshotArtifact()
104         throws Exception
105     {
106         File repository = getTestFile( "target/test-repository" );
107
108         File dir = new File( repository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
109         dir.mkdirs();
110
111         String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
112         FileUtils.writeStringToFile( new File( dir, "artifactId-1.0-alpha-1-" + date + "-1.jar" ), "foo", null );
113
114         Artifact artifact =
115             createArtifactFromRepository( repository, "groupId", "artifactId", "1.0-alpha-1-" + date + "-1" );
116
117         artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
118         assertEquals( 0, reportDatabase.getNumFailures() );
119         assertEquals( 0, reportDatabase.getNumWarnings() );
120         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
121     }
122
123     public void testTooManySnapshotArtifact()
124         throws Exception
125     {
126         File dir = new File( tempRepository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
127         dir.mkdirs();
128
129         String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
130         for ( int i = 1; i <= 5; i++ )
131         {
132             FileUtils.writeStringToFile( new File( dir, "artifactId-1.0-alpha-1-" + date + "-" + i + ".jar" ),
133                                  "foo", null );
134         }
135
136         for ( int i = 1; i <= 5; i++ )
137         {
138             Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId",
139                                                               "1.0-alpha-1-" + date + "-" + i );
140             artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
141         }
142
143         assertEquals( 0, reportDatabase.getNumFailures() );
144         assertEquals( 0, reportDatabase.getNumWarnings() );
145         assertEquals( "Check notices", 3, reportDatabase.getNumNotices() );
146         Iterator artifactIterator = reportDatabase.getArtifactIterator();
147         for ( int i = 1; i <= 3; i++ )
148         {
149             String version = "1.0-alpha-1-" + date + "-" + i;
150             Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId", version );
151             assertArtifactResults( artifactIterator, artifact );
152         }
153     }
154
155     public void testMissingArtifact()
156         throws Exception
157     {
158         Artifact artifact = createArtifact( "foo", "bar", "XP" );
159
160         try
161         {
162             artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
163             fail( "Should not have passed" );
164         }
165         catch ( IllegalStateException e )
166         {
167             assertTrue( true );
168         }
169     }
170 }