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.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;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
44 * EffectiveProjectModelFilterTest
48 public class EffectiveProjectModelFilterTest
49 extends AbstractRepositoryLayerTestCase
51 private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
53 private EffectiveProjectModelFilter lookupEffective()
56 return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
59 private ArchivaProjectModel createArchivaProjectModel( String path )
62 ProjectModelReader reader = new ProjectModel400Reader();
64 File pomFile = new File( getBasedir(), path );
66 return reader.read( pomFile );
69 private ProjectModelResolver createDefaultRepositoryResolver() throws Exception
71 File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
73 ManagedRepositoryContent repo = createManagedRepositoryContent( "defaultTestRepo", "Default Test Repo", defaultRepoDir, "default" );
75 ProjectModelReader reader = new ProjectModel400Reader();
76 ManagedRepositoryProjectResolver resolver = new ManagedRepositoryProjectResolver( repo, reader );
81 public void testBuildEffectiveProject()
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");
92 private void assertEffectiveProject(String pomFile, String effectivePomFile) throws Exception,
93 ProjectModelException {
94 initTestResolverFactory();
95 EffectiveProjectModelFilter filter = lookupEffective();
97 ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY + pomFile );
99 ArchivaProjectModel effectiveModel = filter.filter( startModel );
101 ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/expected-poms/" + effectivePomFile);
103 assertModel( expectedModel, effectiveModel );
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
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.
114 public void testBuildEffectiveSnapshotProject()
117 initTestResolverFactory();
118 EffectiveProjectModelFilter filter = lookupEffective();
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" };
134 for ( int i = 0; i < axisVersions.length; i++ )
136 assertTrue( "Version should be a unique snapshot.", VersionUtil.isUniqueSnapshot( axisVersions[i] ) );
138 ArchivaProjectModel initialModel = createArchivaProjectModel( DEFAULT_REPOSITORY
139 + "/org/apache/axis2/axis2/1.3-SNAPSHOT/axis2-" + axisVersions[i] + ".pom" );
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] );
145 assertEquals( "Base Version <" + baseVersion + "> of filename <" + axisVersions[i]
146 + "> should be equal to what is in model.", initialModel.getVersion(), baseVersion );
148 initialModel.setVersion( axisVersions[i] );
150 assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
153 ArchivaProjectModel effectiveModel = filter.filter( initialModel );
155 assertEquals( "Unique snapshot versions of initial model should be equal.", axisVersions[i], initialModel
157 assertEquals( "Unique snapshot versions of filtered/effective model should be equal.", axisVersions[i],
158 effectiveModel.getVersion() );
162 private ProjectModelResolverFactory initTestResolverFactory()
165 ProjectModelResolverFactory resolverFactory = (ProjectModelResolverFactory) lookup( ProjectModelResolverFactory.class );
167 resolverFactory.getCurrentResolverStack().clearResolvers();
168 resolverFactory.getCurrentResolverStack().addProjectModelResolver( createDefaultRepositoryResolver() );
170 return resolverFactory;
173 private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
175 assertEquals( "Equivalent Models", expectedModel, effectiveModel );
177 assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
178 dumpDependencyList( "Expected", expectedModel.getDependencies() );
179 dumpDependencyList( "Effective", effectiveModel.getDependencies() );
180 assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
181 .getDependencies() );
182 assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
183 .getDependencyManagement() );
186 private void dumpDependencyList( String type, List<Dependency> deps )
190 System.out.println( " Dependencies [" + type + "] is null." );
194 if ( deps.isEmpty() )
196 System.out.println( " Dependencies [" + type + "] dependency list is empty." );
200 System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
201 Iterator<Dependency> it = deps.iterator();
202 while ( it.hasNext() )
204 Dependency dep = it.next();
205 System.out.println( " " + Dependency.toKey( dep ) );
207 System.out.println( "" );
210 private void assertEquivalentLists( String listId, List<?> expectedList, List<?> effectiveList )
212 if ( ( expectedList == null ) && ( effectiveList == null ) )
217 if ( ( expectedList == null ) && ( effectiveList != null ) )
219 fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
222 if ( ( expectedList != null ) && ( effectiveList == null ) )
224 fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
227 assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
230 private void assertContainsSameIndividuals( String listId, List<Individual> expectedList,
231 List<Individual> effectiveList )
233 assertEquivalentLists( listId, expectedList, effectiveList );
235 Map<String, Individual> expectedMap = getIndividualsMap( expectedList );
236 Map<String, Individual> effectiveMap = getIndividualsMap( effectiveList );
238 Iterator<String> it = expectedMap.keySet().iterator();
239 while ( it.hasNext() )
241 String key = (String) it.next();
243 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
247 private void assertContainsSameDependencies( String listId, List<Dependency> expectedList,
248 List<Dependency> effectiveList )
250 assertEquivalentLists( listId, expectedList, effectiveList );
252 Map<String, Dependency> expectedMap = getDependencyMap( expectedList );
253 Map<String, Dependency> effectiveMap = getDependencyMap( effectiveList );
255 Iterator<String> it = expectedMap.keySet().iterator();
256 while ( it.hasNext() )
258 String key = it.next();
260 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
264 private Map<String, Individual> getIndividualsMap( List<Individual> individuals )
266 Map<String, Individual> map = new HashMap<String, Individual>();
267 Iterator<Individual> it = individuals.iterator();
268 while ( it.hasNext() )
270 Individual individual = it.next();
271 String key = individual.getEmail();
272 map.put( key, individual );
277 private Map<String, Dependency> getDependencyMap( List<Dependency> deps )
279 Map<String, Dependency> map = new HashMap<String, Dependency>();
280 Iterator<Dependency> it = deps.iterator();
281 while ( it.hasNext() )
283 Dependency dep = it.next();
284 String key = Dependency.toKey( dep );