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.model.ArchivaProjectModel;
23 import org.apache.maven.archiva.model.ArchivaRepository;
24 import org.apache.maven.archiva.model.Dependency;
25 import org.apache.maven.archiva.model.Individual;
26 import org.apache.maven.archiva.repository.project.ProjectModelException;
27 import org.apache.maven.archiva.repository.project.ProjectModelFilter;
28 import org.apache.maven.archiva.repository.project.ProjectModelReader;
29 import org.apache.maven.archiva.repository.project.ProjectModelResolver;
30 import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
31 import org.apache.maven.archiva.repository.project.resolvers.RepositoryProjectResolver;
32 import org.codehaus.plexus.PlexusTestCase;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
41 * EffectiveProjectModelFilterTest
43 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
46 public class EffectiveProjectModelFilterTest
47 extends PlexusTestCase
49 private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
51 private EffectiveProjectModelFilter lookupEffective()
54 return (EffectiveProjectModelFilter) lookup( ProjectModelFilter.class, "effective" );
57 private ArchivaProjectModel createArchivaProjectModel( String path )
58 throws ProjectModelException
60 ProjectModelReader reader = new ProjectModel400Reader();
62 File pomFile = new File( getBasedir(), path );
64 return reader.read( pomFile );
67 private ProjectModelResolver createDefaultRepositoryResolver()
69 File defaultRepoDir = new File( getBasedir(), DEFAULT_REPOSITORY );
71 ArchivaRepository repo = new ArchivaRepository( "defaultTestRepo", "Default Test Repo", "file://"
72 + defaultRepoDir.getAbsolutePath() );
74 RepositoryProjectResolver resolver = new RepositoryProjectResolver( repo );
79 public void testBuildEffectiveProject()
82 EffectiveProjectModelFilter filter = lookupEffective();
84 filter.addProjectModelResolver( createDefaultRepositoryResolver() );
86 ArchivaProjectModel startModel = createArchivaProjectModel( DEFAULT_REPOSITORY
87 + "/org/apache/maven/archiva/archiva-model/1.0-SNAPSHOT/archiva-model-1.0-SNAPSHOT.pom" );
89 ArchivaProjectModel effectiveModel = filter.filter( startModel );
91 ArchivaProjectModel expectedModel = createArchivaProjectModel( "src/test/effective-poms/"
92 + "/archiva-model-effective.pom" );
94 assertModel( expectedModel, effectiveModel );
97 private void assertModel( ArchivaProjectModel expectedModel, ArchivaProjectModel effectiveModel )
99 assertEquals( "Equivalent Models", expectedModel, effectiveModel );
101 assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
102 dumpDependencyList( "Expected", expectedModel.getDependencies() );
103 dumpDependencyList( "Effective", effectiveModel.getDependencies() );
104 assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(),
105 effectiveModel.getDependencies() );
106 assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(),
107 effectiveModel.getDependencyManagement() );
110 private void dumpDependencyList( String type, List deps )
114 System.out.println( " Dependencies [" + type + "] is null." );
118 if ( deps.isEmpty() )
120 System.out.println( " Dependencies [" + type + "] dependency list is empty." );
124 System.out.println( ".\\ [" + type + "] Dependency List (size:" + deps.size() + ") \\.________________" );
125 Iterator it = deps.iterator();
126 while ( it.hasNext() )
128 Dependency dep = (Dependency) it.next();
129 System.out.println( " " + Dependency.toKey( dep ) );
131 System.out.println( "" );
134 private void assertEquivalentLists( String listId, List expectedList, List effectiveList )
136 if ( ( expectedList == null ) && ( effectiveList == null ) )
141 if ( ( expectedList == null ) && ( effectiveList != null ) )
143 fail( "Effective [" + listId + "] List is instantiated, while expected List is null." );
146 if ( ( expectedList != null ) && ( effectiveList == null ) )
148 fail( "Effective [" + listId + "] List is null, while expected List is instantiated." );
151 assertEquals( "[" + listId + "] List Size", expectedList.size(), expectedList.size() );
154 private void assertContainsSameIndividuals( String listId, List expectedList, List effectiveList )
156 assertEquivalentLists( listId, expectedList, effectiveList );
158 Map expectedMap = getIndividualsMap( expectedList );
159 Map effectiveMap = getIndividualsMap( effectiveList );
161 Iterator it = expectedMap.keySet().iterator();
162 while ( it.hasNext() )
164 String key = (String) it.next();
166 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
170 private void assertContainsSameDependencies( String listId, List expectedList, List effectiveList )
172 assertEquivalentLists( listId, expectedList, effectiveList );
174 Map expectedMap = getDependencyMap( expectedList );
175 Map effectiveMap = getDependencyMap( effectiveList );
177 Iterator it = expectedMap.keySet().iterator();
178 while ( it.hasNext() )
180 String key = (String) it.next();
182 assertTrue( "Should exist in Effective [" + listId + "] list: " + key, effectiveMap.containsKey( key ) );
186 private Map getIndividualsMap( List deps )
188 Map map = new HashMap();
189 Iterator it = deps.iterator();
190 while ( it.hasNext() )
192 Object o = it.next();
193 assertTrue( "List contains Individual entries. (found " + o.getClass().getName() + " instead)",
194 o instanceof Individual );
195 Individual individual = (Individual) o;
196 String key = individual.getEmail();
197 map.put( key, individual );
202 private Map getDependencyMap( List deps )
204 Map map = new HashMap();
205 Iterator it = deps.iterator();
206 while ( it.hasNext() )
208 Object o = it.next();
209 assertTrue( "List contains Dependency entries. (found " + o.getClass().getName() + " instead)",
210 o instanceof Dependency );
211 Dependency dep = (Dependency) o;
212 String key = Dependency.toVersionlessKey( dep );