]> source.dussan.org Git - archiva.git/blob
c74c4957d46d8fd1eb6840bd343ebbebc847e4ba
[archiva.git] /
1 package org.apache.archiva.scheduler.indexing;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
47
48 import javax.inject.Inject;
49 import java.io.File;
50 import java.io.IOException;
51 import java.util.Arrays;
52 import java.util.concurrent.TimeUnit;
53
54 import static org.assertj.core.api.Assertions.assertThat;
55
56 /**
57  * @author Olivier Lamy
58  */
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
61 public class DownloadRemoteIndexTaskTest
62 {
63
64     private Server server;
65     private ServerConnector serverConnector;
66
67     private int port;
68
69     private Logger log = LoggerFactory.getLogger( getClass() );
70
71     @Inject
72     RemoteRepositoryAdmin remoteRepositoryAdmin;
73
74     @Inject
75     DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
76
77     @Inject
78     NexusIndexer nexusIndexer;
79
80     @Before
81     public void initialize()
82         throws Exception
83     {
84         server = new Server( );
85         serverConnector = new ServerConnector( server, new HttpConnectionFactory());
86         server.addConnector( serverConnector );
87         createContext( server, new File( "src/test/" ) );
88         this.server.start();
89         this.port = serverConnector.getLocalPort();
90         log.info( "start server on port {}", this.port );
91     }
92
93     protected void createContext( Server server, File repositoryDirectory )
94         throws IOException
95     {
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 );
102
103     }
104
105     @After
106     public void tearDown()
107         throws Exception
108     {
109         server.stop();
110     }
111
112     @Test
113     public void downloadAndMergeRemoteIndexInEmptyIndex()
114         throws Exception
115     {
116         RemoteRepository remoteRepository = getRemoteRepository();
117
118         remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
119
120         downloadRemoteIndexScheduler.startup();
121
122         downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo", true, true );
123
124         ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
125             10, TimeUnit.SECONDS );
126
127         remoteRepositoryAdmin.deleteRemoteRepository( "test-repo", null );
128
129         // search
130         BooleanQuery iQuery = new BooleanQuery();
131         iQuery.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
132                     BooleanClause.Occur.SHOULD );
133
134         FlatSearchRequest rq = new FlatSearchRequest( iQuery );
135         rq.setContexts(
136             Arrays.asList( nexusIndexer.getIndexingContexts().get( "remote-" + getRemoteRepository().getId() ) ) );
137
138         FlatSearchResponse response = nexusIndexer.searchFlat( rq );
139
140         log.info( "returned hit count:{}", response.getReturnedHitsCount() );
141         assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
142     }
143
144
145     protected RemoteRepository getRemoteRepository()
146     {
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();
152
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;
160     }
161
162 }