1 package org.apache.maven.shared.dependency.tree;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.util.Iterator;
26
27 import org.apache.maven.artifact.Artifact;
28
29
30
31
32
33
34
35
36
37 public class DependencyNodeTest
38 extends AbstractDependencyNodeTest
39 {
40 private DependencyNode rootNode, node1, node2, node3, node4, node5, node6, node7;
41
42 protected void setUp()
43 throws Exception
44 {
45 super.setUp();
46
47
48
49
50
51
52
53
54 node1 = createNode( 1 );
55 node2 = createNode( node1, 2 );
56 node3 = createNode( node1, 3 );
57 node4 = createNode( node2, 4 );
58 node5 = createNode( node2, 5 );
59 node6 = createNode( node5, 6 );
60 node7 = createNode( node3, 7 );
61
62 rootNode = node1;
63 }
64
65 private void assertNode( Iterator<DependencyNode> it, DependencyNode node )
66 {
67 assertTrue( it.hasNext() );
68 assertSame( node, it.next() );
69 }
70
71 public void testPreorderIterator()
72 {
73 Iterator<DependencyNode> it = rootNode.iterator();
74
75 assertNode( it, node1 );
76 assertNode( it, node2 );
77 assertNode( it, node4 );
78 assertNode( it, node5 );
79 assertNode( it, node6 );
80 assertNode( it, node3 );
81 assertNode( it, node7 );
82 assertFalse( it.hasNext() );
83 }
84
85 public void testInverseIterator()
86 {
87 Iterator<DependencyNode> it = rootNode.inverseIterator();
88
89 assertNode( it, node7 );
90 assertNode( it, node3 );
91 assertNode( it, node6 );
92 assertNode( it, node5 );
93 assertNode( it, node4 );
94 assertNode( it, node2 );
95 assertNode( it, node1 );
96 assertFalse( it.hasNext() );
97 }
98
99 public void testToNodeStringIncluded()
100 {
101 Artifact artifact = createArtifact( "g:a:t:1:s" );
102 DependencyNode node = new DependencyNode( artifact );
103
104 assertEquals( "g:a:t:1:s", node.toNodeString() );
105 }
106
107 public void testToNodeStringIncludedWithManagedVersion()
108 {
109 Artifact artifact = createArtifact( "g:a:t:1:s" );
110 DependencyNode node = new DependencyNode( artifact );
111 node.setPremanagedVersion( "2" );
112
113 assertEquals( "g:a:t:1:s (version managed from 2)", node.toNodeString() );
114 }
115
116 public void testToNodeStringIncludedWithManagedScope()
117 {
118 Artifact artifact = createArtifact( "g:a:t:1:s" );
119 DependencyNode node = new DependencyNode( artifact );
120 node.setPremanagedScope( "x" );
121
122 assertEquals( "g:a:t:1:s (scope managed from x)", node.toNodeString() );
123 }
124
125 public void testToNodeStringIncludedWithUpdatedScope()
126 {
127 Artifact artifact = createArtifact( "g:a:t:1:s" );
128 DependencyNode node = new DependencyNode( artifact );
129 node.setOriginalScope( "x" );
130
131 assertEquals( "g:a:t:1:s (scope updated from x)", node.toNodeString() );
132 }
133
134 public void testToNodeStringOmittedForDuplicate()
135 {
136 Artifact artifact = createArtifact( "g:a:t:1:s" );
137 Artifact duplicateArtifact = createArtifact( "g:a:t:1:s" );
138 DependencyNode node = new DependencyNode( artifact, DependencyNode.OMITTED_FOR_DUPLICATE, duplicateArtifact );
139
140 assertEquals( "(g:a:t:1:s - omitted for duplicate)", node.toNodeString() );
141 }
142
143 public void testToNodeStringOmittedForDuplicateWithUpdatedScope()
144 {
145 Artifact artifact = createArtifact( "g:a:t:1:s" );
146 Artifact duplicateArtifact = createArtifact( "g:a:t:1:s" );
147 DependencyNode node = new DependencyNode( artifact, DependencyNode.OMITTED_FOR_DUPLICATE, duplicateArtifact );
148 node.setOriginalScope( "x" );
149
150 assertEquals( "(g:a:t:1:s - scope updated from x; omitted for duplicate)", node.toNodeString() );
151 }
152
153 public void testToNodeStringOmittedForConflict()
154 {
155 Artifact artifact = createArtifact( "g:a:t:1:s" );
156 Artifact conflictArtifact = createArtifact( "g:a:t:2:s" );
157 DependencyNode node = new DependencyNode(artifact, DependencyNode.OMITTED_FOR_CONFLICT, conflictArtifact);
158
159 assertEquals( "(g:a:t:1:s - omitted for conflict with 2)", node.toNodeString() );
160 }
161
162 public void testToNodeStringOmittedForCycle()
163 {
164 Artifact artifact = createArtifact( "g:a:t:1:s" );
165 DependencyNode node = new DependencyNode(artifact, DependencyNode.OMITTED_FOR_CYCLE);
166
167 assertEquals( "(g:a:t:1:s - omitted for cycle)", node.toNodeString() );
168 }
169
170 public void testToString()
171 throws Exception
172 {
173 BufferedReader reader = new BufferedReader( new StringReader( node1.toString() ) );
174
175 assertLine( reader, 1, 0 );
176 assertLine( reader, 2, 1 );
177 assertLine( reader, 4, 2 );
178 assertLine( reader, 5, 2 );
179 assertLine( reader, 6, 3 );
180 assertLine( reader, 3, 1 );
181 assertLine( reader, 7, 2 );
182 }
183
184 public void testOmitForConflict()
185 {
186 Artifact relatedArtifact = createArtifact( createArtifactId( 2, "3" ) );
187 node2.omitForConflict( relatedArtifact );
188
189 assertEquals( DependencyNode.OMITTED_FOR_CONFLICT, node2.getState() );
190 assertEquals( relatedArtifact, node2.getRelatedArtifact() );
191
192 assertTrue( node2.getChildren().isEmpty() );
193 assertNull( node4.getParent() );
194 assertNull( node5.getParent() );
195 }
196
197 public void testOmitForConflictWithDuplicate()
198 {
199 Artifact relatedArtifact = createArtifact( createArtifactId( 2 ) );
200 node2.omitForConflict( relatedArtifact );
201
202 assertEquals( DependencyNode.OMITTED_FOR_DUPLICATE, node2.getState() );
203 assertEquals( relatedArtifact, node2.getRelatedArtifact() );
204
205 assertTrue( node2.getChildren().isEmpty() );
206 assertNull( node4.getParent() );
207 assertNull( node5.getParent() );
208 }
209
210 public void testOmitForCycle()
211 {
212 node2.omitForCycle();
213
214 assertEquals( DependencyNode.OMITTED_FOR_CYCLE, node2.getState() );
215
216 assertTrue( node2.getChildren().isEmpty() );
217 assertNull( node4.getParent() );
218 assertNull( node5.getParent() );
219 }
220
221
222
223
224 public void testGetDepth()
225 {
226 assertEquals( 0, rootNode.getDepth() );
227 assertEquals( 0, node1.getDepth() );
228 assertEquals( 1, node2.getDepth() );
229 assertEquals( 1, node3.getDepth() );
230 assertEquals( 2, node4.getDepth() );
231 assertEquals( 2, node5.getDepth() );
232 assertEquals( 3, node6.getDepth() );
233 assertEquals( 2, node7.getDepth() );
234 }
235
236 private void assertLine( BufferedReader reader, int i, int depth )
237 throws IOException
238 {
239 String line = reader.readLine();
240 StringBuffer sb = new StringBuffer();
241 for ( int j = 0; j < depth; j++ )
242 {
243 sb.append( " " );
244 }
245 sb.append( "groupId" );
246 sb.append( i );
247 sb.append( ":artifactId" );
248 sb.append( i );
249 sb.append( ":jar:" );
250 sb.append( i );
251 sb.append( ":compile" );
252 assertEquals( sb.toString(), line );
253 }
254
255 private DependencyNode createNode( DependencyNode parent, int i )
256 {
257 DependencyNode node = createNode( i );
258
259 parent.addChild( node );
260
261 return node;
262 }
263
264 private DependencyNode createNode( int i )
265 {
266 return createNode( createArtifactId( i ) );
267 }
268
269 private String createArtifactId( int i )
270 {
271 return createArtifactId( i, Integer.toString( i ) );
272 }
273
274 private String createArtifactId( int i, String version )
275 {
276 return "groupId" + i + ":artifactId" + i + ":jar:" + version + ":compile";
277 }
278 }