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.features.IndexCreationFeature;
33 import org.apache.archiva.repository.features.RemoteIndexFeature;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.Indexer;
38 import org.apache.maven.index.MAVEN;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.StringSearchExpression;
41 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
42 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
43 import org.assertj.core.api.Assertions;
44 import org.eclipse.jetty.server.HttpConnectionFactory;
45 import org.eclipse.jetty.server.Server;
46 import org.eclipse.jetty.server.ServerConnector;
47 import org.eclipse.jetty.servlet.DefaultServlet;
48 import org.eclipse.jetty.servlet.ServletContextHandler;
49 import org.eclipse.jetty.servlet.ServletHolder;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
57 import org.springframework.test.context.ContextConfiguration;
59 import javax.inject.Inject;
60 import java.io.IOException;
62 import java.net.URISyntaxException;
63 import java.nio.file.Files;
64 import java.nio.file.Path;
65 import java.nio.file.Paths;
66 import java.util.Arrays;
67 import java.util.concurrent.TimeUnit;
70 * @author Olivier Lamy
72 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
73 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
74 public class DownloadRemoteIndexTaskTest
77 private Server server;
78 private ServerConnector serverConnector;
82 private Logger log = LoggerFactory.getLogger( getClass() );
85 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
91 RepositoryRegistry repositoryRegistry;
94 RepositoryProvider repositoryProvider;
97 public void initialize()
100 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
101 if (Files.exists(cfgFile)) {
102 Files.delete(cfgFile);
105 repositoryRegistry.removeRepository( "test-repo-re" );
106 } catch (Exception e) {
109 server = new Server( );
110 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
111 server.addConnector( serverConnector );
112 createContext( server, Paths.get( "src/test" ) );
114 this.port = serverConnector.getLocalPort();
115 log.info( "start server on port {}", this.port );
118 protected void createContext( Server server, Path repositoryDirectory )
121 ServletContextHandler context = new ServletContextHandler();
122 context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
123 context.setContextPath( "/" );
124 ServletHolder sh = new ServletHolder( DefaultServlet.class );
125 context.addServlet( sh, "/" );
126 server.setHandler( context );
131 public void tearDown()
137 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
138 if (Files.exists(cfgFile)) {
139 Files.delete(cfgFile);
144 public void downloadAndMergeRemoteIndexInEmptyIndex()
147 Path repoDirectory = Paths.get( FileUtils.getBasedir( ), "target/repo-" + Long.toString( System.currentTimeMillis( ) ) );
149 RemoteRepository remoteRepository = getRemoteRepository(repoDirectory);
151 repositoryRegistry.putRepository( remoteRepository);
152 repositoryRegistry.reload();
154 downloadRemoteIndexScheduler.startup();
156 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
158 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
159 10, TimeUnit.SECONDS );
161 repositoryRegistry.removeRepository( "test-repo-re" );
164 BooleanQuery.Builder iQuery = new BooleanQuery.Builder();
165 iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
166 BooleanClause.Occur.SHOULD );
168 remoteRepository = getRemoteRepository( repoDirectory );
169 FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() );
171 Arrays.asList( remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class) ) );
173 FlatSearchResponse response = indexer.searchFlat(rq);
175 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
176 Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
180 protected RemoteRepository getRemoteRepository(Path repoDirectory) throws IOException, URISyntaxException, UnsupportedURIException, RepositoryException
183 EditableRemoteRepository remoteRepository = repositoryProvider.createRemoteInstance( "test-repo-re", "foo" );
184 Path indexDirectory = repoDirectory.resolve( "index" );
185 Files.createDirectories( indexDirectory );
186 remoteRepository.setLocation( new URI( "http://localhost:" + port ) );
187 repoDirectory.toFile().deleteOnExit();
188 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get();
189 rif.setDownloadRemoteIndex( true );
190 rif.setIndexUri( new URI("http://localhost:" + port + "/index-updates/" ) );
191 createIndexingContext( remoteRepository );
193 // IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( );
194 // icf.setLocalIndexPath( remoteRepository.getAsset( "index" ) );
195 return remoteRepository;
198 private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException
200 if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
201 ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType());
203 editableRepo.setIndexingContext(idxManager.createContext(editableRepo));
204 idxManager.updateLocalIndexPath(editableRepo);
205 } catch ( IndexCreationFailedException e) {
206 throw new RepositoryException("Could not create index for repository " + editableRepo.getId() + ": " + e.getMessage(), e);
211 public ArchivaIndexManager getIndexManager( RepositoryType type ) {
212 return repositoryRegistry.getIndexManager( type );