1 package org.apache.maven.archiva.repository.project.filters;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
45 * EffectiveProjectModelFilterTest
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 public class EffectiveProjectModelFilterTest
51 extends AbstractRepositoryLayerTestCase
53 private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
55 private EffectiveProjectModelFilter lookupEffective()
58 return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
61 private ArchivaProjectModel createArchivaProjectModel( String path )
62 throws ProjectModelException
64 ProjectModelReader reader = new ProjectModel400Reader();
66 File pomFile = new File( getBasedir(), path );
68 return reader.read( pomFile );
71 private ProjectModelResolver createDefaultRepositoryResolver()
73 File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
75 ManagedRepositoryConfiguration repo = createRepository( "defaultTestRepo", "Default Test Repo", defaultRepoDir );
77 ProjectModelReader reader = new ProjectModel400Reader();
78 BidirectionalRepositoryLayout layout = new DefaultBidirectionalRepositoryLayout();
79 ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader, layout );
84 public void testBuildEffectiveProject()
87 initTestResolverFactory();
88 EffectiveProjectModelFilter filter = lookupEffective();
90 ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY
91 + "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom" );
93 ArchivaProjectModel effectiveModel = filter.filter( startModel );
95 ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/"
96 + "/archiva-model-effective.pom" );
98 assertModel( expectedModel, effectiveModel );
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
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.
108 public void testBuildEffectiveSnapshotProject()
111 initTestResolverFactory();
112 EffectiveProjectModelFilter filter = lookupEffective();
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" };
128 for ( int i = 0; i < axisVersions.length; i++ )
130 assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
132 ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
133 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
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] );
139 assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
140 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
142 initialModel.setVersion( axisVersions[i] );
144 assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
147 ArchivaProjectModel effectiveModel = filter.filter( initialModel );
149 assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
151 assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
152 effectiveModel.getVersion() );
156 private ProjectModelResolverFactory initTestResolverFactory()
159 ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
161 resolverFactory.getCurrentResolverStack().clearResolvers();
162 resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
164 return resolverFactory;
167 private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
169 assertEquals( "Equivalent Models", expectedModel, effectiveModel );
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() );
180 private void dumpDependencyList( String type, List<Dependency> deps )
184 System.out.println( " Dependencies [" + type + "] is null." );
188 if ( deps.isEmpty() )
190 System.out.println( " Dependencies [" + type + "] dependency list is empty." );
194 System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
195 Iterator<Dependency> it = deps.iterator();
196 while ( it.hasNext() )
198 Dependency dep = it.next();
199 System.out.println( " " + Dependency.toKey( dep ) );
201 System.out.println( "" );
204 private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
206 if ( ( expectedList == null ) && ( effectiveList == null ) )
211 if ( ( expectedList == null ) && ( effectiveList != null ) )
213 fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
216 if ( ( expectedList != null ) && ( effectiveList == null ) )
218 fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
221 assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
224 private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
225 List<Individual> effectiveList )
227 assertEquivalentLists( listId, expectedList, effectiveList );
229 Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
230 Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
232 Iterator<String> it = expectedMap.keySet().iterator();
233 while ( it.hasNext() )
235 String key = (String) it.next();
237 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
241 private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
242 List<Dependency> effectiveList )
244 assertEquivalentLists( listId, expectedList, effectiveList );
246 Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
247 Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
249 Iterator<String> it = expectedMap.keySet().iterator();
250 while ( it.hasNext() )
252 String key = it.next();
254 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
258 private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
260 Map<String, Individual> map = new HashMap<String, Individual>();
261 Iterator<Individual> it = individuals.iterator();
262 while ( it.hasNext() )
264 Individual individual = it.next();
265 String key = individual.getEmail();
266 map.put( key, individual );
271 private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
273 Map<String, Dependency> map = new HashMap<String, Dependency>();
274 Iterator<Dependency> it = deps.iterator();
275 while ( it.hasNext() )
277 Dependency dep = it.next();
278 String key = Dependency.toVersionlessKey( dep );