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