]> source.dussan.org Git - archiva.git/blob
53a8fc6bfffbbe5845d7908a13ce94dbb80650e4
[archiva.git] /
1 package org.apache.archiva.consumers.lucene;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import junit.framework.TestCase;
23 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
26 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.FileTypes;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.taskqueue.TaskQueueException;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41
42 import javax.inject.Inject;
43 import java.io.File;
44 import java.io.IOException;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Set;
50
51 /**
52  * NexusIndexerConsumerTest
53  */
54 @RunWith( SpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
56 public class NexusIndexerConsumerTest
57     extends TestCase
58 {
59     private final class ArchivaTaskSchedulerStub
60         implements ArchivaTaskScheduler<ArtifactIndexingTask>
61     {
62         Set<File> indexed = new HashSet<File>();
63
64         public void queueTask( ArtifactIndexingTask task )
65             throws TaskQueueException
66         {
67             switch ( task.getAction() )
68             {
69                 case ADD:
70                     indexed.add( task.getResourceFile() );
71                     break;
72                 case DELETE:
73                     indexed.remove( task.getResourceFile() );
74                     break;
75                 case FINISH:
76                     try
77                     {
78                         task.getContext().close( false );
79                     }
80                     catch ( IOException e )
81                     {
82                         throw new TaskQueueException( e.getMessage() );
83                     }
84                     break;
85             }
86         }
87     }
88
89     private KnownRepositoryContentConsumer nexusIndexerConsumer;
90
91     private ManagedRepositoryConfiguration repositoryConfig;
92
93     private ArchivaTaskSchedulerStub scheduler;
94
95     @Inject
96     private ApplicationContext applicationContext;
97
98     @Inject
99     private PlexusSisuBridge plexusSisuBridge;
100
101     @Inject
102     private MavenIndexerUtils mavenIndexerUtils;
103
104
105     @Override
106     @Before
107     public void setUp()
108         throws Exception
109     {
110         super.setUp();
111
112         scheduler = new ArchivaTaskSchedulerStub();
113
114         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
115
116         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
117
118         nexusIndexerConsumer =
119             new NexusIndexerConsumer( scheduler, configuration, filetypes, plexusSisuBridge, mavenIndexerUtils );
120
121         // initialize to set the file types to be processed
122         ( (Initializable) nexusIndexerConsumer ).initialize();
123
124         repositoryConfig = new ManagedRepositoryConfiguration();
125         repositoryConfig.setId( "test-repo" );
126         repositoryConfig.setLocation( "target/test-classes/test-repo" );
127         repositoryConfig.setLayout( "default" );
128         repositoryConfig.setName( "Test Repository" );
129         repositoryConfig.setScanned( true );
130         repositoryConfig.setSnapshots( false );
131         repositoryConfig.setReleases( true );
132     }
133
134     @Override
135     @After
136     public void tearDown()
137         throws Exception
138     {
139         // delete created index in the repository
140         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
141         FileUtils.deleteDirectory( indexDir );
142         assertFalse( indexDir.exists() );
143
144         indexDir = new File( repositoryConfig.getLocation(), ".index" );
145         FileUtils.deleteDirectory( indexDir );
146         assertFalse( indexDir.exists() );
147
148         super.tearDown();
149     }
150
151     @Test
152     public void testIndexerIndexArtifact()
153         throws Exception
154     {
155         File artifactFile = new File( repositoryConfig.getLocation(),
156                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
157
158         // begin scan
159         Date now = Calendar.getInstance().getTime();
160         nexusIndexerConsumer.beginScan( repositoryConfig, now );
161         nexusIndexerConsumer.processFile(
162             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
163         nexusIndexerConsumer.completeScan();
164
165         assertTrue( scheduler.indexed.contains( artifactFile ) );
166     }
167
168     @Test
169     public void testIndexerArtifactAlreadyIndexed()
170         throws Exception
171     {
172         File artifactFile = new File( repositoryConfig.getLocation(),
173                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
174
175         // begin scan
176         Date now = Calendar.getInstance().getTime();
177         nexusIndexerConsumer.beginScan( repositoryConfig, now );
178         nexusIndexerConsumer.processFile(
179             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
180         nexusIndexerConsumer.completeScan();
181
182         assertTrue( scheduler.indexed.contains( artifactFile ) );
183
184         // scan and index again
185         now = Calendar.getInstance().getTime();
186         nexusIndexerConsumer.beginScan( repositoryConfig, now );
187         nexusIndexerConsumer.processFile(
188             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
189         nexusIndexerConsumer.completeScan();
190
191         assertTrue( scheduler.indexed.contains( artifactFile ) );
192     }
193
194     @Test
195     public void testIndexerIndexArtifactThenPom()
196         throws Exception
197     {
198         File artifactFile = new File( repositoryConfig.getLocation(),
199                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
200
201         // begin scan
202         Date now = Calendar.getInstance().getTime();
203         nexusIndexerConsumer.beginScan( repositoryConfig, now );
204         nexusIndexerConsumer.processFile(
205             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
206         nexusIndexerConsumer.completeScan();
207
208         assertTrue( scheduler.indexed.contains( artifactFile ) );
209
210         artifactFile =
211             new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
212
213         // scan and index again
214         now = Calendar.getInstance().getTime();
215         nexusIndexerConsumer.beginScan( repositoryConfig, now );
216         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
217         nexusIndexerConsumer.completeScan();
218
219         assertTrue( scheduler.indexed.contains( artifactFile ) );
220     }
221
222     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
223     @Test
224     public void testIncludedFileTypes()
225         throws Exception
226     {
227         List<String> includes = nexusIndexerConsumer.getIncludes();
228         assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
229         assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
230         assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
231         assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
232         assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
233         assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
234     }
235
236 }