diff --git a/maven-core/src/main/java/org/apache/maven/DefaultProjectDependencyGraph.java b/maven-core/src/main/java/org/apache/maven/DefaultProjectDependencyGraph.java index 84d2cc51e2..4074e584de 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultProjectDependencyGraph.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultProjectDependencyGraph.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.maven.execution.ProjectDependencyGraph; import org.apache.maven.project.DuplicateProjectException; @@ -32,7 +33,7 @@ import org.codehaus.plexus.util.dag.CycleDetectedException; /** * Describes the inter-dependencies between projects in the reactor. - * + * * @author Benjamin Bentmann */ class DefaultProjectDependencyGraph @@ -43,12 +44,13 @@ class DefaultProjectDependencyGraph /** * Creates a new project dependency graph based on the specified projects. - * + * * @param projects The projects to create the dependency graph with - * @throws DuplicateProjectException - * @throws CycleDetectedException + * @throws DuplicateProjectException + * @throws CycleDetectedException */ - public DefaultProjectDependencyGraph( Collection projects ) throws CycleDetectedException, DuplicateProjectException + public DefaultProjectDependencyGraph( Collection projects ) + throws CycleDetectedException, DuplicateProjectException { this.sorter = new ProjectSorter( projects ); } @@ -65,14 +67,14 @@ class DefaultProjectDependencyGraph throw new IllegalArgumentException( "project missing" ); } - Collection projectIds = new HashSet(); + Set projectIds = new HashSet(); getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive ); - return getProjects( projectIds ); + return getSortedProjects( projectIds ); } - private void getDownstreamProjects( String projectId, Collection projectIds, boolean transitive ) + private void getDownstreamProjects( String projectId, Set projectIds, boolean transitive ) { for ( String id : sorter.getDependents( projectId ) ) { @@ -90,11 +92,11 @@ class DefaultProjectDependencyGraph throw new IllegalArgumentException( "project missing" ); } - Collection projectIds = new HashSet(); + Set projectIds = new HashSet(); getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive ); - return getProjects( projectIds ); + return getSortedProjects( projectIds ); } private void getUpstreamProjects( String projectId, Collection projectIds, boolean transitive ) @@ -108,21 +110,19 @@ class DefaultProjectDependencyGraph } } - private List getProjects( Collection projectIds ) + private List getSortedProjects( Set projectIds ) { - List projects = new ArrayList( projectIds.size() ); + List result = new ArrayList( projectIds.size() ); - for ( String projectId : projectIds ) + for ( MavenProject mavenProject : sorter.getSortedProjects() ) { - MavenProject project = sorter.getProjectMap().get( projectId ); - - if ( project != null ) + if ( projectIds.contains( ProjectSorter.getId( mavenProject ) ) ) { - projects.add( project ); + result.add( mavenProject ); } } - return projects; + return result; } @Override diff --git a/maven-core/src/test/java/org/apache/maven/DefaultProjectDependencyGraphTest.java b/maven-core/src/test/java/org/apache/maven/DefaultProjectDependencyGraphTest.java new file mode 100644 index 0000000000..668dafb90d --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/DefaultProjectDependencyGraphTest.java @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.maven; + +import junit.framework.TestCase; +import org.apache.maven.execution.ProjectDependencyGraph; +import org.apache.maven.model.Dependency; +import org.apache.maven.project.DuplicateProjectException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.dag.CycleDetectedException; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Kristian Rosenvold + */ +public class DefaultProjectDependencyGraphTest + extends TestCase +{ + + private final MavenProject aProject = createA(); + + private final MavenProject depender1 = createProject( Arrays.asList( toDependency( aProject ) ), "depender1" ); + + private final MavenProject depender2 = createProject( Arrays.asList( toDependency( aProject ) ), "depender2" ); + + private final MavenProject depender3 = createProject( Arrays.asList( toDependency( aProject ) ), "depender3" ); + + private final MavenProject depender4 = + createProject( Arrays.asList( toDependency( aProject ), toDependency( depender3 ) ), "depender4" ); + + private final MavenProject transitiveOnly = + createProject( Arrays.asList( toDependency( depender3 ) ), "depender5" ); + + public void testGetSortedProjects() + throws DuplicateProjectException, CycleDetectedException + { + ProjectDependencyGraph graph = new DefaultProjectDependencyGraph( Arrays.asList( depender1, aProject ) ); + final List sortedProjects = graph.getSortedProjects(); + assertEquals( aProject, sortedProjects.get( 0 ) ); + assertEquals( depender1, sortedProjects.get( 1 ) ); + } + + public void testVerifyExpectedParentStructure() + throws CycleDetectedException, DuplicateProjectException + { + // This test verifies the baseline structure used in susequent tests. If this fails, the rest will fail. + ProjectDependencyGraph graph = threeProjectsDependingOnASingle(); + final List sortedProjects = graph.getSortedProjects(); + assertEquals( aProject, sortedProjects.get( 0 ) ); + assertEquals( depender1, sortedProjects.get( 1 ) ); + assertEquals( depender2, sortedProjects.get( 2 ) ); + assertEquals( depender3, sortedProjects.get( 3 ) ); + } + + public void testVerifyThatDownsteamProjectsComeInSortedOrder() + throws CycleDetectedException, DuplicateProjectException + { + final List downstreamProjects = + threeProjectsDependingOnASingle().getDownstreamProjects( aProject, true ); + assertEquals( depender1, downstreamProjects.get( 0 ) ); + assertEquals( depender2, downstreamProjects.get( 1 ) ); + assertEquals( depender3, downstreamProjects.get( 2 ) ); + } + + public void testTransitivesInOrder() + throws CycleDetectedException, DuplicateProjectException + { + final ProjectDependencyGraph graph = + new DefaultProjectDependencyGraph( Arrays.asList( depender1, depender4, depender2, depender3, aProject ) ); + + final List downstreamProjects = graph.getDownstreamProjects( aProject, true ); + assertEquals( depender1, downstreamProjects.get( 0 ) ); + assertEquals( depender3, downstreamProjects.get( 1 ) ); + assertEquals( depender4, downstreamProjects.get( 2 ) ); + assertEquals( depender2, downstreamProjects.get( 3 ) ); + } + + public void testNonTransitivesInOrder() + throws CycleDetectedException, DuplicateProjectException + { + final ProjectDependencyGraph graph = + new DefaultProjectDependencyGraph( Arrays.asList( depender1, depender4, depender2, depender3, aProject ) ); + + final List downstreamProjects = graph.getDownstreamProjects( aProject, false ); + assertEquals( depender1, downstreamProjects.get( 0 ) ); + assertEquals( depender3, downstreamProjects.get( 1 ) ); + assertEquals( depender4, downstreamProjects.get( 2 ) ); + assertEquals( depender2, downstreamProjects.get( 3 ) ); + } + + public void testWithTranistiveOnly() + throws CycleDetectedException, DuplicateProjectException + { + final ProjectDependencyGraph graph = new DefaultProjectDependencyGraph( + Arrays.asList( depender1, transitiveOnly, depender2, depender3, aProject ) ); + + final List downstreamProjects = graph.getDownstreamProjects( aProject, true ); + assertEquals( depender1, downstreamProjects.get( 0 ) ); + assertEquals( depender3, downstreamProjects.get( 1 ) ); + assertEquals( transitiveOnly, downstreamProjects.get( 2 ) ); + assertEquals( depender2, downstreamProjects.get( 3 ) ); + } + + public void testWithMissingTranistiveOnly() + throws CycleDetectedException, DuplicateProjectException + { + final ProjectDependencyGraph graph = new DefaultProjectDependencyGraph( + Arrays.asList( depender1, transitiveOnly, depender2, depender3, aProject ) ); + + final List downstreamProjects = graph.getDownstreamProjects( aProject, false ); + assertEquals( depender1, downstreamProjects.get( 0 ) ); + assertEquals( depender3, downstreamProjects.get( 1 ) ); + assertEquals( depender2, downstreamProjects.get( 2 ) ); + } + + public void testGetUpstreamProjects() + throws CycleDetectedException, DuplicateProjectException + { + ProjectDependencyGraph graph = threeProjectsDependingOnASingle(); + final List downstreamProjects = graph.getUpstreamProjects( depender1, true ); + assertEquals( aProject, downstreamProjects.get( 0 ) ); + } + + private ProjectDependencyGraph threeProjectsDependingOnASingle() + throws CycleDetectedException, DuplicateProjectException + { + return new DefaultProjectDependencyGraph( Arrays.asList( depender1, depender2, depender3, aProject ) ); + } + + private static MavenProject createA() + { + MavenProject result = new MavenProject(); + result.setGroupId( "org.apache" ); + result.setArtifactId( "A" ); + result.setVersion( "1.2" ); + return result; + } + + static Dependency toDependency( MavenProject mavenProject ) + { + final Dependency dependency = new Dependency(); + dependency.setArtifactId( mavenProject.getArtifactId() ); + dependency.setGroupId( mavenProject.getGroupId() ); + dependency.setVersion( mavenProject.getVersion() ); + return dependency; + } + + private static MavenProject createProject( List dependencies, String artifactId ) + { + MavenProject result = new MavenProject(); + result.setGroupId( "org.apache" ); + result.setArtifactId( artifactId ); + result.setVersion( "1.2" ); + result.setDependencies( dependencies ); + return result; + } + +} \ No newline at end of file