diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-05-06 22:08:19 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-05-06 22:08:19 +0200 |
commit | a13bfb5a01af459e734d2f5b4b36d227a8a03a91 (patch) | |
tree | 6c9f3e512e6556a93fb5b1e55d308da13cb5c312 /archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org | |
parent | 79bee6e84771de9b71e7d957e9f10a17364520ed (diff) | |
download | archiva-a13bfb5a01af459e734d2f5b4b36d227a8a03a91.tar.gz archiva-a13bfb5a01af459e734d2f5b4b36d227a8a03a91.zip |
Moving maven specific modules
Diffstat (limited to 'archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org')
2 files changed, 539 insertions, 0 deletions
diff --git a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java new file mode 100644 index 000000000..758568ba9 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/ArchivaIndexingTaskExecutorTest.java @@ -0,0 +1,352 @@ +package org.apache.archiva.scheduler.indexing.maven; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import junit.framework.TestCase; +import org.apache.archiva.indexer.ArchivaIndexingContext; +import org.apache.archiva.indexer.UnsupportedBaseContextException; +import org.apache.archiva.repository.BasicManagedRepository; +import org.apache.archiva.repository.ManagedRepository; +import org.apache.archiva.repository.ReleaseScheme; +import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.repository.features.IndexCreationFeature; +import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.apache.maven.index.ArtifactInfo; +import org.apache.maven.index.FlatSearchRequest; +import org.apache.maven.index.FlatSearchResponse; +import org.apache.maven.index.Indexer; +import org.apache.maven.index.MAVEN; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.expr.SourcedSearchExpression; +import org.apache.maven.index.expr.StringSearchExpression; +import org.apache.maven.index.updater.DefaultIndexUpdater; +import org.apache.maven.index.updater.IndexUpdateRequest; +import org.apache.maven.index.updater.IndexUpdater; +import org.apache.maven.index_shaded.lucene.search.BooleanClause; +import org.apache.maven.index_shaded.lucene.search.BooleanQuery; +import org.apache.maven.index_shaded.lucene.search.IndexSearcher; +import org.apache.maven.index_shaded.lucene.search.TopDocs; +import org.assertj.core.api.Assertions; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; + +import javax.inject.Inject; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Set; + +/** + * ArchivaIndexingTaskExecutorTest + */ +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +public class ArchivaIndexingTaskExecutorTest + extends TestCase +{ + @Inject + private ArchivaIndexingTaskExecutor indexingExecutor; + + @Inject + RepositoryRegistry repositoryRegistry; + + @Inject + private IndexUpdater indexUpdater; + + private ManagedRepository repo; + + @Inject + private Indexer indexer; + + @Before + @Override + public void setUp() + throws Exception + { + super.setUp(); + + Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath(); + BasicManagedRepository repositoryConfig = new BasicManagedRepository( "test-repo", "Test Repository", baseDir); + Path repoLocation = baseDir.resolve("test-repo" ); + repositoryConfig.setLocation(repoLocation.toUri() ); + repositoryConfig.setLayout( "default" ); + repositoryConfig.setScanned( true ); + repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE ); + repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT ); + repositoryRegistry.putRepository(repositoryConfig); + repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() ); + } + + @After + @Override + public void tearDown() + throws Exception + { + + repositoryRegistry.destroy(); + /* + removeIndexingContext with true cleanup files. + // delete created index in the repository + File indexDir = new File( repositoryConfig.getLocation(), ".indexer" ); + FileUtils.deleteDirectory( indexDir ); + assertFalse( indexDir.exists() ); + + indexDir = new File( repositoryConfig.getLocation(), ".index" ); + FileUtils.deleteDirectory( indexDir ); + assertFalse( indexDir.exists() ); + */ + super.tearDown(); + } + + protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException { + assert repo != null; + ArchivaIndexingContext ctx = repo.getIndexingContext(); + assert ctx != null; + return ctx.getBaseContext(IndexingContext.class); + } + + @Test + public void testAddArtifactToIndex() + throws Exception + { + Path basePath = repo.getLocalPath(); + Path artifactFile = basePath.resolve( + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + ArtifactIndexingTask task = + new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD, + repo.getIndexingContext()); + + indexingExecutor.executeTask( task ); + + task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH, + repo.getIndexingContext() ); + indexingExecutor.executeTask( task ); + + BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( ); + queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ), + BooleanClause.Occur.SHOULD ); + queryBuilder.add( + indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ), + BooleanClause.Occur.SHOULD ); + BooleanQuery q = queryBuilder.build(); + + FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext()); + FlatSearchResponse response = indexer.searchFlat( request ); + + assertTrue( Files.exists(basePath.resolve( ".indexer" )) ); + assertTrue( Files.exists(basePath.resolve(".index" )) ); + assertEquals( 1, response.getTotalHitsCount()); + + Set<ArtifactInfo> results = response.getResults(); + + ArtifactInfo artifactInfo = results.iterator().next(); + assertEquals( "org.apache.archiva", artifactInfo.getGroupId() ); + assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() ); + assertEquals( "test-repo", artifactInfo.getRepository() ); + + } + + @Test + public void testUpdateArtifactInIndex() + throws Exception + { + Path basePath = repo.getLocalPath(); + Path artifactFile = basePath.resolve( + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + ArtifactIndexingTask task = + new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD, + repo.getIndexingContext() ); + + indexingExecutor.executeTask( task ); + indexingExecutor.executeTask( task ); + + BooleanQuery.Builder qb = new BooleanQuery.Builder(); + qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ), + BooleanClause.Occur.SHOULD ); + qb.add( + indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ), + BooleanClause.Occur.SHOULD ); + + IndexingContext ctx = getIndexingContext(); + + IndexSearcher searcher = ctx.acquireIndexSearcher(); + TopDocs topDocs = searcher.search( qb.build(), 10 ); + + //searcher.close(); + ctx.releaseIndexSearcher( searcher ); + + assertTrue( Files.exists(basePath.resolve(".indexer" )) ); + assertTrue( Files.exists(basePath.resolve(".index" )) ); + + // should only return 1 hit! + assertEquals( 1, topDocs.totalHits ); + } + + @Test + public void testRemoveArtifactFromIndex() + throws Exception + { + Path basePath = repo.getLocalPath(); + Path artifactFile = basePath.resolve( + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + ArtifactIndexingTask task = + new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD, + repo.getIndexingContext() ); + + // add artifact to index + indexingExecutor.executeTask( task ); + + BooleanQuery.Builder qb = new BooleanQuery.Builder(); + qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ), + BooleanClause.Occur.SHOULD ); + //q.add( + // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ), + // Occur.SHOULD ); + + IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class ); + FlatSearchRequest flatSearchRequest = + new FlatSearchRequest( qb.build(), ctx ); + + FlatSearchResponse response = indexer.searchFlat( flatSearchRequest ); + + assertTrue( Files.exists(basePath.resolve(".indexer" )) ); + assertTrue( Files.exists(basePath.resolve( ".index" )) ); + + // should return 1 hit + assertEquals( 1, response.getTotalHitsCount() ); + + // remove added artifact from index + task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE, + repo.getIndexingContext()); + indexingExecutor.executeTask( task ); + + task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH, + repo.getIndexingContext() ); + indexingExecutor.executeTask( task ); + + qb = new BooleanQuery.Builder(); + qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ), + BooleanClause.Occur.SHOULD ); + qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, + new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ), + BooleanClause.Occur.SHOULD ); + + assertTrue( Files.exists(basePath.resolve( ".indexer" )) ); + assertTrue( Files.exists(basePath.resolve(".index" )) ); + + flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() ); + + response = indexer.searchFlat( flatSearchRequest ); + // artifact should have been removed from the index! + assertEquals( 0, response.getTotalHitsCount() );//.totalHits ); + + // TODO: test it was removed from the packaged index also + } + + @Test + public void testPackagedIndex() + throws Exception + { + + Path basePath = repo.getLocalPath(); + IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get(); + Path packedIndexDirectory = icf.getLocalPackedIndexPath(); + Path indexerDirectory = icf.getLocalIndexPath(); + + for (Path dir : new Path[] { packedIndexDirectory, indexerDirectory }) { + Files.list(dir).filter(path -> path.getFileName().toString().startsWith("nexus-maven-repository-index")) + .forEach(path -> + { + try { + System.err.println("Deleting " + path); + Files.delete(path); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + + + + Path artifactFile = basePath.resolve( + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + ArtifactIndexingTask task = + new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD, + repo.getIndexingContext() ); + task.setExecuteOnEntireRepo( false ); + + indexingExecutor.executeTask( task ); + + task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH, + repo.getIndexingContext() ); + + task.setExecuteOnEntireRepo( false ); + + indexingExecutor.executeTask( task ); + + assertTrue( Files.exists(packedIndexDirectory) ); + assertTrue( Files.exists(indexerDirectory) ); + + // test packed index file creation + //no more zip + //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists(); + Assertions.assertThat( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.properties" ) )); + Assertions.assertThat( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.gz" ) )); + assertFalse( Files.exists(packedIndexDirectory.resolve("nexus-maven-repository-index.1.gz" ) )); + + // unpack .zip index + //unzipIndex( indexerDirectory.getPath(), destDir.getPath() ); + + DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.toFile() ); + IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher ); + //updateRequest.setLocalIndexCacheDir( indexerDirectory ); + indexUpdater.fetchAndUpdateIndex( updateRequest ); + + BooleanQuery.Builder qb = new BooleanQuery.Builder(); + qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ), + BooleanClause.Occur.SHOULD ); + qb.add( + indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ), + BooleanClause.Occur.SHOULD ); + + FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() ); + FlatSearchResponse response = indexer.searchFlat( request ); + + assertEquals( 1, response.getTotalHitsCount() ); + Set<ArtifactInfo> results = response.getResults(); + + ArtifactInfo artifactInfo = results.iterator().next(); + assertEquals( "org.apache.archiva", artifactInfo.getGroupId() ); + assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() ); + assertEquals( "test-repo", artifactInfo.getRepository() ); + + + } + +} diff --git a/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java new file mode 100644 index 000000000..01921e45d --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-scheduler/src/test/java/org/apache/archiva/scheduler/indexing/maven/DownloadRemoteIndexTaskTest.java @@ -0,0 +1,187 @@ +package org.apache.archiva.scheduler.indexing.maven; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin; +import org.apache.archiva.common.utils.FileUtils; +import org.apache.archiva.repository.RepositoryRegistry; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.apache.maven.index.FlatSearchRequest; +import org.apache.maven.index.FlatSearchResponse; +import org.apache.maven.index.Indexer; +import org.apache.maven.index.MAVEN; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.expr.StringSearchExpression; +import org.apache.maven.index_shaded.lucene.search.BooleanClause; +import org.apache.maven.index_shaded.lucene.search.BooleanQuery; +import org.assertj.core.api.Assertions; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.test.context.ContextConfiguration; + +import javax.inject.Inject; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Locale; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Olivier Lamy + */ +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +public class DownloadRemoteIndexTaskTest +{ + + private Server server; + private ServerConnector serverConnector; + + private int port; + + private Logger log = LoggerFactory.getLogger( getClass() ); + + @Inject + RemoteRepositoryAdmin remoteRepositoryAdmin; + + @Inject + DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler; + + @Inject + Indexer indexer; + + @Inject + RepositoryRegistry repositoryRegistry; + + @Before + public void initialize() + throws Exception + { + Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml"); + if (Files.exists(cfgFile)) { + Files.delete(cfgFile); + } + try { + remoteRepositoryAdmin.deleteRemoteRepository("test-repo-re", null); + } catch (Exception e) { + // Ignore + } + server = new Server( ); + serverConnector = new ServerConnector( server, new HttpConnectionFactory()); + server.addConnector( serverConnector ); + createContext( server, Paths.get( "src/test/" ) ); + this.server.start(); + this.port = serverConnector.getLocalPort(); + log.info( "start server on port {}", this.port ); + } + + protected void createContext( Server server, Path repositoryDirectory ) + throws IOException + { + ServletContextHandler context = new ServletContextHandler(); + context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() ); + context.setContextPath( "/" ); + ServletHolder sh = new ServletHolder( DefaultServlet.class ); + context.addServlet( sh, "/" ); + server.setHandler( context ); + + } + + @After + public void tearDown() + throws Exception + { + if (server!=null) { + server.stop(); + } + Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml"); + if (Files.exists(cfgFile)) { + Files.delete(cfgFile); + } + } + + @Test + public void downloadAndMergeRemoteIndexInEmptyIndex() + throws Exception + { + RemoteRepository remoteRepository = getRemoteRepository(); + + remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null ); + + downloadRemoteIndexScheduler.startup(); + + downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true ); + + ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination( + 10, TimeUnit.SECONDS ); + + remoteRepositoryAdmin.deleteRemoteRepository( "test-repo-re", null ); + + // search + BooleanQuery.Builder iQuery = new BooleanQuery.Builder(); + iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ), + BooleanClause.Occur.SHOULD ); + + remoteRepositoryAdmin.addRemoteRepository(remoteRepository, null); + FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() ); + rq.setContexts( + Arrays.asList( repositoryRegistry.getRemoteRepository(remoteRepository.getId()).getIndexingContext().getBaseContext(IndexingContext.class) ) ); + + FlatSearchResponse response = indexer.searchFlat(rq); + + log.info( "returned hit count:{}", response.getReturnedHitsCount() ); + Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 ); + } + + + protected RemoteRepository getRemoteRepository() throws IOException + { + RemoteRepository remoteRepository = new RemoteRepository( Locale.getDefault()); + Path indexDirectory = + Paths.get( FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) ); + Files.createDirectories( indexDirectory ); + indexDirectory.toFile().deleteOnExit(); + + remoteRepository.setName( "foo" ); + remoteRepository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() ); + remoteRepository.setDownloadRemoteIndex( true ); + remoteRepository.setId( "test-repo-re" ); + remoteRepository.setUrl( "http://localhost:" + port ); + remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" ); + + return remoteRepository; + } + +} |