View Javadoc
1   package org.apache.archiva.indexer.search;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import junit.framework.TestCase;
22  import org.apache.archiva.indexer.util.SearchUtil;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  
26  import java.util.Arrays;
27  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
28  
29  /**
30   * @author Olivier Lamy
31   */
32  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
33  public class MavenRepositorySearchPaginateTest
34      extends TestCase
35  {
36      @Test
37      public void nonPaginatedResult()
38          throws Exception
39      {
40          MavenRepositorySearch search = new MavenRepositorySearch();
41  
42          SearchResults searchResults = build( 10, new SearchResultLimits( 0 ) );
43  
44          searchResults = search.paginate( searchResults );
45  
46          assertEquals( 10, searchResults.getReturnedHitsCount() );
47  
48      }
49  
50      @Test
51      public void nonPaginatedHugeResult()
52          throws Exception
53      {
54          MavenRepositorySearch search = new MavenRepositorySearch();
55  
56          SearchResults origSearchResults = build( 63, new SearchResultLimits( 0 ) );
57  
58          SearchResults searchResults = search.paginate( origSearchResults );
59  
60          assertEquals( 30, searchResults.getReturnedHitsCount() );
61  
62          origSearchResults = build( 63, new SearchResultLimits( 1 ) );
63  
64          searchResults = search.paginate( origSearchResults );
65  
66          assertEquals( 30, searchResults.getReturnedHitsCount() );
67  
68      }
69  
70      @Test
71      public void paginatedResult()
72          throws Exception
73      {
74          MavenRepositorySearch search = new MavenRepositorySearch();
75  
76          SearchResults searchResults = build( 32, new SearchResultLimits( 1 ) );
77  
78          searchResults = search.paginate( searchResults );
79  
80          assertEquals( 2, searchResults.getReturnedHitsCount() );
81  
82      }
83  
84  
85      SearchResults build( int number, SearchResultLimits limits )
86      {
87          SearchResults searchResults = new SearchResults();
88          searchResults.setLimits( limits );
89          for ( int i = 0; i < number; i++ )
90          {
91              SearchResultHit hit = new SearchResultHit();
92              hit.setGroupId( "commons-foo" );
93              hit.setArtifactId( "commons-bar-" + i );
94              hit.setPackaging( "jar" );
95              hit.setVersions( Arrays.asList( "1.0" ) );
96              String id =
97                  SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(), hit.getPackaging() );
98              searchResults.addHit( id, hit );
99          }
100 
101         searchResults.setTotalHits( number );
102         return searchResults;
103 
104     }
105 }