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