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