]> source.dussan.org Git - archiva.git/blob
e36b9fe45cce9b3199ff8f8a030014943a443e1e
[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 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44
45 import javax.inject.Inject;
46 import java.io.File;
47 import java.io.IOException;
48 import java.lang.annotation.Annotation;
49 import java.util.Arrays;
50 import java.util.Collections;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Map;
56
57 /**
58  * RepositoryContentConsumersTest
59  *
60  * @version $Id$
61  */
62 @RunWith( SpringJUnit4ClassRunner.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 AutowireCapableBeanFactory getAutowireCapableBeanFactory( )
381             throws IllegalStateException
382         {
383             throw new UnsupportedOperationException( "Not supported yet." );
384         }
385
386         public String getDisplayName( )
387         {
388             throw new UnsupportedOperationException( "Not supported yet." );
389         }
390
391         public String getId( )
392         {
393             throw new UnsupportedOperationException( "Not supported yet." );
394         }
395
396         public ApplicationContext getParent( )
397         {
398             throw new UnsupportedOperationException( "Not supported yet." );
399         }
400
401         public long getStartupDate( )
402         {
403             throw new UnsupportedOperationException( "Not supported yet." );
404         }
405
406         public boolean containsBeanDefinition( String beanName )
407         {
408             throw new UnsupportedOperationException( "Not supported yet." );
409         }
410
411         public int getBeanDefinitionCount( )
412         {
413             throw new UnsupportedOperationException( "Not supported yet." );
414         }
415
416         public String[] getBeanDefinitionNames( )
417         {
418             throw new UnsupportedOperationException( "Not supported yet." );
419         }
420
421         public String[] getBeanNamesForType( Class type )
422         {
423             throw new UnsupportedOperationException( "Not supported yet." );
424         }
425
426         public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
427         {
428             throw new UnsupportedOperationException( "Not supported yet." );
429         }
430
431         public Map getBeansOfType( Class type )
432             throws BeansException
433         {
434             if ( type == KnownRepositoryContentConsumer.class )
435             {
436                 return convertToMap( knownRepositoryContentConsumer );
437             }
438             if ( type == InvalidRepositoryContentConsumer.class )
439             {
440                 return convertToMap( invalidRepositoryContentConsumers );
441             }
442             throw new UnsupportedOperationException( "Should not have been called" );
443         }
444
445         public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
446             throws BeansException
447         {
448             throw new UnsupportedOperationException( "Not supported yet." );
449         }
450
451         public boolean containsBean( String name )
452         {
453             throw new UnsupportedOperationException( "Not supported yet." );
454         }
455
456         public String[] getAliases( String name )
457         {
458             throw new UnsupportedOperationException( "Not supported yet." );
459         }
460
461         public Object getBean( String name )
462             throws BeansException
463         {
464             throw new UnsupportedOperationException( "Not supported yet." );
465         }
466
467         public Object getBean( String name, Class requiredType )
468             throws BeansException
469         {
470             throw new UnsupportedOperationException( "Not supported yet." );
471         }
472
473         public Object getBean( String name, Object[] args )
474             throws BeansException
475         {
476             throw new UnsupportedOperationException( "Not supported yet." );
477         }
478
479         public Class getType( String name )
480             throws NoSuchBeanDefinitionException
481         {
482             throw new UnsupportedOperationException( "Not supported yet." );
483         }
484
485         public boolean isPrototype( String name )
486             throws NoSuchBeanDefinitionException
487         {
488             throw new UnsupportedOperationException( "Not supported yet." );
489         }
490
491         public boolean isSingleton( String name )
492             throws NoSuchBeanDefinitionException
493         {
494             throw new UnsupportedOperationException( "Not supported yet." );
495         }
496
497         public boolean isTypeMatch( String name, Class targetType )
498             throws NoSuchBeanDefinitionException
499         {
500             throw new UnsupportedOperationException( "Not supported yet." );
501         }
502
503         public boolean containsLocalBean( String name )
504         {
505             throw new UnsupportedOperationException( "Not supported yet." );
506         }
507
508         public BeanFactory getParentBeanFactory( )
509         {
510             throw new UnsupportedOperationException( "Not supported yet." );
511         }
512
513         public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
514         {
515             throw new UnsupportedOperationException( "Not supported yet." );
516         }
517
518         public String getMessage( String code, Object[] args, Locale locale )
519             throws NoSuchMessageException
520         {
521             throw new UnsupportedOperationException( "Not supported yet." );
522         }
523
524         public String getMessage( MessageSourceResolvable resolvable, Locale locale )
525             throws NoSuchMessageException
526         {
527             throw new UnsupportedOperationException( "Not supported yet." );
528         }
529
530         public void publishEvent( ApplicationEvent event )
531         {
532             throw new UnsupportedOperationException( "Not supported yet." );
533         }
534
535         public Resource[] getResources( String locationPattern )
536             throws IOException
537         {
538             throw new UnsupportedOperationException( "Not supported yet." );
539         }
540
541         public ClassLoader getClassLoader( )
542         {
543             throw new UnsupportedOperationException( "Not supported yet." );
544         }
545
546         public Resource getResource( String location )
547         {
548             throw new UnsupportedOperationException( "Not supported yet." );
549         }
550
551         public <T> T getBean( Class<T> tClass )
552             throws BeansException
553         {
554             throw new UnsupportedOperationException( "Not supported yet." );
555         }
556
557         public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
558             throws BeansException
559         {
560             throw new UnsupportedOperationException( "Not supported yet." );
561         }
562
563         public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
564         {
565             throw new UnsupportedOperationException( "Not supported yet." );
566         }
567
568         public Environment getEnvironment()
569         {
570             return null;
571         }
572     }
573 }