]> source.dussan.org Git - archiva.git/blob
86c4b53701f03c62de0381118d8e327ed9f04b1e
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
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 java.io.IOException;
23 import java.util.Locale;
24 import org.apache.commons.lang.SystemUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
30 import org.easymock.MockControl;
31
32 import java.io.File;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import org.springframework.beans.BeansException;
40 import org.springframework.beans.factory.BeanFactory;
41 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
42 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationEvent;
45 import org.springframework.context.MessageSourceResolvable;
46 import org.springframework.context.NoSuchMessageException;
47 import org.springframework.core.io.Resource;
48
49 /**
50  * RepositoryContentConsumersTest
51  *
52  * @version $Id$
53  */
54 public class RepositoryContentConsumersTest
55     extends AbstractRepositoryLayerTestCase
56 {
57     private RepositoryContentConsumers lookupRepositoryConsumers()
58         throws Exception
59     {
60         ArchivaConfiguration configuration = (ArchivaConfiguration)lookup(ArchivaConfiguration.class);
61
62         RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub(configuration);
63                
64         RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
65                                                                                            .getName() );
66         ApplicationContext context = new MockApplicationContext(consumerUtil.getAvailableKnownConsumers(), consumerUtil.getAvailableInvalidConsumers());
67
68         consumerUtilStub.setApplicationContext(context);
69         consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
70         consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );        
71         consumerUtilStub.setArchivaConfiguration( configuration );
72         
73         assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
74         
75         return consumerUtilStub;
76     }
77
78     public void testGetSelectedKnownIds()
79         throws Exception
80     {
81         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
82
83         String expectedKnownIds[] = new String[] {
84             "update-db-artifact",
85             "create-missing-checksums",
86             "update-db-repository-metadata",
87             "validate-checksum",
88             "validate-signature",
89             "index-content",
90             "auto-remove",
91             "auto-rename" };
92
93         List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
94         assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
95         assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
96
97         for ( String expectedId : expectedKnownIds )
98         {
99             assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
100         }
101     }
102
103     public void testGetSelectedInvalidIds()
104         throws Exception
105     {
106         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
107
108         String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
109
110         List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
111         assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
112         assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
113
114         for ( String expectedId : expectedInvalidIds )
115         {
116             assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
117         }
118     }
119
120     public void testGetSelectedKnownConsumerMap()
121         throws Exception
122     {
123         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
124
125         String expectedSelectedKnownIds[] = new String[] {
126             "update-db-artifact",
127             "create-missing-checksums",
128             "update-db-repository-metadata",
129             "validate-checksum",
130             "index-content",
131             "auto-remove",
132             "auto-rename" };
133
134         Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
135         assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
136         assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
137
138         for ( String expectedId : expectedSelectedKnownIds )
139         {
140             KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
141             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
142             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
143         }
144     }
145
146     public void testGetSelectedInvalidConsumerMap()
147         throws Exception
148     {
149         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
150
151         String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
152
153         Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
154             .getSelectedInvalidConsumersMap();
155         assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
156         assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
157
158         for ( String expectedId : expectedSelectedInvalidIds )
159         {
160             InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
161             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
162             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
163         }
164     }
165
166     public void testGetAvailableKnownList()
167         throws Exception
168     {
169         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
170
171         String expectedKnownIds[] = new String[] {
172             "update-db-artifact",
173             "create-missing-checksums",
174             "update-db-repository-metadata",
175             "validate-checksum",
176             "index-content",
177             "auto-remove",
178             "auto-rename",
179             "available-but-unselected" };
180
181         List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
182         assertNotNull( "known consumers should not be null.", knownConsumers );
183         assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
184
185         List<String> expectedIds = Arrays.asList( expectedKnownIds );
186         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
187         {
188             assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
189                         expectedIds.contains( consumer.getId() ) );
190         }
191     }
192
193     public void testGetAvailableInvalidList()
194         throws Exception
195     {
196         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
197
198         String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
199
200         List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
201         assertNotNull( "invalid consumers should not be null.", invalidConsumers );
202         assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
203
204         List<String> expectedIds = Arrays.asList( expectedInvalidIds );
205         for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
206         {
207             assertTrue( "Consumer [" + consumer.getId()
208                 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
209                 .getId() ) );
210         }
211     }
212
213     public void testExecution()
214         throws Exception
215     {
216         MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
217         RepositoryContentConsumers consumers = lookupRepositoryConsumers();
218         KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
219         KnownRepositoryContentConsumer unselectedKnownConsumer =
220             (KnownRepositoryContentConsumer) MockControl.createNiceControl(
221                 KnownRepositoryContentConsumer.class ).getMock();
222
223         consumers.setApplicationContext(new MockApplicationContext(Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null));
224
225         consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
226
227         MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
228         InvalidRepositoryContentConsumer selectedInvalidConsumer =
229             (InvalidRepositoryContentConsumer) invalidControl.getMock();
230         InvalidRepositoryContentConsumer unselectedInvalidConsumer =
231             (InvalidRepositoryContentConsumer) MockControl.createControl(
232                 InvalidRepositoryContentConsumer.class ).getMock();
233
234         consumers.setApplicationContext( new MockApplicationContext(null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer )));
235
236         consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
237
238         ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
239         File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
240
241         Date startTime = new Date( System.currentTimeMillis() );
242         startTime.setTime( 12345678 );
243
244         selectedKnownConsumer.beginScan( repo, startTime );
245         selectedKnownConsumer.getExcludes();
246         knownControl.setReturnValue( Collections.EMPTY_LIST );
247         selectedKnownConsumer.getIncludes();
248         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
249         selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
250         //        knownConsumer.completeScan();
251         knownControl.replay();
252
253         selectedInvalidConsumer.beginScan( repo, startTime );
254         //        invalidConsumer.completeScan();
255         invalidControl.replay();
256
257         consumers.executeConsumers( repo, testFile, true );
258
259         knownControl.verify();
260         invalidControl.verify();
261
262         knownControl.reset();
263         invalidControl.reset();
264
265         File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
266
267         selectedKnownConsumer.beginScan( repo, startTime );
268         selectedKnownConsumer.getExcludes();
269         knownControl.setReturnValue( Collections.EMPTY_LIST );
270         selectedKnownConsumer.getIncludes();
271         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
272         //        knownConsumer.completeScan();
273         knownControl.replay();
274
275         selectedInvalidConsumer.beginScan( repo, startTime );
276         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
277         selectedInvalidConsumer.getId();
278         invalidControl.setReturnValue( "invalid" );
279         //        invalidConsumer.completeScan();
280         invalidControl.replay();
281
282         consumers.executeConsumers( repo, notIncludedTestFile, true );
283
284         knownControl.verify();
285         invalidControl.verify();
286
287         knownControl.reset();
288         invalidControl.reset();
289
290         File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
291
292         selectedKnownConsumer.beginScan( repo, startTime );
293         selectedKnownConsumer.getExcludes();
294         knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
295         //        knownConsumer.completeScan();
296         knownControl.replay();
297
298         selectedInvalidConsumer.beginScan( repo, startTime );
299         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
300         selectedInvalidConsumer.getId();
301         invalidControl.setReturnValue( "invalid" );
302         //        invalidConsumer.completeScan();
303         invalidControl.replay();
304
305         consumers.executeConsumers( repo, excludedTestFile, true );
306
307         knownControl.verify();
308         invalidControl.verify();
309     }
310     
311     /**
312      * Create an OS specific version of the filepath.
313      * Provide path in unix "/" format.
314      */
315     private String _OS( String path )
316     {
317         if ( SystemUtils.IS_OS_WINDOWS )
318         {
319             return path.replace( '/', '\\' );
320         }
321         return path;
322     }
323
324     @SuppressWarnings("unchecked")
325     public class MockApplicationContext implements ApplicationContext
326     {
327         private Map convertToMap(List objects)
328         {
329             HashMap map = new HashMap();
330             for (Object o : objects)
331             {
332                 map.put(o, o);
333             }
334             return map;
335         }
336
337         private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
338
339         private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
340
341         public MockApplicationContext(List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer, List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers)
342         {
343             this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
344             this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
345         }
346
347         public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
348             throw new UnsupportedOperationException("Not supported yet.");
349         }
350
351         public String getDisplayName() {
352             throw new UnsupportedOperationException("Not supported yet.");
353         }
354
355         public String getId() {
356             throw new UnsupportedOperationException("Not supported yet.");
357         }
358
359         public ApplicationContext getParent() {
360             throw new UnsupportedOperationException("Not supported yet.");
361         }
362
363         public long getStartupDate() {
364             throw new UnsupportedOperationException("Not supported yet.");
365         }
366
367         public boolean containsBeanDefinition(String beanName) {
368             throw new UnsupportedOperationException("Not supported yet.");
369         }
370
371         public int getBeanDefinitionCount() {
372             throw new UnsupportedOperationException("Not supported yet.");
373         }
374
375         public String[] getBeanDefinitionNames() {
376             throw new UnsupportedOperationException("Not supported yet.");
377         }
378
379         public String[] getBeanNamesForType(Class type) {
380             throw new UnsupportedOperationException("Not supported yet.");
381         }
382
383         public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
384             throw new UnsupportedOperationException("Not supported yet.");
385         }
386
387         public Map getBeansOfType(Class type) throws BeansException {
388             if (type == KnownRepositoryContentConsumer.class)
389             {
390                 return convertToMap(knownRepositoryContentConsumer);
391             }
392             if (type == InvalidRepositoryContentConsumer.class)
393             {
394                 return convertToMap(invalidRepositoryContentConsumers);
395             }
396             throw new UnsupportedOperationException("Should not have been called");
397         }
398
399         public Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
400             throw new UnsupportedOperationException("Not supported yet.");
401         }
402
403         public boolean containsBean(String name) {
404             throw new UnsupportedOperationException("Not supported yet.");
405         }
406
407         public String[] getAliases(String name) {
408             throw new UnsupportedOperationException("Not supported yet.");
409         }
410
411         public Object getBean(String name) throws BeansException {
412             throw new UnsupportedOperationException("Not supported yet.");
413         }
414
415         public Object getBean(String name, Class requiredType) throws BeansException {
416             throw new UnsupportedOperationException("Not supported yet.");
417         }
418
419         public Object getBean(String name, Object[] args) throws BeansException {
420             throw new UnsupportedOperationException("Not supported yet.");
421         }
422
423         public Class getType(String name) throws NoSuchBeanDefinitionException {
424             throw new UnsupportedOperationException("Not supported yet.");
425         }
426
427         public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
428             throw new UnsupportedOperationException("Not supported yet.");
429         }
430
431         public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
432             throw new UnsupportedOperationException("Not supported yet.");
433         }
434
435         public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
436             throw new UnsupportedOperationException("Not supported yet.");
437         }
438
439         public boolean containsLocalBean(String name) {
440             throw new UnsupportedOperationException("Not supported yet.");
441         }
442
443         public BeanFactory getParentBeanFactory() {
444             throw new UnsupportedOperationException("Not supported yet.");
445         }
446
447         public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
448             throw new UnsupportedOperationException("Not supported yet.");
449         }
450
451         public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
452             throw new UnsupportedOperationException("Not supported yet.");
453         }
454
455         public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
456             throw new UnsupportedOperationException("Not supported yet.");
457         }
458
459         public void publishEvent(ApplicationEvent event) {
460             throw new UnsupportedOperationException("Not supported yet.");
461         }
462
463         public Resource[] getResources(String locationPattern) throws IOException {
464             throw new UnsupportedOperationException("Not supported yet.");
465         }
466
467         public ClassLoader getClassLoader() {
468             throw new UnsupportedOperationException("Not supported yet.");
469         }
470
471         public Resource getResource(String location) {
472             throw new UnsupportedOperationException("Not supported yet.");
473         }
474     }
475 }