You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MavenRepositorySearchPaginateTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package org.apache.archiva.indexer.maven.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. import junit.framework.TestCase;
  21. import org.apache.archiva.indexer.search.SearchResultHit;
  22. import org.apache.archiva.indexer.search.SearchResultLimits;
  23. import org.apache.archiva.indexer.search.SearchResults;
  24. import org.apache.archiva.indexer.util.SearchUtil;
  25. import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
  26. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  27. import org.junit.After;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.test.context.ContextConfiguration;
  32. import java.util.Arrays;
  33. /**
  34. * @author Olivier Lamy
  35. */
  36. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  37. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  38. public class MavenRepositorySearchPaginateTest
  39. extends TestCase
  40. {
  41. @Autowired
  42. ArchivaRepositoryRegistry repositoryRegistry;
  43. @After
  44. public void endTests() {
  45. assert repositoryRegistry!=null;
  46. repositoryRegistry.destroy();
  47. }
  48. @Test
  49. public void nonPaginatedResult()
  50. throws Exception
  51. {
  52. MavenRepositorySearch search = new MavenRepositorySearch();
  53. SearchResults searchResults = build( 10, new SearchResultLimits( 0 ) );
  54. searchResults = search.paginate( searchResults );
  55. assertEquals( 10, searchResults.getReturnedHitsCount() );
  56. }
  57. @Test
  58. public void nonPaginatedHugeResult()
  59. throws Exception
  60. {
  61. MavenRepositorySearch search = new MavenRepositorySearch();
  62. SearchResults origSearchResults = build( 63, new SearchResultLimits( 0 ) );
  63. SearchResults searchResults = search.paginate( origSearchResults );
  64. assertEquals( 30, searchResults.getReturnedHitsCount() );
  65. origSearchResults = build( 63, new SearchResultLimits( 1 ) );
  66. searchResults = search.paginate( origSearchResults );
  67. assertEquals( 30, searchResults.getReturnedHitsCount() );
  68. }
  69. @Test
  70. public void paginatedResult()
  71. throws Exception
  72. {
  73. MavenRepositorySearch search = new MavenRepositorySearch();
  74. SearchResults searchResults = build( 32, new SearchResultLimits( 1 ) );
  75. searchResults = search.paginate( searchResults );
  76. assertEquals( 2, searchResults.getReturnedHitsCount() );
  77. }
  78. SearchResults build( int number, SearchResultLimits limits )
  79. {
  80. SearchResults searchResults = new SearchResults();
  81. searchResults.setLimits( limits );
  82. for ( int i = 0; i < number; i++ )
  83. {
  84. SearchResultHit hit = new SearchResultHit();
  85. hit.setGroupId( "commons-foo" );
  86. hit.setArtifactId( "commons-bar-" + i );
  87. hit.setPackaging( "jar" );
  88. hit.setVersions( Arrays.asList( "1.0" ) );
  89. String id =
  90. SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(), hit.getPackaging() );
  91. searchResults.addHit( id, hit );
  92. }
  93. searchResults.setTotalHits( number );
  94. return searchResults;
  95. }
  96. }