1 package org.apache.archiva.scheduler.indexing;
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.*;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.apache.maven.index.expr.StringSearchExpression;
29 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
30 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
31 import org.eclipse.jetty.server.HttpConnectionFactory;
32 import org.eclipse.jetty.server.Server;
33 import org.eclipse.jetty.server.ServerConnector;
34 import org.eclipse.jetty.servlet.DefaultServlet;
35 import org.eclipse.jetty.servlet.ServletContextHandler;
36 import org.eclipse.jetty.servlet.ServletHolder;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
44 import org.springframework.test.context.ContextConfiguration;
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.Arrays;
52 import java.util.Locale;
53 import java.util.concurrent.TimeUnit;
55 import static org.assertj.core.api.Assertions.assertThat;
58 * @author Olivier Lamy
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class DownloadRemoteIndexTaskTest
65 private Server server;
66 private ServerConnector serverConnector;
70 private Logger log = LoggerFactory.getLogger( getClass() );
73 RemoteRepositoryAdmin remoteRepositoryAdmin;
76 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
82 RepositoryRegistry repositoryRegistry;
85 NexusIndexer nexusIndexer;
88 public void initialize()
91 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
92 if (Files.exists(cfgFile)) {
93 Files.delete(cfgFile);
96 remoteRepositoryAdmin.deleteRemoteRepository("test-repo-re", null);
97 } catch (Exception e) {
100 server = new Server( );
101 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
102 server.addConnector( serverConnector );
103 createContext( server, Paths.get( "src/test/" ) );
105 this.port = serverConnector.getLocalPort();
106 log.info( "start server on port {}", this.port );
109 protected void createContext( Server server, Path repositoryDirectory )
112 ServletContextHandler context = new ServletContextHandler();
113 context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
114 context.setContextPath( "/" );
115 ServletHolder sh = new ServletHolder( DefaultServlet.class );
116 context.addServlet( sh, "/" );
117 server.setHandler( context );
122 public void tearDown()
128 Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
129 if (Files.exists(cfgFile)) {
130 Files.delete(cfgFile);
135 public void downloadAndMergeRemoteIndexInEmptyIndex()
138 RemoteRepository remoteRepository = getRemoteRepository();
140 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
142 downloadRemoteIndexScheduler.startup();
144 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
146 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
147 10, TimeUnit.SECONDS );
149 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo-re", null );
152 BooleanQuery iQuery = new BooleanQuery();
153 iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
154 BooleanClause.Occur.SHOULD );
156 remoteRepositoryAdmin.addRemoteRepository(remoteRepository, null);
157 FlatSearchRequest rq = new FlatSearchRequest( iQuery );
159 Arrays.asList( repositoryRegistry.getRemoteRepository(remoteRepository.getId()).getIndexingContext().getBaseContext(IndexingContext.class) ) );
161 FlatSearchResponse response = indexer.searchFlat(rq);
163 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
164 assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
168 protected RemoteRepository getRemoteRepository() throws IOException
170 RemoteRepository remoteRepository = new RemoteRepository( Locale.getDefault());
171 Path indexDirectory =
172 Paths.get( FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
173 Files.createDirectories( indexDirectory );
174 indexDirectory.toFile().deleteOnExit();
176 remoteRepository.setName( "foo" );
177 remoteRepository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
178 remoteRepository.setDownloadRemoteIndex( true );
179 remoteRepository.setId( "test-repo-re" );
180 remoteRepository.setUrl( "http://localhost:" + port );
181 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
183 return remoteRepository;