1   package org.apache.maven.plugin.dependency;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.shared.dependency.tree.DependencyNode;
29  
30  /**
31   * Tests <code>TreeMojo</code>.
32   * 
33   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
34   * @version $Id: TestTreeMojo.java 728546 2008-12-21 22:56:51Z bentmann $
35   * @since 2.0
36   */
37  public class TestTreeMojo
38      extends AbstractDependencyMojoTestCase
39  {
40      // TestCase methods -------------------------------------------------------
41      
42      /*
43       * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp()
44       */
45      protected void setUp()
46          throws Exception
47      {
48          // required for mojo lookups to work
49          super.setUp( "tree", false );
50      }
51      
52      // tests ------------------------------------------------------------------
53  
54      /**
55       * Tests the proper discovery and configuration of the mojo.
56       * 
57       * @throws Exception
58       */
59      public void testTreeTestEnvironment()
60          throws Exception
61      {
62          File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
63          TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
64          setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ) );
65  
66          assertNotNull( mojo );
67          assertNotNull( mojo.getProject() );
68          MavenProject project = mojo.getProject();
69          project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
70  
71          Set artifacts = this.stubFactory.getScopedArtifacts();
72          Set directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
73          artifacts.addAll( directArtifacts );
74  
75          project.setArtifacts( artifacts );
76          project.setDependencyArtifacts( directArtifacts );
77  
78          mojo.execute();
79          
80          DependencyNode rootNode = mojo.getDependencyTree();
81          assertNodeEquals( "testGroupId:project:jar:1.0:compile", rootNode);
82          assertEquals( 2, rootNode.getChildren().size() );
83          assertChildNodeEquals( "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 0 );
84          assertChildNodeEquals( "testGroupId:release:jar:1.0:compile", rootNode, 1 );
85      }
86      
87      // private methods --------------------------------------------------------
88      
89      private void assertChildNodeEquals( String expectedNode, DependencyNode actualParentNode, int actualChildIndex )
90      {
91          DependencyNode actualNode = (DependencyNode) actualParentNode.getChildren().get( actualChildIndex );
92          
93          assertNodeEquals( expectedNode, actualNode );
94      }
95      
96      private void assertNodeEquals( String expectedNode, DependencyNode actualNode )
97      {
98          String[] tokens = expectedNode.split( ":" );
99  
100         assertNodeEquals( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], actualNode );
101     }
102 
103     private void assertNodeEquals( String expectedGroupId, String expectedArtifactId, String expectedType,
104                                    String expectedVersion, String expectedScope, DependencyNode actualNode )
105     {
106         Artifact actualArtifact = actualNode.getArtifact();
107 
108         assertEquals( "group id", expectedGroupId, actualArtifact.getGroupId() );
109         assertEquals( "artifact id", expectedArtifactId, actualArtifact.getArtifactId() );
110         assertEquals( "type", expectedType, actualArtifact.getType() );
111         assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
112         assertEquals( "scope", expectedScope, actualArtifact.getScope() );
113     }
114 }