View Javadoc
1   package org.apache.maven.plugins.dependency.tree;
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.io.BufferedReader;
24  import java.io.FileReader;
25  import java.util.Set;
26  import java.util.List;
27  import java.util.ArrayList;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
31  import org.apache.maven.plugins.dependency.tree.TreeMojo;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.shared.dependency.graph.DependencyNode;
34  
35  /**
36   * Tests <code>TreeMojo</code>.
37   *
38   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
39   * @version $Id$
40   * @since 2.0
41   */
42  public class TestTreeMojo
43      extends AbstractDependencyMojoTestCase
44  {
45      // TestCase methods -------------------------------------------------------
46  
47      /*
48       * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp()
49       */
50      protected void setUp()
51          throws Exception
52      {
53          // required for mojo lookups to work
54          super.setUp( "tree", false );
55      }
56  
57      // tests ------------------------------------------------------------------
58  
59      public void testVoid()
60      {
61          // TODO: tests disabled during MDEP-339 work, to be reactivated
62      }
63  
64      /**
65       * Tests the proper discovery and configuration of the mojo.
66       *
67       * @throws Exception in case of an error.
68       */
69      public void _testTreeTestEnvironment()
70          throws Exception
71      {
72          File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
73          TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
74  
75          assertNotNull( mojo );
76          assertNotNull( mojo.getProject() );
77          MavenProject project = mojo.getProject();
78          project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
79  
80          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
81          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
82          artifacts.addAll( directArtifacts );
83  
84          project.setArtifacts( artifacts );
85          project.setDependencyArtifacts( directArtifacts );
86  
87          mojo.execute();
88  
89          DependencyNode rootNode = mojo.getDependencyGraph();
90          assertNodeEquals( "testGroupId:project:jar:1.0:compile", rootNode );
91          assertEquals( 2, rootNode.getChildren().size() );
92          assertChildNodeEquals( "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 0 );
93          assertChildNodeEquals( "testGroupId:release:jar:1.0:compile", rootNode, 1 );
94      }
95  
96      /**
97       * Test the DOT format serialization
98       *
99       * @throws Exception in case of an error.
100      */
101     public void _testTreeDotSerializing()
102         throws Exception
103     {
104         List<String> contents = runTreeMojo( "tree1.dot", "dot" );
105         assertTrue( findString( contents, "digraph \"testGroupId:project:jar:1.0:compile\" {" ) );
106         assertTrue( findString( contents,
107                                 "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:snapshot:jar:2.0-SNAPSHOT:compile\"" ) );
108         assertTrue( findString( contents,
109                                 "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:release:jar:1.0:compile\"" ) );
110     }
111 
112     /**
113      * Test the GraphML format serialization
114      * 
115      * @throws Exception in case of an error.
116      */
117     public void _testTreeGraphMLSerializing()
118         throws Exception
119     {
120         List<String> contents = runTreeMojo( "tree1.graphml", "graphml" );
121 
122         assertTrue( findString( contents, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ) );
123         assertTrue( findString( contents, "<y:NodeLabel>testGroupId:project:jar:1.0:compile</y:NodeLabel>" ) );
124         assertTrue( findString( contents,
125                                 "<y:NodeLabel>testGroupId:snapshot:jar:2.0-SNAPSHOT:compile</y:NodeLabel>" ) );
126         assertTrue( findString( contents, "<y:NodeLabel>testGroupId:release:jar:1.0:compile</y:NodeLabel>" ) );
127         assertTrue( findString( contents, "<key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/>" ) );
128         assertTrue( findString( contents, "<key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/>" ) );
129     }
130 
131     /**
132      * Test the TGF format serialization
133      * 
134      * @throws Exception in case of an error.
135      */
136     public void _testTreeTGFSerializing()
137         throws Exception
138     {
139         List<String> contents = runTreeMojo( "tree1.tgf", "tgf" );
140         assertTrue( findString( contents, "testGroupId:project:jar:1.0:compile" ) );
141         assertTrue( findString( contents, "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile" ) );
142         assertTrue( findString( contents, "testGroupId:release:jar:1.0:compile" ) );
143     }
144 
145     /**
146      * Help finding content in the given list of string
147      * 
148      * @param outputFile the outputFile.
149      * @param format The format.
150      * @throws Exception in case of an error.
151      * @return list of strings in the output file
152      */
153     private List<String> runTreeMojo( String outputFile, String format )
154         throws Exception
155     {
156         File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
157         String outputFileName = testDir.getAbsolutePath() + outputFile;
158         TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
159         setVariableValueToObject( mojo, "outputType", format );
160         setVariableValueToObject( mojo, "outputFile", new File( outputFileName ) );
161 
162         assertNotNull( mojo );
163         assertNotNull( mojo.getProject() );
164         MavenProject project = mojo.getProject();
165         project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
166 
167         Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
168         Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
169         artifacts.addAll( directArtifacts );
170 
171         project.setArtifacts( artifacts );
172         project.setDependencyArtifacts( directArtifacts );
173 
174         mojo.execute();
175 
176         BufferedReader fp1 = new BufferedReader( new FileReader( outputFileName ) );
177         List<String> contents = new ArrayList<String>();
178 
179         String line;
180         while ( ( line = fp1.readLine() ) != null )
181         {
182             contents.add( line );
183         }
184         fp1.close();
185 
186         return contents;
187     }
188 
189     /**
190      * Help finding content in the given list of string
191      * 
192      * @param contents The contents.
193      * @param str The content which should be checked for.
194      */
195     private boolean findString( List<String> contents, String str )
196     {
197         for ( String line : contents )
198         {
199             if ( line.contains( str ) )
200             {
201                 // if match then return here
202                 return true;
203             }
204         }
205 
206         // in case no match for the whole list
207         return false;
208     }
209 
210     // private methods --------------------------------------------------------
211 
212     private void assertChildNodeEquals( String expectedNode, DependencyNode actualParentNode, int actualChildIndex )
213     {
214         DependencyNode actualNode = actualParentNode.getChildren().get( actualChildIndex );
215 
216         assertNodeEquals( expectedNode, actualNode );
217     }
218 
219     private void assertNodeEquals( String expectedNode, DependencyNode actualNode )
220     {
221         String[] tokens = expectedNode.split( ":" );
222 
223         assertNodeEquals( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], actualNode );
224     }
225 
226     private void assertNodeEquals( String expectedGroupId, String expectedArtifactId, String expectedType,
227                                    String expectedVersion, String expectedScope, DependencyNode actualNode )
228     {
229         Artifact actualArtifact = actualNode.getArtifact();
230 
231         assertEquals( "group id", expectedGroupId, actualArtifact.getGroupId() );
232         assertEquals( "artifact id", expectedArtifactId, actualArtifact.getArtifactId() );
233         assertEquals( "type", expectedType, actualArtifact.getType() );
234         assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
235         assertEquals( "scope", expectedScope, actualArtifact.getScope() );
236     }
237 }