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 4.0KB

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