package org.apache.maven.plugin.pom; /* * 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. */ import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.List; import org.apache.maven.model.Dependency; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; /** * Tests TreeMojo. * */ public class TestSortMojo extends AbstractPomMojoTestCase { // TestCase methods ------------------------------------------------------- /* * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp() */ protected void setUp() throws Exception { // required for mojo lookups to work super.setUp( "sort", false ); } // tests ------------------------------------------------------------------ /** * Tests the proper sorting of dependencies. * * @throws Exception */ public void testSortDependencies() throws Exception { File testPom = new File( getBasedir(), "target/test-classes/unit/dependency-sort-test/pom.xml" ); Xpp3Dom testPomDom = Xpp3DomBuilder.build( new FileReader( testPom ) ); SortDependenciesMojo mojo = new SortDependenciesMojo(); MavenProject testProject = new MavenProject(); List dependencies = this.extractDependencyConfiguration(testPomDom); testProject.setDependencies(dependencies); mojo.setProject(testProject); File outputFile = new File(testDir.getAbsolutePath() + "/sortOutput.txt"); setVariableValueToObject( mojo, "outputFile", outputFile ); setVariableValueToObject( mojo, "ascending", Boolean.TRUE ); // Test basic sorting functionality mojo.execute(); assertDependencyEquals((Dependency)dependencies.get(0), "group a", "artifact b", "5"); assertDependencyEquals((Dependency)dependencies.get(1), "group b", "artifact a", "2"); assertDependencyEquals((Dependency)dependencies.get(2), "group b", "artifact b", "2"); assertDependencyEquals((Dependency)dependencies.get(3), "group c", "artifact a", "1"); assertDependencyEquals((Dependency)dependencies.get(4), "group c", "artifact a", "2"); assertDependencyEquals((Dependency)dependencies.get(5), "group c", "artifact c", "4"); // Test groupByScope setVariableValueToObject( mojo, "groupByScope", Boolean.TRUE ); mojo.execute(); //List dependencies = extractDependencies(children); assertDependencyEquals((Dependency)dependencies.get(0), "group b", "artifact a", "2"); assertDependencyEquals((Dependency)dependencies.get(1), "group c", "artifact a", "1"); assertDependencyEquals((Dependency)dependencies.get(2), "group c", "artifact a", "2"); assertDependencyEquals((Dependency)dependencies.get(3), "group c", "artifact c", "4"); // provided scope assertDependencyEquals((Dependency)dependencies.get(4), "group b", "artifact b", "2"); //test scope assertDependencyEquals((Dependency)dependencies.get(5), "group a", "artifact b", "5"); } // private methods -------------------------------------------------------- private void assertDependencyEquals( Dependency dependency, String groupId, String artifactId, String version ) { assertEquals( dependency.getGroupId(), groupId ); assertEquals( dependency.getArtifactId(), artifactId ); assertEquals( dependency.getVersion(), version ); } /** * @param pomDom * @return the List of dependencies */ protected List extractDependencyConfiguration( Xpp3Dom pomDom ) throws Exception { List dependencies = null; Xpp3Dom dependenciesElement = pomDom.getChild( "dependencies" ); if ( dependenciesElement != null ) { Xpp3Dom[] dependencyElements = dependenciesElement.getChildren(); dependencies = extractDependencies(dependencyElements); } return dependencies; } protected List extractDependencies( Xpp3Dom [] dependencyElements ) { List dependencies = new ArrayList(); for (int i = 0; i < dependencyElements.length; i++) { Xpp3Dom dependencyElement = dependencyElements[i]; Dependency dependency = new Dependency(); String dependencyElementArtifactId = dependencyElement .getChild("artifactId").getValue(); dependency.setArtifactId(dependencyElementArtifactId); String dependencyElementGrouptId = dependencyElement.getChild( "groupId").getValue(); dependency.setGroupId(dependencyElementGrouptId); Xpp3Dom dependencyElementVersion = dependencyElement .getChild("version"); if (dependencyElementVersion != null) { dependency.setVersion(dependencyElementVersion.getValue()); } Xpp3Dom dependencyElementScope = dependencyElement.getChild("scope"); if (dependencyElementScope != null) { dependency.setScope(dependencyElementScope.getValue()); } Xpp3Dom dependencyElementClassifier = dependencyElement.getChild("classifier"); if (dependencyElementClassifier != null) { dependency.setClassifier(dependencyElementClassifier.getValue()); } Xpp3Dom dependencyElementType = dependencyElement.getChild("type"); if (dependencyElementType != null) { dependency.setType(dependencyElementType.getValue()); } dependencies.add(dependency); } return dependencies; } }