]> source.dussan.org Git - archiva.git/blob
aeeaa6788f13c3c0aa373910bc7e9a0d23df576a
[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.utils.FileUtils;
24 import org.apache.archiva.repository.RepositoryRegistry;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.maven.index.*;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.apache.maven.index.expr.StringSearchExpression;
29 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
30 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
31 import org.eclipse.jetty.server.HttpConnectionFactory;
32 import org.eclipse.jetty.server.Server;
33 import org.eclipse.jetty.server.ServerConnector;
34 import org.eclipse.jetty.servlet.DefaultServlet;
35 import org.eclipse.jetty.servlet.ServletContextHandler;
36 import org.eclipse.jetty.servlet.ServletHolder;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
44 import org.springframework.test.context.ContextConfiguration;
45
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.Arrays;
52 import java.util.Locale;
53 import java.util.concurrent.TimeUnit;
54
55 import static org.assertj.core.api.Assertions.assertThat;
56
57 /**
58  * @author Olivier Lamy
59  */
60 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
62 public class DownloadRemoteIndexTaskTest
63 {
64
65     private Server server;
66     private ServerConnector serverConnector;
67
68     private int port;
69
70     private Logger log = LoggerFactory.getLogger( getClass() );
71
72     @Inject
73     RemoteRepositoryAdmin remoteRepositoryAdmin;
74
75     @Inject
76     DefaultDownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
77
78     @Inject
79     Indexer indexer;
80
81     @Inject
82     RepositoryRegistry repositoryRegistry;
83
84     @Inject
85     NexusIndexer nexusIndexer;
86
87     @Before
88     public void initialize()
89         throws Exception
90     {
91         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
92         if (Files.exists(cfgFile)) {
93             Files.delete(cfgFile);
94         }
95         try {
96             remoteRepositoryAdmin.deleteRemoteRepository("test-repo-re", null);
97         } catch (Exception e) {
98             // Ignore
99         }
100         server = new Server( );
101         serverConnector = new ServerConnector( server, new HttpConnectionFactory());
102         server.addConnector( serverConnector );
103         createContext( server, Paths.get( "src/test/" ) );
104         this.server.start();
105         this.port = serverConnector.getLocalPort();
106         log.info( "start server on port {}", this.port );
107     }
108
109     protected void createContext( Server server, Path repositoryDirectory )
110         throws IOException
111     {
112         ServletContextHandler context = new ServletContextHandler();
113         context.setResourceBase( repositoryDirectory.toAbsolutePath().toString() );
114         context.setContextPath( "/" );
115         ServletHolder sh = new ServletHolder( DefaultServlet.class );
116         context.addServlet( sh, "/" );
117         server.setHandler( context );
118
119     }
120
121     @After
122     public void tearDown()
123         throws Exception
124     {
125         if (server!=null) {
126             server.stop();
127         }
128         Path cfgFile = Paths.get("target/appserver-base/conf/archiva.xml");
129         if (Files.exists(cfgFile)) {
130             Files.delete(cfgFile);
131         }
132     }
133
134     @Test
135     public void downloadAndMergeRemoteIndexInEmptyIndex()
136         throws Exception
137     {
138         RemoteRepository remoteRepository = getRemoteRepository();
139
140         remoteRepositoryAdmin.addRemoteRepository( remoteRepository, null );
141
142         downloadRemoteIndexScheduler.startup();
143
144         downloadRemoteIndexScheduler.scheduleDownloadRemote( "test-repo-re", true, true );
145
146         ( (ThreadPoolTaskScheduler) downloadRemoteIndexScheduler.getTaskScheduler() ).getScheduledExecutor().awaitTermination(
147             10, TimeUnit.SECONDS );
148
149         remoteRepositoryAdmin.deleteRemoteRepository( "test-repo-re", null );
150
151         // search
152         BooleanQuery iQuery = new BooleanQuery();
153         iQuery.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-logging" ) ),
154                     BooleanClause.Occur.SHOULD );
155
156         remoteRepositoryAdmin.addRemoteRepository(remoteRepository,  null);
157         FlatSearchRequest rq = new FlatSearchRequest( iQuery );
158         rq.setContexts(
159             Arrays.asList( repositoryRegistry.getRemoteRepository(remoteRepository.getId()).getIndexingContext().getBaseContext(IndexingContext.class) ) );
160
161         FlatSearchResponse response = indexer.searchFlat(rq);
162
163         log.info( "returned hit count:{}", response.getReturnedHitsCount() );
164         assertThat( response.getReturnedHitsCount() ).isEqualTo( 8 );
165     }
166
167
168     protected RemoteRepository getRemoteRepository() throws IOException
169     {
170         RemoteRepository remoteRepository = new RemoteRepository( Locale.getDefault());
171         Path indexDirectory =
172             Paths.get( FileUtils.getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
173         Files.createDirectories( indexDirectory );
174         indexDirectory.toFile().deleteOnExit();
175
176         remoteRepository.setName( "foo" );
177         remoteRepository.setIndexDirectory( indexDirectory.toAbsolutePath().toString() );
178         remoteRepository.setDownloadRemoteIndex( true );
179         remoteRepository.setId( "test-repo-re" );
180         remoteRepository.setUrl( "http://localhost:" + port );
181         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/index-updates/" );
182
183         return remoteRepository;
184     }
185
186 }