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 junit.framework.TestCase;
22 import org.apache.archiva.admin.model.beans.RemoteRepository;
23 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.utils.FileUtil;
26 import org.apache.lucene.search.BooleanClause;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.maven.index.FlatSearchRequest;
29 import org.apache.maven.index.FlatSearchResponse;
30 import org.apache.maven.index.MAVEN;
31 import org.apache.maven.index.NexusIndexer;
32 import org.apache.maven.index.expr.StringSearchExpression;
33 import org.eclipse.jetty.server.Connector;
34 import org.eclipse.jetty.server.HttpConnectionFactory;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.server.ServerConnector;
37 import org.eclipse.jetty.servlet.DefaultServlet;
38 import org.eclipse.jetty.servlet.ServletContextHandler;
39 import org.eclipse.jetty.servlet.ServletHolder;
40 import static org.assertj.core.api.Assertions.assertThat;
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;
52 import java.io.IOException;
53 import java.util.Arrays;
54 import java.util.concurrent.TimeUnit;
55 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
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;
79 PlexusSisuBridge plexusSisuBridge;
81 NexusIndexer nexusIndexer;
84 public void initialize()
87 server = new Server( );
88 serverConnector = new ServerConnector( server, new HttpConnectionFactory());
89 server.addConnector( serverConnector );
90 createContext( server, new File( "src/test/" ) );
92 this.port = serverConnector.getLocalPort();
93 log.info( "start server on port {}", this.port );
94 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
97 protected void createContext( Server server, File repositoryDirectory )
100 ServletContextHandler context = new ServletContextHandler();
101 context.setResourceBase( repositoryDirectory.getAbsolutePath() );
102 context.setContextPath( "/" );
103 ServletHolder sh = new ServletHolder( DefaultServlet.class );
104 context.addServlet( sh, "/" );
105 server.setHandler( context );
110 public void tearDown()
117 public void downloadAndMergeRemoteIndexInEmptyIndex()
120 RemoteRepository remoteRepository = getRemoteRepository();
122 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
124 downloadRemoteIndexScheduler.startup();
126 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
128 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
129 10, TimeUnit.SECONDS );
131 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
134 BooleanQuery iQuery = new BooleanQuery();
135 iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
136 BooleanClause.Occur.SHOULD );
138 FlatSearchRequest rq = new FlatSearchRequest( iQuery );
140 Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
142 FlatSearchResponse response = nexusIndexer.searchFlat( rq );
144 log.info( "returned hit count:{}", response.getReturnedHitsCount() );
145 assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
149 protected RemoteRepository getRemoteRepository()
151 RemoteRepository remoteRepository = new RemoteRepository();
152 File indexDirectory =
153 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
154 indexDirectory.mkdirs();
155 indexDirectory.deleteOnExit();
157 remoteRepository.setName( "foo" );
158 remoteRepository.setIndexDirectory( indexDirectory.getAbsolutePath() );
159 remoteRepository.setDownloadRemoteIndex( true );
160 remoteRepository.setId( "test-repo" );
161 remoteRepository.setUrl( "http://localhost:" + port );
162 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
163 return remoteRepository;