View Javadoc

1   package org.apache.maven.shared.dependency.tree.traversal;
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.StringWriter;
23  
24  import org.apache.maven.shared.dependency.tree.AbstractDependencyNodeTest;
25  import org.apache.maven.shared.dependency.tree.DependencyNode;
26  
27  /**
28   * Tests <code>SerializingDependencyNodeVisitor</code>.
29   * 
30   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
31   * @version $Id: SerializingDependencyNodeVisitorTest.java 1100703 2011-05-08 08:27:33Z hboutemy $
32   * @see SerializingDependencyNodeVisitor
33   */
34  public class SerializingDependencyNodeVisitorTest
35      extends AbstractDependencyNodeTest
36  {
37      // constants --------------------------------------------------------------
38  
39      private static final String NEWLINE = System.getProperty( "line.separator" );
40  
41      // fields -----------------------------------------------------------------
42  
43      private StringWriter writer;
44  
45      private SerializingDependencyNodeVisitor serializer;
46  
47      // TestCase methods -------------------------------------------------------
48  
49      /**
50       * {@inheritDoc}
51       */
52      protected void setUp()
53          throws Exception
54      {
55          writer = new StringWriter();
56  
57          serializer = new SerializingDependencyNodeVisitor( writer, SerializingDependencyNodeVisitor.STANDARD_TOKENS );
58      }
59  
60      // tests ------------------------------------------------------------------
61  
62      public void testSingleNode()
63      {
64          DependencyNode rootNode = createNode( "g:p:t:1" );
65  
66          assertTree( "g:p:t:1" + NEWLINE, rootNode );
67      }
68      
69      public void testNodeWithChild()
70      {
71          DependencyNode rootNode = createNode( "g:p:t:1" );
72          rootNode.addChild( createNode( "g:a:t:1" ) );
73  
74          assertTree(
75              "g:p:t:1" + NEWLINE + 
76              "\\- g:a:t:1" + NEWLINE,
77              rootNode );
78      }
79      
80      public void testNodeWithMultipleChildren()
81      {
82          DependencyNode rootNode = createNode( "g:p:t:1" );
83          rootNode.addChild( createNode( "g:a:t:1" ) );
84          rootNode.addChild( createNode( "g:b:t:1" ) );
85          rootNode.addChild( createNode( "g:c:t:1" ) );
86          
87          assertTree(
88              "g:p:t:1" + NEWLINE + 
89              "+- g:a:t:1" + NEWLINE + 
90              "+- g:b:t:1" + NEWLINE + 
91              "\\- g:c:t:1" + NEWLINE,
92              rootNode );
93      }
94      
95      public void testNodeWithGrandchild()
96      {
97          DependencyNode rootNode = createNode( "g:p:t:1" );
98          DependencyNode childNode = createNode( "g:a:t:1" );
99          rootNode.addChild( childNode );
100         childNode.addChild( createNode( "g:b:t:1" ) );
101         
102         assertTree(
103             "g:p:t:1" + NEWLINE + 
104             "\\- g:a:t:1" + NEWLINE + 
105             "   \\- g:b:t:1" + NEWLINE,
106             rootNode );
107     }
108     
109     public void testNodeWithMultipleGrandchildren()
110     {
111         DependencyNode rootNode = createNode( "g:p:t:1" );
112         DependencyNode child1Node = createNode( "g:a:t:1" );
113         rootNode.addChild( child1Node );
114         child1Node.addChild( createNode( "g:b:t:1" ) );
115         DependencyNode child2Node = createNode( "g:c:t:1" );
116         rootNode.addChild( child2Node );
117         child2Node.addChild( createNode( "g:d:t:1" ) );
118         
119         assertTree(
120             "g:p:t:1" + NEWLINE + 
121             "+- g:a:t:1" + NEWLINE + 
122             "|  \\- g:b:t:1" + NEWLINE +
123             "\\- g:c:t:1" + NEWLINE + 
124             "   \\- g:d:t:1" + NEWLINE,
125             rootNode );
126     }
127     
128     // private methods --------------------------------------------------------
129 
130     private void assertTree( String expectedTree, DependencyNode actualNode )
131     {
132         actualNode.accept( serializer );
133 
134         assertEquals( expectedTree, writer.toString() );
135     }
136 }