]> source.dussan.org Git - archiva.git/blob
c4f04182b8ec614069f2726bcdcfbfe89df1ba83
[archiva.git] /
1 package org.apache.maven.archiva.repository.project.filters;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.maven.archiva.common.utils.VersionUtil;
23 import org.apache.maven.archiva.model.ArchivaProjectModel;
24 import org.apache.maven.archiva.model.Dependency;
25 import org.apache.maven.archiva.model.Individual;
26 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.project.ProjectModelException;
29 import org.apache.maven.archiva.repository.project.ProjectModelFilter;
30 import org.apache.maven.archiva.repository.project.ProjectModelReader;
31 import org.apache.maven.archiva.repository.project.ProjectModelResolver;
32 import org.apache.maven.archiva.repository.project.ProjectModelResolverFactory;
33 import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
34 import org.apache.maven.archiva.repository.project.resolvers.ManagedRepositoryProjectResolver;
35
36 import java.io.File;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41
42 /**
43  * EffectiveProjectModelFilterTest 
44  *
45  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46  * @version $Id$
47  */
48 public class EffectiveProjectModelFilterTest
49     extends AbstractRepositoryLayerTestCase
50 {
51     private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
52
53     private EffectiveProjectModelFilter lookupEffective()
54         throws Exception
55     {
56         return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
57     }
58
59     private ArchivaProjectModel createArchivaProjectModel( String path )
60         throws ProjectModelException
61     {
62         ProjectModelReader reader = new ProjectModel400Reader();
63
64         File pomFile = new File( getBasedir(), path );
65
66         return reader.read( pomFile );
67     }
68
69     private ProjectModelResolver createDefaultRepositoryResolver() throws Exception
70     {
71         File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
72
73         ManagedRepositoryContent repo = createManagedRepositoryContent( "defaultTestRepo", "Default Test Repo", defaultRepoDir, "default" );
74
75         ProjectModelReader reader = new ProjectModel400Reader();
76         ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader );
77
78         return resolver;
79     }
80
81     public void testBuildEffectiveProject()
82         throws Exception
83     {
84         initTestResolverFactory();
85         EffectiveProjectModelFilter filter = lookupEffective();
86
87         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY
88             + "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom" );
89
90         ArchivaProjectModel effectiveModel = filter.filter( startModel );
91
92         ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/"
93             + "/archiva-model-effective.pom" );
94
95         assertModel( expectedModel, effectiveModel );
96     }
97
98     /**
99      * [MRM-510] In Repository Browse, the first unique snapshot version clicked is getting persisted in the 
100      * request resulting to 'version does not match' error
101      * 
102      * The purpose of this test is ensure that timestamped SNAPSHOTS do not cache improperly, and each timestamped
103      * pom can be loaded through the effective project filter correctly.
104      */
105     public void testBuildEffectiveSnapshotProject()
106         throws Exception
107     {
108         initTestResolverFactory();
109         EffectiveProjectModelFilter filter = lookupEffective();
110
111         String axisVersions[] = new String[] {
112             "1.3-20070725.210059-1",
113             "1.3-20070725.232304-2",
114             "1.3-20070726.053327-3",
115             "1.3-20070726.173653-5",
116             "1.3-20070727.113106-7",
117             "1.3-20070728.053229-10",
118             "1.3-20070728.112043-11",
119             "1.3-20070729.171937-16",
120             "1.3-20070730.232112-20",
121             "1.3-20070731.113304-21",
122             "1.3-20070731.172936-22",
123             "1.3-20070802.113139-29" };
124
125         for ( int i = 0; i < axisVersions.length; i++ )
126         {
127             assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
128
129             ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
130                 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
131
132             // This is the process that ProjectModelToDatabaseConsumer uses, so we mimic it here.
133             // This logic is related to the MRM-510 jira.
134             String baseVersion = VersionUtil.getBaseVersion( axisVersions[i] );
135
136             assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
137                 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
138
139             initialModel.setVersion( axisVersions[i] );
140
141             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
142                 .getVersion() );
143
144             ArchivaProjectModel effectiveModel = filter.filter( initialModel );
145
146             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
147                 .getVersion() );
148             assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
149                           effectiveModel.getVersion() );
150         }
151     }
152
153     private ProjectModelResolverFactory initTestResolverFactory()
154         throws Exception
155     {
156         ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
157
158         resolverFactory.getCurrentResolverStack().clearResolvers();
159         resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
160
161         return resolverFactory;
162     }
163
164     private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
165     {
166         assertEquals( "Equivalent Models", expectedModel, effectiveModel );
167
168         assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
169         dumpDependencyList( "Expected", expectedModel.getDependencies() );
170         dumpDependencyList( "Effective", effectiveModel.getDependencies() );
171         assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
172             .getDependencies() );
173         assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
174             .getDependencyManagement() );
175     }
176
177     private void dumpDependencyList( String type, List<Dependency> deps )
178     {
179         if ( deps == null )
180         {
181             System.out.println( " Dependencies [" + type + "] is null." );
182             return;
183         }
184
185         if ( deps.isEmpty() )
186         {
187             System.out.println( " Dependencies [" + type + "] dependency list is empty." );
188             return;
189         }
190
191         System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
192         Iterator<Dependency> it = deps.iterator();
193         while ( it.hasNext() )
194         {
195             Dependency dep = it.next();
196             System.out.println( "  " + Dependency.toKey( dep ) );
197         }
198         System.out.println( "" );
199     }
200
201     private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
202     {
203         if ( ( expectedList == null ) && ( effectiveList == null ) )
204         {
205             return;
206         }
207
208         if ( ( expectedList == null ) && ( effectiveList != null ) )
209         {
210             fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
211         }
212
213         if ( ( expectedList != null ) && ( effectiveList == null ) )
214         {
215             fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
216         }
217
218         assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
219     }
220
221     private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
222                                                 List<Individual> effectiveList )
223     {
224         assertEquivalentLists( listId, expectedList, effectiveList );
225
226         Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
227         Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
228
229         Iterator<String> it = expectedMap.keySet().iterator();
230         while ( it.hasNext() )
231         {
232             String key = (String) it.next();
233
234             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
235         }
236     }
237
238     private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
239                                                  List<Dependency> effectiveList )
240     {
241         assertEquivalentLists( listId, expectedList, effectiveList );
242
243         Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
244         Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
245
246         Iterator<String> it = expectedMap.keySet().iterator();
247         while ( it.hasNext() )
248         {
249             String key = it.next();
250
251             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
252         }
253     }
254
255     private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
256     {
257         Map<String, Individual> map = new HashMap<String, Individual>();
258         Iterator<Individual> it = individuals.iterator();
259         while ( it.hasNext() )
260         {
261             Individual individual = it.next();
262             String key = individual.getEmail();
263             map.put( key, individual );
264         }
265         return map;
266     }
267
268     private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
269     {
270         Map<String, Dependency> map = new HashMap<String, Dependency>();
271         Iterator<Dependency> it = deps.iterator();
272         while ( it.hasNext() )
273         {
274             Dependency dep = it.next();
275             String key = Dependency.toVersionlessKey( dep );
276             map.put( key, dep );
277         }
278         return map;
279     }
280 }