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