1 package org.apache.archiva.scheduler.indexing.maven;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.ArchivaIndexManager;
23 import org.apache.archiva.indexer.IndexCreationFailedException;
24 import org.apache.archiva.repository.EditableRemoteRepository;
25 import org.apache.archiva.repository.EditableRepository;
26 import org.apache.archiva.repository.RemoteRepository;
27 import org.apache.archiva.repository.RepositoryException;
28 import org.apache.archiva.repository.RepositoryProvider;
29 import org.apache.archiva.repository.RepositoryRegistry;
30 import org.apache.archiva.repository.RepositoryType;
31 import org.apache.archiva.repository.UnsupportedURIException;
32 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
33 import org.apache.archiva.repository.features.IndexCreationFeature;
34 import org.apache.archiva.repository.features.RemoteIndexFeature;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.maven.index.FlatSearchRequest;
37 import org.apache.maven.index.FlatSearchResponse;
38 import org.apache.maven.index.Indexer;
39 import org.apache.maven.index.MAVEN;
40 import org.apache.maven.index.context.IndexingContext;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
43 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
44 import org.assertj.core.api.Assertions;
45 import org.eclipse.jetty.server.HttpConnectionFactory;
46 import org.eclipse.jetty.server.Server;
47 import org.eclipse.jetty.server.ServerConnector;
48 import org.eclipse.jetty.servlet.DefaultServlet;
49 import org.eclipse.jetty.servlet.ServletContextHandler;
50 import org.eclipse.jetty.servlet.ServletHolder;
51 import org.junit.After;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
58 import org.springframework.test.context.ContextConfiguration;
60 import javax.inject.Inject;
61 import java.io.IOException;
63 import java.net.URISyntaxException;
64 import java.nio.file.Files;
65 import java.nio.file.Path;
66 import java.nio.file.Paths;
67 import java.util.Arrays;
68 import java.util.concurrent.TimeUnit;
71 * @author Olivier Lamy
73 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
74 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
75 public class DownloadRemoteIndexTaskTest
78 private Server server;
79 private ServerConnector serverConnector;
83 private Logger log = LoggerFactory.getLogger( getClass() );
86 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
92 RepositoryRegistry repositoryRegistry;
94 @SuppressWarnings( "unused" )
96 RepositoryHandlerDependencies repositoryHandlerDependencies;
99 RepositoryProvider repositoryProvider;
102 public void initialize()
105 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
106 if (Files.exists(cfgFile)) {
107 Files.delete(cfgFile);
110 repositoryRegistry.removeRepository( "test-repo-re" );
111 } catch (Exception e) {
114 server = new Server( );
115 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
116 server.addConnector( serverConnector );
117 createContext( server, Paths.get( "src/test" ) );
119 this.port = serverConnector.getLocalPort();
120 log.info( "start server on port {}", this.port );
123 protected void createContext( Server server, Path repositoryDirectory )
126 ServletContextHandler context = new ServletContextHandler();
127 context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
128 context.setContextPath( "/" );
129 ServletHolder sh = new ServletHolder( DefaultServlet.class );
130 context.addServlet( sh, "/" );
131 server.setHandler( context );
136 public void tearDown()
142 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
143 if (Files.exists(cfgFile)) {
144 Files.delete(cfgFile);
149 public void downloadAndMergeRemoteIndexInEmptyIndex()
152 Path repoDirectory = Paths.get( FileUtils.getBasedir( ), "target/repo-" + Long.toString( System.currentTimeMillis( ) ) );
154 RemoteRepository remoteRepository = getRemoteRepository(repoDirectory);
156 repositoryRegistry.putRepository( remoteRepository);
157 repositoryRegistry.reload();
159 downloadRemoteIndexScheduler.startup();
161 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
163 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
164 10, TimeUnit.SECONDS );
166 repositoryRegistry.removeRepository( "test-repo-re" );
169 BooleanQuery.Builder iQuery = new BooleanQuery.Builder();
170 iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
171 BooleanClause.Occur.SHOULD );
173 remoteRepository = getRemoteRepository( repoDirectory );
174 FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() );
176 Arrays.asList( remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class) ) );
178 FlatSearchResponse response = indexer.searchFlat(rq);
180 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
181 Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
185 protected RemoteRepository getRemoteRepository(Path repoDirectory) throws IOException, URISyntaxException, UnsupportedURIException, RepositoryException
188 EditableRemoteRepository remoteRepository = repositoryProvider.createRemoteInstance( "test-repo-re", "foo" );
189 Path indexDirectory = repoDirectory.resolve( "index" );
190 Files.createDirectories( indexDirectory );
191 remoteRepository.setLocation( new URI( "http://localhost:" + port ) );
192 repoDirectory.toFile().deleteOnExit();
193 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get();
194 rif.setDownloadRemoteIndex( true );
195 rif.setIndexUri( new URI("http://localhost:" + port + "/index-updates/" ) );
196 createIndexingContext( remoteRepository );
198 // IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( );
199 // icf.setLocalIndexPath( remoteRepository.getAsset( "index" ) );
200 return remoteRepository;
203 private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException
205 if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
206 ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType());
208 editableRepo.setIndexingContext(idxManager.createContext(editableRepo));
209 idxManager.updateLocalIndexPath(editableRepo);
210 } catch ( IndexCreationFailedException e) {
211 throw new RepositoryException("Could not create index for repository " + editableRepo.getId() + ": " + e.getMessage(), e);
216 public ArchivaIndexManager getIndexManager( RepositoryType type ) {
217 return repositoryRegistry.getIndexManager( type );