]> source.dussan.org Git - archiva.git/blob
26ba23322b1249648b58e5c66d96bb66c0aae79c
[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  * @version $Id$
46  */
47 public class EffectiveProjectModelFilterTest
48     extends AbstractRepositoryLayerTestCase
49 {
50     private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
51
52     private EffectiveProjectModelFilter lookupEffective()
53         throws Exception
54     {
55         return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
56     }
57
58     private ArchivaProjectModel createArchivaProjectModel( String path )
59         throws ProjectModelException
60     {
61         ProjectModelReader reader = new ProjectModel400Reader();
62
63         File pomFile = new File( getBasedir(), path );
64
65         return reader.read( pomFile );
66     }
67
68     private ProjectModelResolver createDefaultRepositoryResolver() throws Exception
69     {
70         File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
71
72         ManagedRepositoryContent repo = createManagedRepositoryContent( "defaultTestRepo", "Default Test Repo", defaultRepoDir, "default" );
73
74         ProjectModelReader reader = new ProjectModel400Reader();
75         ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader );
76
77         return resolver;
78     }
79
80     public void testBuildEffectiveProject()
81         throws Exception
82     {
83         assertEffectiveProject(
84                 "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom",
85         "/archiva-model-effective.pom");
86         assertEffectiveProject(
87                 "/test-project/test-project-endpoint-ejb/2.4.4/test-project-endpoint-ejb-2.4.4.pom",
88         "/test-project-model-effective.pom");
89     }
90
91     private void assertEffectiveProject(String pomFile, String effectivePomFile) throws Exception,
92             ProjectModelException {
93         initTestResolverFactory();
94         EffectiveProjectModelFilter filter = lookupEffective();
95
96         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
97
98         ArchivaProjectModel effectiveModel = filter.filter( startModel );
99
100         ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/" + effectivePomFile);
101
102         assertModel( expectedModel, effectiveModel );
103     }
104
105
106     /**
107      * [MRM-510] In Repository Browse, the first unique snapshot version clicked is getting persisted in the 
108      * request resulting to 'version does not match' error
109      * 
110      * The purpose of this test is ensure that timestamped SNAPSHOTS do not cache improperly, and each timestamped
111      * pom can be loaded through the effective project filter correctly.
112      */
113     public void testBuildEffectiveSnapshotProject()
114         throws Exception
115     {
116         initTestResolverFactory();
117         EffectiveProjectModelFilter filter = lookupEffective();
118
119         String axisVersions[] = new String[] {
120             "1.3-20070725.210059-1",
121             "1.3-20070725.232304-2",
122             "1.3-20070726.053327-3",
123             "1.3-20070726.173653-5",
124             "1.3-20070727.113106-7",
125             "1.3-20070728.053229-10",
126             "1.3-20070728.112043-11",
127             "1.3-20070729.171937-16",
128             "1.3-20070730.232112-20",
129             "1.3-20070731.113304-21",
130             "1.3-20070731.172936-22",
131             "1.3-20070802.113139-29" };
132
133         for ( int i = 0; i < axisVersions.length; i++ )
134         {
135             assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
136
137             ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
138                 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
139
140             // This is the process that ProjectModelToDatabaseConsumer uses, so we mimic it here.
141             // This logic is related to the MRM-510 jira.
142             String baseVersion = VersionUtil.getBaseVersion( axisVersions[i] );
143
144             assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
145                 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
146
147             initialModel.setVersion( axisVersions[i] );
148
149             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
150                 .getVersion() );
151
152             ArchivaProjectModel effectiveModel = filter.filter( initialModel );
153
154             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
155                 .getVersion() );
156             assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
157                           effectiveModel.getVersion() );
158         }
159     }
160
161     private ProjectModelResolverFactory initTestResolverFactory()
162         throws Exception
163     {
164         ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
165
166         resolverFactory.getCurrentResolverStack().clearResolvers();
167         resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
168
169         return resolverFactory;
170     }
171
172     private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
173     {
174         assertEquals( "Equivalent Models", expectedModel, effectiveModel );
175
176         assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
177         dumpDependencyList( "Expected", expectedModel.getDependencies() );
178         dumpDependencyList( "Effective", effectiveModel.getDependencies() );
179         assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
180             .getDependencies() );
181         assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
182             .getDependencyManagement() );
183     }
184
185     private void dumpDependencyList( String type, List<Dependency> deps )
186     {
187         if ( deps == null )
188         {
189             System.out.println( " Dependencies [" + type + "] is null." );
190             return;
191         }
192
193         if ( deps.isEmpty() )
194         {
195             System.out.println( " Dependencies [" + type + "] dependency list is empty." );
196             return;
197         }
198
199         System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
200         Iterator<Dependency> it = deps.iterator();
201         while ( it.hasNext() )
202         {
203             Dependency dep = it.next();
204             System.out.println( "  " + Dependency.toKey( dep ) );
205         }
206         System.out.println( "" );
207     }
208
209     private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
210     {
211         if ( ( expectedList == null ) && ( effectiveList == null ) )
212         {
213             return;
214         }
215
216         if ( ( expectedList == null ) && ( effectiveList != null ) )
217         {
218             fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
219         }
220
221         if ( ( expectedList != null ) && ( effectiveList == null ) )
222         {
223             fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
224         }
225
226         assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
227     }
228
229     private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
230                                                 List<Individual> effectiveList )
231     {
232         assertEquivalentLists( listId, expectedList, effectiveList );
233
234         Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
235         Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
236
237         Iterator<String> it = expectedMap.keySet().iterator();
238         while ( it.hasNext() )
239         {
240             String key = (String) it.next();
241
242             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
243         }
244     }
245
246     private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
247                                                  List<Dependency> effectiveList )
248     {
249         assertEquivalentLists( listId, expectedList, effectiveList );
250
251         Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
252         Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
253
254         Iterator<String> it = expectedMap.keySet().iterator();
255         while ( it.hasNext() )
256         {
257             String key = it.next();
258
259             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
260         }
261     }
262
263     private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
264     {
265         Map<String, Individual> map = new HashMap<String, Individual>();
266         Iterator<Individual> it = individuals.iterator();
267         while ( it.hasNext() )
268         {
269             Individual individual = it.next();
270             String key = individual.getEmail();
271             map.put( key, individual );
272         }
273         return map;
274     }
275
276     private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
277     {
278         Map<String, Dependency> map = new HashMap<String, Dependency>();
279         Iterator<Dependency> it = deps.iterator();
280         while ( it.hasNext() )
281         {
282             Dependency dep = it.next();
283             String key = Dependency.toKey( dep );
284             map.put( key, dep );
285         }
286         return map;
287     }
288 }