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.admin.model.beans.RemoteRepository;
22 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.repository.RepositoryRegistry;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.maven.index.FlatSearchRequest;
27 import org.apache.maven.index.FlatSearchResponse;
28 import org.apache.maven.index.Indexer;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.context.IndexingContext;
31 import org.apache.maven.index.expr.StringSearchExpression;
32 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
33 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
34 import org.assertj.core.api.Assertions;
35 import org.eclipse.jetty.server.HttpConnectionFactory;
36 import org.eclipse.jetty.server.Server;
37 import org.eclipse.jetty.server.ServerConnector;
38 import org.eclipse.jetty.servlet.DefaultServlet;
39 import org.eclipse.jetty.servlet.ServletContextHandler;
40 import org.eclipse.jetty.servlet.ServletHolder;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
48 import org.springframework.test.context.ContextConfiguration;
50 import javax.inject.Inject;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.Arrays;
56 import java.util.Locale;
57 import java.util.concurrent.TimeUnit;
59 import static org.assertj.core.api.Assertions.assertThat;
62 * @author Olivier Lamy
64 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
65 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
66 public class DownloadRemoteIndexTaskTest
69 private Server server;
70 private ServerConnector serverConnector;
74 private Logger log = LoggerFactory.getLogger( getClass() );
77 RemoteRepositoryAdmin remoteRepositoryAdmin;
80 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
86 RepositoryRegistry repositoryRegistry;
89 public void initialize()
92 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
93 if (Files.exists(cfgFile)) {
94 Files.delete(cfgFile);
97 remoteRepositoryAdmin.deleteRemoteRepository("test-repo-re", null);
98 } catch (Exception e) {
101 server = new Server( );
102 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
103 server.addConnector( serverConnector );
104 createContext( server, Paths.get( "src/test/" ) );
106 this.port = serverConnector.getLocalPort();
107 log.info( "start server on port {}", this.port );
110 protected void createContext( Server server, Path repositoryDirectory )
113 ServletContextHandler context = new ServletContextHandler();
114 context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
115 context.setContextPath( "/" );
116 ServletHolder sh = new ServletHolder( DefaultServlet.class );
117 context.addServlet( sh, "/" );
118 server.setHandler( context );
123 public void tearDown()
129 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
130 if (Files.exists(cfgFile)) {
131 Files.delete(cfgFile);
136 public void downloadAndMergeRemoteIndexInEmptyIndex()
139 RemoteRepository remoteRepository = getRemoteRepository();
141 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
143 downloadRemoteIndexScheduler.startup();
145 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
147 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
148 10, TimeUnit.SECONDS );
150 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo-re", null );
153 BooleanQuery.Builder iQuery = new BooleanQuery.Builder();
154 iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
155 BooleanClause.Occur.SHOULD );
157 remoteRepositoryAdmin.addRemoteRepository(remoteRepository, null);
158 FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() );
160 Arrays.asList( repositoryRegistry.getRemoteRepository(remoteRepository.getId()).getIndexingContext().getBaseContext(IndexingContext.class) ) );
162 FlatSearchResponse response = indexer.searchFlat(rq);
164 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
165 Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
169 protected RemoteRepository getRemoteRepository() throws IOException
171 RemoteRepository remoteRepository = new RemoteRepository( Locale.getDefault());
172 Path indexDirectory =
173 Paths.get( FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
174 Files.createDirectories( indexDirectory );
175 indexDirectory.toFile().deleteOnExit();
177 remoteRepository.setName( "foo" );
178 remoteRepository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
179 remoteRepository.setDownloadRemoteIndex( true );
180 remoteRepository.setId( "test-repo-re" );
181 remoteRepository.setUrl( "http://localhost:" + port );
182 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
184 return remoteRepository;