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