]> source.dussan.org Git - archiva.git/blob
b18261c36bcc73a15ce930738896497ddbdac075
[archiva.git] /
1 package org.apache.archiva.scheduler.indexing.maven;
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.common.utils.FileUtils;
22 import org.apache.archiva.indexer.ArchivaIndexManager;
23 import org.apache.archiva.indexer.IndexCreationFailedException;
24 import org.apache.archiva.repository.EditableRemoteRepository;
25 import org.apache.archiva.repository.EditableRepository;
26 import org.apache.archiva.repository.RemoteRepository;
27 import org.apache.archiva.repository.RepositoryException;
28 import org.apache.archiva.repository.RepositoryProvider;
29 import org.apache.archiva.repository.RepositoryRegistry;
30 import org.apache.archiva.repository.RepositoryType;
31 import org.apache.archiva.repository.UnsupportedURIException;
32 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
33 import org.apache.archiva.repository.features.IndexCreationFeature;
34 import org.apache.archiva.repository.features.RemoteIndexFeature;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.maven.index.FlatSearchRequest;
37 import org.apache.maven.index.FlatSearchResponse;
38 import org.apache.maven.index.Indexer;
39 import org.apache.maven.index.MAVEN;
40 import org.apache.maven.index.context.IndexingContext;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
43 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
44 import org.assertj.core.api.Assertions;
45 import org.eclipse.jetty.server.HttpConnectionFactory;
46 import org.eclipse.jetty.server.Server;
47 import org.eclipse.jetty.server.ServerConnector;
48 import org.eclipse.jetty.servlet.DefaultServlet;
49 import org.eclipse.jetty.servlet.ServletContextHandler;
50 import org.eclipse.jetty.servlet.ServletHolder;
51 import org.junit.After;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
58 import org.springframework.test.context.ContextConfiguration;
59
60 import javax.inject.Inject;
61 import java.io.IOException;
62 import java.net.URI;
63 import java.net.URISyntaxException;
64 import java.nio.file.Files;
65 import java.nio.file.Path;
66 import java.nio.file.Paths;
67 import java.util.Arrays;
68 import java.util.concurrent.TimeUnit;
69
70 /**
71  * @author Olivier Lamy
72  */
73 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
74 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
75 public class DownloadRemoteIndexTaskTest
76 {
77
78     private Server server;
79     private ServerConnector serverConnector;
80
81     private int port;
82
83     private Logger log = LoggerFactory.getLogger( getClass() );
84
85     @Inject
86     DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
87
88     @Inject
89     Indexer indexer;
90
91     @Inject
92     RepositoryRegistry repositoryRegistry;
93
94     @SuppressWarnings( "unused" )
95     @Inject
96     RepositoryHandlerDependencies repositoryHandlerDependencies;
97
98     @Inject
99     RepositoryProvider repositoryProvider;
100
101     @Before
102     public void initialize()
103         throws Exception
104     {
105         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
106         if (Files.exists(cfgFile)) {
107             Files.delete(cfgFile);
108         }
109         try {
110             repositoryRegistry.removeRepository( "test-repo-re" );
111         } catch (Exception e) {
112             // Ignore
113         }
114         server = new Server( );
115         serverConnector = new ServerConnector( server, new HttpConnectionFactory());
116         server.addConnector( serverConnector );
117         createContext( server, Paths.get( "src/test" ) );
118         this.server.start();
119         this.port = serverConnector.getLocalPort();
120         log.info( "start server on port {}", this.port );
121     }
122
123     protected void createContext( Server server, Path repositoryDirectory )
124         throws IOException
125     {
126         ServletContextHandler context = new ServletContextHandler();
127         context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
128         context.setContextPath( "/" );
129         ServletHolder sh = new ServletHolder( DefaultServlet.class );
130         context.addServlet( sh, "/" );
131         server.setHandler( context );
132
133     }
134
135     @After
136     public void tearDown()
137         throws Exception
138     {
139         if (server!=null) {
140             server.stop();
141         }
142         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
143         if (Files.exists(cfgFile)) {
144             Files.delete(cfgFile);
145         }
146     }
147
148     @Test
149     public void downloadAndMergeRemoteIndexInEmptyIndex()
150         throws Exception
151     {
152         Path repoDirectory = Paths.get( FileUtils.getBasedir( ), "target/repo-" + Long.toString( System.currentTimeMillis( ) ) );
153
154         RemoteRepository remoteRepository = getRemoteRepository(repoDirectory);
155
156         repositoryRegistry.putRepository( remoteRepository);
157         repositoryRegistry.reload();
158
159         downloadRemoteIndexScheduler.startup();
160
161         downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
162
163         ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
164             10, TimeUnit.SECONDS );
165
166         repositoryRegistry.removeRepository( "test-repo-re" );
167
168         // search
169         BooleanQuery.Builder iQuery = new BooleanQuery.Builder();
170         iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
171                     BooleanClause.Occur.SHOULD );
172
173         remoteRepository = getRemoteRepository( repoDirectory );
174         FlatSearchRequest rq = new FlatSearchRequest( iQuery.build() );
175         rq.setContexts(
176             Arrays.asList( remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class) ) );
177
178         FlatSearchResponse response = indexer.searchFlat(rq);
179
180         log.info( "returned hit count:{}", response.getReturnedHitsCount() );
181         Assertions.assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
182     }
183
184
185     protected RemoteRepository getRemoteRepository(Path repoDirectory) throws IOException, URISyntaxException, UnsupportedURIException, RepositoryException
186     {
187
188         EditableRemoteRepository remoteRepository = repositoryProvider.createRemoteInstance( "test-repo-re", "foo" );
189         Path indexDirectory = repoDirectory.resolve( "index" );
190         Files.createDirectories( indexDirectory );
191         remoteRepository.setLocation( new URI( "http://localhost:" + port ) );
192         repoDirectory.toFile().deleteOnExit();
193         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get();
194         rif.setDownloadRemoteIndex( true );
195         rif.setIndexUri( new URI("http://localhost:" + port + "/index-updates/" ) );
196         createIndexingContext( remoteRepository );
197
198         // IndexCreationFeature icf = remoteRepository.getFeature( IndexCreationFeature.class ).get( );
199         // icf.setLocalIndexPath( remoteRepository.getAsset(  "index" ) );
200         return remoteRepository;
201     }
202
203     private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException
204     {
205         if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
206             ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType());
207             try {
208                 editableRepo.setIndexingContext(idxManager.createContext(editableRepo));
209                 idxManager.updateLocalIndexPath(editableRepo);
210             } catch ( IndexCreationFailedException e) {
211                 throw new RepositoryException("Could not create index for repository " + editableRepo.getId() + ": " + e.getMessage(), e);
212             }
213         }
214     }
215
216     public ArchivaIndexManager getIndexManager( RepositoryType type ) {
217         return repositoryRegistry.getIndexManager( type );
218     }
219 }