View Javadoc

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.ArtifactReference;
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.ManagedRepositoryContent;
29  import org.apache.maven.archiva.repository.project.ProjectModelException;
30  import org.apache.maven.archiva.repository.project.ProjectModelFilter;
31  import org.apache.maven.archiva.repository.project.ProjectModelReader;
32  import org.apache.maven.archiva.repository.project.ProjectModelResolver;
33  import org.apache.maven.archiva.repository.project.ProjectModelResolverFactory;
34  import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
35  import org.apache.maven.archiva.repository.project.resolvers.ManagedRepositoryProjectResolver;
36  import org.apache.maven.archiva.xml.XMLException;
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   * @version $Id: EffectiveProjectModelFilterTest.java 801946 2009-08-07 11:00:03Z oching $
48   */
49  public class EffectiveProjectModelFilterTest
50      extends AbstractRepositoryLayerTestCase
51  {
52      private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
53  
54      private EffectiveProjectModelFilter lookupEffective()
55          throws Exception
56      {
57          return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
58      }
59  
60      private ArchivaProjectModel createArchivaProjectModel( String path )
61          throws XMLException
62      {
63          ProjectModelReader reader = new ProjectModel400Reader();
64  
65          File pomFile = new File( getBasedir(), path );
66  
67          return reader.read( pomFile );
68      }
69  
70      private ProjectModelResolver createDefaultRepositoryResolver() throws Exception
71      {
72          File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
73  
74          ManagedRepositoryContent repo = createManagedRepositoryContent( "defaultTestRepo", "Default Test Repo", defaultRepoDir, "default" );
75  
76          ProjectModelReader reader = new ProjectModel400Reader();
77          ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader );
78  
79          return resolver;
80      }
81  
82      public void testBuildEffectiveProject()
83          throws Exception
84      {
85          assertEffectiveProject(
86                  "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom",
87          "/archiva-model-effective.pom");
88          
89          assertEffectiveProject(
90                  "/test-project/test-project-endpoint-ejb/2.4.4/test-project-endpoint-ejb-2.4.4.pom",
91          "/test-project-model-effective.pom");
92      }
93  
94      private void assertEffectiveProject(String pomFile, String effectivePomFile) throws Exception,
95              ProjectModelException {
96          initTestResolverFactory();
97          EffectiveProjectModelFilter filter = lookupEffective();
98  
99          ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
100 
101         ArchivaProjectModel effectiveModel = filter.filter( startModel );
102 
103         ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/" + effectivePomFile);
104 
105         assertModel( expectedModel, effectiveModel );
106     }
107 
108 
109     /**
110      * [MRM-510] In Repository Browse, the first unique snapshot version clicked is getting persisted in the 
111      * request resulting to 'version does not match' error
112      * 
113      * The purpose of this test is ensure that timestamped SNAPSHOTS do not cache improperly, and each timestamped
114      * pom can be loaded through the effective project filter correctly.
115      */
116     public void testBuildEffectiveSnapshotProject()
117         throws Exception
118     {
119         initTestResolverFactory();
120         EffectiveProjectModelFilter filter = lookupEffective();
121 
122         String axisVersions[] = new String[] {
123             "1.3-20070725.210059-1",
124             "1.3-20070725.232304-2",
125             "1.3-20070726.053327-3",
126             "1.3-20070726.173653-5",
127             "1.3-20070727.113106-7",
128             "1.3-20070728.053229-10",
129             "1.3-20070728.112043-11",
130             "1.3-20070729.171937-16",
131             "1.3-20070730.232112-20",
132             "1.3-20070731.113304-21",
133             "1.3-20070731.172936-22",
134             "1.3-20070802.113139-29" };
135 
136         for ( int i = 0; i < axisVersions.length; i++ )
137         {
138             assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
139 
140             ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
141                 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
142 
143             // This is the process that ProjectModelToDatabaseConsumer uses, so we mimic it here.
144             // This logic is related to the MRM-510 jira.
145             String baseVersion = VersionUtil.getBaseVersion( axisVersions[i] );
146 
147             assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
148                 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
149 
150             initialModel.setVersion( axisVersions[i] );
151 
152             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
153                 .getVersion() );
154 
155             ArchivaProjectModel effectiveModel = filter.filter( initialModel );
156 
157             assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
158                 .getVersion() );
159             assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
160                           effectiveModel.getVersion() );
161         }
162     }
163     
164     /*
165      * Test before and after the properties are evaluated. pom snippet: <maven.version>2.0.5</maven.version>
166      * <wagon.version>1.0-beta-2</wagon.version> <plexus-security.version>1.0-alpha-10-SNAPSHOT</plexus-security.version>
167      */
168     public void testEffectiveProjectProperty()
169         throws Exception
170     {
171         initTestResolverFactory();
172         EffectiveProjectModelFilter filter = lookupEffective();
173 
174         String pomFile = "/org/apache/maven/archiva/archiva/1.0-SNAPSHOT/archiva-1.0-SNAPSHOT.pom";
175         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
176 
177         String plexusSecurityVersion = "1.0-alpha-10-SNAPSHOT";
178         String wagonVersion = "1.0-beta-2";
179 
180         boolean passedPlexusVersionChecking = false;
181         boolean passedWagonVersionChecking = false;
182 
183         List<Dependency> startDeps = startModel.getDependencyManagement();
184         for ( Dependency startDep : startDeps )
185         {
186             if ( "org.codehaus.plexus.security".equals( startDep.getGroupId() ) )
187             {
188                 assertEquals( startDep.getVersion(), "${plexus-security.version}" );
189             }
190             else if ( "org.apache.maven.wagon".equals( startDep.getGroupId() ) )
191             {
192                 assertEquals( startDep.getVersion(), "${wagon.version}" );
193             }
194         }
195 
196         ArchivaProjectModel effectiveModel = filter.filter( startModel );
197 
198         List<Dependency> effectiveDeps = effectiveModel.getDependencyManagement();
199         for ( Dependency dependency : effectiveDeps )
200         {
201             if ( "org.codehaus.plexus.security".equals( dependency.getGroupId() ) )
202             {
203                 assertEquals( dependency.getVersion(), plexusSecurityVersion );
204 
205                 if ( !passedPlexusVersionChecking )
206                 {
207                     passedPlexusVersionChecking = true;
208                 }
209             }
210             else if ( "org.apache.maven.wagon".equals( dependency.getGroupId() ) )
211             {
212                 assertEquals( dependency.getVersion(), wagonVersion );
213 
214                 if ( !passedWagonVersionChecking )
215                 {
216                     passedWagonVersionChecking = true;
217                 }
218             }
219 
220         }
221         assertTrue( passedPlexusVersionChecking );
222         assertTrue( passedWagonVersionChecking );
223     }
224 
225     // MRM-1194
226     public void testEffectiveProjectPropertyExistingParentHasUniqueSnapshotVersion()
227         throws Exception
228     {
229         initTestResolverFactory();
230         EffectiveProjectModelFilter filter = lookupEffective();
231 
232         String pomFile = "/org/apache/archiva/sample-project/2.1-SNAPSHOT/sample-project-2.1-SNAPSHOT.pom";
233         ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
234 
235         String buildHelperPluginVersion = "1.0";
236 
237         boolean passedBuildHelperVersionChecking = false;
238  
239         List<ArtifactReference> startPlugins = startModel.getPlugins();
240         for( ArtifactReference plugin : startPlugins )
241         {
242             if( "build-helper-maven-plugin".equals( plugin.getArtifactId() ) )
243             {
244                 assertEquals( "${build-helper-maven-plugin.version}", plugin.getVersion() );
245             }
246         }
247         
248         ArchivaProjectModel effectiveModel = filter.filter( startModel );
249         
250         List<ArtifactReference> effectivePlugins = effectiveModel.getPlugins();
251         for( ArtifactReference plugin : effectivePlugins )
252         {
253             if( "build-helper-maven-plugin".equals( plugin.getArtifactId() ) )
254             {
255                 assertEquals( buildHelperPluginVersion, plugin.getVersion() );
256                 
257                 if ( !passedBuildHelperVersionChecking )
258                 {
259                     passedBuildHelperVersionChecking = true;
260                 }
261             }
262         }
263         
264         assertTrue( passedBuildHelperVersionChecking );
265     }
266     
267     
268     private ProjectModelResolverFactory initTestResolverFactory()
269         throws Exception
270     {
271         ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
272 
273         resolverFactory.getCurrentResolverStack().clearResolvers();
274         resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
275 
276         return resolverFactory;
277     }
278 
279     private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
280     {
281         assertEquals( "Equivalent Models", expectedModel, effectiveModel );
282 
283         assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
284         dumpDependencyList( "Expected", expectedModel.getDependencies() );
285         dumpDependencyList( "Effective", effectiveModel.getDependencies() );
286         assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
287             .getDependencies() );
288         assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
289             .getDependencyManagement() );         
290     }
291 
292     private void dumpDependencyList( String type, List<Dependency> deps )
293     {
294         if ( deps == null )
295         {
296             System.out.println( " Dependencies [" + type + "] is null." );
297             return;
298         }
299 
300         if ( deps.isEmpty() )
301         {
302             System.out.println( " Dependencies [" + type + "] dependency list is empty." );
303             return;
304         }
305 
306         System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
307         Iterator<Dependency> it = deps.iterator();
308         while ( it.hasNext() )
309         {
310             Dependency dep = it.next();
311             System.out.println( "  " + Dependency.toKey( dep ) );
312         }
313         System.out.println( "" );
314     }
315 
316     private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
317     {
318         if ( ( expectedList == null ) && ( effectiveList == null ) )
319         {
320             return;
321         }
322 
323         if ( ( expectedList == null ) && ( effectiveList != null ) )
324         {
325             fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
326         }
327 
328         if ( ( expectedList != null ) && ( effectiveList == null ) )
329         {
330             fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
331         }
332 
333         assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
334     }
335 
336     private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
337                                                 List<Individual> effectiveList )
338     {
339         assertEquivalentLists( listId, expectedList, effectiveList );
340 
341         Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
342         Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
343 
344         Iterator<String> it = expectedMap.keySet().iterator();
345         while ( it.hasNext() )
346         {
347             String key = (String) it.next();
348 
349             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
350         }
351     }
352 
353     private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
354                                                  List<Dependency> effectiveList )
355     {
356         assertEquivalentLists( listId, expectedList, effectiveList );
357 
358         Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
359         Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
360 
361         Iterator<String> it = expectedMap.keySet().iterator();
362         while ( it.hasNext() )
363         {
364             String key = it.next();
365 
366             assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
367         }
368     }
369 
370     private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
371     {
372         Map<String, Individual> map = new HashMap<String, Individual>();
373         Iterator<Individual> it = individuals.iterator();
374         while ( it.hasNext() )
375         {
376             Individual individual = it.next();
377             String key = individual.getEmail();
378             map.put( key, individual );
379         }
380         return map;
381     }
382 
383     private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
384     {
385         Map<String, Dependency> map = new HashMap<String, Dependency>();
386         Iterator<Dependency> it = deps.iterator();
387         while ( it.hasNext() )
388         {
389             Dependency dep = it.next();
390             String key = Dependency.toKey( dep );
391             map.put( key, dep );
392         }
393         return map;
394     }
395 }