View Javadoc

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