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