From 16b13f831a1d41139e59bdeb89086357e88bf70a Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Mon, 12 Oct 2009 08:08:29 +0000 Subject: [PATCH] [MRM-1254] Add more unit tests for searching artifacts o more unit tests for advanced search and quick search (code coverage is now 88%) git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@824257 13f79535-47bb-0310-9956-ffa450edef68 --- .../search/NexusRepositorySearchTest.java | 288 +++++++++++++++--- .../1.0/classname-search-1.0.jar | Bin 0 -> 2220 bytes .../1.0/classname-search-1.0.pom | 18 ++ .../archiva-webapp/1.0/archiva-webapp-1.0.pom | 23 ++ .../archiva-webapp/1.0/archiva-webapp-1.0.war | 0 5 files changed, 292 insertions(+), 37 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.jar create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.pom create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.pom create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java index fcdb2ba23..b04c2f985 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java @@ -81,7 +81,12 @@ public class NexusRepositorySearchTest config = new Configuration(); config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) ); + config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) ); + } + private void createIndexForQuickSearch() + throws IOException, UnsupportedExistingLuceneIndexException + { List files = new ArrayList(); files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); @@ -113,6 +118,9 @@ public class NexusRepositorySearchTest { FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); + + FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); + assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); super.tearDown(); } @@ -146,6 +154,8 @@ public class NexusRepositorySearchTest public void testQuickSearch() throws Exception { + createIndexForQuickSearch(); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -186,6 +196,8 @@ public class NexusRepositorySearchTest public void testQuickSearchWithPagination() throws Exception { + createIndexForQuickSearch(); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -197,7 +209,7 @@ public class NexusRepositorySearchTest archivaConfigControl.replay(); - SearchResults results = search.search( "user", selectedRepos, "org", limits, null ); + SearchResults results = search.search( "user", selectedRepos, "org", limits, new ArrayList() ); archivaConfigControl.verify(); @@ -229,6 +241,8 @@ public class NexusRepositorySearchTest public void testArtifactFoundInMultipleRepositories() throws Exception { + createIndexForQuickSearch(); + List files = new ArrayList(); files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); @@ -262,15 +276,14 @@ public class NexusRepositorySearchTest archivaConfigControl.reset(); - FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); - assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); - // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found } public void testNoMatchFound() throws Exception { + createIndexForQuickSearch(); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -290,7 +303,7 @@ public class NexusRepositorySearchTest throws Exception { List selectedRepos = new ArrayList(); - selectedRepos.add( "non-existing-repo" ); + selectedRepos.add( TEST_REPO_1 ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); @@ -303,9 +316,28 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); } + public void testRepositoryNotFound() + throws Exception + { + List selectedRepos = new ArrayList(); + selectedRepos.add( "non-existing-repo" ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null ); + assertNotNull( results ); + assertEquals( 0, results.getTotalHits() ); + + archivaConfigControl.verify(); + } + public void testSearchWithinSearchResults() throws Exception { + createIndexForQuickSearch(); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -329,7 +361,9 @@ public class NexusRepositorySearchTest assertEquals( 1, hit.getVersions().size() ); assertEquals( "1.0", hit.getVersions().get( 0 ) ); } - + + // tests for advanced search + public void testAdvancedSearch() throws Exception { @@ -340,18 +374,15 @@ public class NexusRepositorySearchTest "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); createIndex( TEST_REPO_2, files ); - List selectedRepos = new ArrayList(); - selectedRepos.add( TEST_REPO_1 ); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_2 ); - - config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) ); SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); searchFields.setVersion( "1.0" ); searchFields.setRepositories( selectedRepos ); - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -360,31 +391,40 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); assertNotNull( results ); - assertEquals( 2, results.getTotalHits() ); + assertEquals( 1, results.getTotalHits() ); - FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); - assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); + SearchResultHit hit = results.getHits().get( 0 ); + assertEquals( "org.apache.archiva", hit.getGroupId() ); + assertEquals( "archiva-search", hit.getArtifactId() ); + assertEquals( "1.0", hit.getVersions().get( 0 ) ); } public void testAdvancedSearchWithPagination() throws Exception { List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + - "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); - createIndex( TEST_REPO_2, files ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/classname-search/1.0/classname-search-1.0.jar" ) ); + + createIndex( TEST_REPO_1, files ); List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - selectedRepos.add( TEST_REPO_2 ); - - config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) ); SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); - searchFields.setVersion( "1.0" ); searchFields.setRepositories( selectedRepos ); // page 1 @@ -392,7 +432,7 @@ public class NexusRepositorySearchTest SearchResultLimits limits = new SearchResultLimits( 0 ); limits.setPageSize( 1 ); - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -401,7 +441,7 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); assertNotNull( results ); - assertEquals( 2, results.getTotalHits() ); + assertEquals( 3, results.getTotalHits() ); assertEquals( 1, results.getHits().size() ); // page 2 @@ -410,7 +450,7 @@ public class NexusRepositorySearchTest limits = new SearchResultLimits( 1 ); limits.setPageSize( 1 ); - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -419,11 +459,8 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); assertNotNull( results ); - assertEquals( 2, results.getTotalHits() ); - assertEquals( 1, results.getHits().size() ); - - FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); - assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); + assertEquals( 3, results.getTotalHits() ); + assertEquals( 1, results.getHits().size() ); } // MRM-981 - artifactIds with numeric characters aren't found in advanced search @@ -439,8 +476,6 @@ public class NexusRepositorySearchTest List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - - config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) ); SearchFields searchFields = new SearchFields(); searchFields.setArtifactId( "artifactid-numeric" ); @@ -455,11 +490,190 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); assertNotNull( results ); - assertEquals( 2, results.getTotalHits() ); - - FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); - assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); + assertEquals( 2, results.getTotalHits() ); } + + public void testAdvancedSearchNoRepositoriesConfigured() + throws Exception + { + SearchFields searchFields = new SearchFields(); + searchFields.setArtifactId( "archiva" ); + searchFields.setRepositories( null ); - // TODO: add test when an existing index already exists + try + { + SearchResults results = search.search( "user", searchFields, null ); + fail( "A RepositorySearchExcecption should have been thrown." ); + } + catch( RepositorySearchException e ) + { + assertEquals( "Repositories cannot be null.", e.getMessage() ); + } + } + + public void testAdvancedSearchSearchFieldsAreNull() + throws Exception + { + List selectedRepos = new ArrayList(); + selectedRepos.add( TEST_REPO_1 ); + + SearchFields searchFields = new SearchFields(); + searchFields.setRepositories( selectedRepos ); + + try + { + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", searchFields, null ); + + archivaConfigControl.verify(); + + fail( "A RepositorySearchExcecption should have been thrown." ); + } + catch( RepositorySearchException e ) + { + assertEquals( "No search fields set.", e.getMessage() ); + } + } + + public void testAdvancedSearchSearchFieldsAreBlank() + throws Exception + { + List selectedRepos = new ArrayList(); + selectedRepos.add( TEST_REPO_1 ); + + SearchFields searchFields = new SearchFields(); + searchFields.setGroupId( "" ); + searchFields.setArtifactId( "" ); + searchFields.setVersion( "" ); + searchFields.setPackaging( "" ); + searchFields.setClassName( "" ); + + searchFields.setRepositories( selectedRepos ); + + try + { + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", searchFields, null ); + + archivaConfigControl.verify(); + + fail( "A RepositorySearchExcecption should have been thrown." ); + } + catch( RepositorySearchException e ) + { + assertEquals( "No search fields set.", e.getMessage() ); + } + } + + public void testAdvancedSearchJarArtifacts() + throws Exception + { + List files = new ArrayList(); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/classname-search/1.0/classname-search-1.0.jar" ) ); + + createIndex( TEST_REPO_1, files ); + + List selectedRepos = new ArrayList(); + selectedRepos.add( TEST_REPO_1 ); + + SearchFields searchFields = new SearchFields(); + searchFields.setPackaging( "jar" ); + searchFields.setRepositories( selectedRepos ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", searchFields, null ); + + archivaConfigControl.verify(); + + assertNotNull( results ); + assertEquals( 5, results.getTotalHits() ); + } + + public void testAdvancedSearchClassname() + throws Exception + { + List files = new ArrayList(); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/classname-search/1.0/classname-search-1.0.jar" ) ); + + createIndex( TEST_REPO_1, files ); + + List selectedRepos = new ArrayList(); + selectedRepos.add( TEST_REPO_1 ); + + SearchFields searchFields = new SearchFields(); + searchFields.setClassName( "com.classname.search.App" ); + searchFields.setRepositories( selectedRepos ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", searchFields, null ); + + archivaConfigControl.verify(); + + assertNotNull( results ); + assertEquals( 1, results.getTotalHits() ); + + SearchResultHit hit = results.getHits().get( 0 ); + assertEquals( "com", hit.getGroupId() ); + assertEquals( "classname-search", hit.getArtifactId() ); + assertEquals( "1.0", hit.getVersions().get( 0 ) ); + } + + public void testAdvancedSearchNoIndexFound() + throws Exception + { + List selectedRepos = new ArrayList(); + selectedRepos.add( TEST_REPO_1 ); + + SearchFields searchFields = new SearchFields(); + searchFields.setGroupId( "org.apache.archiva" ); + searchFields.setRepositories( selectedRepos ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + + archivaConfigControl.replay(); + + SearchResults results = search.search( "user", searchFields, null ); + + archivaConfigControl.verify(); + + assertNotNull( results );assertEquals( 0, results.getTotalHits() ); + } } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.jar b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.jar new file mode 100644 index 0000000000000000000000000000000000000000..0572c67fefff5c17938fad63ae08ffa9a9df1e9d GIT binary patch literal 2220 zcmWIWW@h1H0D+6SKGt9cl;8x?zOEsTx}JV+`TKp8LNVy^>_G*?!>6sYT37w%*+G?r=;Md{7n1If=!^d5O8Hc+`qws4Y%SEK1IR zYk`DI#K*;Mv5X81+Dr@#a=5fO78K}#?JN!S^*-z%Pe?VTX7T@O?=EnK=Quf!|?VoVekS|AM zL*l}&S)IPpr8$xr5;xK`=A3jm_MGSDx5W91_;gmeP7Xh|gdEQuO zEIJzS|GbEE=EhfL24Zo4+F$K;+4$jQ{|imgRmSl_yUSCw)Uum?r>x!7l4hXhx$MP* ztm>P4GZg*vC)P-Ac*55uma3)irk#uAjho31_FDYiyr&# zQ8w9`bJe@n7BQ}++c%tuj_y;H^xSy)e)y!x*V@>sKA$`D?hMD~d$Yt^&+Dza{7y4} z&-?NzS`q=mFC0xgde**teY!Ev)3K{2=$)S5zZJ&|RUWIol_Bg~}d+=&&uo_+z|v7g0s7Vl41XJ`L}&$ z>7F0Q-?F&>%dxIoZn$`2&myksE23G77hMzhYJCr^Nv*ziNxw?1D(v~H2r-{U(O-Bg zRh};LTr}6fwy0k($Nzq?)UF=m*};!KL|yhj9)2H|5+GT0TkUI4P|=kJ#6~29bU{&m zL26M+W@>S8ng3aDT`wI^-$0L5As>8oyw5%k`gZnw@CSdrKm(H?1Cwn=hMSGdf;c{K z1R59_YdiymbW8@Z^lphP|K$DF%Thjp{%Wt_`W!MwozEVuH$42mo2U z56Fb%1%y*Ul>1@_ROUheNG}gCccAKpXC#;r*vnv; zAxj$VaTx(Gk&&H)p0Pp21_Xeda|4)B;lT$hL|_iVUW_0#vod37Mh{4I+d;(&!T>Yu z2EfA=QOLlY#KC~An1PwOq_KiX6QP9;BA~G6Dult0fQbezpirU_=2Vcuh&&eH&B_Kc PgBu9{1A}@xD~JaGnunyB literal 0 HcmV?d00001 diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.pom new file mode 100644 index 000000000..80b54abc2 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/com/classname-search/1.0/classname-search-1.0.pom @@ -0,0 +1,18 @@ + + 4.0.0 + com + classname-search + jar + 1.0 + classname-search + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.pom new file mode 100644 index 000000000..ca28dc132 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.pom @@ -0,0 +1,23 @@ + + 4.0.0 + org.apache.archiva + archiva-webapp + war + 1.0 + Archiva Webapp + http://archiva.apache.org + + + junit + junit + 3.8.1 + test + + + log4j + log4j + 1.2.8 + + + diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war new file mode 100644 index 000000000..e69de29bb -- 2.39.5