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.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.utils.FileUtil;
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.MAVEN;
29 import org.apache.maven.index.NexusIndexer;
30 import org.apache.maven.index.expr.StringSearchExpression;
31 import org.apache.maven.index.shaded.lucene.search.BooleanClause;
32 import org.apache.maven.index.shaded.lucene.search.BooleanQuery;
33 import org.eclipse.jetty.server.HttpConnectionFactory;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.server.ServerConnector;
36 import org.eclipse.jetty.servlet.DefaultServlet;
37 import org.eclipse.jetty.servlet.ServletContextHandler;
38 import org.eclipse.jetty.servlet.ServletHolder;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
46 import org.springframework.test.context.ContextConfiguration;
48 import javax.inject.Inject;
50 import java.io.IOException;
51 import java.util.Arrays;
52 import java.util.concurrent.TimeUnit;
54 import static org.assertj.core.api.Assertions.assertThat;
57 * @author Olivier Lamy
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
61 public class DownloadRemoteIndexTaskTest
64 private Server server;
65 private ServerConnector serverConnector;
69 private Logger log = LoggerFactory.getLogger( getClass() );
72 RemoteRepositoryAdmin remoteRepositoryAdmin;
75 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
78 NexusIndexer nexusIndexer;
81 public void initialize()
84 server = new Server( );
85 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
86 server.addConnector( serverConnector );
87 createContext( server, new File( "src/test/" ) );
89 this.port = serverConnector.getLocalPort();
90 log.info( "start server on port {}", this.port );
93 protected void createContext( Server server, File repositoryDirectory )
96 ServletContextHandler context = new ServletContextHandler();
97 context.setResourceBase( repositoryDirectory.getAbsolutePath() );
98 context.setContextPath( "/" );
99 ServletHolder sh = new ServletHolder( DefaultServlet.class );
100 context.addServlet( sh, "/" );
101 server.setHandler( context );
106 public void tearDown()
113 public void downloadAndMergeRemoteIndexInEmptyIndex()
116 RemoteRepository remoteRepository = getRemoteRepository();
118 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
120 downloadRemoteIndexScheduler.startup();
122 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
124 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
125 10, TimeUnit.SECONDS );
127 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
130 BooleanQuery iQuery = new BooleanQuery();
131 iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
132 BooleanClause.Occur.SHOULD );
134 FlatSearchRequest rq = new FlatSearchRequest( iQuery );
136 Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
138 FlatSearchResponse response = nexusIndexer.searchFlat( rq );
140 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
141 assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
145 protected RemoteRepository getRemoteRepository()
147 RemoteRepository remoteRepository = new RemoteRepository();
148 File indexDirectory =
149 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
150 indexDirectory.mkdirs();
151 indexDirectory.deleteOnExit();
153 remoteRepository.setName( "foo" );
154 remoteRepository.setIndexDirectory( indexDirectory.getAbsolutePath() );
155 remoteRepository.setDownloadRemoteIndex( true );
156 remoteRepository.setId( "test-repo" );
157 remoteRepository.setUrl( "http://localhost:" + port );
158 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
159 return remoteRepository;