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.FileUtil;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.apache.maven.index.FlatSearchRequest;
26 import org.apache.maven.index.FlatSearchResponse;
27 import org.apache.maven.index.MAVEN;
28 import org.apache.maven.index.NexusIndexer;
29 import org.apache.maven.index.expr.StringSearchExpression;
30 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
31 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
32 import org.eclipse.jetty.server.HttpConnectionFactory;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.ServerConnector;
35 import org.eclipse.jetty.servlet.DefaultServlet;
36 import org.eclipse.jetty.servlet.ServletContextHandler;
37 import org.eclipse.jetty.servlet.ServletHolder;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
45 import org.springframework.test.context.ContextConfiguration;
47 import javax.inject.Inject;
49 import java.io.IOException;
50 import java.util.Arrays;
51 import java.util.concurrent.TimeUnit;
53 import static org.assertj.core.api.Assertions.assertThat;
56 * @author Olivier Lamy
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public class DownloadRemoteIndexTaskTest
63 private Server server;
64 private ServerConnector serverConnector;
68 private Logger log = LoggerFactory.getLogger( getClass() );
71 RemoteRepositoryAdmin remoteRepositoryAdmin;
74 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
77 NexusIndexer nexusIndexer;
80 public void initialize()
83 server = new Server( );
84 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
85 server.addConnector( serverConnector );
86 createContext( server, new File( "src/test/" ) );
88 this.port = serverConnector.getLocalPort();
89 log.info( "start server on port {}", this.port );
92 protected void createContext( Server server, File repositoryDirectory )
95 ServletContextHandler context = new ServletContextHandler();
96 context.setResourceBase( repositoryDirectory.getAbsolutePath() );
97 context.setContextPath( "/" );
98 ServletHolder sh = new ServletHolder( DefaultServlet.class );
99 context.addServlet( sh, "/" );
100 server.setHandler( context );
105 public void tearDown()
112 public void downloadAndMergeRemoteIndexInEmptyIndex()
115 RemoteRepository remoteRepository = getRemoteRepository();
117 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
119 downloadRemoteIndexScheduler.startup();
121 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
123 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
124 10, TimeUnit.SECONDS );
126 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
129 BooleanQuery iQuery = new BooleanQuery();
130 iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
131 BooleanClause.Occur.SHOULD );
133 FlatSearchRequest rq = new FlatSearchRequest( iQuery );
135 Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
137 FlatSearchResponse response = nexusIndexer.searchFlat( rq );
139 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
140 assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
144 protected RemoteRepository getRemoteRepository()
146 RemoteRepository remoteRepository = new RemoteRepository();
147 File indexDirectory =
148 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
149 indexDirectory.mkdirs();
150 indexDirectory.deleteOnExit();
152 remoteRepository.setName( "foo" );
153 remoteRepository.setIndexDirectory( indexDirectory.getAbsolutePath() );
154 remoteRepository.setDownloadRemoteIndex( true );
155 remoteRepository.setId( "test-repo" );
156 remoteRepository.setUrl( "http://localhost:" + port );
157 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
158 return remoteRepository;