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