1 package org.apache.archiva.consumers.lucene;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.common.ArchivaException;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.FileTypes;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
36 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
37 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
38 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
39 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.codehaus.plexus.taskqueue.TaskQueueException;
43 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
44
45
46
47
48 public class NexusIndexerConsumerTest
49 extends PlexusInSpringTestCase
50 {
51 private final class ArchivaTaskSchedulerStub
52 implements ArchivaTaskScheduler
53 {
54 Set<File> indexed = new HashSet<File>();
55
56 public void startup()
57 throws ArchivaException
58 {
59 }
60
61 public void scheduleDatabaseTasks()
62 throws TaskExecutionException
63 {
64 }
65
66 public void queueRepositoryTask( RepositoryTask task )
67 throws TaskQueueException
68 {
69 }
70
71 public void queueIndexingTask( ArtifactIndexingTask task )
72 throws TaskQueueException
73 {
74 switch ( task.getAction() )
75 {
76 case ADD:
77 indexed.add( task.getResourceFile() );
78 break;
79 case DELETE:
80 indexed.remove( task.getResourceFile() );
81 break;
82 case FINISH:
83 try
84 {
85 task.getContext().close( false );
86 }
87 catch ( IOException e )
88 {
89 throw new TaskQueueException( e.getMessage() );
90 }
91 break;
92 }
93 }
94
95 public void queueDatabaseTask( DatabaseTask task )
96 throws TaskQueueException
97 {
98 }
99
100 public boolean isProcessingRepositoryTask( String repositoryId )
101 {
102 return false;
103 }
104
105 public boolean isProcessingDatabaseTask()
106 {
107 return false;
108 }
109 }
110
111 private KnownRepositoryContentConsumer nexusIndexerConsumer;
112
113 private ManagedRepositoryConfiguration repositoryConfig;
114
115 private ArchivaTaskSchedulerStub scheduler;
116
117 @Override
118 protected void setUp()
119 throws Exception
120 {
121 super.setUp();
122
123 scheduler = new ArchivaTaskSchedulerStub();
124
125 ArchivaConfiguration configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
126
127 FileTypes filetypes = (FileTypes) lookup( FileTypes.class );
128
129 nexusIndexerConsumer = new NexusIndexerConsumer( scheduler, configuration, filetypes );
130
131
132 ( (Initializable) nexusIndexerConsumer ).initialize();
133
134 repositoryConfig = new ManagedRepositoryConfiguration();
135 repositoryConfig.setId( "test-repo" );
136 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
137 repositoryConfig.setLayout( "default" );
138 repositoryConfig.setName( "Test Repository" );
139 repositoryConfig.setScanned( true );
140 repositoryConfig.setSnapshots( false );
141 repositoryConfig.setReleases( true );
142 }
143
144 @Override
145 protected void tearDown()
146 throws Exception
147 {
148
149 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
150 FileUtils.deleteDirectory( indexDir );
151 assertFalse( indexDir.exists() );
152
153 indexDir = new File( repositoryConfig.getLocation(), ".index" );
154 FileUtils.deleteDirectory( indexDir );
155 assertFalse( indexDir.exists() );
156
157 super.tearDown();
158 }
159
160 public void testIndexerIndexArtifact()
161 throws Exception
162 {
163 File artifactFile =
164 new File( repositoryConfig.getLocation(),
165 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
166
167
168 Date now = Calendar.getInstance().getTime();
169 nexusIndexerConsumer.beginScan( repositoryConfig, now );
170 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
171 nexusIndexerConsumer.completeScan();
172
173 assertTrue( scheduler.indexed.contains( artifactFile ) );
174 }
175
176 public void testIndexerArtifactAlreadyIndexed()
177 throws Exception
178 {
179 File artifactFile =
180 new File( repositoryConfig.getLocation(),
181 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
182
183
184 Date now = Calendar.getInstance().getTime();
185 nexusIndexerConsumer.beginScan( repositoryConfig, now );
186 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
187 nexusIndexerConsumer.completeScan();
188
189 assertTrue( scheduler.indexed.contains( artifactFile ) );
190
191
192 now = Calendar.getInstance().getTime();
193 nexusIndexerConsumer.beginScan( repositoryConfig, now );
194 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
195 nexusIndexerConsumer.completeScan();
196
197 assertTrue( scheduler.indexed.contains( artifactFile ) );
198 }
199
200 public void testIndexerIndexArtifactThenPom()
201 throws Exception
202 {
203 File artifactFile =
204 new File( repositoryConfig.getLocation(),
205 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
206
207
208 Date now = Calendar.getInstance().getTime();
209 nexusIndexerConsumer.beginScan( repositoryConfig, now );
210 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
211 nexusIndexerConsumer.completeScan();
212
213 assertTrue( scheduler.indexed.contains( artifactFile ) );
214
215 artifactFile =
216 new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
217
218
219 now = Calendar.getInstance().getTime();
220 nexusIndexerConsumer.beginScan( repositoryConfig, now );
221 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
222 nexusIndexerConsumer.completeScan();
223
224 assertTrue( scheduler.indexed.contains( artifactFile ) );
225 }
226
227
228 public void testIncludedFileTypes()
229 throws Exception
230 {
231 List<String> includes = nexusIndexerConsumer.getIncludes();
232 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
233 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
234 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
235 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
236 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
237 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
238 }
239
240 @Override
241 protected String getPlexusConfigLocation()
242 {
243 return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml";
244 }
245 }