]> source.dussan.org Git - archiva.git/blob
fae996e6fe80c56bc167dfae02ba943cc53c711e
[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 import org.apache.maven.archiva.xml.XMLException;
36
37 import java.io.File;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * EffectiveProjectModelFilterTest 
45  *
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 XMLException
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         assertEffectiveProject(
85                 "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom",
86         "/archiva-model-effective.pom");
87         assertEffectiveProject(
88                 "/test-project/test-project-endpoint-ejb/2.4.4/test-project-endpoint-ejb-2.4.4.pom",
89         "/test-project-model-effective.pom");
90     }
91
92     private void assertEffectiveProject(String pomFile, String effectivePomFile) throws Exception,
93             ProjectModelException {
94         initTestResolverFactory();
95         EffectiveProjectModelFilter filter = lookupEffective();
96
97         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
98
99         ArchivaProjectModel effectiveModel = filter.filter( startModel );
100
101         ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/" + effectivePomFile);
102
103         assertModel( expectedModel, effectiveModel );
104     }
105
106
107     /**
108      * [MRM-510] In Repository Browse, the first unique snapshot version clicked is getting persisted in the 
109      * request resulting to 'version does not match' error
110      * 
111      * The purpose of this test is ensure that timestamped SNAPSHOTS do not cache improperly, and each timestamped
112      * pom can be loaded through the effective project filter correctly.
113      */
114     public void testBuildEffectiveSnapshotProject()
115         throws Exception
116     {
117         initTestResolverFactory();
118         EffectiveProjectModelFilter filter = lookupEffective();
119
120         String axisVersions[] = new String[] {
121             "1.3-20070725.210059-1",
122             "1.3-20070725.232304-2",
123             "1.3-20070726.053327-3",
124             "1.3-20070726.173653-5",
125             "1.3-20070727.113106-7",
126             "1.3-20070728.053229-10",
127             "1.3-20070728.112043-11",
128             "1.3-20070729.171937-16",
129             "1.3-20070730.232112-20",
130             "1.3-20070731.113304-21",
131             "1.3-20070731.172936-22",
132             "1.3-20070802.113139-29" };
133
134         for ( int i = 0; i < axisVersions.length; i++ )
135         {
136             assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
137
138             ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
139                 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
140
141             // This is the process that ProjectModelToDatabaseConsumer uses, so we mimic it here.
142             // This logic is related to the MRM-510 jira.
143             String baseVersion = VersionUtil.getBaseVersion( axisVersions[i] );
144
145             assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
146                 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
147
148             initialModel.setVersion( axisVersions[i] );
149
150             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
151                 .getVersion() );
152
153             ArchivaProjectModel effectiveModel = filter.filter( initialModel );
154
155             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
156                 .getVersion() );
157             assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
158                           effectiveModel.getVersion() );
159         }
160     }
161     
162     /*
163      * Test before and after the properties are evaluated. pom snippet: <maven.version>2.0.5</maven.version>
164      * <wagon.version>1.0-beta-2</wagon.version> <plexus-security.version>1.0-alpha-10-SNAPSHOT</plexus-security.version>
165      */
166     public void testEffectiveProjectProperty()
167         throws Exception
168     {
169         initTestResolverFactory();
170         EffectiveProjectModelFilter filter = lookupEffective();
171
172         String pomFile = "/org/apache/maven/archiva/archiva/1.0-SNAPSHOT/archiva-1.0-SNAPSHOT.pom";
173         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
174
175         String plexusSecurityVersion = "1.0-alpha-10-SNAPSHOT";
176         String wagonVersion = "1.0-beta-2";
177
178         boolean passedPlexusVersionChecking = false;
179         boolean passedWagonVersionChecking = false;
180
181         List<Dependency> startDeps = startModel.getDependencyManagement();
182         for ( Dependency startDep : startDeps )
183         {
184             if ( "org.codehaus.plexus.security".equals( startDep.getGroupId() ) )
185             {
186                 assertEquals( startDep.getVersion(), "${plexus-security.version}" );
187             }
188             else if ( "org.apache.maven.wagon".equals( startDep.getGroupId() ) )
189             {
190                 assertEquals( startDep.getVersion(), "${wagon.version}" );
191             }
192         }
193
194         ArchivaProjectModel effectiveModel = filter.filter( startModel );
195
196         List<Dependency> effectiveDeps = effectiveModel.getDependencyManagement();
197         for ( Dependency dependency : effectiveDeps )
198         {
199             if ( "org.codehaus.plexus.security".equals( dependency.getGroupId() ) )
200             {
201                 assertEquals( dependency.getVersion(), plexusSecurityVersion );
202
203                 if ( !passedPlexusVersionChecking )
204                 {
205                     passedPlexusVersionChecking = true;
206                 }
207             }
208             else if ( "org.apache.maven.wagon".equals( dependency.getGroupId() ) )
209             {
210                 assertEquals( dependency.getVersion(), wagonVersion );
211
212                 if ( !passedWagonVersionChecking )
213                 {
214                     passedWagonVersionChecking = true;
215                 }
216             }
217
218         }
219         assertTrue( passedPlexusVersionChecking );
220         assertTrue( passedWagonVersionChecking );
221     }
222
223     private ProjectModelResolverFactory initTestResolverFactory()
224         throws Exception
225     {
226         ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
227
228         resolverFactory.getCurrentResolverStack().clearResolvers();
229         resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
230
231         return resolverFactory;
232     }
233
234     private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
235     {
236         assertEquals( "Equivalent Models", expectedModel, effectiveModel );
237
238         assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
239         dumpDependencyList( "Expected", expectedModel.getDependencies() );
240         dumpDependencyList( "Effective", effectiveModel.getDependencies() );
241         assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
242             .getDependencies() );
243         assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
244             .getDependencyManagement() );
245     }
246
247     private void dumpDependencyList( String type, List<Dependency> deps )
248     {
249         if ( deps == null )
250         {
251             System.out.println( " Dependencies [" + type + "] is null." );
252             return;
253         }
254
255         if ( deps.isEmpty() )
256         {
257             System.out.println( " Dependencies [" + type + "] dependency list is empty." );
258             return;
259         }
260
261         System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
262         Iterator<Dependency> it = deps.iterator();
263         while ( it.hasNext() )
264         {
265             Dependency dep = it.next();
266             System.out.println( "  " + Dependency.toKey( dep ) );
267         }
268         System.out.println( "" );
269     }
270
271     private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
272     {
273         if ( ( expectedList == null ) && ( effectiveList == null ) )
274         {
275             return;
276         }
277
278         if ( ( expectedList == null ) && ( effectiveList != null ) )
279         {
280             fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
281         }
282
283         if ( ( expectedList != null ) && ( effectiveList == null ) )
284         {
285             fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
286         }
287
288         assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
289     }
290
291     private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
292                                                 List<Individual> effectiveList )
293     {
294         assertEquivalentLists( listId, expectedList, effectiveList );
295
296         Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
297         Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
298
299         Iterator<String> it = expectedMap.keySet().iterator();
300         while ( it.hasNext() )
301         {
302             String key = (String) it.next();
303
304             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
305         }
306     }
307
308     private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
309                                                  List<Dependency> effectiveList )
310     {
311         assertEquivalentLists( listId, expectedList, effectiveList );
312
313         Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
314         Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
315
316         Iterator<String> it = expectedMap.keySet().iterator();
317         while ( it.hasNext() )
318         {
319             String key = it.next();
320
321             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
322         }
323     }
324
325     private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
326     {
327         Map<String, Individual> map = new HashMap<String, Individual>();
328         Iterator<Individual> it = individuals.iterator();
329         while ( it.hasNext() )
330         {
331             Individual individual = it.next();
332             String key = individual.getEmail();
333             map.put( key, individual );
334         }
335         return map;
336     }
337
338     private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
339     {
340         Map<String, Dependency> map = new HashMap<String, Dependency>();
341         Iterator<Dependency> it = deps.iterator();
342         while ( it.hasNext() )
343         {
344             Dependency dep = it.next();
345             String key = Dependency.toKey( dep );
346             map.put( key, dep );
347         }
348         return map;
349     }
350 }