View Javadoc

1   package org.apache.maven.index.treeview;
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  
24  import org.apache.maven.index.AbstractNexusIndexerTest;
25  import org.apache.maven.index.NexusIndexer;
26  import org.apache.maven.index.treeview.DefaultTreeNodeFactory;
27  import org.apache.maven.index.treeview.IndexTreeView;
28  import org.apache.maven.index.treeview.TreeNode;
29  
30  public class IndexTreeViewTest
31      extends AbstractNexusIndexerTest
32  {
33      protected File repo = new File( getBasedir(), "src/test/repo" );
34  
35      protected IndexTreeView indexTreeView;
36  
37      protected boolean debug = false;
38  
39      @Override
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44  
45          indexTreeView = lookup( IndexTreeView.class );
46      }
47  
48      @Override
49      protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
50          throws Exception
51      {
52          context = nexusIndexer.addIndexingContext( "test-minimal", "test", repo, indexDir, null, null, MIN_CREATORS );
53          nexusIndexer.scan( context );
54      }
55  
56      protected int prettyPrint( boolean debug, TreeNode node, int level )
57          throws Exception
58      {
59          if ( debug )
60          {
61              System.out.print( node.getPath() + " := " + node.getNodeName() + ", type=" + node.getType() );
62              System.out.println();
63          }
64  
65          int files = node.isLeaf() ? 1 : 0;
66  
67          if ( !node.isLeaf() )
68          {
69              for ( TreeNode child : node.listChildren() )
70              {
71                  files += prettyPrint( debug, child, level + 2 );
72              }
73          }
74  
75          if ( debug && level == 0 )
76          {
77              System.out.println( " ===== " );
78              System.out.println( " TOTAL LEAFS:  " + files );
79          }
80  
81          return files;
82      }
83  
84      public void testRoot()
85          throws Exception
86      {
87          TreeViewRequest req =
88              new TreeViewRequest( new DefaultTreeNodeFactory( context.getRepositoryId() ), "/", context );
89          TreeNode root = indexTreeView.listNodes( req );
90  
91          int leafsFound = prettyPrint( debug, root, 0 );
92  
93          assertEquals( "The group name should be here", "/", root.getNodeName() );
94          assertEquals( 12, root.getChildren().size() );
95          assertEquals( 49, leafsFound );
96      }
97  
98      public void testPathIsAboveRealGroup()
99          throws Exception
100     {
101         TreeViewRequest req =
102             new TreeViewRequest( new DefaultTreeNodeFactory( context.getRepositoryId() ), "/org/", context );
103         TreeNode root = indexTreeView.listNodes( req );
104 
105         int leafsFound = prettyPrint( debug, root, 0 );
106 
107         assertEquals( "The group name should be here", "org", root.getNodeName() );
108         assertEquals( 4, root.getChildren().size() );
109         assertEquals( 22, leafsFound );
110     }
111 
112     public void testPathIsRealGroup()
113         throws Exception
114     {
115         TreeViewRequest req =
116             new TreeViewRequest( new DefaultTreeNodeFactory( context.getRepositoryId() ), "/org/slf4j/", context );
117         TreeNode root = indexTreeView.listNodes( req );
118 
119         int leafsFound = prettyPrint( debug, root, 0 );
120 
121         assertEquals( "The group name should be here", "slf4j", root.getNodeName() );
122         assertEquals( 3, root.getChildren().size() );
123         assertEquals( 10, leafsFound );
124     }
125 
126     public void testPathIsRealGroupArtifact()
127         throws Exception
128     {
129         TreeViewRequest req =
130             new TreeViewRequest( new DefaultTreeNodeFactory( context.getRepositoryId() ), "/org/slf4j/slf4j-log4j12/",
131                 context );
132         TreeNode root = indexTreeView.listNodes( req );
133 
134         int leafsFound = prettyPrint( debug, root, 0 );
135 
136         assertEquals( "slf4j-log4j12", root.getNodeName() );
137         assertEquals( 1, root.getChildren().size() );
138         assertEquals( 4, leafsFound );
139     }
140 
141     public void testPathIsRealGroupArtifactVersion()
142         throws Exception
143     {
144         TreeViewRequest req =
145             new TreeViewRequest( new DefaultTreeNodeFactory( context.getRepositoryId() ),
146                 "/org/slf4j/slf4j-log4j12/1.4.1/", context );
147         TreeNode root = indexTreeView.listNodes( req );
148 
149         int leafsFound = prettyPrint( debug, root, 0 );
150 
151         assertEquals( "The group name should be here", "1.4.1", root.getNodeName() );
152         assertEquals( 1, root.getChildren().size() );
153         assertEquals( 4, leafsFound );
154     }
155 }