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.Server;
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;
52 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
55 * @author Olivier Lamy
57 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
58 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
59 public class DownloadRemoteIndexTaskTest
63 private Server server;
67 private Logger log = LoggerFactory.getLogger( getClass() );
70 RemoteRepositoryAdmin remoteRepositoryAdmin;
73 DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
76 PlexusSisuBridge plexusSisuBridge;
78 NexusIndexer nexusIndexer;
81 public void initialize()
85 server = new Server( 0 );
86 createContext( server, new File( "src/test/" ) );
89 Connector connector = this.server.getConnectors()[0];
90 this.port = connector.getLocalPort();
91 log.info( "start server on port " + this.port );
92 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
95 protected void createContext( Server server, File repositoryDirectory )
98 ServletContextHandler context = new ServletContextHandler();
99 context.setResourceBase( repositoryDirectory.getAbsolutePath() );
100 context.setContextPath( "/" );
101 ServletHolder sh = new ServletHolder( DefaultServlet.class );
102 context.addServlet( sh, "/" );
103 server.setHandler( context );
108 public void tearDown()
116 public void downloadAndMergeRemoteIndexInEmptyIndex()
119 RemoteRepository remoteRepository = getRemoteRepository();
121 remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
123 downloadRemoteIndexScheduler.startup();
125 downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
127 ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
128 10, TimeUnit.SECONDS );
130 remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
133 BooleanQuery iQuery = new BooleanQuery();
134 iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
135 BooleanClause.Occur.SHOULD );
137 FlatSearchRequest rq = new FlatSearchRequest( iQuery );
139 Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
141 FlatSearchResponse response = nexusIndexer.searchFlat( rq );
143 log.info( "returned hit count:" + response.getReturnedHitsCount() );
144 assertEquals( 8, response.getReturnedHitsCount() );
148 protected RemoteRepository getRemoteRepository()
150 RemoteRepository remoteRepository = new RemoteRepository();
151 File indexDirectory =
152 new File( FileUtil.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
153 indexDirectory.mkdirs();
154 indexDirectory.deleteOnExit();
156 remoteRepository.setName( "foo" );
157 remoteRepository.setIndexDirectory( indexDirectory.getAbsolutePath() );
158 remoteRepository.setDownloadRemoteIndex( true );
159 remoteRepository.setId( "test-repo" );
160 remoteRepository.setUrl( "http://localhost:" + port );
161 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
162 return remoteRepository;