View Javadoc
1   package org.apache.maven.plugin.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.plugin.dependency.AbstractDependencyMojoTestCase;
31  import org.apache.maven.plugin.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: TestTreeMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
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
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
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
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, "<y:NodeLabel>testGroupId:snapshot:jar:2.0-SNAPSHOT:compile</y:NodeLabel>" ) );
125         assertTrue( findString( contents, "<y:NodeLabel>testGroupId:release:jar:1.0:compile</y:NodeLabel>" ) );
126         assertTrue( findString( contents, "<key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/>" ) );
127         assertTrue( findString( contents, "<key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/>" ) );
128     }
129 
130     /**
131      * Test the TGF format serialization
132      *
133      * @throws Exception
134      */
135     public void _testTreeTGFSerializing()
136         throws Exception
137     {
138         List<String> contents = runTreeMojo( "tree1.tgf", "tgf" );
139         assertTrue( findString( contents, "testGroupId:project:jar:1.0:compile" ) );
140         assertTrue( findString( contents, "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile" ) );
141         assertTrue( findString( contents, "testGroupId:release:jar:1.0:compile" ) );
142     }
143 
144     /**
145      * Help finding content in the given list of string
146      * @param outputFile
147      * @param format
148      * @return list of strings in the output file
149      */
150     private List<String> runTreeMojo( String outputFile, String format )
151              throws Exception
152     {
153         File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
154         String outputFileName = testDir.getAbsolutePath() + outputFile;
155         TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
156         setVariableValueToObject( mojo, "outputType", format );
157         setVariableValueToObject( mojo, "outputFile", new File( outputFileName ) );
158 
159         assertNotNull( mojo );
160         assertNotNull( mojo.getProject() );
161         MavenProject project = mojo.getProject();
162         project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
163 
164         Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
165         Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
166         artifacts.addAll( directArtifacts );
167 
168         project.setArtifacts( artifacts );
169         project.setDependencyArtifacts( directArtifacts );
170 
171         mojo.execute();
172 
173         BufferedReader fp1 = new BufferedReader( new FileReader( outputFileName ) );
174         List<String> contents = new ArrayList<String>();
175 
176         String line;
177         while ( ( line = fp1.readLine() ) != null )
178         {
179             contents.add( line );
180         }
181         fp1.close();
182 
183         return contents ;
184     }
185 
186     /**
187      * Help finding content in the given list of string
188      * @param contents
189      * @param str
190      */
191     private boolean findString( List<String> contents, String str )
192     {
193         for ( String line : contents )
194         {
195             if (line.contains(str))
196             {
197                 // if match then return here
198                 return true;
199             }
200         }
201 
202         // in case no match for the whole list
203         return false;
204     }
205 
206     // private methods --------------------------------------------------------
207 
208     private void assertChildNodeEquals( String expectedNode, DependencyNode actualParentNode, int actualChildIndex )
209     {
210         DependencyNode actualNode = actualParentNode.getChildren().get( actualChildIndex );
211 
212         assertNodeEquals( expectedNode, actualNode );
213     }
214 
215     private void assertNodeEquals( String expectedNode, DependencyNode actualNode )
216     {
217         String[] tokens = expectedNode.split( ":" );
218 
219         assertNodeEquals( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], actualNode );
220     }
221 
222     private void assertNodeEquals( String expectedGroupId, String expectedArtifactId, String expectedType,
223                                    String expectedVersion, String expectedScope, DependencyNode actualNode )
224     {
225         Artifact actualArtifact = actualNode.getArtifact();
226 
227         assertEquals( "group id", expectedGroupId, actualArtifact.getGroupId() );
228         assertEquals( "artifact id", expectedArtifactId, actualArtifact.getArtifactId() );
229         assertEquals( "type", expectedType, actualArtifact.getType() );
230         assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
231         assertEquals( "scope", expectedScope, actualArtifact.getScope() );
232     }
233 }