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.

MavenRepositorySearchTest.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. package org.apache.archiva.maven.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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.common.utils.FileUtils;
  20. import org.apache.archiva.indexer.search.RepositorySearchException;
  21. import org.apache.archiva.indexer.search.SearchFields;
  22. import org.apache.archiva.indexer.search.SearchResultHit;
  23. import org.apache.archiva.indexer.search.SearchResultLimits;
  24. import org.apache.archiva.indexer.search.SearchResults;
  25. import org.apache.archiva.maven.indexer.util.SearchUtil;
  26. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.springframework.test.context.ContextConfiguration;
  30. import java.nio.file.Path;
  31. import java.nio.file.Paths;
  32. import java.util.ArrayList;
  33. import java.util.Arrays;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.List;
  37. import java.util.Locale;
  38. import static org.mockito.Mockito.reset;
  39. import static org.mockito.Mockito.when;
  40. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  41. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  42. public class MavenRepositorySearchTest
  43. extends AbstractMavenRepositorySearch
  44. {
  45. private void createSimpleIndex( boolean scan )
  46. throws Exception
  47. {
  48. List<Path> files = new ArrayList<>();
  49. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
  50. "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
  51. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
  52. "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ));
  53. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos", TEST_REPO_1,
  54. "org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ));
  55. createIndex( TEST_REPO_1, files, scan );
  56. }
  57. private void createIndexContainingMoreArtifacts( boolean scan )
  58. throws Exception
  59. {
  60. List<Path> files = new ArrayList<>();
  61. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  62. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
  63. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  64. + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
  65. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  66. + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
  67. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  68. + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
  69. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
  70. "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
  71. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  72. + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
  73. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
  74. "target/repos/" + TEST_REPO_1 + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
  75. createIndex( TEST_REPO_1, files, scan );
  76. }
  77. private void createIndexContainingMultipleArtifactsSameVersion( boolean scan )
  78. throws Exception
  79. {
  80. List<Path> files = new ArrayList<>();
  81. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  82. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
  83. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  84. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom" ) );
  85. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  86. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0-sources.jar" ) );
  87. createIndex( TEST_REPO_1, files, scan );
  88. }
  89. @Test
  90. public void testQuickSearch()
  91. throws Exception
  92. {
  93. createSimpleIndex( false );
  94. List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
  95. // search artifactId
  96. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  97. when( archivaConfig.getConfiguration()).thenReturn(config);
  98. SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
  99. assertNotNull( results );
  100. SearchResultHit hit =
  101. results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
  102. assertNotNull( "hit null in result " + results.getHits(), hit );
  103. assertEquals( "org.apache.archiva", hit.getGroupId() );
  104. assertEquals( "archiva-search", hit.getArtifactId() );
  105. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  106. reset( archivaConfig );
  107. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  108. when( archivaConfig.getConfiguration()).thenReturn(config);
  109. // search groupId
  110. results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
  111. assertNotNull( results );
  112. assertEquals( "total hints not 3", 3, results.getTotalHits() );
  113. //TODO: search for class & package names
  114. }
  115. @Test
  116. public void testQuickSearchNotWithClassifier()
  117. throws Exception
  118. {
  119. createSimpleIndex( true );
  120. List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
  121. // search artifactId
  122. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  123. when( archivaConfig.getConfiguration()).thenReturn(config);
  124. SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
  125. assertNotNull( results );
  126. SearchResultHit hit =
  127. results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
  128. assertNotNull( "hit null in result " + results.getHits(), hit );
  129. assertEquals( "org.apache.archiva", hit.getGroupId() );
  130. assertEquals( "archiva-search", hit.getArtifactId() );
  131. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  132. reset( archivaConfig );
  133. // search groupId
  134. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  135. when( archivaConfig.getConfiguration()).thenReturn(config);
  136. results = search.search( "user", selectedRepos, "archiva-search", null, null );
  137. assertNotNull( results );
  138. assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
  139. //TODO: search for class & package names
  140. }
  141. @Test
  142. public void testQuickSearchMultipleArtifactsSameVersion()
  143. throws Exception
  144. {
  145. createIndexContainingMultipleArtifactsSameVersion( false );
  146. List<String> selectedRepos = new ArrayList<>();
  147. selectedRepos.add( TEST_REPO_1 );
  148. // search artifactId
  149. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  150. when( archivaConfig.getConfiguration()).thenReturn(config);
  151. SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
  152. assertNotNull( results );
  153. assertEquals( 3, results.getTotalHits() );
  154. SearchResultHit hit = results.getHits().get( 0 );
  155. assertEquals( "org.apache.archiva", hit.getGroupId() );
  156. assertEquals( "archiva-search", hit.getArtifactId() );
  157. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  158. //only 1 version of 1.0 is retrieved
  159. assertEquals( 1, hit.getVersions().size() );
  160. }
  161. @Test
  162. public void testMultipleArtifactsSameVersionWithClassifier()
  163. throws Exception
  164. {
  165. createIndexContainingMultipleArtifactsSameVersion( true );
  166. List<String> selectedRepos = new ArrayList<>();
  167. selectedRepos.add( TEST_REPO_1 );
  168. // search artifactId
  169. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  170. when( archivaConfig.getConfiguration()).thenReturn(config);
  171. SearchFields searchFields = new SearchFields();
  172. searchFields.setGroupId( "org.apache.archiva" );
  173. searchFields.setArtifactId( "archiva-search" );
  174. searchFields.setClassifier( "sources" );
  175. searchFields.setRepositories( selectedRepos );
  176. SearchResults results = search.search( "user", searchFields, null );
  177. assertNotNull( results );
  178. assertEquals( 1, results.getTotalHits() );
  179. SearchResultHit hit = results.getHits().get( 0 );
  180. assertEquals( "org.apache.archiva", hit.getGroupId() );
  181. assertEquals( "archiva-search", hit.getArtifactId() );
  182. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  183. //only 1 version of 1.0 is retrieved
  184. assertEquals( 1, hit.getVersions().size() );
  185. }
  186. // search for existing artifact using multiple keywords
  187. @Test
  188. public void testQuickSearchWithMultipleKeywords()
  189. throws Exception
  190. {
  191. createSimpleIndex( false );
  192. List<String> selectedRepos = new ArrayList<>();
  193. selectedRepos.add( TEST_REPO_1 );
  194. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  195. when( archivaConfig.getConfiguration()).thenReturn(config);
  196. SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
  197. assertNotNull( results );
  198. assertEquals( 1, results.getTotalHits() );
  199. }
  200. @Test
  201. public void testQuickSearchWithPagination()
  202. throws Exception
  203. {
  204. createSimpleIndex( true );
  205. List<String> selectedRepos = new ArrayList<>();
  206. selectedRepos.add( TEST_REPO_1 );
  207. // page 1
  208. SearchResultLimits limits = new SearchResultLimits( 0 );
  209. limits.setPageSize( 1 );
  210. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  211. when( archivaConfig.getConfiguration()).thenReturn(config);
  212. SearchResults results = search.search( "user", selectedRepos, "org", limits, Collections.emptyList() );
  213. assertNotNull( results );
  214. assertEquals( 1, results.getHits().size() );
  215. assertEquals( "total hits not 9 for page1 " + results, 9, results.getTotalHits() );
  216. assertEquals( "returned hits not 1 for page1 " + results, 1, results.getReturnedHitsCount() );
  217. assertEquals( limits, results.getLimits() );
  218. reset( archivaConfig );
  219. // page 2
  220. limits = new SearchResultLimits( 1 );
  221. limits.setPageSize( 1 );
  222. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  223. when( archivaConfig.getConfiguration()).thenReturn(config);
  224. results = search.search( "user", selectedRepos, "org", limits, null );
  225. assertNotNull( results );
  226. assertEquals( "hits not 1", 1, results.getHits().size() );
  227. assertEquals( "total hits not 9 for page 2 " + results, 9, results.getTotalHits() );
  228. assertEquals( "returned hits not 1 for page2 " + results, 1, results.getReturnedHitsCount() );
  229. assertEquals( limits, results.getLimits() );
  230. }
  231. @Test
  232. public void testArtifactFoundInMultipleRepositories()
  233. throws Exception
  234. {
  235. createSimpleIndex( true );
  236. List<Path> files = new ArrayList<>();
  237. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
  238. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
  239. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
  240. + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
  241. createIndex( TEST_REPO_2, files, false );
  242. List<String> selectedRepos = new ArrayList<>();
  243. selectedRepos.add( TEST_REPO_1 );
  244. selectedRepos.add( TEST_REPO_2 );
  245. config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
  246. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  247. when( archivaConfig.getConfiguration()).thenReturn(config);
  248. // wait lucene flush.....
  249. Thread.sleep( 2000 );
  250. SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
  251. assertNotNull( results );
  252. SearchResultHit hit =
  253. results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
  254. assertNotNull(hit);
  255. assertEquals( "org.apache.archiva", hit.getGroupId() );
  256. assertEquals( "archiva-search", hit.getArtifactId() );
  257. assertEquals( "not 2 version for hit " + hit + "::" + niceDisplay( results ), 2, hit.getVersions().size() );
  258. assertTrue( hit.getVersions().contains( "1.0" ) );
  259. assertTrue( hit.getVersions().contains( "1.1" ) );
  260. // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
  261. }
  262. @Test
  263. public void testNoMatchFound()
  264. throws Exception
  265. {
  266. createSimpleIndex( false );
  267. List<String> selectedRepos = new ArrayList<>();
  268. selectedRepos.add( TEST_REPO_1 );
  269. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  270. when( archivaConfig.getConfiguration()).thenReturn(config);
  271. SearchResults results = search.search( "user", selectedRepos, "dfghdfkweriuasndsaie", null, null );
  272. assertNotNull( results );
  273. assertEquals( 0, results.getTotalHits() );
  274. }
  275. @Test
  276. public void testNoIndexFound()
  277. throws Exception
  278. {
  279. List<String> selectedRepos = new ArrayList<>();
  280. selectedRepos.add( TEST_REPO_1 );
  281. // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  282. // when( archivaConfig.getConfiguration()).thenReturn(config);
  283. // archivaConfigControl.replay();
  284. SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
  285. assertNotNull( results );
  286. assertEquals( 0, results.getTotalHits() );
  287. }
  288. @Test
  289. public void testRepositoryNotFound()
  290. throws Exception
  291. {
  292. List<String> selectedRepos = new ArrayList<>();
  293. selectedRepos.add( "non-existing-repo" );
  294. // archivaConfigControl.replay();
  295. SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
  296. assertNotNull( results );
  297. assertEquals( 0, results.getTotalHits() );
  298. }
  299. @Test
  300. public void testSearchWithinSearchResults()
  301. throws Exception
  302. {
  303. createSimpleIndex( true );
  304. List<String> selectedRepos = new ArrayList<>();
  305. selectedRepos.add( TEST_REPO_1 );
  306. List<String> previousSearchTerms = new ArrayList<>();
  307. previousSearchTerms.add( "archiva-test" );
  308. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  309. when( archivaConfig.getConfiguration()).thenReturn(config);
  310. SearchResults results = search.search( "user", selectedRepos, "1.0", null, previousSearchTerms );
  311. assertNotNull( results );
  312. assertEquals( "total hints not 1", 1, results.getTotalHits() );
  313. SearchResultHit hit = results.getHits().get( 0 );
  314. assertEquals( "org.apache.archiva", hit.getGroupId() );
  315. assertEquals( "archiva-test", hit.getArtifactId() );
  316. assertEquals( "versions not 1", 1, hit.getVersions().size() );
  317. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  318. }
  319. // tests for advanced search
  320. @Test
  321. public void testAdvancedSearch()
  322. throws Exception
  323. {
  324. List<Path> files = new ArrayList<>();
  325. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
  326. + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
  327. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2
  328. + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
  329. createIndex( TEST_REPO_2, files, false );
  330. List<String> selectedRepos = new ArrayList<>();
  331. selectedRepos.add( TEST_REPO_2 );
  332. SearchFields searchFields = new SearchFields();
  333. searchFields.setGroupId( "org.apache.archiva" );
  334. searchFields.setVersion( "1.0" );
  335. searchFields.setRepositories( selectedRepos );
  336. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  337. when( archivaConfig.getConfiguration()).thenReturn(config);
  338. SearchResults results = search.search( "user", searchFields, null );
  339. assertNotNull( results );
  340. assertEquals( 1, results.getTotalHits() );
  341. SearchResultHit hit = results.getHits().get( 0 );
  342. assertEquals( "org.apache.archiva", hit.getGroupId() );
  343. assertEquals( "archiva-search", hit.getArtifactId() );
  344. assertEquals( "1.0", hit.getVersions().get( 0 ) );
  345. }
  346. @Test
  347. public void testAdvancedSearchWithPagination()
  348. throws Exception
  349. {
  350. createIndexContainingMoreArtifacts( false );
  351. List<String> selectedRepos = new ArrayList<>();
  352. selectedRepos.add( TEST_REPO_1 );
  353. SearchFields searchFields = new SearchFields();
  354. searchFields.setGroupId( "org.apache.archiva" );
  355. searchFields.setRepositories( selectedRepos );
  356. // page 1
  357. SearchResultLimits limits = new SearchResultLimits( 0 );
  358. limits.setPageSize( 1 );
  359. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  360. when( archivaConfig.getConfiguration()).thenReturn(config);
  361. SearchResults results = search.search( "user", searchFields, limits );
  362. assertNotNull( results );
  363. assertEquals( 4, results.getTotalHits() );
  364. assertEquals( 1, results.getHits().size() );
  365. // page 2
  366. reset( archivaConfig );
  367. limits = new SearchResultLimits( 1 );
  368. limits.setPageSize( 1 );
  369. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  370. when( archivaConfig.getConfiguration()).thenReturn(config);
  371. results = search.search( "user", searchFields, limits );
  372. assertNotNull( results );
  373. assertEquals( 4, results.getTotalHits() );
  374. assertEquals( 1, results.getHits().size() );
  375. }
  376. // MRM-981 - artifactIds with numeric characters aren't found in advanced search
  377. @Test
  378. public void testAdvancedSearchArtifactIdHasNumericChar()
  379. throws Exception
  380. {
  381. List<Path> files = new ArrayList<>();
  382. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(),
  383. "target/repos/" + TEST_REPO_1 + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
  384. files.add( Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_1
  385. + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
  386. createIndex( TEST_REPO_1, files, true );
  387. List<String> selectedRepos = new ArrayList<>();
  388. selectedRepos.add( TEST_REPO_1 );
  389. SearchFields searchFields = new SearchFields();
  390. searchFields.setArtifactId( "artifactid-numeric" );
  391. searchFields.setRepositories( selectedRepos );
  392. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  393. when( archivaConfig.getConfiguration()).thenReturn(config);
  394. SearchResults results = search.search( "user", searchFields, null );
  395. assertNotNull( results );
  396. assertEquals( 2, results.getTotalHits() );
  397. }
  398. @Test
  399. public void testAdvancedSearchNoRepositoriesConfigured()
  400. throws Exception
  401. {
  402. SearchFields searchFields = new SearchFields();
  403. searchFields.setArtifactId( "archiva" );
  404. searchFields.setRepositories( null );
  405. try
  406. {
  407. search.search( "user", searchFields, null );
  408. fail( "A RepositorySearchExcecption should have been thrown." );
  409. }
  410. catch ( RepositorySearchException e )
  411. {
  412. assertEquals( "Repositories cannot be null.", e.getMessage() );
  413. }
  414. }
  415. @Test
  416. public void testAdvancedSearchSearchFieldsAreNull()
  417. throws Exception
  418. {
  419. List<String> selectedRepos = new ArrayList<>();
  420. selectedRepos.add( TEST_REPO_1 );
  421. SearchFields searchFields = new SearchFields();
  422. searchFields.setRepositories( selectedRepos );
  423. try
  424. {
  425. // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  426. // when( archivaConfig.getConfiguration()).thenReturn(config);
  427. // archivaConfigControl.replay();
  428. search.search( "user", searchFields, null );
  429. fail( "A RepositorySearchExcecption should have been thrown." );
  430. }
  431. catch ( RepositorySearchException e )
  432. {
  433. assertEquals( "No search fields set.", e.getMessage() );
  434. }
  435. }
  436. @Test
  437. public void testAdvancedSearchSearchFieldsAreBlank()
  438. throws Exception
  439. {
  440. List<String> selectedRepos = new ArrayList<>();
  441. selectedRepos.add( TEST_REPO_1 );
  442. SearchFields searchFields = new SearchFields();
  443. searchFields.setGroupId( "" );
  444. searchFields.setArtifactId( "" );
  445. searchFields.setVersion( "" );
  446. searchFields.setPackaging( "" );
  447. searchFields.setClassName( "" );
  448. searchFields.setRepositories( selectedRepos );
  449. try
  450. {
  451. // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  452. // when( archivaConfig.getConfiguration()).thenReturn(config);
  453. // archivaConfigControl.replay();
  454. search.search( "user", searchFields, null );
  455. fail( "A RepositorySearchException should have been thrown." );
  456. }
  457. catch ( RepositorySearchException e )
  458. {
  459. assertEquals( "No search fields set.", e.getMessage() );
  460. }
  461. }
  462. @Test
  463. public void testAdvancedSearchAllSearchCriteriaSpecified()
  464. throws Exception
  465. {
  466. createSimpleIndex( true );
  467. List<String> selectedRepos = new ArrayList<>();
  468. selectedRepos.add( TEST_REPO_1 );
  469. SearchFields searchFields = new SearchFields();
  470. searchFields.setGroupId( "org.apache.archiva" );
  471. searchFields.setArtifactId( "archiva-test" );
  472. searchFields.setVersion( "2.0" );
  473. searchFields.setPackaging( "jar" );
  474. searchFields.setClassName( "org.apache.archiva.test.App" );
  475. searchFields.setRepositories( selectedRepos );
  476. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  477. when( archivaConfig.getConfiguration()).thenReturn(config);
  478. SearchResults results = search.search( "user", searchFields, null );
  479. assertNotNull( results );
  480. assertEquals( "total hints not 1" + results, 1, results.getTotalHits() );
  481. SearchResultHit hit = results.getHits().get( 0 );
  482. assertEquals( "org.apache.archiva", hit.getGroupId() );
  483. assertEquals( "archiva-test", hit.getArtifactId() );
  484. assertEquals( "version not 2.0", "2.0", hit.getVersions().get( 0 ) );
  485. }
  486. @Test
  487. public void testAdvancedSearchJarArtifacts()
  488. throws Exception
  489. {
  490. createIndexContainingMoreArtifacts( true );
  491. List<String> selectedRepos = new ArrayList<>();
  492. selectedRepos.add( TEST_REPO_1 );
  493. SearchFields searchFields = new SearchFields();
  494. searchFields.setPackaging( "jar" );
  495. searchFields.setRepositories( selectedRepos );
  496. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  497. when( archivaConfig.getConfiguration()).thenReturn(config);
  498. SearchResults results = search.search( "user", searchFields, null );
  499. assertNotNull( results );
  500. assertEquals( "not 8 but " + results.getTotalHits() + ":" + niceDisplay( results ), 8, results.getTotalHits() );
  501. }
  502. @Test
  503. public void testAdvancedSearchWithIncorrectPackaging()
  504. throws Exception
  505. {
  506. createSimpleIndex( true );
  507. List<String> selectedRepos = new ArrayList<>();
  508. selectedRepos.add( TEST_REPO_1 );
  509. SearchFields searchFields = new SearchFields();
  510. searchFields.setGroupId( "org.apache.archiva" );
  511. searchFields.setArtifactId( "archiva-test" );
  512. searchFields.setVersion( "2.0" );
  513. searchFields.setPackaging( "war" );
  514. searchFields.setRepositories( selectedRepos );
  515. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  516. when( archivaConfig.getConfiguration()).thenReturn(config);
  517. SearchResults results = search.search( "user", searchFields, null );
  518. assertNotNull( results );
  519. assertEquals( 0, results.getTotalHits() );
  520. }
  521. @Test
  522. public void testAdvancedSearchClassname()
  523. throws Exception
  524. {
  525. createIndexContainingMoreArtifacts( true );
  526. List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
  527. SearchFields searchFields = new SearchFields();
  528. searchFields.setClassName( "com.classname.search.App" );
  529. searchFields.setRepositories( selectedRepos );
  530. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  531. when( archivaConfig.getConfiguration()).thenReturn(config);
  532. SearchResults results = search.search( "user", searchFields, null );
  533. assertNotNull( results );
  534. assertEquals( "totalHits not 1 results " + results, 1, results.getTotalHits() );
  535. SearchResultHit hit = results.getHits().get( 0 );
  536. assertEquals( "groupId not com", "com", hit.getGroupId() );
  537. assertEquals( "arttifactId not classname-search", "classname-search", hit.getArtifactId() );
  538. assertEquals( " hits.version(0) not 1.0", "1.0", hit.getVersions().get( 0 ) );
  539. }
  540. @Test
  541. public void testAdvancedSearchNoIndexFound()
  542. throws Exception
  543. {
  544. List<String> selectedRepos = new ArrayList<>();
  545. selectedRepos.add( TEST_REPO_1 );
  546. SearchFields searchFields = new SearchFields();
  547. searchFields.setGroupId( "org.apache.archiva" );
  548. searchFields.setRepositories( selectedRepos );
  549. // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  550. // when( archivaConfig.getConfiguration()).thenReturn(config);
  551. // archivaConfigControl.replay();
  552. SearchResults results = search.search( "user", searchFields, null );
  553. assertNotNull( results );
  554. assertEquals( 0, results.getTotalHits() );
  555. }
  556. @Test
  557. public void testAdvancedSearchClassNameInWar()
  558. throws Exception
  559. {
  560. createIndexContainingMoreArtifacts( true );
  561. List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
  562. SearchFields searchFields = new SearchFields();
  563. searchFields.setClassName( "SomeClass" );
  564. searchFields.setRepositories( selectedRepos );
  565. when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  566. when( archivaConfig.getConfiguration()).thenReturn(config);
  567. SearchResults results = search.search( "user", searchFields, null );
  568. assertNotNull( results );
  569. assertEquals( 1, results.getHits().size() );
  570. assertEquals( "test-webapp", results.getHits().get( 0 ).getArtifactId() );
  571. }
  572. @Test
  573. public void getAllGroupIds()
  574. throws Exception
  575. {
  576. createIndexContainingMoreArtifacts( true );
  577. List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
  578. when( archivaConfig.getConfiguration() ).thenReturn( config );
  579. Collection<String> groupIds = search.getAllGroupIds( "user", selectedRepos );
  580. log.info( "groupIds: {}", groupIds );
  581. assertEquals( 3, groupIds.size() );
  582. assertTrue( groupIds.contains( "com" ) );
  583. assertTrue( groupIds.contains( "org.apache.felix" ) );
  584. assertTrue( groupIds.contains( "org.apache.archiva" ) );
  585. }
  586. @Test
  587. public void testSearchWithUnknownRepo()
  588. throws Exception
  589. {
  590. createIndexContainingMoreArtifacts( true );
  591. List<String> selectedRepos = Arrays.asList( "foo" );
  592. SearchFields searchFields = new SearchFields();
  593. searchFields.setClassName( "SomeClass" );
  594. searchFields.setRepositories( selectedRepos );
  595. SearchResults results = search.search( "user", searchFields, null );
  596. assertNotNull( results );
  597. assertEquals( 0, results.getHits().size() );
  598. }
  599. @Test
  600. public void nolimitedResult()
  601. throws Exception
  602. {
  603. Path repo = Paths.get("target/repo-release-index-test/repo-release");
  604. try {
  605. Path indexDirectory = repo.resolve(".indexer");
  606. Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("repo-release.zip").toURI());
  607. FileUtils.unzip(zipFile, repo.getParent());
  608. // IndexUpgrader.main(new String[]{indexDirectory.toAbsolutePath().toString(), "-delete-prior-commits"});
  609. createIndex(REPO_RELEASE, Collections.emptyList(), false, indexDirectory, false);
  610. // indexer.addIndexingContext( REPO_RELEASE, REPO_RELEASE, repo.toFile(), indexDirectory.toFile(),
  611. // repo.toUri().toURL().toExternalForm(),
  612. // indexDirectory.toUri().toURL().toString(), indexCreators );
  613. SearchResultLimits limits = new SearchResultLimits(SearchResultLimits.ALL_PAGES);
  614. limits.setPageSize(300);
  615. // when( archivaConfig.getDefaultLocale() ).thenReturn( Locale.getDefault( ) );
  616. when(archivaConfig.getConfiguration()).thenReturn(config);
  617. SearchResults searchResults = search.search(null, Arrays.asList(REPO_RELEASE), //
  618. "org.example", limits, //
  619. Collections.emptyList());
  620. log.info("results: {}", searchResults.getHits().size());
  621. assertEquals(255, searchResults.getHits().size());
  622. SearchFields searchFields = new SearchFields();
  623. searchFields.setGroupId("org.example");
  624. searchFields.setRepositories(Arrays.asList(REPO_RELEASE));
  625. searchResults = search.search(null, searchFields, limits);
  626. log.info("results: {}", searchResults.getHits().size());
  627. assertEquals(255, searchResults.getHits().size());
  628. } finally {
  629. FileUtils.deleteQuietly(repo);
  630. }
  631. }
  632. }